Search in sources :

Example 21 with IgniteInstanceResource

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

the class GridEventConsumeSelfTest method testResources.

/**
     * @throws Exception If failed.
     */
public void testResources() throws Exception {
    final Collection<UUID> nodeIds = new HashSet<>();
    final AtomicInteger cnt = new AtomicInteger();
    final CountDownLatch latch = new CountDownLatch(GRID_CNT);
    UUID consumeId = grid(0).events().remoteListen(new P2<UUID, Event>() {

        @IgniteInstanceResource
        private Ignite grid;

        @Override
        public boolean apply(UUID nodeId, Event evt) {
            info("Event from " + nodeId + " [" + evt.shortDisplay() + ']');
            assertEquals(EVT_JOB_STARTED, evt.type());
            assertNotNull(grid);
            nodeIds.add(nodeId);
            cnt.incrementAndGet();
            latch.countDown();
            return true;
        }
    }, new P1<Event>() {

        @IgniteInstanceResource
        private Ignite grid;

        @Override
        public boolean apply(Event evt) {
            assertNotNull(grid);
            return true;
        }
    }, EVT_JOB_STARTED);
    try {
        assertNotNull(consumeId);
        grid(0).compute().broadcast(F.noop());
        assert latch.await(2, SECONDS);
        assertEquals(GRID_CNT, nodeIds.size());
        assertEquals(GRID_CNT, cnt.get());
    } finally {
        grid(0).events().stopRemoteListen(consumeId);
    }
}
Also used : IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) JobEvent(org.apache.ignite.events.JobEvent) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) CountDownLatch(java.util.concurrent.CountDownLatch) HashSet(java.util.HashSet) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet)

Example 22 with IgniteInstanceResource

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

the class IgniteExecutorServiceTest method testExecute.

/**
     * @throws Exception Thrown in case of test failure.
     */
public void testExecute() throws Exception {
    Ignite ignite = G.ignite(getTestIgniteInstanceName());
    ExecutorService srvc = createExecutorService(ignite);
    srvc.execute(new Runnable() {

        @IgniteInstanceResource
        private Ignite ignite;

        @Override
        public void run() {
            System.out.println("Test message.");
            assert this.ignite != null;
        }
    });
    srvc.execute(new TestRunnable());
    srvc.shutdown();
}
Also used : IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) ExecutorService(java.util.concurrent.ExecutorService) Ignite(org.apache.ignite.Ignite)

Example 23 with IgniteInstanceResource

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

the class IgniteSemaphoreAbstractSelfTest method checkSemaphore.

/**
     * @throws Exception If failed.
     */
private void checkSemaphore() throws Exception {
    // Test API.
    checkAcquire();
    checkRelease();
    checkFailoverSafe();
    // Test main functionality.
    IgniteSemaphore semaphore1 = grid(0).semaphore("semaphore", -2, true, true);
    assertEquals(-2, semaphore1.availablePermits());
    IgniteFuture<Object> fut = grid(0).compute().callAsync(new IgniteCallable<Object>() {

        @IgniteInstanceResource
        private Ignite ignite;

        @LoggerResource
        private IgniteLogger log;

        @Nullable
        @Override
        public Object call() throws Exception {
            // Test semaphore in multiple threads on each node.
            IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

                @Nullable
                @Override
                public Object call() throws Exception {
                    IgniteSemaphore semaphore = ignite.semaphore("semaphore", -2, true, true);
                    assert semaphore != null && semaphore.availablePermits() == -2;
                    log.info("Thread is going to wait on semaphore: " + Thread.currentThread().getName());
                    assert semaphore.tryAcquire(1, 1, MINUTES);
                    log.info("Thread is again runnable: " + Thread.currentThread().getName());
                    semaphore.release();
                    return null;
                }
            }, 5, "test-thread");
            fut.get();
            return null;
        }
    });
    Thread.sleep(3000);
    semaphore1.release(2);
    assert semaphore1.availablePermits() == 0;
    semaphore1.release(1);
    // Ensure there are no hangs.
    fut.get();
    // Test operations on removed semaphore.
    semaphore1.close();
    checkRemovedSemaphore(semaphore1);
}
Also used : LoggerResource(org.apache.ignite.resources.LoggerResource) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) ExpectedException(org.junit.rules.ExpectedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteSemaphore(org.apache.ignite.IgniteSemaphore) Ignite(org.apache.ignite.Ignite) IgniteLogger(org.apache.ignite.IgniteLogger) Nullable(org.jetbrains.annotations.Nullable)

Example 24 with IgniteInstanceResource

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

the class GridCacheDhtMultiBackupTest method testPut.

/**
     * @throws Exception If failed
     */
public void testPut() throws Exception {
    try {
        Ignite g = G.start("examples/config/example-cache.xml");
        if (g.cluster().nodes().size() < 5)
            U.warn(log, "Topology is too small for this test. " + "Run with 4 remote nodes or more having large number of backup nodes.");
        g.compute().run(new CAX() {

            @IgniteInstanceResource
            private Ignite g;

            @Override
            public void applyx() {
                X.println("Checking whether cache is empty.");
                IgniteCache<SampleKey, SampleValue> cache = g.cache("partitioned");
                assert cache.localSize() == 0;
            }
        });
        IgniteCache<SampleKey, SampleValue> cache = g.cache("partitioned");
        int cnt = 0;
        for (int key = 0; key < 1000; key++) {
            SampleKey key1 = new SampleKey(key);
            if (!g.cluster().localNode().id().equals(g.affinity("partitioned").mapKeyToNode(key1).id())) {
                cache.put(key1, new SampleValue(key));
                cnt++;
            }
        }
        X.println(">>> Put count: " + cnt);
    } finally {
        G.stopAll(false);
    }
}
Also used : IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteCache(org.apache.ignite.IgniteCache) Ignite(org.apache.ignite.Ignite) CAX(org.apache.ignite.internal.util.typedef.CAX)

Example 25 with IgniteInstanceResource

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

the class ComputeBroadcastExample method gatherSystemInfo.

/**
     * Gather system info from all nodes and print it out.
     *
     * @param ignite Ignite instance.
     * @throws IgniteException if failed.
     */
private static void gatherSystemInfo(Ignite ignite) throws IgniteException {
    // Gather system info from all nodes.
    Collection<String> res = ignite.compute().broadcast(new IgniteCallable<String>() {

        // Automatically inject ignite instance.
        @IgniteInstanceResource
        private Ignite ignite;

        @Override
        public String call() {
            System.out.println();
            System.out.println("Executing task on node: " + ignite.cluster().localNode().id());
            return "Node ID: " + ignite.cluster().localNode().id() + "\n" + "OS: " + System.getProperty("os.name") + " " + System.getProperty("os.version") + " " + System.getProperty("os.arch") + "\n" + "User: " + System.getProperty("user.name") + "\n" + "JRE: " + System.getProperty("java.runtime.name") + " " + System.getProperty("java.runtime.version");
        }
    });
    // Print result.
    System.out.println();
    System.out.println("Nodes system information:");
    System.out.println();
    for (String r : res) {
        System.out.println(r);
        System.out.println();
    }
}
Also used : IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) Ignite(org.apache.ignite.Ignite)

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