Java Code Examples for org.apache.ignite.Ignite#semaphore()

The following examples show how to use org.apache.ignite.Ignite#semaphore() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GridCacheAbstractDataStructuresFailoverSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testSemaphoreFailoverSafe() throws Exception {
    try (final IgniteSemaphore semaphore = grid(0).semaphore(STRUCTURE_NAME, 20, true, true)) {
        Ignite g = startGrid(NEW_IGNITE_INSTANCE_NAME);

        IgniteSemaphore semaphore2 = g.semaphore(STRUCTURE_NAME, 20, true, false);

        assertEquals(20, semaphore2.availablePermits());

        semaphore2.acquire(10);

        stopGrid(NEW_IGNITE_INSTANCE_NAME);

        waitForCondition(new PA() {
            @Override public boolean apply() {
                return semaphore.availablePermits() == 20;
            }
        }, 2000);
    }
}
 
Example 2
Source File: SemaphoreFailoverNoWaitingAcquirerTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception If failed.
 */
private void doTest() throws Exception {
    try {
        startGrids(GRID_CNT);

        Ignite ignite = grid(0);

        IgniteSemaphore sem = ignite.semaphore("sem", 1, true, true);

        assertEquals(1, sem.availablePermits());

        sem.acquire(1);

        assertEquals(0, sem.availablePermits());

        ignite.close();

        awaitPartitionMapExchange();
        IgniteSemaphore sem2 = grid(1).semaphore("sem", 1, true, true);

        assertTrue("Could not aquire after 'restart'",sem2.tryAcquire(1, 5000, TimeUnit.MILLISECONDS));
    }
    finally {
        stopAllGrids();
    }
}
 
Example 3
Source File: GridCacheDataStructuresLoadTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
@Override public void applyx(Ignite ignite) {
    IgniteSemaphore s = ignite.semaphore(TEST_SEMAPHORE_NAME, SEMAPHORE_INIT_CNT, false, true);

    for (int i = 0; i < operationsPerTx; i++) {
        if ((i % 2) == 0)
            s.release();
        else
            s.acquire();

        long cnt = writes.incrementAndGet();

        if (cnt % WRITE_LOG_MOD == 0)
            info("Performed " + cnt + " writes.");
    }
}
 
Example 4
Source File: GridCacheDataStructuresLoadTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
@Override public void applyx(Ignite ignite) {
    IgniteSemaphore s = ignite.semaphore(TEST_SEMAPHORE_NAME, SEMAPHORE_INIT_CNT, false, true);

    for (int i = 0; i < operationsPerTx; i++) {
        s.availablePermits();

        long cnt = reads.incrementAndGet();

        if (cnt % READ_LOG_MOD == 0)
            info("Performed " + cnt + " reads.");
    }
}
 
Example 5
Source File: GridCacheAbstractDataStructuresFailoverSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testSemaphoreNonFailoverSafe() throws Exception {
    try (IgniteSemaphore sem = grid(0).semaphore(STRUCTURE_NAME, 20, false, true)) {
        Ignite g = startGrid(NEW_IGNITE_INSTANCE_NAME);

        IgniteSemaphore sem2 = g.semaphore(STRUCTURE_NAME, 20, false, false);

        sem2.acquire(20);

        assertEquals(0, sem.availablePermits());

        new Timer().schedule(new TimerTask() {
            @Override public void run() {
                stopGrid(NEW_IGNITE_INSTANCE_NAME);
            }
        }, 2000);

        try {
            sem.acquire(1);
        }
        catch (IgniteInterruptedException ignored) {
            // Expected exception.
            return;
        }
    }

    fail("Thread hasn't been interrupted");
}
 
Example 6
Source File: SemaphoreFailoverSafeReleasePermitsTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @throws Exception If failed.
 */
private void doTest() throws Exception {
    try {
        startGrids(GRID_CNT);

        Ignite ignite = grid(0);

        IgniteSemaphore sem = ignite.semaphore("sem", 1, true, true);

        // Initialize second semaphore before the first one is broken.
        IgniteSemaphore sem2 = grid(1).semaphore("sem", 1, true, true);

        assertEquals(1, sem.availablePermits());

        sem.acquire(1);

        assertEquals(0, sem.availablePermits());

        ignite.close();

        awaitPartitionMapExchange();

        assertTrue(sem2.tryAcquire(1, 5000, TimeUnit.MILLISECONDS));
    }
    finally {
        stopAllGrids();
    }
}
 
Example 7
Source File: IgniteSemaphoreAbstractSelfTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * Implementation of ignite data structures internally uses special system caches, need make sure
 * that transaction on these system caches do not intersect with transactions started by user.
 *
 * @throws Exception If failed.
 */
@Test
public void testIsolation() throws Exception {
    Ignite ignite = grid(0);

    CacheConfiguration cfg = new CacheConfiguration(DEFAULT_CACHE_NAME);

    cfg.setName("myCache");
    cfg.setAtomicityMode(TRANSACTIONAL);
    cfg.setWriteSynchronizationMode(FULL_SYNC);

    IgniteCache<Integer, Integer> cache = ignite.getOrCreateCache(cfg);

    try {
        IgniteSemaphore semaphore = ignite.semaphore("testIsolation", 1, true, true);

        assertNotNull(semaphore);

        try (Transaction tx = ignite.transactions().txStart()) {
            cache.put(1, 1);

            assertEquals(1, semaphore.availablePermits());

            semaphore.acquire();

            tx.rollback();
        }

        assertEquals(0, cache.size());

        assertEquals(0, semaphore.availablePermits());

        semaphore.close();

        assertTrue(semaphore.removed());
    }
    finally {
        ignite.destroyCache(cfg.getName());
    }
}
 
Example 8
Source File: IgniteClientReconnectAtomicsTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testSemaphoreReconnect() throws Exception {
    Ignite client = grid(serverCount());

    assertTrue(client.cluster().localNode().isClient());

    Ignite srv = ignite(0);

    IgniteSemaphore clientSemaphore = client.semaphore("semaphore1", 3, false, true);

    assertEquals(3, clientSemaphore.availablePermits());

    final IgniteSemaphore srvSemaphore = srv.semaphore("semaphore1", 3, false, false);

    assertEquals(3, srvSemaphore.availablePermits());

    reconnectClientNode(client, srv, new Runnable() {
        @Override public void run() {
            srvSemaphore.acquire();
        }
    });

    assertEquals(2, srvSemaphore.availablePermits());
    assertEquals(2, clientSemaphore.availablePermits());

    srvSemaphore.acquire();

    assertEquals(1, srvSemaphore.availablePermits());
    assertEquals(1, clientSemaphore.availablePermits());

    clientSemaphore.acquire();

    assertEquals(0, srvSemaphore.availablePermits());
    assertEquals(0, clientSemaphore.availablePermits());

    assertFalse(srvSemaphore.tryAcquire());
    assertFalse(srvSemaphore.tryAcquire());
}