Search in sources :

Example 1 with CAX

use of org.apache.ignite.internal.util.typedef.CAX in project ignite by apache.

the class TxDeadlockCauseTest method testCauseObject.

/**
 * @param nodes Nodes count.
 * @param keysCnt Keys count.
 * @param timeout Timeout.
 * @param isolation TransactionIsolation.
 * @param oneOp Determines whether {@link IgniteCache#getAndPut(java.lang.Object, java.lang.Object)}
 *              instead of {@link IgniteCache#get(java.lang.Object)} and {@link IgniteCache#put(java.lang.Object, java.lang.Object)} operations sequence.
 * @throws Exception If failed.
 */
public void testCauseObject(int nodes, final int keysCnt, final long timeout, final TransactionIsolation isolation, final boolean oneOp) throws Exception {
    final Ignite ignite = grid(new Random().nextInt(nodes));
    final IgniteCache<Integer, Account> cache = ignite.cache(DEFAULT_CACHE_NAME);
    final List<Integer> keys = new ArrayList<>(keysCnt);
    for (int i = 0; i < keysCnt; i++) {
        keys.add(i);
        cache.put(i, new Account(i, i * 100));
    }
    final List<Integer> keysReversed = new ArrayList<>(keys);
    Collections.reverse(keysReversed);
    final AtomicBoolean reverse = new AtomicBoolean();
    final AtomicReference<Exception> ex = new AtomicReference<>();
    final CyclicBarrier barrier = new CyclicBarrier(2);
    IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            try (Transaction tx = ignite.transactions().txStart(TransactionConcurrency.PESSIMISTIC, isolation, timeout, keys.size())) {
                List<Integer> keys0 = getAndFlip(reverse) ? keys : keysReversed;
                for (int i = 0; i < keys0.size(); i++) {
                    Integer key = keys0.get(i);
                    if (oneOp)
                        cache.getAndPut(key, new Account(key, (key + 1) * 100));
                    else
                        cache.put(key, new Account(cache.get(key).id, (key + 1) * 100));
                    if (i == 0)
                        barrier.await(timeout >> 1, TimeUnit.MILLISECONDS);
                }
                tx.commit();
            } catch (Exception e) {
                ex.compareAndSet(null, e);
            }
        }
    }, 2, "tx");
    fut.get(timeout << 1);
    Exception e = ex.get();
    assertNotNull(e);
    boolean detected = X.hasCause(e, TransactionDeadlockException.class);
    if (!detected)
        U.error(log, "Failed to detect a deadlock.", e);
    else
        log.info(X.cause(e, TransactionDeadlockException.class).getMessage());
    assertTrue(detected);
    try {
        assertEquals(TransactionTimeoutException.class, e.getCause().getClass());
        assertEquals(TransactionDeadlockException.class, e.getCause().getCause().getClass());
    } catch (AssertionError err) {
        U.error(log, "Unexpected exception structure.", e);
        throw err;
    }
}
Also used : ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Random(java.util.Random) Ignite(org.apache.ignite.Ignite) ArrayList(java.util.ArrayList) List(java.util.List) CAX(org.apache.ignite.internal.util.typedef.CAX)

Example 2 with CAX

use of org.apache.ignite.internal.util.typedef.CAX in project ignite by apache.

the class IgfsStreamsSelfTest method testCreateFileFragmented.

/**
 * @throws Exception If failed.
 */
