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);
});
}
}