/
ClusterExecutorImpl.java
328 lines (305 loc) · 13.1 KB
/
ClusterExecutorImpl.java
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package org.infinispan.manager.impl;
import org.infinispan.commons.CacheException;
import org.infinispan.manager.ClusterExecutor;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.remoting.inboundhandler.DeliverOrder;
import org.infinispan.remoting.responses.ExceptionResponse;
import org.infinispan.remoting.responses.Response;
import org.infinispan.remoting.responses.SuccessfulResponse;
import org.infinispan.remoting.transport.Address;
import org.infinispan.remoting.transport.jgroups.CommandAwareRpcDispatcher;
import org.infinispan.remoting.transport.jgroups.JGroupsAddress;
import org.infinispan.remoting.transport.jgroups.JGroupsAddressCache;
import org.infinispan.remoting.transport.jgroups.JGroupsTransport;
import org.infinispan.remoting.transport.jgroups.SingleResponseFuture;
import org.infinispan.remoting.transport.jgroups.SuspectException;
import org.infinispan.util.SerializableFunction;
import org.infinispan.util.SerializableRunnable;
import org.infinispan.util.TriConsumer;
import org.infinispan.util.concurrent.CompletableFutures;
import org.infinispan.util.concurrent.TimeoutException;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
import org.jgroups.blocks.ResponseMode;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* Cluster executor implementation
*
* @author wburns
* @since 8.2
*/
public class ClusterExecutorImpl implements ClusterExecutor {
private static final Log log = LogFactory.getLog(ClusterExecutorImpl.class);
private static final boolean isTrace = log.isTraceEnabled();
private final Predicate<? super Address> predicate;
private final EmbeddedCacheManager manager;
private final JGroupsTransport transport;
private final long time;
private final TimeUnit unit;
private final Executor localExecutor;
private final Address me;
public ClusterExecutorImpl(Predicate<? super Address> predicate, EmbeddedCacheManager manager,
JGroupsTransport transport, long time, TimeUnit unit, Executor localExecutor) {
this.predicate = predicate;
this.manager = manager;
this.transport = transport;
if (transport != null) {
this.me = Objects.requireNonNull(transport.getAddress(),
"Transport was not started before retrieving a ClusterExecutor!");
} else {
// If there was no transport this cache manager cannot have a cluster, so we just always execute
this.me = null;
}
this.time = time;
this.unit = unit;
this.localExecutor = localExecutor;
}
/**
* Returns the targets we should use for JGroups. This excludes the local node if it is a target.
* @return
*/
private List<org.jgroups.Address> getJGroupsTargets() {
List<org.jgroups.Address> list;
if (transport == null) {
return Collections.emptyList();
}
List<Address> ispnMembers = transport.getMembers();
int size = ispnMembers.size();
if (size == 0) {
list = Collections.emptyList();
} else {
if (predicate == null) {
if (size == 1) {
Address member = ispnMembers.get(0);
if (member.equals(me)) {
list = Collections.emptyList();
} else {
list = Collections.singletonList(convertToJGroupsAddress(member));
}
} else {
list = ispnMembers.stream()
.filter(a -> !a.equals(me))
.map(ClusterExecutorImpl::convertToJGroupsAddress)
.collect(Collectors.toList());
}
} else {
list = ispnMembers.stream()
.filter(a -> !a.equals(me))
.filter(predicate)
.map(ClusterExecutorImpl::convertToJGroupsAddress)
.collect(Collectors.toList());
}
}
return list;
}
private static org.jgroups.Address convertToJGroupsAddress(Address address) {
return ((JGroupsAddress) address).getJGroupsAddress();
}
private <T> CompletableFuture<T> startLocalInvocation(Function<? super EmbeddedCacheManager, ? extends T> callable) {
if (me == null || predicate == null || predicate.test(me)) {
if (isTrace) {
log.trace("Submitting callable to local node on executor thread! - Usually remote command thread pool");
}
return CompletableFuture.supplyAsync(() -> {
try {
return callable.apply(manager);
} catch (Throwable t) {
handleCallableRuntimeThrowable(t);
throw new CacheException("Problems invoking command.", t);
}
}, localExecutor);
} else {
return null;
}
}
private CompletableFuture<Void> startLocalInvocation(Runnable runnable) {
if (me == null || predicate == null || predicate.test(me)) {
if (isTrace) {
log.trace("Submitting runnable to local node on executor thread! - Usually remote command thread pool");
}
return CompletableFuture.runAsync(runnable, localExecutor);
} else {
return null;
}
}
@Override
public void execute(Runnable runnable) {
executeRunnable(runnable, ResponseMode.GET_ALL);
}
private void rethrowException(Throwable t) {
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else {
throw new RuntimeException(t);
}
}
private void handleCallableRuntimeThrowable(Throwable t) {
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else if (t instanceof Error) {
throw (Error) t;
}
}
private CompletableFuture<?> executeRunnable(Runnable runnable, ResponseMode mode) {
CompletableFuture<?> localFuture = startLocalInvocation(runnable);
List<org.jgroups.Address> targets = getJGroupsTargets();
int size = targets.size();
CompletableFuture<?> remoteFuture;
if (size == 1) {
org.jgroups.Address target = targets.get(0);
if (isTrace) {
log.tracef("Submitting runnable to single remote node - JGroups Address %s", target);
}
CommandAwareRpcDispatcher card = transport.getCommandAwareRpcDispatcher();
remoteFuture = card.invokeRemoteCommand(target, new ReplicableCommandRunnable(runnable), mode,
unit.toMillis(time), DeliverOrder.NONE).handle((r, t) -> {
if (t != null) {
rethrowException(t);
}
if (r.wasReceived()) {
if (r.hasException()) {
rethrowException(r.getException());
}
} else if (r.wasSuspected()) {
throw log.remoteNodeSuspected(JGroupsAddressCache.fromJGroupsAddress(target));
} else {
throw log.remoteNodeTimedOut(JGroupsAddressCache.fromJGroupsAddress(target), time, unit);
}
return null;
});
} else if (size > 1) {
CommandAwareRpcDispatcher card = transport.getCommandAwareRpcDispatcher();
remoteFuture = card.invokeRemoteCommands(targets, new ReplicableCommandRunnable(runnable), mode,
unit.toMillis(time), null, DeliverOrder.NONE);
} else if (localFuture != null) {
return localFuture;
} else {
return CompletableFutures.completedExceptionFuture(new SuspectException("No available nodes!"));
}
// remoteFuture is guaranteed to be non null at this point
if (localFuture != null && mode != ResponseMode.GET_NONE) {
return CompletableFuture.allOf(localFuture, remoteFuture);
}
return remoteFuture;
}
@Override
public void execute(SerializableRunnable runnable) {
execute((Runnable) runnable);
}
@Override
public CompletableFuture<Void> submit(Runnable command) {
return executeRunnable(command, ResponseMode.GET_ALL).handle((r, t) -> {
if (t != null) {
rethrowException(t);
}
return null;
} );
}
@Override
public CompletableFuture<Void> submit(SerializableRunnable runnable) {
return submit((Runnable) runnable);
}
@Override
public <V> CompletableFuture<Void> submitConsumer(Function<? super EmbeddedCacheManager, ? extends V> function,
TriConsumer<? super Address, ? super V, ? super Throwable> triConsumer) {
CompletableFuture<V> localFuture = startLocalInvocation(function);
if (localFuture != null) {
localFuture.whenComplete((r, t) -> {
if (t != null) {
triConsumer.accept(me, null, t);
} else {
triConsumer.accept(me, r, null);
}
});
}
List<org.jgroups.Address> targets = getJGroupsTargets();
int size = targets.size();
if (size > 0) {
CompletableFuture<?>[] futures = new CompletableFuture[size];
for (int i = 0; i < size; ++i) {
CommandAwareRpcDispatcher card = transport.getCommandAwareRpcDispatcher();
org.jgroups.Address target = targets.get(i);
if (isTrace) {
log.tracef("Submitting consumer to single remote node - JGroups Address %s", target);
}
SingleResponseFuture srf = card.invokeRemoteCommand(target, new ReplicableCommandManagerFunction(function),
ResponseMode.GET_ALL, unit.toMillis(time), DeliverOrder.NONE);
futures[i] = srf.whenComplete((r, t) -> {
if (t != null) {
triConsumer.accept(JGroupsAddressCache.fromJGroupsAddress(r.getSender()), null, t);
} else if (r.wasReceived()) {
if (r.hasException()) {
triConsumer.accept(JGroupsAddressCache.fromJGroupsAddress(r.getSender()), null, r.getException());
} else {
Response response = r.getValue();
if (response instanceof SuccessfulResponse) {
triConsumer.accept(JGroupsAddressCache.fromJGroupsAddress(r.getSender()),
(V) ((SuccessfulResponse) response).getResponseValue(), null);
} else if (response instanceof ExceptionResponse) {
triConsumer.accept(JGroupsAddressCache.fromJGroupsAddress(r.getSender()),
null, ((ExceptionResponse) response).getException());
} else {
triConsumer.accept(JGroupsAddressCache.fromJGroupsAddress(r.getSender()),
null, new IllegalStateException("Response was neither successful or an exception!"));
}
}
} else if (r.wasSuspected()) {
triConsumer.accept(JGroupsAddressCache.fromJGroupsAddress(r.getSender()), null,
new SuspectException());
} else {
// We throw it so it is propagated to the parent CompletableFuture
throw new TimeoutException();
}
});
}
CompletableFuture<Void> remoteFutures = CompletableFuture.allOf(futures);
return localFuture != null ? localFuture.handle((r, t) -> null).thenCombine(remoteFutures, (t, u) -> null) : remoteFutures;
} else if (localFuture != null) {
return localFuture.handle((r, t) -> null);
} else {
return CompletableFuture.completedFuture(null);
}
}
@Override
public <V> CompletableFuture<Void> submitConsumer(SerializableFunction<? super EmbeddedCacheManager, ? extends V> function,
TriConsumer<? super Address, ? super V, ? super Throwable> triConsumer) {
return submitConsumer((Function<EmbeddedCacheManager, V>) function, triConsumer);
}
@Override
public ClusterExecutor timeout(long time, TimeUnit unit) {
if (time <= 0) {
throw new IllegalArgumentException("Time must be greater than 0!");
}
Objects.requireNonNull(unit, "TimeUnit must be non null!");
if (this.time == time && this.unit == unit) {
return this;
}
return new ClusterExecutorImpl(predicate, manager, transport, time, unit, localExecutor);
}
@Override
public ClusterExecutor filterTargets(Predicate<? super Address> predicate) {
return new ClusterExecutorImpl(predicate, manager, transport, time, unit, localExecutor);
}
@Override
public ClusterExecutor filterTargets(Collection<Address> addresses) {
return filterTargets(address -> addresses.contains(address));
}
@Override
public ClusterExecutor noFilter() {
if (predicate == null) {
return this;
}
return new ClusterExecutorImpl(predicate, manager, transport, time, unit, localExecutor);
}
}