Search in sources :

Example 1 with ConcurrentHashMap8

use of org.jsr166.ConcurrentHashMap8 in project ignite by apache.

the class GridCacheQueryManager method start0.

/** {@inheritDoc} */
@Override
public void start0() throws IgniteCheckedException {
    CacheConfiguration ccfg = cctx.config();
    qryProcEnabled = QueryUtils.isEnabled(ccfg);
    qryProc = cctx.kernalContext().query();
    cacheName = cctx.name();
    enabled = qryProcEnabled || (isIndexingSpiEnabled() && !CU.isSystemCache(cacheName));
    maxIterCnt = ccfg.getMaxQueryIteratorsCount();
    detailMetricsSz = ccfg.getQueryDetailMetricsSize();
    if (detailMetricsSz > 0)
        detailMetrics = new ConcurrentHashMap8<>(detailMetricsSz);
    lsnr = new GridLocalEventListener() {

        @Override
        public void onEvent(Event evt) {
            UUID nodeId = ((DiscoveryEvent) evt).eventNode().id();
            Map<Long, GridFutureAdapter<QueryResult<K, V>>> futs = qryIters.remove(nodeId);
            if (futs != null) {
                for (Map.Entry<Long, GridFutureAdapter<QueryResult<K, V>>> entry : futs.entrySet()) {
                    final Object rcpt = recipient(nodeId, entry.getKey());
                    entry.getValue().listen(new CIX1<IgniteInternalFuture<QueryResult<K, V>>>() {

                        @Override
                        public void applyx(IgniteInternalFuture<QueryResult<K, V>> f) throws IgniteCheckedException {
                            f.get().closeIfNotShared(rcpt);
                        }
                    });
                }
            }
            Map<Long, GridFutureAdapter<FieldsResult>> fieldsFuts = fieldsQryRes.remove(nodeId);
            if (fieldsFuts != null) {
                for (Map.Entry<Long, GridFutureAdapter<FieldsResult>> entry : fieldsFuts.entrySet()) {
                    final Object rcpt = recipient(nodeId, entry.getKey());
                    entry.getValue().listen(new CIX1<IgniteInternalFuture<FieldsResult>>() {

                        @Override
                        public void applyx(IgniteInternalFuture<FieldsResult> f) throws IgniteCheckedException {
                            f.get().closeIfNotShared(rcpt);
                        }
                    });
                }
            }
        }
    };
    cctx.events().addListener(lsnr, EVT_NODE_LEFT, EVT_NODE_FAILED);
    qryTopVer = cctx.startTopologyVersion();
    assert qryTopVer != null : cctx.name();
}
Also used : CIX1(org.apache.ignite.internal.util.typedef.CIX1) GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) ConcurrentHashMap8(org.jsr166.ConcurrentHashMap8) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) CacheQueryExecutedEvent(org.apache.ignite.events.CacheQueryExecutedEvent) Event(org.apache.ignite.events.Event) CacheQueryReadEvent(org.apache.ignite.events.CacheQueryReadEvent) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) UUID(java.util.UUID) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) GridLeanMap(org.apache.ignite.internal.util.GridLeanMap) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 2 with ConcurrentHashMap8

use of org.jsr166.ConcurrentHashMap8 in project ignite by apache.

the class CacheContinuousQueryCounterAbstractTest method testLoadCache.

/**
     * @throws Exception If failed.
     */
public void testLoadCache() throws Exception {
    IgniteCache<Integer, Integer> cache = grid(0).cache(CACHE_NAME);
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    final Map<Integer, T2<Integer, Long>> map = new ConcurrentHashMap8<>();
    final CountDownLatch latch = new CountDownLatch(10);
    qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> e : evts) {
                map.put(e.getKey(), new T2<>(e.getValue(), e.unwrap(CacheQueryEntryEvent.class).getPartitionUpdateCounter()));
                latch.countDown();
            }
        }
    });
    try (QueryCursor<Cache.Entry<Integer, Integer>> ignored = cache.query(qry)) {
        cache.loadCache(null, 0);
        assert latch.await(LATCH_TIMEOUT, MILLISECONDS) : "Count: " + latch.getCount();
        assertEquals(10, map.size());
        for (int i = 0; i < 10; i++) {
            assertEquals(i, (int) map.get(i).get1());
            assertEquals((long) 1, (long) map.get(i).get2());
        }
    }
}
Also used : ConcurrentHashMap8(org.jsr166.ConcurrentHashMap8) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) T2(org.apache.ignite.internal.util.typedef.T2)

Example 3 with ConcurrentHashMap8

