Search in sources :

Example 26 with IgniteInstanceResource

use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.

the class IgniteCacheLockPartitionOnAffinityRunTest method testReleasePartitionJobImplementMasterLeave.

/**
     * @throws Exception If failed.
     */
public void testReleasePartitionJobImplementMasterLeave() throws Exception {
    final int orgId = primaryKey(grid(0).cache(Organization.class.getSimpleName()));
    try {
        grid(1).compute().affinityRunAsync(Arrays.asList(Organization.class.getSimpleName(), Person.class.getSimpleName()), new Integer(orgId), new RunnableWithMasterLeave() {

            @IgniteInstanceResource
            private Ignite ignite;

            @Override
            public void onMasterNodeLeft(ComputeTaskSession ses) throws IgniteException {
            // No-op.
            }

            @Override
            public void run() {
                try {
                    checkPartitionsReservations((IgniteEx) ignite, orgId, 1);
                } catch (Exception e) {
                    e.printStackTrace();
                    fail("Unexpected exception");
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ignored) {
                // No-op.
                }
            }
        });
        stopGrid(1, true);
        Thread.sleep(3000);
        awaitPartitionMapExchange();
        checkPartitionsReservations(grid(0), orgId, 0);
    } finally {
        startGrid(1);
        awaitPartitionMapExchange();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteException(org.apache.ignite.IgniteException) IgniteEx(org.apache.ignite.internal.IgniteEx) Ignite(org.apache.ignite.Ignite) ComputeTaskSession(org.apache.ignite.compute.ComputeTaskSession) IgniteException(org.apache.ignite.IgniteException) IOException(java.io.IOException)

Example 27 with IgniteInstanceResource

use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.

the class IgniteCacheLockPartitionOnAffinityRunTest method testReleasePartitionJobMasterLeave.

/**
     * @throws Exception If failed.
     */
public void testReleasePartitionJobMasterLeave() throws Exception {
    final int orgId = primaryKey(grid(0).cache(Organization.class.getSimpleName()));
    try {
        grid(1).compute().affinityRunAsync(Arrays.asList(Organization.class.getSimpleName(), Person.class.getSimpleName()), new Integer(orgId), new IgniteRunnable() {

            @IgniteInstanceResource
            private Ignite ignite;

            @Override
            public void run() {
                try {
                    checkPartitionsReservations((IgniteEx) ignite, orgId, 1);
                } catch (Exception e) {
                    e.printStackTrace();
                    fail("Unexpected exception");
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ignored) {
                // No-op.
                }
            }
        });
        stopGrid(1, true);
        Thread.sleep(3000);
        awaitPartitionMapExchange();
        checkPartitionsReservations(grid(0), orgId, 0);
    } finally {
        startGrid(1);
        awaitPartitionMapExchange();
    }
    try {
        grid(1).compute().affinityCallAsync(Arrays.asList(Organization.class.getSimpleName(), Person.class.getSimpleName()), new Integer(orgId), new IgniteCallable<Object>() {

            @IgniteInstanceResource
            private Ignite ignite;

            @Override
            public Object call() {
                try {
                    checkPartitionsReservations((IgniteEx) ignite, orgId, 1);
                } catch (Exception e) {
                    e.printStackTrace();
                    fail("Unexpected exception");
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ignored) {
                // No-op.
                }
                return null;
            }
        });
        stopGrid(1, true);
        Thread.sleep(3000);
        awaitPartitionMapExchange();
        checkPartitionsReservations(grid(0), orgId, 0);
    } finally {
        startGrid(1);
        awaitPartitionMapExchange();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteEx(org.apache.ignite.internal.IgniteEx) Ignite(org.apache.ignite.Ignite) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) IgniteException(org.apache.ignite.IgniteException) IOException(java.io.IOException)

Example 28 with IgniteInstanceResource

use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.

the class ClosureServiceClientsNodesTest method testClientClosure.

/**
     * @throws Exception If failed.
     */
public void testClientClosure() throws Exception {
    for (int i = 0; i < NODES_CNT; i++) {
        log.info("Iteration: " + i);
        Ignite ignite = grid(i);
        Collection<String> res = ignite.compute(ignite.cluster().forClients()).broadcast(new IgniteCallable<String>() {

            @IgniteInstanceResource
            Ignite ignite;

            @Override
            public String call() throws Exception {
                assertTrue(ignite.configuration().isClientMode());
                return ignite.name();
            }
        });
        assertEquals(1, res.size());
        assertEquals(getTestIgniteInstanceName(0), F.first(res));
    }
}
Also used : IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) Ignite(org.apache.ignite.Ignite)

Example 29 with IgniteInstanceResource

use of org.apache.ignite.resources.IgniteInstanceResource 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)

Example 30 with IgniteInstanceResource

use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.

the class GridCacheQueueApiSelfAbstractTest method testAffinityRun.

/**
     * @throws Exception If failed.
     */
public void testAffinityRun() throws Exception {
    final CollectionConfiguration colCfg = collectionConfiguration();
    colCfg.setCollocated(false);
    colCfg.setCacheMode(CacheMode.PARTITIONED);
    try (final IgniteQueue<Integer> queue1 = grid(0).queue("Queue1", 0, colCfg)) {
        GridTestUtils.assertThrows(log, new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                queue1.affinityRun(new IgniteRunnable() {

                    @Override
                    public void run() {
                    // No-op.
                    }
                });
                return null;
            }
        }, IgniteException.class, "Failed to execute affinityRun() for non-collocated queue: " + queue1.name() + ". This operation is supported only for collocated queues.");
    }
    colCfg.setCollocated(true);
    try (final IgniteQueue<Integer> queue2 = grid(0).queue("Queue2", 0, colCfg)) {
        queue2.add(100);
        queue2.affinityRun(new IgniteRunnable() {

            @IgniteInstanceResource
            private IgniteEx ignite;

            @Override
            public void run() {
                assertTrue(ignite.cachex("datastructures_0").affinity().isPrimaryOrBackup(ignite.cluster().localNode(), "Queue2"));
                assertEquals(100, queue2.take().intValue());
            }
        });
    }
}
Also used : IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteEx(org.apache.ignite.internal.IgniteEx) CollectionConfiguration(org.apache.ignite.configuration.CollectionConfiguration) IgniteException(org.apache.ignite.IgniteException) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable)

Aggregations

IgniteInstanceResource (org.apache.ignite.resources.IgniteInstanceResource)44 Ignite (org.apache.ignite.Ignite)36 IgniteException (org.apache.ignite.IgniteException)17 IgniteCache (org.apache.ignite.IgniteCache)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 IgniteEx (org.apache.ignite.internal.IgniteEx)10 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)7 UUID (java.util.UUID)7 IgniteCallable (org.apache.ignite.lang.IgniteCallable)7 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)7 IgniteLogger (org.apache.ignite.IgniteLogger)6 ClusterNode (org.apache.ignite.cluster.ClusterNode)6 LoggerResource (org.apache.ignite.resources.LoggerResource)6 Collection (java.util.Collection)5 Callable (java.util.concurrent.Callable)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 HashMap (java.util.HashMap)4 List (java.util.List)4 Cache (javax.cache.Cache)4