ComponentRegistry registry = cache.getComponentRegistry();
RpcManager rpc = cache.getRpcManager();
InvocationContextContainer icc = cache.getInvocationContextContainer();
DistributionManager dm = cache.getDistributionManager();
InterceptorChain invoker = registry.getComponent(InterceptorChain.class);
CommandsFactory factory = registry.getComponent(CommandsFactory.class);
MapReduceCommand cmd = null;
MapReduceCommand selfCmd = null;
Map<Address, Response> results = new HashMap<Address, Response>();
if (inputTaskKeysEmpty()) {
cmd = factory.buildMapReduceCommand(mapper, reducer, rpc.getAddress(), keys);
selfCmd = cmd;
try {
log.debugf("Invoking %s across entire cluster ", cmd);
Map<Address, Response> map = rpc.invokeRemotely(null, cmd, true, false);
log.debugf("Invoked %s across entire cluster, results are %s", cmd, map);
results.putAll(map);
} catch (Throwable e) {
throw new CacheException("Could not invoke MapReduce task on remote nodes ", e);
}
} else {
Map<Address, List<KIn>> keysToNodes = mapKeysToNodes();
log.debugf("Keys to nodes mapping is " + keysToNodes);
List<MapReduceFuture> futures = new ArrayList<MapReduceFuture>();
for (Entry<Address, List<KIn>> e : keysToNodes.entrySet()) {
Address address = e.getKey();
List<KIn> keys = e.getValue();
if (address.equals(rpc.getAddress())) {
selfCmd = factory.buildMapReduceCommand(clone(mapper), clone(reducer), rpc.getAddress(), keys);
} else {
cmd = factory.buildMapReduceCommand(mapper, reducer, rpc.getAddress(), keys);
try {
log.debugf("Invoking %s on %s", cmd, address);
MapReduceFuture future = new MapReduceFuture();
futures.add(future);
rpc.invokeRemotelyInFuture(Collections.singleton(address), cmd, future);