Search in sources :

Example 96 with Ignite

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

the class GridCacheAbstractDataStructuresFailoverSelfTest method testSemaphoreSingleNodeFailure.

/**
     * @throws Exception If failed.
     */
public void testSemaphoreSingleNodeFailure() throws Exception {
    final Ignite i1 = grid(0);
    IgniteSemaphore sem1 = i1.semaphore(STRUCTURE_NAME, 1, false, true);
    sem1.acquire();
    IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            boolean failed = true;
            IgniteSemaphore sem2 = i1.semaphore(STRUCTURE_NAME, 1, false, true);
            try {
                sem2.acquire();
            } catch (Exception ignored) {
                failed = false;
            } finally {
                assertFalse(failed);
                sem2.release();
            }
            return null;
        }
    });
    while (!sem1.hasQueuedThreads()) {
        try {
            Thread.sleep(1);
        } catch (InterruptedException ignored) {
            fail();
        }
    }
    i1.close();
    fut.get();
}
Also used : Ignite(org.apache.ignite.Ignite) IgniteSemaphore(org.apache.ignite.IgniteSemaphore) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteException(org.apache.ignite.IgniteException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException)

Example 97 with Ignite

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

the class GridCacheAbstractDataStructuresFailoverSelfTest method doTestReentrantLock.

/**
     * @throws Exception If failed.
     */
private void doTestReentrantLock(final ConstantTopologyChangeWorker topWorker, final boolean failoverSafe, final boolean fair) throws Exception {
    IgniteEx ig = grid(0);
    try (IgniteLock lock = ig.reentrantLock(STRUCTURE_NAME, failoverSafe, fair, true)) {
        IgniteInternalFuture<?> fut = topWorker.startChangingTopology(new IgniteClosure<Ignite, Void>() {

            @Override
            public Void apply(Ignite ignite) {
                final IgniteLock l = ignite.reentrantLock(STRUCTURE_NAME, failoverSafe, fair, false);
                final AtomicBoolean done = new AtomicBoolean(false);
                GridTestUtils.runAsync(new Callable<Void>() {

                    @Override
                    public Void call() throws Exception {
                        try {
                            l.lock();
                        } finally {
                            done.set(true);
                        }
                        return null;
                    }
                });
                // Wait until l.lock() has been called.
                while (!l.hasQueuedThreads() && !done.get()) {
                // No-op.
                }
                return null;
            }
        });
        while (!fut.isDone()) {
            try {
                lock.lock();
            } catch (IgniteException e) {
                // Exception may happen in non-failoversafe mode.
                if (failoverSafe)
                    throw e;
            } finally {
                // Broken lock cannot be used in non-failoversafe mode.
                if (!lock.isBroken() || failoverSafe) {
                    assertTrue(lock.isHeldByCurrentThread());
                    lock.unlock();
                    assertFalse(lock.isHeldByCurrentThread());
                }
            }
        }
        fut.get();
        for (Ignite g : G.allGrids()) {
            IgniteLock l = g.reentrantLock(STRUCTURE_NAME, failoverSafe, fair, false);
            assertTrue(g.name(), !l.isHeldByCurrentThread() || lock.isBroken());
        }
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteException(org.apache.ignite.IgniteException) IgniteEx(org.apache.ignite.internal.IgniteEx) IgniteLock(org.apache.ignite.IgniteLock) Ignite(org.apache.ignite.Ignite) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable)

Example 98 with Ignite

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

the class GridCacheAbstractDataStructuresFailoverSelfTest method testAtomicSequenceInitialization.

/**
     * @throws Exception If failed.
     */
public void testAtomicSequenceInitialization() throws Exception {
    int threadCnt = 3;
    final AtomicInteger idx = new AtomicInteger(gridCount());
    IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new CA() {

        @Override
        public void apply() {
            int id = idx.getAndIncrement();
            try {
                log.info("Start node: " + id);
                startGrid(id);
                Thread.sleep(1000);
            } catch (Exception e) {
                throw F.wrap(e);
            } finally {
                stopGrid(id);
                info("Thread finished.");
            }
        }
    }, threadCnt, "test-thread");
    while (!fut.isDone()) {
        grid(0).compute().call(new IgniteCallable<Object>() {

            /** */
            @IgniteInstanceResource
            private Ignite g;

            @Override
            public Object call() throws Exception {
                IgniteAtomicSequence seq = g.atomicSequence(STRUCTURE_NAME, 1, true);
                assert seq != null;
                for (int i = 0; i < 1000; i++) seq.getAndIncrement();
                return null;
            }
        });
    }
    fut.get();
}
Also used : IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteAtomicSequence(org.apache.ignite.IgniteAtomicSequence) Ignite(org.apache.ignite.Ignite) CA(org.apache.ignite.internal.util.typedef.CA) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteException(org.apache.ignite.IgniteException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException)

