/
ClusteredQueryInvoker.java
90 lines (80 loc) · 3.87 KB
/
ClusteredQueryInvoker.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
package org.infinispan.query.clustered;
import static org.infinispan.util.concurrent.CompletableFutures.await;
import static org.infinispan.util.concurrent.CompletableFutures.sequence;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.hibernate.search.util.common.SearchException;
import org.hibernate.search.util.common.SearchTimeoutException;
import org.infinispan.AdvancedCache;
import org.infinispan.commons.util.Util;
import org.infinispan.remoting.inboundhandler.DeliverOrder;
import org.infinispan.remoting.rpc.RpcManager;
import org.infinispan.remoting.rpc.RpcOptions;
import org.infinispan.remoting.transport.Address;
import org.infinispan.remoting.transport.impl.SingleResponseCollector;
/**
* Invoke a ClusteredQueryCommand on the cluster, including on own node.
*
* @author Israel Lacerra <israeldl@gmail.com>
* @author Sanne Grinovero <sanne@infinispan.org> (C) 2011 Red Hat Inc.
* @since 5.1
*/
final class ClusteredQueryInvoker {
private final RpcManager rpcManager;
private final AdvancedCache<?, ?> cache;
private final Address myAddress;
private final RpcOptions rpcOptions;
private final QueryPartitioner partitioner;
ClusteredQueryInvoker(AdvancedCache<?, ?> cache) {
this.cache = cache;
this.rpcManager = cache.getRpcManager();
this.myAddress = rpcManager.getAddress();
this.rpcOptions = new RpcOptions(DeliverOrder.NONE, 10000, TimeUnit.MILLISECONDS);
this.partitioner = new QueryPartitioner(cache);
}
/**
* Broadcast this ClusteredQueryOperation to all cluster nodes. Each node will query a specific number of segments,
* with the local mode having preference to process as much segments as possible. The remainder segments will be
* processed by the respective primary owners.
*
* @param operation The {@link ClusteredQueryOperation} to perform cluster wide.
* @return the responses
*/
List<QueryResponse> broadcast(ClusteredQueryOperation operation) {
Map<Address, BitSet> split = partitioner.split();
SegmentsClusteredQueryCommand localCommand = new SegmentsClusteredQueryCommand(cache.getName(), operation, split.get(myAddress));
// invoke on own node
CompletionStage<QueryResponse> localResponse = localInvoke(localCommand);
List<CompletableFuture<QueryResponse>> futureRemoteResponses = split.entrySet().stream()
.filter(e -> !e.getKey().equals(myAddress)).map(e -> {
Address address = e.getKey();
BitSet segments = e.getValue();
SegmentsClusteredQueryCommand cmd = new SegmentsClusteredQueryCommand(cache.getName(), operation, segments);
return rpcManager.invokeCommand(address, cmd, SingleResponseCollector.validOnly(), rpcOptions).toCompletableFuture();
}).map(a -> a.thenApply(r -> (QueryResponse) r.getResponseValue())).collect(Collectors.toList());
List<QueryResponse> results = new ArrayList<>();
try {
results.add(await(localResponse.toCompletableFuture()));
results.addAll(await(sequence(futureRemoteResponses)));
} catch (InterruptedException e) {
throw new SearchException("Interrupted while searching locally", e);
} catch (ExecutionException e) {
Throwable rootCause = Util.getRootCause(e);
if (rootCause instanceof SearchTimeoutException) {
throw (SearchTimeoutException) rootCause;
}
throw new SearchException("Exception while searching locally", e);
}
return results;
}
private CompletionStage<QueryResponse> localInvoke(SegmentsClusteredQueryCommand cmd) {
return cmd.perform(cache);
}
}