@Override public RpcOptions getSyncRpcOptions() { return actual.getSyncRpcOptions(); }
@Override public RpcOptions getSyncRpcOptions() { return realOne.getSyncRpcOptions(); }
@Override public RpcOptions getSyncRpcOptions() { return delegate.getSyncRpcOptions(); }
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; } }
/** * 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(); }
/** * 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); }
/** * Commits a remote 1PC transaction that is already in MARK_COMMIT state */ public int onePhaseCommitRemoteTransaction(GlobalTransaction gtx, List<WriteCommand> modifications) { RpcManager rpcManager = cache.getRpcManager(); CommandsFactory factory = cache.getComponentRegistry().getCommandsFactory(); try { //only pessimistic tx are committed in 1PC and it doesn't use versions. PrepareCommand command = factory.buildPrepareCommand(gtx, modifications, true); CompletionStage<Void> cs = rpcManager.invokeCommandOnAll(command, validOnly(), rpcManager.getSyncRpcOptions()); factory.initializeReplicableCommand(command, false); command.invokeAsync().join(); cs.toCompletableFuture().join(); forgetTransaction(gtx, rpcManager, factory); return loggingCompleted(true) == Status.OK ? XAResource.XA_OK : XAException.XAER_RMERR; } catch (Throwable throwable) { //transaction should commit but we still can have exceptions (timeouts or similar) return XAException.XAER_RMERR; } }
/** * Rollbacks a transaction that is remove in all the cluster members. */ public final void rollbackRemoteTransaction(GlobalTransaction gtx) { RpcManager rpcManager = cache.getRpcManager(); CommandsFactory factory = cache.getComponentRegistry().getCommandsFactory(); try { RollbackCommand rollbackCommand = factory.buildRollbackCommand(gtx); rollbackCommand.setTopologyId(rpcManager.getTopologyId()); CompletionStage<Void> cs = rpcManager .invokeCommandOnAll(rollbackCommand, validOnly(), rpcManager.getSyncRpcOptions()); factory.initializeReplicableCommand(rollbackCommand, false); rollbackCommand.invokeAsync().join(); cs.toCompletableFuture().join(); } catch (Throwable throwable) { throw Util.rewrapAsCacheException(throwable); } finally { forgetTransaction(gtx, rpcManager, factory); } }
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 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 testInvokeCommandCollection() throws Exception { ClusteredGetCommand command = TestingUtil.extractCommandsFactory(cache(0)).buildClusteredGetCommand("key", 0, 0L); RpcManager rpcManager0 = cache(0).getAdvancedCache().getRpcManager(); Exceptions.expectException(IllegalArgumentException.class, () -> rpcManager0.invokeCommand(Arrays.asList(address(0)), command, SingleResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions())); command.setTopologyId(rpcManager0.getTopologyId()); CompletionStage<Map<Address, Response>> stage1 = rpcManager0.invokeCommand(Arrays.asList(address(0)), command, MapResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions()); assertResponse(Collections.emptyMap(), stage1); CompletionStage<Map<Address, Response>> stage2 = rpcManager0.invokeCommand(Arrays.asList(address(1)), command, MapResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions()); assertResponse(Collections.singletonMap(address(1), SUCCESSFUL_EMPTY_RESPONSE), stage2); CompletionStage<Map<Address, Response>> stage3 = rpcManager0.invokeCommand(Arrays.asList(address(0), address(1)), command, MapResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions()); assertResponse(Collections.singletonMap(address(1), SUCCESSFUL_EMPTY_RESPONSE), stage3); }
public void testInvokeCommandStaggered() throws Exception { ClusteredGetCommand command = TestingUtil.extractCommandsFactory(cache(0)).buildClusteredGetCommand("key", 0, 0L); RpcManager rpcManager0 = cache(0).getAdvancedCache().getRpcManager(); Exceptions.expectException(IllegalArgumentException.class, () -> rpcManager0.invokeCommandStaggered(Arrays.asList(address(0)), command, SingleResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions())); command.setTopologyId(rpcManager0.getTopologyId()); CompletionStage<ValidResponse> stage1 = rpcManager0.invokeCommandStaggered(Arrays.asList(address(0)), command, SingleResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions()); assertResponse(null, stage1); CompletionStage<ValidResponse> stage2 = rpcManager0.invokeCommandStaggered(Arrays.asList(address(1)), command, SingleResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions()); assertResponse(SUCCESSFUL_EMPTY_RESPONSE, stage2); CompletionStage<ValidResponse> stage3 = rpcManager0.invokeCommandStaggered(Arrays.asList(address(0), address(1)), command, SingleResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions()); assertResponse(SUCCESSFUL_EMPTY_RESPONSE, stage3); CompletionStage<ValidResponse> stage4 = rpcManager0.invokeCommandStaggered(Arrays.asList(address(0), address(1), address(2)), command, SingleResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions()); assertResponse(SUCCESSFUL_EMPTY_RESPONSE, stage4); }
public void testInvokeCommands() throws Exception { ClusteredGetCommand command = TestingUtil.extractCommandsFactory(cache(0)).buildClusteredGetCommand("key", 0, 0L); RpcManager rpcManager0 = cache(0).getAdvancedCache().getRpcManager(); Exceptions.expectException(IllegalArgumentException.class, () -> { rpcManager0.invokeCommands(Arrays.asList(address(1)), a -> command, MapResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions()); }); command.setTopologyId(rpcManager0.getTopologyId()); CompletionStage<Map<Address, Response>> stage1 = rpcManager0.invokeCommands(Arrays.asList(address(0)), a -> command, MapResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions()); assertResponse(Collections.emptyMap(), stage1); CompletionStage<Map<Address, Response>> stage2 = rpcManager0.invokeCommands(Arrays.asList(address(1)), a -> command, MapResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions()); assertResponse(Collections.singletonMap(address(1), SUCCESSFUL_EMPTY_RESPONSE), stage2); CompletionStage<Map<Address, Response>> stage3 = rpcManager0.invokeCommands(Arrays.asList(address(0), address(1)), a -> command, MapResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions()); assertResponse(Collections.singletonMap(address(1), SUCCESSFUL_EMPTY_RESPONSE), stage3); CompletionStage<Map<Address, Response>> stage4 = rpcManager0.invokeCommands(Arrays.asList(address(0), address(1), address(2)), a -> command, MapResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions()); assertResponse(makeMap(address(1), SUCCESSFUL_EMPTY_RESPONSE, address(2), SUCCESSFUL_EMPTY_RESPONSE), stage4); }
public void testInvokeCommand1() throws Exception { ClusteredGetCommand command = TestingUtil.extractCommandsFactory(cache(0)).buildClusteredGetCommand("key", 0, 0L); RpcManager rpcManager0 = cache(0).getAdvancedCache().getRpcManager(); Exceptions.expectException(IllegalArgumentException.class, () -> rpcManager0.invokeCommand(address(0), command, SingleResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions())); command.setTopologyId(rpcManager0.getTopologyId()); CompletionStage<ValidResponse> stage1 = rpcManager0.invokeCommand(address(0), command, SingleResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions()); assertResponse(null, stage1); CompletionStage<ValidResponse> stage2 = rpcManager0.invokeCommand(address(1), command, SingleResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions()); assertResponse(SUCCESSFUL_EMPTY_RESPONSE, stage2); CompletionStage<ValidResponse> stage3 = rpcManager0.invokeCommand(SUSPECT, command, SingleResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions()); Exceptions.expectExecutionException(SuspectException.class, stage3.toCompletableFuture()); }
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()); Address dest = destination.getAddress(); if (this.shouldSendSync(originLocal)) { log.debugf("Sending sync works %s to %s", works, dest); Response response = rpcManager.blocking(rpcManager.invokeCommand(dest, indexUpdateCommand, SingleResponseCollector.validOnly(), rpcManager.getSyncRpcOptions())); log.debugf("Response %s obtained for command %s", response, works); } else { log.debugf("Sending async works %s to %s", works, dest); CompletionStage<ValidResponse> result = rpcManager.invokeCommand( dest, indexUpdateCommand, SingleResponseCollector.validOnly(), rpcManager.getSyncRpcOptions()); 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); }); } }
}); when(rpcManager.getSyncRpcOptions()).thenReturn(new RpcOptions(DeliverOrder.NONE, 10000, TimeUnit.MILLISECONDS)); when(rpcManager.blocking(any(CompletionStage.class))).thenAnswer(invocation -> ((CompletionStage) invocation .getArgument(0)).toCompletableFuture().join());
rpcManager.getSyncRpcOptions()); } else { rpcManager.sendToMany(owners, command, DeliverOrder.NONE);
public void testInvokeCommandOnAll() throws Exception { ClusteredGetCommand command = TestingUtil.extractCommandsFactory(cache(0)).buildClusteredGetCommand("key", 0, 0L); RpcManager rpcManager0 = cache(0).getAdvancedCache().getRpcManager(); Exceptions.expectException(IllegalArgumentException.class, () -> rpcManager0.invokeCommandOnAll(command, SingleResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions())); command.setTopologyId(rpcManager0.getTopologyId()); CompletionStage<Map<Address, Response>> stage1 = rpcManager0.invokeCommandOnAll(command, MapResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions()); assertResponse(makeMap(address(1), SUCCESSFUL_EMPTY_RESPONSE, address(2), SUCCESSFUL_EMPTY_RESPONSE), stage1); }
rpcManager.invokeCommandOnAll(rpcCommand, VoidResponseCollector.validOnly(), rpcManager.getSyncRpcOptions()) .handle((aVoid, throwable) -> {
public void testInvokeCommandOnAllSuspect() throws Exception { DistributionManager distributionManager = cache(0).getAdvancedCache().getDistributionManager(); CacheTopology initialTopology = distributionManager.getCacheTopology(); assertEquals(CacheTopology.Phase.NO_REBALANCE, initialTopology.getPhase()); try { ClusteredGetCommand command = TestingUtil.extractCommandsFactory(cache(0)).buildClusteredGetCommand("key", 0, 0L); RpcManager rpcManager0 = cache(0).getAdvancedCache().getRpcManager(); // Add a node to the cache topology, but not to the JGroups cluster view List<Address> newMembers = new ArrayList<>(initialTopology.getMembers()); newMembers.add(SUSPECT); ConsistentHash newCH = new ReplicatedConsistentHashFactory().create(MurmurHash3.getInstance(), 1, 1, newMembers, null); CacheTopology suspectTopology = new CacheTopology(initialTopology.getTopologyId(), initialTopology.getRebalanceId(), newCH, null, null, CacheTopology.Phase.NO_REBALANCE, newCH.getMembers(), null); distributionManager.setCacheTopology(suspectTopology); command.setTopologyId(rpcManager0.getTopologyId()); CompletionStage<Map<Address, Response>> stage1 = rpcManager0.invokeCommandOnAll(command, MapResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions()); Exceptions.expectExecutionException(SuspectException.class, stage1.toCompletableFuture()); } finally { distributionManager.setCacheTopology(initialTopology); } }