Search in sources :

Example 41 with IgniteAtomicSequence

use of org.apache.ignite.IgniteAtomicSequence in project ignite by apache.

the class GridCachePartitionedAtomicSequenceMultiThreadedTest method checkUpdate.

/**
 * @param updated Whether use updated values.
 * @throws Exception If failed.
 */
private void checkUpdate(boolean updated) throws Exception {
    String seqName = UUID.randomUUID().toString();
    final IgniteAtomicSequence seq = grid(0).atomicSequence(seqName, 0L, true);
    long curVal = 0;
    Random r = new Random();
    for (int i = 0; i < ITERATION_NUM; i++) {
        long delta = r.nextInt(10) + 1;
        long retVal = updated ? seq.addAndGet(delta) : seq.getAndAdd(delta);
        assertEquals(updated ? curVal + delta : curVal, retVal);
        curVal += delta;
    }
}
Also used : Random(java.util.Random) IgniteAtomicSequence(org.apache.ignite.IgniteAtomicSequence)

Example 42 with IgniteAtomicSequence

use of org.apache.ignite.IgniteAtomicSequence in project ignite by apache.

the class GridCachePartitionedAtomicSequenceTxSelfTest method testIsolation.

/**
 * Tests isolation of system and user transactions.
 */
@Test
public void testIsolation() {
    IgniteAtomicSequence seq = ignite(0).atomicSequence(SEQ_NAME, 0, true);
    CacheConfiguration<Object, Object> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
    ccfg.setAtomicityMode(TRANSACTIONAL);
    IgniteCache<Object, Object> cache = ignite(0).getOrCreateCache(ccfg);
    try (Transaction tx = ignite(0).transactions().txStart()) {
        seq.getAndIncrement();
        cache.put(1, 1);
        tx.rollback();
    }
    assertEquals(0, cache.size());
    assertEquals(new Long(1L), U.field(seq, "locVal"));
    assertEquals(new Long(SEQ_CACHE_SIZE - 1), U.field(seq, "upBound"));
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) IgniteAtomicSequence(org.apache.ignite.IgniteAtomicSequence) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 43 with IgniteAtomicSequence

use of org.apache.ignite.IgniteAtomicSequence in project ignite by apache.

the class IgniteSequenceInternalCleanupTest method testDeactivate.

/**
 */
@Test
public void testDeactivate() throws Exception {
    try {
        Ignite ignite = startGridsMultiThreaded(GRIDS_CNT);
        ignite.cache("test0").put(0, 0);
        int id = 0;
        for (Ignite ig : G.allGrids()) {
            IgniteAtomicSequence seq = ig.atomicSequence("testSeq", 0, true);
            long id0 = seq.getAndIncrement();
            assertEquals(id0, id);
            id += SEQ_RESERVE;
        }
        doSleep(1000);
        long puts = ignite.cache("test0").metrics().getCachePuts();
        assertEquals(1, puts);
        grid(GRIDS_CNT - 1).cluster().active(false);
        ignite.cluster().active(true);
        long putsAfter = ignite.cache("test0").metrics().getCachePuts();
        assertEquals(0, putsAfter);
    } finally {
        stopAllGrids();
    }
}
Also used : IgniteAtomicSequence(org.apache.ignite.IgniteAtomicSequence) Ignite(org.apache.ignite.Ignite) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 44 with IgniteAtomicSequence

use of org.apache.ignite.IgniteAtomicSequence in project ignite by apache.

the class IgnitePersistentStoreDataStructuresTest method testSequence.

/**
 * @throws Exception If failed.
 */
@Test
public void testSequence() throws Exception {
    Ignite ignite = startGrids(4);
    ignite.active(true);
    IgniteAtomicSequence sequence = ignite.atomicSequence("testSequence", 0, true);
    int i = 0;
    while (i < 1000) {
        sequence.incrementAndGet();
        i++;
    }
    stopAllGrids();
    ignite = startGrids(4);
    ignite.active(true);
    sequence = ignite.atomicSequence("testSequence", 0, false);
    assertTrue(sequence.incrementAndGet() > i);
}
Also used : IgniteAtomicSequence(org.apache.ignite.IgniteAtomicSequence) Ignite(org.apache.ignite.Ignite) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 45 with IgniteAtomicSequence

use of org.apache.ignite.IgniteAtomicSequence in project ignite by apache.

the class IgniteClientReconnectAtomicsTest method testAtomicSeqReconnectRemoved.

/**
 * @throws Exception If failed.
 */
@Test
public void testAtomicSeqReconnectRemoved() throws Exception {
    Ignite client = grid(serverCount());
    assertTrue(client.cluster().localNode().isClient());
    Ignite srv = ignite(0);
    final IgniteAtomicSequence clientAtomicSeq = client.atomicSequence("atomicSeqRmv", 0, true);
    clientAtomicSeq.batchSize(1);
    assertEquals(1L, clientAtomicSeq.incrementAndGet());
    final IgniteAtomicSequence srvAtomicSeq = srv.atomicSequence("atomicSeqRmv", 0, false);
    srvAtomicSeq.batchSize(1);
    assertEquals(1001L, srvAtomicSeq.incrementAndGet());
    reconnectClientNode(client, srv, new Runnable() {

        @Override
        public void run() {
            srvAtomicSeq.close();
            assert srvAtomicSeq.removed();
        }
    });
    GridTestUtils.assertThrows(log, new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            for (int i = 0; i < 2000; i++) clientAtomicSeq.incrementAndGet();
            return null;
        }
    }, IgniteException.class, null);
    IgniteAtomicSequence newClientAtomicSeq = client.atomicSequence("atomicSeqRmv", 0, true);
    assertEquals(0, newClientAtomicSeq.get());
    assertEquals(1, newClientAtomicSeq.incrementAndGet());
    newClientAtomicSeq.close();
}
Also used : IgniteAtomicSequence(org.apache.ignite.IgniteAtomicSequence) Ignite(org.apache.ignite.Ignite) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteException(org.apache.ignite.IgniteException) Test(org.junit.Test)

Aggregations

IgniteAtomicSequence (org.apache.ignite.IgniteAtomicSequence)47 Test (org.junit.Test)29 Ignite (org.apache.ignite.Ignite)20 IgniteException (org.apache.ignite.IgniteException)10 IgniteAtomicsAbstractTest (org.apache.ignite.internal.processors.cache.datastructures.IgniteAtomicsAbstractTest)9 AtomicConfiguration (org.apache.ignite.configuration.AtomicConfiguration)6 IgniteAtomicLong (org.apache.ignite.IgniteAtomicLong)5 ArrayList (java.util.ArrayList)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 IOException (java.io.IOException)3 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)3 IgniteCountDownLatch (org.apache.ignite.IgniteCountDownLatch)3 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)3 HashSet (java.util.HashSet)2 Random (java.util.Random)2 UUID (java.util.UUID)2 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)2 Callable (java.util.concurrent.Callable)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 IgniteAtomicReference (org.apache.ignite.IgniteAtomicReference)2