Search in sources :

Example 6 with ComputeJobAdapter

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);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) LoggerResource(org.apache.ignite.resources.LoggerResource) HashMap(java.util.HashMap) CacheException(javax.cache.CacheException) IgniteCache(org.apache.ignite.IgniteCache) ComputeJobAdapter(org.apache.ignite.compute.ComputeJobAdapter) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteException(org.apache.ignite.IgniteException) Iterator(java.util.Iterator) Ignite(org.apache.ignite.Ignite) IgniteLogger(org.apache.ignite.IgniteLogger)

Aggregations

ComputeJobAdapter (org.apache.ignite.compute.ComputeJobAdapter)6 HashMap (java.util.HashMap)4 ClusterNode (org.apache.ignite.cluster.ClusterNode)4 ArrayList (java.util.ArrayList)3 Ignite (org.apache.ignite.Ignite)3 UUID (java.util.UUID)2 IgniteException (org.apache.ignite.IgniteException)2 ComputeJob (org.apache.ignite.compute.ComputeJob)2 ComputeTaskSession (org.apache.ignite.compute.ComputeTaskSession)2 IgniteInstanceResource (org.apache.ignite.resources.IgniteInstanceResource)2 Serializable (java.io.Serializable)1 Iterator (java.util.Iterator)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 CacheException (javax.cache.CacheException)1 IgniteCache (org.apache.ignite.IgniteCache)1 IgniteLogger (org.apache.ignite.IgniteLogger)1 LoggerResource (org.apache.ignite.resources.LoggerResource)1 Nullable (org.jetbrains.annotations.Nullable)1 AbstractBeanFactory (org.springframework.beans.factory.support.AbstractBeanFactory)1 XmlBeanFactory (org.springframework.beans.factory.xml.XmlBeanFactory)1