Example 99 with Ignite

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

the class GridCacheAbstractDataStructuresFailoverSelfTest method testUncommitedTxLeave.

/**
     * @throws Exception If failed.
     */
public void testUncommitedTxLeave() throws Exception {
    final int val = 10;
    grid(0).atomicLong(STRUCTURE_NAME, val, true);
    GridTestUtils.runAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            Ignite g = startGrid(NEW_IGNITE_INSTANCE_NAME);
            try {
                g.transactions().txStart();
                g.cache(TRANSACTIONAL_CACHE_NAME).put(1, 1);
                assertEquals(val + 1, g.atomicLong(STRUCTURE_NAME, val, false).incrementAndGet());
            } finally {
                stopGrid(NEW_IGNITE_INSTANCE_NAME);
            }
            return null;
        }
    }).get();
    waitForDiscovery(G.allGrids().toArray(new Ignite[gridCount()]));
    assertEquals(val + 1, grid(0).atomicLong(STRUCTURE_NAME, val, false).get());
}
Also used : Ignite(org.apache.ignite.Ignite) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable)

Example 100 with Ignite

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

the class GridCacheAbstractDataStructuresFailoverSelfTest method testAtomicLongFailsWhenServersLeft.

/**
     * @throws Exception If failed.
     */
public void testAtomicLongFailsWhenServersLeft() throws Exception {
    client = true;
    Ignite ignite = startGrid(gridCount());
    new Timer().schedule(new TimerTask() {

        @Override
        public void run() {
            for (int i = 0; i < gridCount(); i++) stopGrid(i);
        }
    }, 10_000);
    long stopTime = U.currentTimeMillis() + TEST_TIMEOUT / 2;
    IgniteAtomicLong atomic = ignite.atomicLong(STRUCTURE_NAME, 10, true);
    try {
        while (U.currentTimeMillis() < stopTime) assertEquals(10, atomic.get());
    } catch (IgniteException ignore) {
        // Test that client does not hang.
        return;
    }
    fail();
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask) IgniteException(org.apache.ignite.IgniteException) IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) Ignite(org.apache.ignite.Ignite)

Aggregations

Ignite (org.apache.ignite.Ignite)1560 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)275 CountDownLatch (java.util.concurrent.CountDownLatch)188 IgniteException (org.apache.ignite.IgniteException)187 Transaction (org.apache.ignite.transactions.Transaction)166 IgniteCache (org.apache.ignite.IgniteCache)161 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)161 ArrayList (java.util.ArrayList)135 ClusterNode (org.apache.ignite.cluster.ClusterNode)121 UUID (java.util.UUID)104 Event (org.apache.ignite.events.Event)104 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)98 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)97 CacheException (javax.cache.CacheException)94 HashMap (java.util.HashMap)78 IgniteKernal (org.apache.ignite.internal.IgniteKernal)71 Map (java.util.Map)61 NearCacheConfiguration (org.apache.ignite.configuration.NearCacheConfiguration)61 Callable (java.util.concurrent.Callable)60 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)60