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