use of org.jsr166.ConcurrentHashMap8 in project ignite by apache.

the class GridCacheAbstractRemoveFailureTest method putAndRemove.

/**
     * @param duration Test duration.
     * @param txConcurrency Transaction concurrency if test explicit transaction.
     * @param txIsolation Transaction isolation if test explicit transaction.
     * @throws Exception If failed.
     */
private void putAndRemove(long duration, final TransactionConcurrency txConcurrency, final TransactionIsolation txIsolation) throws Exception {
    assertEquals(testClientNode(), (boolean) grid(0).configuration().isClientMode());
    grid(0).destroyCache(DEFAULT_CACHE_NAME);
    CacheConfiguration<Integer, Integer> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
    ccfg.setWriteSynchronizationMode(FULL_SYNC);
    ccfg.setCacheMode(cacheMode());
    if (cacheMode() == PARTITIONED)
        ccfg.setBackups(1);
    ccfg.setAtomicityMode(atomicityMode());
    ccfg.setNearConfiguration(nearCache());
    final IgniteCache<Integer, Integer> sndCache0 = grid(0).createCache(ccfg);
    final AtomicBoolean stop = new AtomicBoolean();
    final AtomicLong cntr = new AtomicLong();
    final AtomicLong errCntr = new AtomicLong();
    // Expected values in cache.
    final Map<Integer, GridTuple<Integer>> expVals = new ConcurrentHashMap8<>();
    final AtomicReference<CyclicBarrier> cmp = new AtomicReference<>();
    IgniteInternalFuture<?> updateFut = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            Thread.currentThread().setName("update-thread");
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            IgniteTransactions txs = sndCache0.unwrap(Ignite.class).transactions();
            while (!stop.get()) {
                for (int i = 0; i < 100; i++) {
                    int key = rnd.nextInt(KEYS_CNT);
                    boolean put = rnd.nextInt(0, 100) > 10;
                    while (true) {
                        try {
                            if (put) {
                                boolean failed = false;
                                if (txConcurrency != null) {
                                    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
                                        sndCache0.put(key, i);
                                        tx.commit();
                                    } catch (CacheException | IgniteException e) {
                                        if (!X.hasCause(e, ClusterTopologyCheckedException.class)) {
                                            log.error("Unexpected error: " + e);
                                            throw e;
                                        }
                                        failed = true;
                                    }
                                } else
                                    sndCache0.put(key, i);
                                if (!failed)
                                    expVals.put(key, F.t(i));
                            } else {
                                boolean failed = false;
                                if (txConcurrency != null) {
                                    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
                                        sndCache0.remove(key);
                                        tx.commit();
                                    } catch (CacheException | IgniteException e) {
                                        if (!X.hasCause(e, ClusterTopologyCheckedException.class)) {
                                            log.error("Unexpected error: " + e);
                                            throw e;
                                        }
                                        failed = true;
                                    }
                                } else
                                    sndCache0.remove(key);
                                if (!failed)
                                    expVals.put(key, F.<Integer>t(null));
                            }
                            break;
                        } catch (CacheException e) {
                            if (put)
                                log.error("Put failed [key=" + key + ", val=" + i + ']', e);
                            else
                                log.error("Remove failed [key=" + key + ']', e);
                            errCntr.incrementAndGet();
                        }
                    }
                }
                cntr.addAndGet(100);
                CyclicBarrier barrier = cmp.get();
                if (barrier != null) {
                    log.info("Wait data check.");
                    barrier.await(60_000, TimeUnit.MILLISECONDS);
                    log.info("Finished wait data check.");
                }
            }
            return null;
        }
    });
    IgniteInternalFuture<?> killFut = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            Thread.currentThread().setName("restart-thread");
            while (!stop.get()) {
                U.sleep(random(KILL_DELAY.get1(), KILL_DELAY.get2()));
                killAndRestart(stop);
                CyclicBarrier barrier = cmp.get();
                if (barrier != null) {
                    log.info("Wait data check.");
                    barrier.await(60_000, TimeUnit.MILLISECONDS);
                    log.info("Finished wait data check.");
                }
            }
            return null;
        }
    });
    try {
        long stopTime = duration + U.currentTimeMillis();
        long nextAssert = U.currentTimeMillis() + ASSERT_FREQ;
        while (U.currentTimeMillis() < stopTime) {
            long start = System.nanoTime();
            long ops = cntr.longValue();
            U.sleep(1000);
            long diff = cntr.longValue() - ops;
            double time = (System.nanoTime() - start) / 1_000_000_000d;
            long opsPerSecond = (long) (diff / time);
            log.info("Operations/second: " + opsPerSecond);
            if (U.currentTimeMillis() >= nextAssert) {
                CyclicBarrier barrier = new CyclicBarrier(3, new Runnable() {

                    @Override
                    public void run() {
                        try {
                            cmp.set(null);
                            log.info("Checking cache content.");
                            assertCacheContent(expVals);
                            log.info("Finished check cache content.");
                        } catch (Throwable e) {
                            log.error("Unexpected error: " + e, e);
                            throw e;
                        }
                    }
                });
                log.info("Start cache content check.");
                cmp.set(barrier);
                try {
                    barrier.await(60_000, TimeUnit.MILLISECONDS);
                } catch (TimeoutException e) {
                    U.dumpThreads(log);
                    fail("Failed to check cache content: " + e);
                }
                log.info("Cache content check done.");
                nextAssert = System.currentTimeMillis() + ASSERT_FREQ;
            }
        }
    } finally {
        stop.set(true);
    }
    killFut.get();
    updateFut.get();
    log.info("Test finished. Update errors: " + errCntr.get());
}
Also used : CacheException(javax.cache.CacheException) IgniteTransactions(org.apache.ignite.IgniteTransactions) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) TimeoutException(java.util.concurrent.TimeoutException) ConcurrentHashMap8(org.jsr166.ConcurrentHashMap8) GridTuple(org.apache.ignite.internal.util.lang.GridTuple) AtomicReference(java.util.concurrent.atomic.AtomicReference) TimeoutException(java.util.concurrent.TimeoutException) CacheException(javax.cache.CacheException) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) Transaction(org.apache.ignite.transactions.Transaction) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 4 with ConcurrentHashMap8

