use of org.apache.ignite.compute.ComputeJobAdapter in project ignite by apache.
the class GridCachePutAllTask method map.
/** {@inheritDoc} */
@Override
public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode> subgrid, @Nullable final Collection<Integer> data) {
assert !subgrid.isEmpty();
// Give preference to wanted node. Otherwise, take the first one.
ClusterNode targetNode = F.find(subgrid, subgrid.get(0), new IgnitePredicate<ClusterNode>() {
/** {@inheritDoc} */
@Override
public boolean apply(ClusterNode e) {
return preferredNode.equals(e.id());
}
});
return Collections.singletonMap(new ComputeJobAdapter() {
@LoggerResource
private IgniteLogger log;
@IgniteInstanceResource
private Ignite ignite;
@Override
public Object execute() {
if (DEBUG_DATA)
log.info("Going to put data: " + data);
else
log.info("Going to put data [size=" + data.size() + ']');
IgniteCache<Object, Object> cache = ignite.cache(cacheName);
assert cache != null;
HashMap<Integer, Integer> putMap = U.newLinkedHashMap(TX_BOUND);
Iterator<Integer> it = data.iterator();
int cnt = 0;
final int RETRIES = 5;
while (it.hasNext()) {
Integer val = it.next();
putMap.put(val, val);
if (++cnt == TX_BOUND) {
if (DEBUG_DATA)
log.info("Putting keys to cache: " + putMap.keySet());
else
log.info("Putting keys to cache [size=" + putMap.size() + ']');
for (int i = 0; i < RETRIES; i++) {
try {
cache.putAll(putMap);
break;
} catch (CacheException e) {
if (i < RETRIES - 1)
log.info("Put error, will retry: " + e);
else
throw new IgniteException(e);
}
}
cnt = 0;
putMap = U.newLinkedHashMap(TX_BOUND);
}
}
assert cnt < TX_BOUND;
assert putMap.size() == (data.size() % TX_BOUND) : "putMap.size() = " + putMap.size();
if (DEBUG_DATA)
log.info("Putting keys to cache: " + putMap.keySet());
else
log.info("Putting keys to cache [size=" + putMap.size() + ']');
for (int i = 0; i < RETRIES; i++) {
try {
cache.putAll(putMap);
break;
} catch (CacheException e) {
if (i < RETRIES - 1)
log.info("Put error, will retry: " + e);
else
throw new IgniteException(e);
}
}
if (DEBUG_DATA)
log.info("Finished putting data: " + data);
else
log.info("Finished putting data [size=" + data.size() + ']');
return data;
}
}, targetNode);
}
Aggregations