Search in sources :

Example 21 with IgniteSemaphore

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

the class IgniteSemaphoreAbstractSelfTest method removeSemaphore.

/**
 * @param semaphoreName Semaphore name.
 * @throws Exception If failed.
 */
private void removeSemaphore(final String semaphoreName) throws Exception {
    IgniteSemaphore semaphore = grid(RND.nextInt(NODES_CNT)).semaphore(semaphoreName, 10, false, true);
    assert semaphore != null;
    if (semaphore.availablePermits() < 0)
        semaphore.release(-semaphore.availablePermits());
    // Remove semaphore on random node.
    IgniteSemaphore semaphore0 = grid(RND.nextInt(NODES_CNT)).semaphore(semaphoreName, 0, false, true);
    assertNotNull(semaphore0);
    semaphore0.close();
    // Ensure semaphore is removed on all nodes.
    assert GridTestUtils.waitForCondition(new GridAbsPredicateX() {

        @Override
        public boolean applyx() throws IgniteCheckedException {
            for (Ignite g : G.allGrids()) {
                if (((IgniteKernal) g).context().dataStructures().semaphore(semaphoreName, null, 10, true, false) != null)
                    return false;
            }
            return true;
        }
    }, 5_000);
    checkRemovedSemaphore(semaphore);
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridAbsPredicateX(org.apache.ignite.internal.util.lang.GridAbsPredicateX) IgniteSemaphore(org.apache.ignite.IgniteSemaphore) Ignite(org.apache.ignite.Ignite)

Example 22 with IgniteSemaphore

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

the class IgniteSemaphoreAbstractSelfTest method testIsolation.

/**
 * 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.
 */
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());
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) IgniteSemaphore(org.apache.ignite.IgniteSemaphore) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Aggregations

IgniteSemaphore (org.apache.ignite.IgniteSemaphore)22 Ignite (org.apache.ignite.Ignite)15 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 IOException (java.io.IOException)4 IgniteException (org.apache.ignite.IgniteException)4 IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)4 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)3 Nullable (org.jetbrains.annotations.Nullable)3 ExpectedException (org.junit.rules.ExpectedException)3 ArrayList (java.util.ArrayList)2 Callable (java.util.concurrent.Callable)2 IgniteLock (org.apache.ignite.IgniteLock)2 IgniteCallable (org.apache.ignite.lang.IgniteCallable)2 Closeable (java.io.Closeable)1 InvalidObjectException (java.io.InvalidObjectException)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 IgniteAtomicLong (org.apache.ignite.IgniteAtomicLong)1