public void testCreateFileFragmented() throws Exception {
    IgfsEx impl = (IgfsEx) grid(0).fileSystem("igfs");
    String metaCacheName = grid(0).igfsx("igfs").configuration().getMetaCacheConfiguration().getName();
    final String dataCacheName = grid(0).igfsx("igfs").configuration().getDataCacheConfiguration().getName();
    IgfsFragmentizerManager fragmentizer = impl.context().fragmentizer();
    GridTestUtils.setFieldValue(fragmentizer, "fragmentizerEnabled", false);
    IgfsPath path = new IgfsPath("/file");
    try {
        IgniteFileSystem fs0 = grid(0).fileSystem("igfs");
        IgniteFileSystem fs1 = grid(1).fileSystem("igfs");
        IgniteFileSystem fs2 = grid(2).fileSystem("igfs");
        try (IgfsOutputStream out = fs0.create(path, 128, false, 1, CFG_GRP_SIZE, F.asMap(IgfsUtils.PROP_PREFER_LOCAL_WRITES, "true"))) {
            // 1.5 blocks
            byte[] data = new byte[CFG_BLOCK_SIZE * 3 / 2];
            Arrays.fill(data, (byte) 1);
            out.write(data);
        }
        try (IgfsOutputStream out = fs1.append(path, false)) {
            // 1.5 blocks.
            byte[] data = new byte[CFG_BLOCK_SIZE * 3 / 2];
            Arrays.fill(data, (byte) 2);
            out.write(data);
        }
        // After this we should have first two block colocated with grid 0 and last block colocated with grid 1.
        IgfsFileImpl fileImpl = (IgfsFileImpl) fs.info(path);
        GridCacheAdapter<Object, Object> metaCache = ((IgniteKernal) grid(0)).internalCache(metaCacheName);
        IgfsEntryInfo fileInfo = (IgfsEntryInfo) metaCache.get(fileImpl.fileId());
        IgfsFileMap map = fileInfo.fileMap();
        List<IgfsFileAffinityRange> ranges = map.ranges();
        assertEquals(2, ranges.size());
        assertTrue(ranges.get(0).startOffset() == 0);
        assertTrue(ranges.get(0).endOffset() == 2 * CFG_BLOCK_SIZE - 1);
        assertTrue(ranges.get(1).startOffset() == 2 * CFG_BLOCK_SIZE);
        assertTrue(ranges.get(1).endOffset() == 3 * CFG_BLOCK_SIZE - 1);
        // Validate data read after colocated writes.
        try (IgfsInputStream in = fs2.open(path)) {
            // Validate first part of file.
            for (int i = 0; i < CFG_BLOCK_SIZE * 3 / 2; i++) assertEquals((byte) 1, in.read());
            // Validate second part of file.
            for (int i = 0; i < CFG_BLOCK_SIZE * 3 / 2; i++) assertEquals((byte) 2, in.read());
            assertEquals(-1, in.read());
        }
    } finally {
        GridTestUtils.setFieldValue(fragmentizer, "fragmentizerEnabled", true);
        boolean hasData = false;
        for (int i = 0; i < NODES_CNT; i++) hasData |= !grid(i).cachex(dataCacheName).isEmpty();
        assertTrue(hasData);
        fs.delete(path, true);
    }
    GridTestUtils.retryAssert(log, ASSERT_RETRIES, ASSERT_RETRY_INTERVAL, new CAX() {

        @Override
        public void applyx() {
            for (int i = 0; i < NODES_CNT; i++) assertTrue(grid(i).cachex(dataCacheName).isEmpty());
        }
    });
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) IgfsInputStream(org.apache.ignite.igfs.IgfsInputStream) IgniteFileSystem(org.apache.ignite.IgniteFileSystem) IgfsOutputStream(org.apache.ignite.igfs.IgfsOutputStream) IgfsPath(org.apache.ignite.igfs.IgfsPath) CAX(org.apache.ignite.internal.util.typedef.CAX)

Example 3 with CAX

use of org.apache.ignite.internal.util.typedef.CAX in project ignite by apache.

the class IgniteCacheQueryMultiThreadedSelfTest method _testMultiThreadedSwapUnswapLongString.

