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