private void sendCommand(ReplicableCommand command, List<LuceneWork> workList, boolean sync) { rpcManager.invokeRemotely(recipients, command, rpcManager.getDefaultRpcOptions(sync)); log.workListRemotedTo(workList, masterAddress); }
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); }); } }
private void sendCommand(ReplicableCommand command, List<LuceneWork> workList, boolean sync) { rpcManager.invokeRemotely(recipients, command, rpcManager.getDefaultRpcOptions(sync)); log.workListRemotedTo(workList, masterAddress); }
@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; }
/** * Send this ClusteredQueryCommand to a node. * * @param address Address of the destination node * @param clusteredQueryCommand * @return the response */ QueryResponse unicast(Address address, ClusteredQueryCommand clusteredQueryCommand) { if (address.equals(myAddress)) { Future<QueryResponse> localResponse = localInvoke(clusteredQueryCommand); try { return localResponse.get(); } catch (InterruptedException e) { throw new SearchException("Interrupted while searching locally", e); } catch (ExecutionException e) { throw new SearchException("Exception while searching locally", e); } } else { Map<Address, Response> responses = rpcManager.invokeRemotely(Collections.singletonList(address), clusteredQueryCommand, rpcOptions); List<QueryResponse> queryResponses = cast(responses); return queryResponses.get(0); } }
/** * Broadcast this ClusteredQueryCommand to all cluster nodes. The command will be also invoked on local node. * * @param clusteredQueryCommand * @return the responses */ List<QueryResponse> broadcast(ClusteredQueryCommand clusteredQueryCommand) { // invoke on own node Future<QueryResponse> localResponse = localInvoke(clusteredQueryCommand); Map<Address, Response> responses = rpcManager.invokeRemotely(null, clusteredQueryCommand, rpcOptions); List<QueryResponse> queryResponses = cast(responses); try { queryResponses.add(localResponse.get()); } catch (InterruptedException e) { throw new SearchException("Interrupted while searching locally", e); } catch (ExecutionException e) { throw new SearchException("Exception while searching locally", e); } return queryResponses; }
/** * Broadcast this ClusteredQueryCommand to all cluster nodes. The command will be also invoked on * local node. * * @param clusteredQuery * @return A list with all responses */ public List<QueryResponse> broadcast(ClusteredQueryCommand clusteredQuery) { // invoke on own node Future<QueryResponse> localResponse = localInvoke(clusteredQuery); Map<Address, Response> responses = rpcManager.invokeRemotely(null, clusteredQuery, rpcOptions); List<QueryResponse> objects = cast(responses); final QueryResponse localReturnValue; try { localReturnValue = localResponse.get(); } catch (InterruptedException e1) { throw new SearchException("interrupted while searching locally", e1); } catch (ExecutionException e1) { throw new SearchException("Exception while searching locally", e1); } objects.add(localReturnValue); return objects; }
@SuppressWarnings("unchecked") private void assertIndexCall(RpcManager rpcManager, String indexName, boolean sync) { ArgumentCaptor<IndexUpdateCommand> argument = ArgumentCaptor.forClass(IndexUpdateCommand.class); RpcOptions rpcOptions = rpcManager.getDefaultRpcOptions(sync); verify(rpcManager, atLeastOnce()).invokeRemotely(anyCollection(), argument.capture(), eq(rpcOptions)); boolean indexCalled = false; for (IndexUpdateCommand updateCommand : argument.getAllValues()) { indexCalled |= updateCommand.getIndexName().equals(indexName); } assertTrue(indexCalled); }
addresss.add(address); Map<Address, Response> responses = rpcManager.invokeRemotely(addresss, clusteredQuery, rpcOptions); List<QueryResponse> objects = cast(responses); return objects.get(0).getFetchedValue();
@Override public Map<Address, Response> invokeRemotely(Map<Address, ReplicableCommand> rpcs, RpcOptions options) { rpcs.forEach((key, value) -> rpcCollector .addRPC(new RpcDetail(getAddress(), value, cacheName, Collections.singleton(key)))); return delegate.invokeRemotely(rpcs, options); }
@Override public Map<Address, Response> invokeRemotely(Collection<Address> recipients, ReplicableCommand rpc, RpcOptions options) { long start = timeService.time(); Map<Address, Response> responseMap = actual.invokeRemotely(recipients, rpc, options); updateStats(rpc, options.responseMode().isSynchronous(), timeService.timeDuration(start, NANOSECONDS), recipients); return responseMap; }
@Override public Map<Address, Response> invokeRemotely(Collection<Address> recipients, ReplicableCommand rpc, RpcOptions options) { rpcCollector.addRPC(new RpcDetail(getAddress(), rpc, cacheName, recipients)); return delegate.invokeRemotely(recipients, rpc, options); }
@BeforeMethod private void createAndInitStateReceiver() { CommandsFactory commandsFactory = mock(CommandsFactory.class); InternalDataContainer dataContainer = mock(InternalDataContainer.class); RpcManager rpcManager = mock(RpcManager.class); CacheNotifier cacheNotifier = mock(CacheNotifier.class); ExecutorService stateTransferExecutor = Executors.newSingleThreadExecutor(); when(rpcManager.invokeRemotely(any(Collection.class), any(StateRequestCommand.class), any(RpcOptions.class))) .thenAnswer(invocation -> { Collection<Address> recipients = (Collection<Address>) invocation.getArguments()[0]; Address recipient = recipients.iterator().next(); StateRequestCommand cmd = (StateRequestCommand) invocation.getArguments()[1]; Map<Address, Response> results = new HashMap<>(1); if (cmd.getType().equals(StateRequestCommand.Type.START_CONSISTENCY_CHECK) || cmd.getType().equals(StateRequestCommand.Type.CANCEL_CONSISTENCY_CHECK)) { results.put(recipient, SuccessfulResponse.SUCCESSFUL_EMPTY_RESPONSE); } return results; }); when(rpcManager.getRpcOptionsBuilder(any(ResponseMode.class))).thenAnswer(invocation -> { Object[] args = invocation.getArguments(); return new RpcOptionsBuilder(10000, TimeUnit.MILLISECONDS, (ResponseMode) args[0], DeliverOrder.PER_SENDER); }); StateReceiverImpl stateReceiver = new StateReceiverImpl(); TestingUtil.inject(stateReceiver, cacheNotifier, commandsFactory, dataContainer, rpcManager, stateTransferExecutor); stateReceiver.start(); stateReceiver.onDataRehash(createEventImpl(2, 4, Event.Type.DATA_REHASHED)); this.localizedCacheTopology = createLocalizedCacheTopology(4); this.stateReceiver = spy(stateReceiver); }
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"); }
/** * Broadcast an evict-all command with the given cache instance. * @param cache instance used to broadcast command * */ public static void broadcastEvictAll(AdvancedCache cache) { final RpcManager rpcManager = cache.getRpcManager(); if ( rpcManager != null ) { // Only broadcast evict all if it's clustered final CacheCommandInitializer factory = cache.getComponentRegistry() .getComponent( CacheCommandInitializer.class ); final boolean isSync = isSynchronousCache( cache ); final EvictAllCommand cmd = factory.buildEvictAllCommand(ByteString.fromString(cache.getName())); final RpcOptions options = rpcManager.getDefaultRpcOptions( isSync ); rpcManager.invokeRemotely( null, cmd, options ); } }
/** * Test to make sure that invokeRemotely returns the result from the remote side. */ public void testInvokeRemotely() { RpcManager rpcManager = cache(0, "testCache").getAdvancedCache().getRpcManager(); ReplicableCommand command = createReplicableCommandForTest(EXPECTED_RETURN_VALUE); Map<Address, Response> remoteResponses = rpcManager.invokeRemotely(null, command, rpcManager.getDefaultRpcOptions(true)); log.tracef("Responses were: %s", remoteResponses); assertEquals(1, remoteResponses.size()); Response response = remoteResponses.values().iterator().next(); assertNotNull(response); assertTrue(response.isValid()); assertTrue(response.isSuccessful()); assertTrue(response instanceof SuccessfulResponse); Object value = ((SuccessfulResponse) response).getResponseValue(); assertEquals(EXPECTED_RETURN_VALUE, value); }
/** * Test to make sure that invokeRemotely results in a RemoteException. */ public void testInvokeRemotelyWithRemoteException() { RpcManager rpcManager = cache(0, "testCache").getAdvancedCache().getRpcManager(); ReplicableCommand command = createReplicableCommandForTest(new IllegalArgumentException("exception!")); try { rpcManager.invokeRemotely(null, command, rpcManager.getDefaultRpcOptions(true)); 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); } }
/** * 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); } } }
/** * Test to make sure that invokeRemotely with a ResponseMode argument returns the result from the remote side. */ public void testInvokeRemotelyWithResponseMode() { RpcManager rpcManager = cache(0, "testCache").getAdvancedCache().getRpcManager(); ReplicableCommand command = createReplicableCommandForTest(EXPECTED_RETURN_VALUE); RpcOptions rpcOptions = rpcManager.getRpcOptionsBuilder(ResponseMode.SYNCHRONOUS_IGNORE_LEAVERS) .timeout(5000, TimeUnit.MILLISECONDS).build(); Map<Address, Response> remoteResponses = rpcManager.invokeRemotely(null, command, rpcOptions); log.tracef("Responses were: %s", remoteResponses); assertEquals(1, remoteResponses.size()); Response response = remoteResponses.values().iterator().next(); assertNotNull(response); assertTrue(response.isValid()); assertTrue(response.isSuccessful()); assertTrue(response instanceof SuccessfulResponse); Object value = ((SuccessfulResponse) response).getResponseValue(); assertEquals(EXPECTED_RETURN_VALUE, value); }
new EmbeddedMetadata.Builder().build(), EnumUtil.EMPTY_BIT_SET); Map<Address,Response> responseMap = rpcManager.invokeRemotely(addresses, cmd, rpcManager.getDefaultRpcOptions(true, DeliverOrder.NONE)); assert responseMap.size() == 2; rpcManager.invokeRemotely(addresses, cmd, rpcManager.getDefaultRpcOptions(true, DeliverOrder.NONE)); assert false: "invokeRemotely should have thrown an exception"; } catch (SuspectException e) {