use of org.jsr166.ConcurrentHashMap8 in project ignite by apache.

the class GridOffHeapPartitionedMapAbstractSelfTest method testPartitionIteratorMultithreaded.

/**
     * @throws Exception If failed.
     */
public void testPartitionIteratorMultithreaded() throws Exception {
    initCap = 10;
    map = newMap();
    final AtomicInteger rehashes = new AtomicInteger();
    final AtomicInteger releases = new AtomicInteger();
    map.eventListener(new GridOffHeapEventListener() {

        @Override
        public void onEvent(GridOffHeapEvent evt) {
            switch(evt) {
                case REHASH:
                    rehashes.incrementAndGet();
                    break;
                case RELEASE:
                    releases.incrementAndGet();
                    break;
                // No-op.
                default:
            }
        }
    });
    final int max = 64;
    int threads = 5;
    final Map<String, String> m = new ConcurrentHashMap8<>(max);
    multithreaded(new Callable() {

        @Override
        public Object call() throws Exception {
            for (int p = 0; p < parts; p++) {
                for (int i = 0; i < max; i++) {
                    String key = string();
                    String val = string();
                    // info("Storing [i=" + i + ", key=" + key + ", val=" + val + ']');
                    m.put(key, val);
                    assertTrue(map.put(p, hash(key), key.getBytes(), val.getBytes()));
                    assertTrue(map.contains(p, hash(key), key.getBytes()));
                    assertNotNull(map.get(p, hash(key), key.getBytes()));
                    assertEquals(new String(map.get(p, hash(key), key.getBytes())), val);
                    try (GridCloseableIterator<IgniteBiTuple<byte[], byte[]>> it = map.iterator(p)) {
                        while (it.hasNext()) {
                            IgniteBiTuple<byte[], byte[]> t = it.next();
                            String k = new String(t.get1());
                            String v = new String(t.get2());
                            // info("Entry [k=" + k + ", v=" + v + ']');
                            assertEquals(m.get(k), v);
                        }
                    }
                }
            }
            return null;
        }
    }, threads);
    assertEquals(max * threads * parts, map.size());
    info("Stats [size=" + map.size() + ", rehashes=" + rehashes + ", releases=" + releases + ']');
    assertTrue(rehashes.get() > 0);
    assertEquals(rehashes.get(), releases.get());
}
Also used : GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) ConcurrentHashMap8(org.jsr166.ConcurrentHashMap8) Callable(java.util.concurrent.Callable) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 5 with ConcurrentHashMap8

use of org.jsr166.ConcurrentHashMap8 in project ignite by apache.

the class IgniteCacheAbstractQuerySelfTest method testTextQueryEvents.

/**
     * @throws Exception If failed.
     */
