Search in sources :

Example 1 with READ_WRITE_SAFE

use of org.apache.ignite.cache.PartitionLossPolicy.READ_WRITE_SAFE in project ignite by apache.

the class BinaryMetadataRegistrationInsideEntryProcessorTest method testContinuousQueryAndBinaryObjectBuilder.

/**
 * Continuously execute multiple EntryProcessors with having continuous queries in parallel.
 * This used to lead to several deadlocks.
 *
 * @throws Exception If failed.
 */
@Test
public void testContinuousQueryAndBinaryObjectBuilder() throws Exception {
    startGrids(3).cluster().active(true);
    grid(0).createCache(new CacheConfiguration<>().setName(CACHE_NAME).setAtomicityMode(ATOMIC).setBackups(2).setCacheMode(PARTITIONED).setWriteSynchronizationMode(FULL_SYNC).setPartitionLossPolicy(READ_WRITE_SAFE));
    IgniteEx client1 = startClientGrid(getConfiguration().setIgniteInstanceName("client1"));
    IgniteEx client2 = startClientGrid(getConfiguration().setIgniteInstanceName("client2"));
    AtomicBoolean stop = new AtomicBoolean();
    AtomicInteger keyCntr = new AtomicInteger();
    AtomicInteger binaryTypeCntr = new AtomicInteger();
    /**
     */
    class MyEntryProcessor implements CacheEntryProcessor<Object, Object, Object> {

        /**
         * Cached int value retrieved from {@code binaryTypeCntr} variable.
         */
        private int i;

        /**
         */
        public MyEntryProcessor(int i) {
            this.i = i;
        }

        /**
         */
        @IgniteInstanceResource
        Ignite ignite;

        /**
         * {@inheritDoc}
         */
        @Override
        public Object process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException {
            BinaryObjectBuilder builder = ignite.binary().builder("my_type");
            builder.setField("new_field" + i, i);
            entry.setValue(builder.build());
            return null;
        }
    }
    IgniteInternalFuture fut1 = GridTestUtils.runMultiThreadedAsync(() -> {
        IgniteCache<Object, Object> cache = client1.cache(CACHE_NAME).withKeepBinary();
        while (!stop.get()) {
            Integer key = keyCntr.getAndIncrement();
            cache.put(key, key);
            cache.invoke(key, new MyEntryProcessor(binaryTypeCntr.get()));
            binaryTypeCntr.incrementAndGet();
        }
    }, 8, "writer-thread");
    IgniteInternalFuture fut2 = GridTestUtils.runAsync(() -> {
        IgniteCache<Object, Object> cache = client2.cache(CACHE_NAME).withKeepBinary();
        while (!stop.get()) {
            ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
            qry.setInitialQuery(new ScanQuery<>((key, val) -> true));
            qry.setLocalListener(evts -> {
            });
            // noinspection EmptyTryBlock
            try (QueryCursor<Cache.Entry<Object, Object>> cursor = cache.query(qry)) {
            // No-op.
            }
        }
    });
    doSleep(10_000);
    stop.set(true);
    fut1.get(10, TimeUnit.SECONDS);
    fut2.get(10, TimeUnit.SECONDS);
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) READ_WRITE_SAFE(org.apache.ignite.cache.PartitionLossPolicy.READ_WRITE_SAFE) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) IgniteEx(org.apache.ignite.internal.IgniteEx) EntryProcessorException(javax.cache.processor.EntryProcessorException) EntryProcessor(javax.cache.processor.EntryProcessor) MutableEntry(javax.cache.processor.MutableEntry) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) After(org.junit.After) Cache(javax.cache.Cache) PARTITIONED(org.apache.ignite.cache.CacheMode.PARTITIONED) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) TcpDiscoveryVmIpFinder(org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) FULL_SYNC(org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC) IgniteCache(org.apache.ignite.IgniteCache) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) TimeUnit(java.util.concurrent.TimeUnit) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) QueryCursor(org.apache.ignite.cache.query.QueryCursor) TcpDiscoverySpi(org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi) ATOMIC(org.apache.ignite.cache.CacheAtomicityMode.ATOMIC) Collections(java.util.Collections) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MutableEntry(javax.cache.processor.MutableEntry) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor) IgniteEx(org.apache.ignite.internal.IgniteEx) Ignite(org.apache.ignite.Ignite) MutableEntry(javax.cache.processor.MutableEntry) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Cache (javax.cache.Cache)1 EntryProcessor (javax.cache.processor.EntryProcessor)1 EntryProcessorException (javax.cache.processor.EntryProcessorException)1 MutableEntry (javax.cache.processor.MutableEntry)1 Ignite (org.apache.ignite.Ignite)1 IgniteCache (org.apache.ignite.IgniteCache)1 BinaryObjectBuilder (org.apache.ignite.binary.BinaryObjectBuilder)1 ATOMIC (org.apache.ignite.cache.CacheAtomicityMode.ATOMIC)1 CacheEntryProcessor (org.apache.ignite.cache.CacheEntryProcessor)1 PARTITIONED (org.apache.ignite.cache.CacheMode.PARTITIONED)1 FULL_SYNC (org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC)1 READ_WRITE_SAFE (org.apache.ignite.cache.PartitionLossPolicy.READ_WRITE_SAFE)1 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)1 QueryCursor (org.apache.ignite.cache.query.QueryCursor)1