Search in sources :

Example 31 with IgniteRunnable

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

the class IgniteAtomicStampedExample method main.

/**
     * Executes example.
     *
     * @param args Command line arguments, none required.
     * @throws Exception If example execution failed.
     */
public static void main(String[] args) throws Exception {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println(">>> Atomic stamped example started.");
        // Make name of atomic stamped.
        String stampedName = UUID.randomUUID().toString();
        // Make value of atomic stamped.
        String val = UUID.randomUUID().toString();
        // Make stamp of atomic stamped.
        String stamp = UUID.randomUUID().toString();
        // Initialize atomic stamped.
        IgniteAtomicStamped<String, String> stamped = ignite.atomicStamped(stampedName, val, stamp, true);
        System.out.println("Atomic stamped initial [value=" + stamped.value() + ", stamp=" + stamped.stamp() + ']');
        // Make closure for checking atomic stamped.
        IgniteRunnable c = new StampedUpdateClosure(stampedName);
        // Check atomic stamped on all cluster nodes.
        ignite.compute().broadcast(c);
        // Make new value of atomic stamped.
        String newVal = UUID.randomUUID().toString();
        // Make new stamp of atomic stamped.
        String newStamp = UUID.randomUUID().toString();
        System.out.println("Try to change value and stamp of atomic stamped with wrong expected value and stamp.");
        stamped.compareAndSet("WRONG EXPECTED VALUE", newVal, "WRONG EXPECTED STAMP", newStamp);
        // Check atomic stamped on all cluster nodes.
        // Atomic stamped value and stamp shouldn't be changed.
        ignite.compute().run(c);
        System.out.println("Try to change value and stamp of atomic stamped with correct value and stamp.");
        stamped.compareAndSet(val, newVal, stamp, newStamp);
        // Check atomic stamped on all cluster nodes.
        // Atomic stamped value and stamp should be changed.
        ignite.compute().run(c);
    }
    System.out.println();
    System.out.println("Finished atomic stamped example...");
    System.out.println("Check all nodes for output (this node is also part of the cluster).");
}
Also used : Ignite(org.apache.ignite.Ignite) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable)

Example 32 with IgniteRunnable

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

the class EventsExample method localListen.

/**
     * Listen to events that happen only on local node.
     *
     * @throws IgniteException If failed.
     */
private static void localListen() throws IgniteException {
    System.out.println();
    System.out.println(">>> Local event listener example.");
    Ignite ignite = Ignition.ignite();
    IgnitePredicate<TaskEvent> lsnr = new IgnitePredicate<TaskEvent>() {

        @Override
        public boolean apply(TaskEvent evt) {
            System.out.println("Received task event [evt=" + evt.name() + ", taskName=" + evt.taskName() + ']');
            // Return true to continue listening.
            return true;
        }
    };
    // Register event listener for all local task execution events.
    ignite.events().localListen(lsnr, EVTS_TASK_EXECUTION);
    // Generate task events.
    ignite.compute().withName("example-event-task").run(new IgniteRunnable() {

        @Override
        public void run() {
            System.out.println("Executing sample job.");
        }
    });
    // Unsubscribe local task event listener.
    ignite.events().stopLocalListen(lsnr);
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) TaskEvent(org.apache.ignite.events.TaskEvent) Ignite(org.apache.ignite.Ignite) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable)

Example 33 with IgniteRunnable

use of org.apache.ignite.lang.IgniteRunnable 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 34 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 35 with IgniteRunnable

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

the class TcpCommunicationSpiDropNodesTest method testTwoNodesEachOther.

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

        @Override
        public boolean apply(ClusterNode locNode, ClusterNode rmtNode) {
            return block && (locNode.order() == 2 || locNode.order() == 4) && (rmtNode.order() == 2 || rmtNode.order() == 4);
        }
    };
    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;
    final CyclicBarrier barrier = new CyclicBarrier(2);
    IgniteInternalFuture<Void> fut1 = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            barrier.await();
            grid(1).compute().withNoFailover().broadcast(new IgniteRunnable() {

                @Override
                public void run() {
                // No-op.
                }
            });
            return null;
        }
    });
    IgniteInternalFuture<Void> fut2 = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            barrier.await();
            grid(3).compute().withNoFailover().broadcast(new IgniteRunnable() {

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

        @Override
        public boolean apply() {
            return grid(2).cluster().nodes().size() == NODES_CNT - 1;
        }
    }, 5000);
    try {
        fut1.get();
    } catch (IgniteCheckedException e) {
    // No-op.
    }
    try {
        fut2.get();
    } catch (IgniteCheckedException e) {
    // No-op.
    }
    long failedNodeOrder = 1 + 2 + 3 + 4;
    for (ClusterNode node : grid(0).cluster().nodes()) failedNodeOrder -= node.order();
    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() != failedNodeOrder) {
                    assertEquals(NODES_CNT - 1, ignite.cluster().nodes().size());
                    for (ClusterNode node : ignite.cluster().nodes()) assertTrue(node.order() != failedNodeOrder);
                    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) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) CyclicBarrier(java.util.concurrent.CyclicBarrier) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteEx(org.apache.ignite.internal.IgniteEx) Event(org.apache.ignite.events.Event)

Aggregations

IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)45 Ignite (org.apache.ignite.Ignite)21 IgniteException (org.apache.ignite.IgniteException)11 IgniteEx (org.apache.ignite.internal.IgniteEx)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 IgniteInstanceResource (org.apache.ignite.resources.IgniteInstanceResource)7 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 ClusterNode (org.apache.ignite.cluster.ClusterNode)5 UUID (java.util.UUID)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 IgniteCompute (org.apache.ignite.IgniteCompute)4 TaskEvent (org.apache.ignite.events.TaskEvent)4 IgniteFuture (org.apache.ignite.lang.IgniteFuture)4 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 Event (org.apache.ignite.events.Event)3 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)3 Collection (java.util.Collection)2 RuntimeCamelException (org.apache.camel.RuntimeCamelException)2