Search in sources :

Example 36 with IgniteRunnable

use of org.apache.ignite.lang.IgniteRunnable in project ignite by apache.

the class GridCacheSetAbstractSelfTest method testAffinityRun.

/**
 * @throws Exception If failed.
 */
public void testAffinityRun() throws Exception {
    final CollectionConfiguration colCfg = collectionConfiguration();
    colCfg.setCollocated(false);
    colCfg.setCacheMode(CacheMode.PARTITIONED);
    colCfg.setGroupName("testGroup");
    try (final IgniteSet<Integer> set1 = grid(0).set("Set1", colCfg)) {
        GridTestUtils.assertThrows(log, new Callable<Void>() {

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

                    @Override
                    public void run() {
                    // No-op.
                    }
                });
                return null;
            }
        }, IgniteException.class, "Failed to execute affinityRun() for non-collocated set: " + set1.name() + ". This operation is supported only for collocated sets.");
    }
    colCfg.setCollocated(true);
    try (final IgniteSet<Integer> set2 = grid(0).set("Set2", colCfg)) {
        set2.add(100);
        final String cacheName = cctx(set2).name();
        set2.affinityRun(new IgniteRunnable() {

            @IgniteInstanceResource
            private IgniteEx ignite;

            @Override
            public void run() {
                assertTrue(ignite.cachex(cacheName).affinity().isPrimaryOrBackup(ignite.cluster().localNode(), "Set2"));
                assertEquals(100, set2.iterator().next().intValue());
            }
        });
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteEx(org.apache.ignite.internal.IgniteEx) CollectionConfiguration(org.apache.ignite.configuration.CollectionConfiguration) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable)

Example 37 with IgniteRunnable

use of org.apache.ignite.lang.IgniteRunnable in project ignite by apache.

the class IgniteComputeCustomExecutorSelfTest method testAllComputeApiByCustomExecutor.

/**
 * @throws Exception If fails.
 */
public void testAllComputeApiByCustomExecutor() throws Exception {
    IgniteCompute comp = grid(0).compute().withExecutor(EXEC_NAME0);
    comp.affinityRun(CACHE_NAME, primaryKey(grid(1).cache(CACHE_NAME)), new IgniteRunnable() {

        @Override
        public void run() {
            assertTrue(Thread.currentThread().getName().contains(EXEC_NAME0));
        }
    });
    comp.affinityCall(CACHE_NAME, 0, new IgniteCallable<Object>() {

        @Override
        public Object call() throws Exception {
            assertTrue(Thread.currentThread().getName().contains(EXEC_NAME0));
            return null;
        }
    });
    comp.broadcast(new IgniteRunnable() {

        @Override
        public void run() {
            assertTrue(Thread.currentThread().getName().contains(EXEC_NAME0));
        }
    });
    comp.broadcast(new IgniteCallable<Object>() {

        @Override
        public Object call() throws Exception {
            assertTrue(Thread.currentThread().getName().contains(EXEC_NAME0));
            return null;
        }
    });
    comp.broadcast(new IgniteClosure<Object, Object>() {

        @Override
        public Object apply(Object o) {
            assertTrue(Thread.currentThread().getName().contains(EXEC_NAME0));
            return null;
        }
    }, 0);
    comp.apply(new IgniteClosure<Object, Object>() {

        @Override
        public Object apply(Object o) {
            assertTrue(Thread.currentThread().getName().contains(EXEC_NAME0));
            return null;
        }
    }, 0);
    comp.apply(new IgniteClosure<Integer, Object>() {

        @Override
        public Object apply(Integer o) {
            assertTrue(Thread.currentThread().getName().contains(EXEC_NAME0));
            return null;
        }
    }, Collections.singletonList(0));
    comp.apply(new IgniteClosure<Integer, Object>() {

        @Override
        public Object apply(Integer o) {
            assertTrue(Thread.currentThread().getName().contains(EXEC_NAME0));
            return null;
        }
    }, Collections.singletonList(0), new IgniteReducer<Object, Object>() {

        @Override
        public boolean collect(@Nullable Object o) {
            return true;
        }

        @Override
        public Object reduce() {
            return null;
        }
    });
    List<IgniteCallable<Object>> calls = new ArrayList<>();
    for (int i = 0; i < GRID_CNT * 2; ++i) {
        calls.add(new IgniteCallable<Object>() {

            @Override
            public Object call() throws Exception {
                assertTrue(Thread.currentThread().getName().contains(EXEC_NAME0));
                return null;
            }
        });
    }
    comp.call(calls.get(0));
    comp.call(calls);
    comp.call(calls, new IgniteReducer<Object, Object>() {

        @Override
        public boolean collect(@Nullable Object o) {
            return true;
        }

        @Override
        public Object reduce() {
            return null;
        }
    });
    List<IgniteRunnable> runs = new ArrayList<>();
    for (int i = 0; i < GRID_CNT * 2; ++i) {
        runs.add(new IgniteRunnable() {

            @Override
            public void run() {
                assertTrue(Thread.currentThread().getName().contains(EXEC_NAME0));
            }
        });
    }
    comp.run(runs.get(0));
    comp.run(runs);
    comp.execute(TestTask.class, null);
}
Also used : ArrayList(java.util.ArrayList) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) IgniteException(org.apache.ignite.IgniteException) IgniteCallable(org.apache.ignite.lang.IgniteCallable) IgniteCompute(org.apache.ignite.IgniteCompute)

Example 38 with IgniteRunnable

use of org.apache.ignite.lang.IgniteRunnable in project ignite by apache.