public void testTextQueryEvents() throws Exception {
    final Map<UUID, Person> map = new ConcurrentHashMap8<>();
    final CountDownLatch latch = new CountDownLatch(2);
    final CountDownLatch execLatch = new CountDownLatch(cacheMode() == REPLICATED ? 1 : gridCount());
    final IgniteCache<UUID, Person> cache = jcache(UUID.class, Person.class);
    IgnitePredicate[] objReadLsnrs = new IgnitePredicate[gridCount()];
    IgnitePredicate[] qryExecLsnrs = new IgnitePredicate[gridCount()];
    for (int i = 0; i < gridCount(); i++) {
        IgnitePredicate<Event> objReadPred = new IgnitePredicate<Event>() {

            @Override
            public boolean apply(Event evt) {
                assert evt instanceof CacheQueryReadEvent;
                CacheQueryReadEvent<UUID, Person> qe = (CacheQueryReadEvent<UUID, Person>) evt;
                assertEquals(FULL_TEXT.name(), qe.queryType());
                assertEquals(cache.getName(), qe.cacheName());
                assertEquals("Person", qe.className());
                assertEquals("White", qe.clause());
                assertNull(qe.scanQueryFilter());
                assertNull(qe.continuousQueryFilter());
                assertNull(qe.arguments());
                map.put(qe.key(), qe.value());
                latch.countDown();
                return true;
            }
        };
        grid(i).events().localListen(objReadPred, EVT_CACHE_QUERY_OBJECT_READ);
        objReadLsnrs[i] = objReadPred;
        IgnitePredicate<Event> qryExecPred = new IgnitePredicate<Event>() {

            @Override
            public boolean apply(Event evt) {
                assert evt instanceof CacheQueryExecutedEvent;
                CacheQueryExecutedEvent qe = (CacheQueryExecutedEvent) evt;
                assertEquals(FULL_TEXT.name(), qe.queryType());
                assertEquals(cache.getName(), qe.cacheName());
                assertEquals("Person", qe.className());
                assertEquals("White", qe.clause());
                assertNull(qe.scanQueryFilter());
                assertNull(qe.continuousQueryFilter());
                assertNull(qe.arguments());
                execLatch.countDown();
                return true;
            }
        };
        grid(i).events().localListen(qryExecPred, EVT_CACHE_QUERY_EXECUTED);
        qryExecLsnrs[i] = qryExecPred;
    }
    try {
        UUID k1 = UUID.randomUUID();
        UUID k2 = UUID.randomUUID();
        UUID k3 = UUID.randomUUID();
        cache.put(k1, new Person("Bob White", 1000));
        cache.put(k2, new Person("Tom White", 1000));
        cache.put(k3, new Person("Mike Green", 1000));
        QueryCursor<Cache.Entry<UUID, Person>> q = cache.query(new TextQuery<UUID, Person>(Person.class, "White"));
        q.getAll();
        assert latch.await(1000, MILLISECONDS);
        assert execLatch.await(1000, MILLISECONDS);
        assertEquals(2, map.size());
        assertEquals("Bob White", map.get(k1).name());
        assertEquals("Tom White", map.get(k2).name());
    } finally {
        for (int i = 0; i < gridCount(); i++) {
            grid(i).events().stopLocalListen(objReadLsnrs[i]);
            grid(i).events().stopLocalListen(qryExecLsnrs[i]);
        }
    }
}
Also used : CacheQueryReadEvent(org.apache.ignite.events.CacheQueryReadEvent) ConcurrentHashMap8(org.jsr166.ConcurrentHashMap8) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CountDownLatch(java.util.concurrent.CountDownLatch) CacheQueryExecutedEvent(org.apache.ignite.events.CacheQueryExecutedEvent) CacheQueryExecutedEvent(org.apache.ignite.events.CacheQueryExecutedEvent) Event(org.apache.ignite.events.Event) CacheQueryReadEvent(org.apache.ignite.events.CacheQueryReadEvent) UUID(java.util.UUID)

Aggregations

ConcurrentHashMap8 (org.jsr166.ConcurrentHashMap8)19 CountDownLatch (java.util.concurrent.CountDownLatch)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 CacheEntryEvent (javax.cache.event.CacheEntryEvent)5 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)5 Random (java.util.Random)4 UUID (java.util.UUID)4 CacheQueryExecutedEvent (org.apache.ignite.events.CacheQueryExecutedEvent)3 CacheQueryReadEvent (org.apache.ignite.events.CacheQueryReadEvent)3 Event (org.apache.ignite.events.Event)3 Callable (java.util.concurrent.Callable)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)2 IgniteKernal (org.apache.ignite.internal.IgniteKernal)2 GridIoManager (org.apache.ignite.internal.managers.communication.GridIoManager)2 GridMessageListener (org.apache.ignite.internal.managers.communication.GridMessageListener)2