public Map<Address, Response> forwardCommandIfNeeded(TopologyAffectedCommand command, Set<Object> affectedKeys,
Address origin, boolean sync) {
int cmdTopologyId = command.getTopologyId();
// forward commands with older topology ids to their new targets
// but we need to make sure we have the latest topology
CacheTopology cacheTopology = getCacheTopology();
int localTopologyId = cacheTopology != null ? cacheTopology.getTopologyId() : -1;
// if it's a tx/lock/write command, forward it to the new owners
if (trace) {
log.tracef("CommandTopologyId=%s, localTopologyId=%s", cmdTopologyId, localTopologyId);
}
if (cmdTopologyId < localTopologyId) {
ConsistentHash writeCh = cacheTopology.getWriteConsistentHash();
Set<Address> newTargets = new HashSet<Address>(writeCh.locateAllOwners(affectedKeys));
newTargets.remove(rpcManager.getAddress());
// Forwarding to the originator would create a cycle
// TODO This may not be the "real" originator, but one of the original recipients
// or even one of the nodes that one of the original recipients forwarded the command to.