/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@SuppressWarnings({ "TooBroadScope" })
public void _testMultiThreadedSwapUnswapLongString() throws Exception {
    fail("http://atlassian.gridgain.com/jira/browse/GG-11216");
    int threadCnt = 50;
    final int keyCnt = 2000;
    final int valCnt = 10000;
    final Ignite g = grid(0);
    // Put test values into cache.
    final IgniteCache<Integer, Object> c = cache(Integer.class, Object.class);
    assertEquals(0, g.cache(DEFAULT_CACHE_NAME).size());
    assertEquals(0, c.query(new SqlQuery(Object.class, "1 = 1")).getAll().size());
    Random rnd = new Random();
    for (int i = 0; i < keyCnt; i += 1 + rnd.nextInt(3)) {
        c.put(i, rnd.nextBoolean() ? (long) rnd.nextInt(valCnt) : String.valueOf(rnd.nextInt(valCnt)));
        if (evictsEnabled() && rnd.nextBoolean())
            c.localEvict(Arrays.asList(i));
    }
    final AtomicBoolean done = new AtomicBoolean();
    IgniteInternalFuture<?> fut = multithreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            Random rnd = new Random();
            while (!done.get()) {
                int key = rnd.nextInt(keyCnt);
                switch(rnd.nextInt(5)) {
                    case 0:
                        c.put(key, rnd.nextBoolean() ? (long) rnd.nextInt(valCnt) : String.valueOf(rnd.nextInt(valCnt)));
                        break;
                    case 1:
                        if (evictsEnabled())
                            c.localEvict(Arrays.asList(key));
                        break;
                    case 2:
                        c.remove(key);
                        break;
                    case 3:
                        c.get(key);
                        break;
                    case 4:
                        int from = rnd.nextInt(valCnt);
                        c.query(new SqlQuery(Object.class, "_val between ? and ?").setArgs(from, from + 250)).getAll();
                }
            }
        }
    }, threadCnt);
    Thread.sleep(DURATION);
    done.set(true);
    fut.get();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SqlQuery(org.apache.ignite.cache.query.SqlQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Random(java.util.Random) Ignite(org.apache.ignite.Ignite) CAX(org.apache.ignite.internal.util.typedef.CAX)

Example 4 with CAX

use of org.apache.ignite.internal.util.typedef.CAX in project ignite by apache.

the class GridCacheSequenceMultiNodeAbstractSelfTest method testMarshalling.

/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
@Test
public void testMarshalling() throws Exception {
    String seqName = UUID.randomUUID().toString();
    final IgniteAtomicSequence seq = grid(0).atomicSequence(seqName, 0, true);
    grid(1).compute().run(new CAX() {

        @Override
        public void applyx() {
            assertNotNull(seq);
            for (int i = 0; i < RETRIES; i++) seq.incrementAndGet();
        }
    });
}
Also used : IgniteAtomicSequence(org.apache.ignite.IgniteAtomicSequence) CAX(org.apache.ignite.internal.util.typedef.CAX) Test(org.junit.Test)

Example 5 with CAX

use of org.apache.ignite.internal.util.typedef.CAX in project ignite by apache.

the class GridMultipleJobsSelfTest method runTest.

/**
 * @param jobsNum Number of jobs.
 * @param threadNum Number of threads.
 * @param jobCls Job class.
 * @throws Exception If failed.
 */
private void runTest(final int jobsNum, int threadNum, final Class<? extends IgniteCallable<Boolean>> jobCls) throws Exception {
    final Ignite ignite1 = grid(1);
    final CountDownLatch latch = new CountDownLatch(jobsNum);
    final AtomicInteger jobsCnt = new AtomicInteger();
    final AtomicInteger resCnt = new AtomicInteger();
    GridTestUtils.runMultiThreaded(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            while (true) {
                int cnt = jobsCnt.incrementAndGet();
                if (cnt > jobsNum)
                    break;
                IgniteCallable<Boolean> job;
                try {
                    job = jobCls.newInstance();
                } catch (Exception e) {
                    throw new IgniteCheckedException("Could not instantiate a job.", e);
                }
                IgniteFuture<Boolean> fut = ignite1.compute().callAsync(job);
                if (cnt % LOG_MOD == 0)
                    X.println("Submitted jobs: " + cnt);
                fut.listen(new CIX1<IgniteFuture<Boolean>>() {

                    @Override
                    public void applyx(IgniteFuture<Boolean> f) {
                        try {
                            assert f.get();
                        } finally {
                            latch.countDown();
                            long cnt = resCnt.incrementAndGet();
                            if (cnt % LOG_MOD == 0)
                                X.println("Results count: " + cnt);
                        }
                    }
                });
            }
        }
    }, threadNum, "TEST-THREAD");
    latch.await();
}
Also used : CIX1(org.apache.ignite.internal.util.typedef.CIX1) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteCallable(org.apache.ignite.lang.IgniteCallable) IgniteFuture(org.apache.ignite.lang.IgniteFuture) Ignite(org.apache.ignite.Ignite) CAX(org.apache.ignite.internal.util.typedef.CAX) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteCheckedException(org.apache.ignite.IgniteCheckedException)

Aggregations

CAX (org.apache.ignite.internal.util.typedef.CAX)29 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)20 Test (org.junit.Test)20 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)17 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 Ignite (org.apache.ignite.Ignite)14 IgniteCache (org.apache.ignite.IgniteCache)11 SqlQuery (org.apache.ignite.cache.query.SqlQuery)8 AbstractIndexingCommonTest (org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest)8 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)8 Random (java.util.Random)7 List (java.util.List)6 Collection (java.util.Collection)5 Cache (javax.cache.Cache)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)4 GridCacheQueryManager (org.apache.ignite.internal.processors.cache.query.GridCacheQueryManager)4 ArrayList (java.util.ArrayList)3 AtomicIntegerArray (java.util.concurrent.atomic.AtomicIntegerArray)3 CacheException (javax.cache.CacheException)3