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

The following examples show how to use org.apache.ignite.Ignite#reentrantLock() . 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: GridCacheDataStructuresLoadTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
@Override public void applyx(Ignite ignite) {
    IgniteLock r = ignite.reentrantLock(TEST_REENTRANT_LOCK_NAME, true, false, true);

    for (int i = 0; i < operationsPerTx; i++) {
        r.isLocked();

        long cnt = reads.incrementAndGet();

        if (cnt % READ_LOG_MOD == 0)
            info("Performed " + cnt + " reads.");
    }
}
 
Example 2
Source File: GridCacheDataStructuresLoadTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
@Override public void applyx(Ignite ignite) {
    IgniteLock r = ignite.reentrantLock(TEST_REENTRANT_LOCK_NAME, true, false, true);

    for (int i = 0; i < operationsPerTx; i++) {
        if ((i % 2) == 0)
            r.lock();
        else
            r.unlock();

        long cnt = writes.incrementAndGet();

        if (cnt % WRITE_LOG_MOD == 0)
            info("Performed " + cnt + " writes.");
    }
}
 
Example 3
Source File: IgniteLockExample.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override public void run() {
    System.out.println("Consumer started. ");

    Ignite g = Ignition.ignite();

    IgniteLock lock = g.reentrantLock(reentrantLockName, true, false, true);

    // Condition to wait on when queue is full.
    IgniteCondition notFull = lock.getOrCreateCondition(NOT_FULL);

    // Signaled to wait on when queue is empty.
    IgniteCondition notEmpty = lock.getOrCreateCondition(NOT_EMPTY);

    // Signaled when job is done.
    IgniteCondition done = lock.getOrCreateCondition(SYNC_NAME);

    IgniteCache<String, Integer> cache = g.cache(CACHE_NAME);

    for (int i = 0; i < OPS_COUNT; i++) {
        try {
            lock.lock();

            int val = cache.get(QUEUE_ID);

            while (val <= 0) {
                System.out.println("Queue is empty. Consumer [nodeId=" +
                    Ignition.ignite().cluster().localNode().id() + " paused.");

                notEmpty.await();

                val = cache.get(QUEUE_ID);
            }

            val--;

            System.out.println("Consumer [nodeId=" + Ignition.ignite().cluster().localNode().id() +
                ", available=" + val + ']');

            cache.put(QUEUE_ID, val);

            notFull.signalAll();
        }
        finally {
            lock.unlock();
        }
    }

    System.out.println("Consumer finished [nodeId=" + Ignition.ignite().cluster().localNode().id() + ']');

    try {
        lock.lock();

        int count = cache.get(SYNC_NAME);

        count--;

        cache.put(SYNC_NAME, count);

        // Signals the master thread.
        done.signal();
    }
    finally {
        lock.unlock();
    }
}
 
Example 4
Source File: IgniteLockAbstractSelfTest.java    From ignite with Apache License 2.0 2 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 {
        IgniteLock lock = ignite.reentrantLock("lock", true, true, true);

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

            boolean success = lock.tryLock(1, MILLISECONDS);

            assertTrue(success);

            tx.rollback();
        }

        assertEquals(0, cache.size());

        assertTrue(lock.isLocked());

        lock.unlock();

        assertFalse(lock.isLocked());

        lock.close();

        assertTrue(lock.removed());
    }
    finally {
        ignite.destroyCache(cfg.getName());
    }
}
 
Example 5
Source File: IgniteClientReconnectAtomicsTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
private void testReentrantLockReconnect(final boolean fair) throws Exception {
    Ignite client = grid(serverCount());

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

    Ignite srv = ignite(0);

    IgniteLock clientLock = client.reentrantLock("lock1", true, fair, true);

    assertEquals(false, clientLock.isLocked());

    final IgniteLock srvLock = srv.reentrantLock("lock1", true, fair, true);

    assertEquals(false, srvLock.isLocked());

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

    assertTrue(srvLock.isLocked());
    assertTrue(clientLock.isLocked());

    assertEquals(1, srvLock.getHoldCount());

    srvLock.lock();

    assertTrue(srvLock.isLocked());
    assertTrue(clientLock.isLocked());

    assertEquals(2, srvLock.getHoldCount());

    srvLock.unlock();

    assertTrue(srvLock.isLocked());
    assertTrue(clientLock.isLocked());

    assertEquals(1, srvLock.getHoldCount());

    srvLock.unlock();

    assertFalse(srvLock.isLocked());
    assertFalse(clientLock.isLocked());

    assertEquals(0, srvLock.getHoldCount());
}