the class GridFactorySelfTest method testCurrentIgnite.

/**
 * @throws Exception If failed.
 */
public void testCurrentIgnite() throws Exception {
    final String LEFT = "LEFT";
    final String RIGHT = "RIGHT";
    try {
        Ignite iLEFT = startGrid(LEFT);
        Ignite iRIGHT = startGrid(RIGHT);
        waitForDiscovery(iLEFT, iRIGHT);
        iLEFT.compute(iLEFT.cluster().forRemotes()).run(new IgniteRunnable() {

            @Override
            public void run() {
                assert Ignition.localIgnite().name().equals(RIGHT);
            }
        });
        iRIGHT.compute(iRIGHT.cluster().forRemotes()).run(new IgniteRunnable() {

            @Override
            public void run() {
                assert Ignition.localIgnite().name().equals(LEFT);
            }
        });
    } finally {
        stopAllGrids();
    }
}
Also used : Ignite(org.apache.ignite.Ignite) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable)

Example 39 with IgniteRunnable

use of org.apache.ignite.lang.IgniteRunnable in project ignite by apache.

the class GridMarshallerAbstractTest method testCompute.

/**
 * @throws Exception If failed.
 */
public void testCompute() throws Exception {
    IgniteConfiguration cfg = optimize(getConfiguration("g1"));
    try (Ignite g1 = G.start(cfg)) {
        IgniteCompute compute = compute(grid().cluster().forNode(g1.cluster().localNode()));
        compute.run(new IgniteRunnable() {

            @Override
            public void run() {
            // No-op.
            }
        });
        GridMarshallerTestBean inBean = newTestBean(compute);
        byte[] buf = marshal(inBean);
        GridMarshallerTestBean outBean = unmarshal(buf);
        assert inBean.getObjectField() != null;
        assert outBean.getObjectField() != null;
        assert inBean.getObjectField().getClass().equals(IgniteComputeImpl.class);
        assert outBean.getObjectField().getClass().equals(IgniteComputeImpl.class);
        assert inBean != outBean;
        assert inBean.equals(outBean);
        ClusterGroup inPrj = compute.clusterGroup();
        ClusterGroup outPrj = ((IgniteCompute) outBean.getObjectField()).clusterGroup();
        assert inPrj.getClass().equals(outPrj.getClass());
        assert F.eqNotOrdered(inPrj.nodes(), outPrj.nodes());
        outBean.checkNullResources();
    }
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) Ignite(org.apache.ignite.Ignite) IgniteCompute(org.apache.ignite.IgniteCompute) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable)

Example 40 with IgniteRunnable

use of org.apache.ignite.lang.IgniteRunnable in project ignite by apache.

the class TcpCommunicationSpiDropNodesTest method testOneNode.

/**
 * @throws Exception If failed.
 */
public void testOneNode() throws Exception {
    pred = new IgniteBiPredicate<ClusterNode, ClusterNode>() {

        @Override
        public boolean apply(ClusterNode locNode, ClusterNode rmtNode) {
            return block && rmtNode.order() == 3;
        }
    };
    startGrids(NODES_CNT);
    final CountDownLatch latch = new CountDownLatch(1);
    grid(0).events().localListen(new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event event) {
            latch.countDown();
            return true;
        }
    }, EVT_NODE_FAILED);
    // Wait for write timeout and closing idle connections.
    U.sleep(1000);
    block = true;
    grid(0).compute().broadcast(new IgniteRunnable() {

        @Override
        public void run() {
        // No-op.
        }
    });
    assertTrue(latch.await(15, TimeUnit.SECONDS));
    assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return grid(3).cluster().topologyVersion() == NODES_CNT + 1;
        }
    }, 5000));
    for (int i = 0; i < 10; i++) {
        U.sleep(1000);
        assertEquals(NODES_CNT - 1, grid(0).cluster().nodes().size());
        int liveNodesCnt = 0;
        for (int j = 0; j < NODES_CNT; j++) {
            IgniteEx ignite;
            try {
                ignite = grid(j);
                log.info("Checking topology for grid(" + j + "): " + ignite.cluster().nodes());
                ClusterNode locNode = ignite.localNode();
                if (locNode.order() != 3) {
                    assertEquals(NODES_CNT - 1, ignite.cluster().nodes().size());
                    for (ClusterNode node : ignite.cluster().nodes()) assertTrue(node.order() != 3);
                    liveNodesCnt++;
                }
            } catch (Exception e) {
                log.info("Checking topology for grid(" + j + "): no grid in topology.");
            }
        }
        assertEquals(NODES_CNT - 1, liveNodesCnt);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteEx(org.apache.ignite.internal.IgniteEx) Event(org.apache.ignite.events.Event) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) IgniteCheckedException(org.apache.ignite.IgniteCheckedException)

Aggregations

IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)43 Ignite (org.apache.ignite.Ignite)16 IgniteException (org.apache.ignite.IgniteException)12 IgniteEx (org.apache.ignite.internal.IgniteEx)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)7 IgniteInstanceResource (org.apache.ignite.resources.IgniteInstanceResource)7 ClusterNode (org.apache.ignite.cluster.ClusterNode)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 UUID (java.util.UUID)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 IgniteFuture (org.apache.ignite.lang.IgniteFuture)4 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 Event (org.apache.ignite.events.Event)3 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)3 GridTimeoutObject (org.apache.ignite.internal.processors.timeout.GridTimeoutObject)3 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)3 Serializable (java.io.Serializable)2 RuntimeCamelException (org.apache.camel.RuntimeCamelException)2