protected ControlledRpcManager(RpcManager realOne, Cache<?, ?> cache) { super(realOne); this.cache = cache; executor = Executors.newScheduledThreadPool( 0, r -> new Thread(r, "ControlledRpc-" + threadCount.getAndIncrement() + "," + realOne.getAddress())); }
private void sendCommand(ReplicableCommand command, List<LuceneWork> workList, boolean sync) { rpcManager.invokeRemotely(recipients, command, rpcManager.getDefaultRpcOptions(sync)); log.workListRemotedTo(workList, masterAddress); }
@Override public List<Address> getMembers() { return actual.getMembers(); }
public Object invokeRemotelyAsync(List<Address> finalOwners, InvocationContext rCtx, WriteCommand writeCmd) { if (rCtx.isOriginLocal() && writeCmd.isSuccessful()) { // This is called with the entry locked. In order to avoid deadlocks we must not wait for RPC while // holding the lock, therefore we'll return a future and wait for it in LockingInterceptor after // unlocking (and committing) the entry. if (isSynchronous(writeCmd)) { if (finalOwners != null) { return rpcManager.invokeCommand( finalOwners, writeCmd, MapResponseCollector.ignoreLeavers(finalOwners.size()), rpcManager.getSyncRpcOptions()); } else { return rpcManager.invokeCommandOnAll(writeCmd, MapResponseCollector.ignoreLeavers(), rpcManager.getSyncRpcOptions()); } } else { rpcManager.sendToMany(finalOwners, writeCmd, DeliverOrder.NONE); } } return null; } }
ClusteredQueryInvoker(AdvancedCache<?, ?> cache, ExecutorService asyncExecutor) { this.cache = cache; this.asyncExecutor = asyncExecutor; this.rpcManager = cache.getRpcManager(); this.myAddress = rpcManager.getAddress(); this.rpcOptions = rpcManager.getRpcOptionsBuilder(ResponseMode.SYNCHRONOUS).timeout(10000, TimeUnit.MILLISECONDS).build(); }
private void rollbackRemote(ComponentRegistry cr, CacheXid cacheXid, TxState state) { RollbackCommand rpcCommand = cr.getCommandsFactory().buildRollbackCommand(state.getGlobalTransaction()); RpcManager rpcManager = cr.getComponent(RpcManager.class); rpcCommand.setTopologyId(rpcManager.getTopologyId()); rpcManager.invokeCommandOnAll(rpcCommand, VoidResponseCollector.validOnly(), rpcManager.getSyncRpcOptions()) .thenRun(() -> { //ignore exception so the rollback can be retried. //if a node doesn't find the remote transaction, it returns null. TxFunction function = new SetCompletedTransactionFunction(false); rwMap.eval(cacheXid, function); }); }
public void testInvokeCommandCollectionSuspect() throws Exception { ClusteredGetCommand command = TestingUtil.extractCommandsFactory(cache(0)).buildClusteredGetCommand("key", 0, 0L); RpcManager rpcManager0 = cache(0).getAdvancedCache().getRpcManager(); command.setTopologyId(rpcManager0.getTopologyId()); CompletionStage<Map<Address, Response>> stage1 = rpcManager0.invokeCommand(Arrays.asList(SUSPECT), command, MapResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions()); Exceptions.expectExecutionException(SuspectException.class, stage1.toCompletableFuture()); CompletionStage<Map<Address, Response>> stage2 = rpcManager0.invokeCommand(Arrays.asList(address(0), SUSPECT), command, MapResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions()); Exceptions.expectExecutionException(SuspectException.class, stage2.toCompletableFuture()); CompletionStage<Map<Address, Response>> stage3 = rpcManager0.invokeCommand(Arrays.asList(address(0), address(1), SUSPECT), command, MapResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions()); Exceptions.expectExecutionException(SuspectException.class, stage3.toCompletableFuture()); }
private Object broadcastClearIfNotLocal(InvocationContext rCtx, VisitableCommand rCommand, Object rv) { FlagAffectedCommand flagCmd = (FlagAffectedCommand) rCommand; if ( !isLocalModeForced( flagCmd ) ) { // just broadcast the clear command - this is simplest! if ( rCtx.isOriginLocal() ) { ((TopologyAffectedCommand) rCommand).setTopologyId(rpcManager.getTopologyId()); if (isSynchronous(flagCmd)) { // the result value will be ignored, we don't need to propagate rv return asyncValue(rpcManager.invokeCommandOnAll(rCommand, VoidResponseCollector.ignoreLeavers(), syncRpcOptions)); } else { rpcManager.sendToAll(rCommand, DeliverOrder.NONE); } } } return rv; }
private void doTest(ResponseFilter filter, boolean totalOrder, boolean broadcast) { RpcManager rpcManager = advancedCache(0, CACHE_NAME).getRpcManager(); RpcOptionsBuilder builder = rpcManager.getRpcOptionsBuilder(ResponseMode.SYNCHRONOUS, totalOrder ? DeliverOrder.TOTAL : DeliverOrder.NONE) .timeout(1000, TimeUnit.MILLISECONDS); ArrayList<Address> recipients = null; if (!broadcast) { List<Address> members = rpcManager.getMembers(); recipients = new ArrayList<>(2); recipients.add(members.get(2)); recipients.add(members.get(3)); } if (filter != null) { builder.responseFilter(filter); } rpcManager.invokeRemotely(recipients, new SleepingCacheRpcCommand(ByteString.fromString(CACHE_NAME), 5000), builder.build()); Assert.fail("Timeout exception wasn't thrown"); }
private void sendRemotely(List<LuceneWork> works, ShardAddress destination, boolean originLocal) { String cacheName = indexManager.getCacheName(); AffinityUpdateCommand indexUpdateCommand = new AffinityUpdateCommand(ByteString.fromString(cacheName)); byte[] serializedModel = indexManager.getSerializer().toSerializedModel(works); indexUpdateCommand.setSerializedWorkList(serializedModel); indexUpdateCommand.setIndexName(destination.getShard()); List<Address> dest = Collections.singletonList(destination.getAddress()); if (this.shouldSendSync(originLocal)) { log.debugf("Sending sync works %s to %s", works, dest); Map<Address, Response> response = rpcManager.invokeRemotely(dest, indexUpdateCommand, rpcManager.getDefaultRpcOptions(true)); log.debugf("Response %s obtained for command %s", response, works); } else { log.debugf("Sending async works %s to %s", works, dest); CompletableFuture<Map<Address, Response>> result = rpcManager.invokeRemotelyAsync(dest, indexUpdateCommand, rpcManager.getDefaultRpcOptions(this.shouldSendSync(originLocal))); result.whenComplete((responses, error) -> { if (error != null) { log.error("Error forwarding index job", error); } log.debugf("Async work %s applied successfully with response %s", works, responses); }); } }
@Override public Transport getTransport() { return actual.getTransport(); }
/** * Test to make sure that invokeRemotely with a ResponseMode argument returns the result from the remote side. */ public void testInvokeRemotelyWithResponseModeWithRemoteException() { RpcManager rpcManager = cache(0, "testCache").getAdvancedCache().getRpcManager(); ReplicableCommand command = createReplicableCommandForTest(new IllegalArgumentException("exception!")); try { RpcOptions rpcOptions = rpcManager.getRpcOptionsBuilder(ResponseMode.SYNCHRONOUS_IGNORE_LEAVERS) .timeout(5000, TimeUnit.MILLISECONDS).build(); rpcManager.invokeRemotely(null, command, rpcOptions); fail("Expected RemoteException not thrown"); } catch (RemoteException e) { assertTrue(e.getCause() instanceof IllegalArgumentException); assertEquals("exception!", e.getCause().getMessage()); } catch (Exception ex) { fail("Expected exception not thrown but instead we got : " + ex); } } }
@Override public RpcOptions getDefaultRpcOptions(boolean sync, DeliverOrder deliverOrder) { return actual.getDefaultRpcOptions(sync, deliverOrder); }
public void testCancelAndGet() throws Exception { DistributedExecutorService des = createDES(getCache()); List<Address> cacheMembers = getCache().getAdvancedCache().getRpcManager().getMembers(); List<Address> members = new ArrayList<>(cacheMembers); assertEquals(caches(cacheName()).size(), members.size()); members.remove(getCache().getAdvancedCache().getRpcManager().getAddress()); DistributedTaskBuilder<Integer> tb = des.createDistributedTaskBuilder(new SleepingSimpleCallable(latchHolder)); final Future<Integer> future = des.submit(members.get(0),tb.build()); future.cancel(true); expectException(CancellationException.class, () -> future.get()); latchHolder.get().open(); }
@Override public final <T> CompletionStage<T> invokeCommand(Address target, ReplicableCommand command, ResponseCollector<T> collector, RpcOptions rpcOptions) { return performRequest(Collections.singleton(target), command, collector, c -> realOne.invokeCommand(target, command, c, rpcOptions), rpcOptions); }
@Override public RpcOptionsBuilder getRpcOptionsBuilder(ResponseMode responseMode, DeliverOrder deliverOrder) { return actual.getRpcOptionsBuilder(responseMode, deliverOrder); }
@Override public Map<Address, Response> invokeRemotely(Map<Address, ReplicableCommand> rpcs, RpcOptions options) { long start = timeService.time(); Map<Address, Response> responseMap = actual.invokeRemotely(rpcs, options); for (Entry<Address, ReplicableCommand> entry : rpcs.entrySet()) { // TODO: This is giving a time for all rpcs combined... updateStats(entry.getValue(), options.responseMode().isSynchronous(), timeService.timeDuration(start, NANOSECONDS), Collections.singleton(entry.getKey())); } return responseMap; }
@Override public <T> CompletionStage<T> invokeCommandOnAll(ReplicableCommand command, ResponseCollector<T> collector, RpcOptions rpcOptions) { long start = timeService.time(); CompletionStage<T> request = actual.invokeCommandOnAll(command, collector, rpcOptions); return request.thenApply(responseMap -> { updateStats(command, true, timeService.timeDuration(start, NANOSECONDS), actual.getTransport().getMembers()); return responseMap; }); }
/** * Completes the transaction in the cache when the originator no longer belongs to the cache topology. */ private CompletableFuture<Void> completeWithRemoteCommand(AdvancedCache<?, ?> cache, RpcManager rpcManager, TxState state) throws Throwable { CommandsFactory commandsFactory = cache.getComponentRegistry().getCommandsFactory(); CacheRpcCommand command = buildRemoteCommand(cache.getCacheConfiguration(), commandsFactory, state); CompletableFuture<Void> remote = rpcManager .invokeCommandOnAll(command, validOnly(), rpcManager.getSyncRpcOptions()) .handle(handler()) .toCompletableFuture(); commandsFactory.initializeReplicableCommand(command, false); CompletableFuture<Void> local = command.invokeAsync().handle(handler()); return CompletableFuture.allOf(remote, local); }
/** * Completes the transaction in the cache when the originator is still in the cache topology. */ private CompletableFuture<Void> forwardCompleteCommand(ByteString cacheName, RpcManager rpcManager, TxState state) { //TODO what if the originator crashes in the meanwhile? //actually, the reaper would rollback the transaction later... Address originator = state.getOriginator(); CacheRpcCommand command = buildForwardCommand(cacheName, state.getTimeout()); return rpcManager.invokeCommand(originator, command, validOnly(), rpcManager.getSyncRpcOptions()) .handle(handler()) .toCompletableFuture(); }