You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HazelcastMemberImpl.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.process.cluster.hz;
  21. import com.hazelcast.cluster.Cluster;
  22. import com.hazelcast.cluster.Member;
  23. import com.hazelcast.cluster.MemberSelector;
  24. import com.hazelcast.core.HazelcastInstance;
  25. import com.hazelcast.core.HazelcastInstanceNotActiveException;
  26. import com.hazelcast.core.IExecutorService;
  27. import com.hazelcast.core.MultiExecutionCallback;
  28. import com.hazelcast.cp.IAtomicReference;
  29. import java.util.Map;
  30. import java.util.Set;
  31. import java.util.UUID;
  32. import java.util.concurrent.ExecutionException;
  33. import java.util.concurrent.Future;
  34. import java.util.concurrent.TimeUnit;
  35. import java.util.concurrent.TimeoutException;
  36. import java.util.concurrent.locks.Lock;
  37. import java.util.stream.Collectors;
  38. import org.slf4j.LoggerFactory;
  39. class HazelcastMemberImpl implements HazelcastMember {
  40. private final HazelcastInstance hzInstance;
  41. HazelcastMemberImpl(HazelcastInstance hzInstance) {
  42. this.hzInstance = hzInstance;
  43. }
  44. @Override
  45. public <E> IAtomicReference<E> getAtomicReference(String name) {
  46. return hzInstance.getCPSubsystem().getAtomicReference(name);
  47. }
  48. @Override
  49. public <K, V> Map<K, V> getReplicatedMap(String s) {
  50. return hzInstance.getReplicatedMap(s);
  51. }
  52. @Override
  53. public UUID getUuid() {
  54. return hzInstance.getLocalEndpoint().getUuid();
  55. }
  56. @Override
  57. public Set<UUID> getMemberUuids() {
  58. return hzInstance.getCluster().getMembers().stream().map(Member::getUuid).collect(Collectors.toSet());
  59. }
  60. @Override
  61. public Lock getLock(String s) {
  62. return hzInstance.getCPSubsystem().getLock(s);
  63. }
  64. @Override
  65. public long getClusterTime() {
  66. return hzInstance.getCluster().getClusterTime();
  67. }
  68. @Override
  69. public Cluster getCluster() {
  70. return hzInstance.getCluster();
  71. }
  72. @Override
  73. public <T> DistributedAnswer<T> call(DistributedCall<T> callable, MemberSelector memberSelector, long timeoutMs)
  74. throws InterruptedException {
  75. IExecutorService executor = hzInstance.getExecutorService("default");
  76. Map<Member, Future<T>> futures = executor.submitToMembers(callable, memberSelector);
  77. try {
  78. DistributedAnswer<T> distributedAnswer = new DistributedAnswer<>();
  79. long maxTime = System.currentTimeMillis() + timeoutMs;
  80. for (Map.Entry<Member, Future<T>> entry : futures.entrySet()) {
  81. long remainingMs = Math.max(maxTime - System.currentTimeMillis(), 5L);
  82. try {
  83. T answer = entry.getValue().get(remainingMs, TimeUnit.MILLISECONDS);
  84. distributedAnswer.setAnswer(entry.getKey(), answer);
  85. } catch (ExecutionException e) {
  86. distributedAnswer.setFailed(entry.getKey(), e);
  87. } catch (TimeoutException e) {
  88. distributedAnswer.setTimedOut(entry.getKey());
  89. }
  90. }
  91. return distributedAnswer;
  92. } finally {
  93. futures.values().forEach(f -> f.cancel(true));
  94. }
  95. }
  96. @Override
  97. public <T> void callAsync(DistributedCall<T> callable, MemberSelector memberSelector, DistributedCallback<T> callback) {
  98. IExecutorService executor = hzInstance.getExecutorService("default");
  99. // callback doesn't handle failures, so we need to make sure the callable won't fail!
  100. executor.submitToMembers(callable, memberSelector, new MultiExecutionCallback() {
  101. @Override
  102. public void onResponse(Member member, Object value) {
  103. // nothing to do when each node responds
  104. }
  105. @Override
  106. public void onComplete(Map<Member, Object> values) {
  107. callback.onComplete((Map<Member, T>) values);
  108. }
  109. });
  110. }
  111. @Override
  112. public void close() {
  113. try {
  114. hzInstance.shutdown();
  115. } catch (HazelcastInstanceNotActiveException e) {
  116. LoggerFactory.getLogger(getClass()).debug("Unable to shutdown Hazelcast member", e);
  117. }
  118. }
  119. }