Search in sources :

Example 6 with LoggerResource

use of org.apache.ignite.resources.LoggerResource 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 7 with LoggerResource

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

the class IgniteLockAbstractSelfTest method checkReentrantLock.

/**
     * @throws Exception If failed.
     */
private void checkReentrantLock(final boolean fair) throws Exception {
    // Test API.
    checkLock(fair);
    checkFailoverSafe(fair);
    // Test main functionality.
    IgniteLock lock1 = grid(0).reentrantLock("lock", true, fair, true);
    assertFalse(lock1.isLocked());
    lock1.lock();
    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 reentrant lock in multiple threads on each node.
            IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

                @Nullable
                @Override
                public Object call() throws Exception {
                    IgniteLock lock = ignite.reentrantLock("lock", true, fair, true);
                    assert lock != null;
                    log.info("Thread is going to wait on reentrant lock: " + Thread.currentThread().getName());
                    assert lock.tryLock(1, MINUTES);
                    log.info("Thread is again runnable: " + Thread.currentThread().getName());
                    lock.unlock();
                    return null;
                }
            }, 5, "test-thread");
            fut.get();
            return null;
        }
    });
    Thread.sleep(3000);
    assert lock1.isHeldByCurrentThread();
    assert lock1.getHoldCount() == 1;
    lock1.lock();
    assert lock1.isHeldByCurrentThread();
    assert lock1.getHoldCount() == 2;
    lock1.unlock();
    lock1.unlock();
    // Ensure there are no hangs.
    fut.get();
    // Test operations on removed lock.
    lock1.close();
    checkRemovedReentrantLock(lock1);
}
Also used : LoggerResource(org.apache.ignite.resources.LoggerResource) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) ExpectedException(org.junit.rules.ExpectedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteLock(org.apache.ignite.IgniteLock) Ignite(org.apache.ignite.Ignite) IgniteLogger(org.apache.ignite.IgniteLogger) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with LoggerResource

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

the class IgniteCountDownLatchAbstractSelfTest method checkLatch.

/**
     * @throws Exception If failed.
     */
private void checkLatch() throws Exception {
    // Test API.
    checkAutoDelete();
    checkAwait();
    checkCountDown();
    // Test main functionality.
    IgniteCountDownLatch latch1 = grid(0).countDownLatch("latch", 2, false, true);
    assertEquals(2, latch1.count());
    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 latch in multiple threads on each node.
            IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

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

Example 9 with LoggerResource

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

the class GridLoggerInjectionSelfTest method testClosureField.

/**
     * Test that closure gets right log category injected on all nodes using field injection.
     *
     * @throws Exception If failed.
     */
public void testClosureField() throws Exception {
    Ignite ignite = grid(0);
    ignite.compute().call(new IgniteCallable<Object>() {

        @LoggerResource(categoryClass = GridLoggerInjectionSelfTest.class)
        private IgniteLogger log;

        @Override
        public Object call() throws Exception {
            if (log instanceof GridLoggerProxy) {
                Object category = U.field(log, "ctgr");
                assertTrue("Logger created for the wrong category.", category.toString().contains(GridLoggerInjectionSelfTest.class.getName()));
            } else
                fail("This test should be run with proxy logger.");
            return null;
        }
    });
}
Also used : LoggerResource(org.apache.ignite.resources.LoggerResource) GridLoggerProxy(org.apache.ignite.internal.GridLoggerProxy) Ignite(org.apache.ignite.Ignite) IgniteLogger(org.apache.ignite.IgniteLogger) IOException(java.io.IOException)

Aggregations

Ignite (org.apache.ignite.Ignite)9 IgniteLogger (org.apache.ignite.IgniteLogger)9 LoggerResource (org.apache.ignite.resources.LoggerResource)9 IOException (java.io.IOException)6 IgniteInstanceResource (org.apache.ignite.resources.IgniteInstanceResource)6 Callable (java.util.concurrent.Callable)3 GridLoggerProxy (org.apache.ignite.internal.GridLoggerProxy)3 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)3 IgniteCallable (org.apache.ignite.lang.IgniteCallable)3 Nullable (org.jetbrains.annotations.Nullable)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 IgniteException (org.apache.ignite.IgniteException)2 IgniteFileSystem (org.apache.ignite.IgniteFileSystem)2 FileSystemResource (org.apache.ignite.resources.FileSystemResource)2 ExpectedException (org.junit.rules.ExpectedException)2 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 CacheException (javax.cache.CacheException)1 IgniteCache (org.apache.ignite.IgniteCache)1 IgniteCountDownLatch (org.apache.ignite.IgniteCountDownLatch)1