Search in sources :

Example 86 with IgniteInternalFuture

use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.

the class ReadWriteLockMultiThreadedTest method testReadLockAcquire.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings({ "LockAcquiredButNotSafelyReleased" })
public void testReadLockAcquire() throws Exception {
    final ReadWriteLock lock = new ReentrantReadWriteLock();
    lock.writeLock().lock();
    X.println("Write lock acquired: " + lock);
    IgniteInternalFuture fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Nullable
        @Override
        public Object call() throws Exception {
            X.println("Attempting to acquire read lock: " + lock);
            lock.readLock().lock();
            try {
                X.println("Read lock acquired: " + lock);
                return null;
            } finally {
                lock.readLock().unlock();
            }
        }
    }, 1, "read-lock");
    Thread.sleep(2000);
    X.println(">>> Dump threads now! <<<");
    Thread.sleep(15 * 1000);
    X.println("Write lock released.");
    lock.writeLock().unlock();
    fut.get();
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Nullable(org.jetbrains.annotations.Nullable)

Example 87 with IgniteInternalFuture

use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.

the class GridSharedFsCheckpointSpiMultiThreadedSelfTest method testSpi.

/**
 * @throws Exception If failed.
 */
public void testSpi() throws Exception {
    final AtomicInteger writeFinished = new AtomicInteger();
    final AtomicBoolean fail = new AtomicBoolean();
    IgniteInternalFuture fut1 = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Nullable
        @Override
        public Object call() throws Exception {
            try {
                byte[] state = createTestArray((byte) 1);
                for (int i = 0; i < ITER_CNT; i++) getSpi().saveCheckpoint(CHECK_POINT_KEY, state, 0, true);
                return null;
            } finally {
                writeFinished.incrementAndGet();
            }
        }
    }, THREAD_CNT, "writer-1");
    IgniteInternalFuture fut2 = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Nullable
        @Override
        public Object call() throws Exception {
            try {
                byte[] state = createTestArray((byte) 2);
                for (int i = 0; i < ITER_CNT; i++) getSpi().saveCheckpoint(CHECK_POINT_KEY, state, 0, true);
                return null;
            } finally {
                writeFinished.incrementAndGet();
            }
        }
    }, THREAD_CNT, "writer-2");
    IgniteInternalFuture fut3 = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Nullable
        @Override
        public Object call() throws Exception {
            while (writeFinished.get() < THREAD_CNT * 2) {
                try {
                    byte[] state = getSpi().loadCheckpoint(CHECK_POINT_KEY);
                    if (state == null)
                        continue;
                    assert state.length == ARRAY_SIZE;
                    boolean has1 = false;
                    boolean has2 = false;
                    for (int j = 0; j < ARRAY_SIZE; j++) {
                        switch(state[j]) {
                            case 1:
                                has1 = true;
                                assert !has2;
                                break;
                            case 2:
                                has2 = true;
                                assert !has1;
                                break;
                            default:
                                assert false : "Unexpected value in state: " + state[j];
                        }
                    }
                    info(">>>>>>> Checkpoint is fine.");
                } catch (Throwable e) {
                    error("Failed to load checkpoint: " + e.getMessage());
                    fail.set(true);
                }
            }
            return null;
        }
    }, 1, "reader");
    fut1.get();
    fut2.get();
    fut3.get();
    assert !fail.get();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Nullable(org.jetbrains.annotations.Nullable)

Example 88 with IgniteInternalFuture

use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.

the class CacheTopologyCommandHandlerTest method topologyCommandOnDynamicCacheCreateDestroy.

/**
 * @param node Ignite node.
 * @param req Rest request.
 * @throws Exception If failed.
 */
private void topologyCommandOnDynamicCacheCreateDestroy(final Ignite node, GridRestTopologyRequest req) throws Exception {
    GridTopologyCommandHandler hnd = new GridTopologyCommandHandler(((IgniteKernal) node).context());
    final AtomicReference<Exception> ex = new AtomicReference<>();
    final long deadline = System.currentTimeMillis() + 5000;
    final AtomicInteger cntr = new AtomicInteger();
    IgniteInternalFuture fut = GridTestUtils.runMultiThreadedAsync(new Runnable() {

        @Override
        public void run() {
            int startIdx = cntr.getAndAdd(4);
            int idx = 0;
            boolean start = true;
            while (System.currentTimeMillis() < deadline) {
                try {
                    if (start)
                        node.createCache("cache" + (startIdx + idx));
                    else
                        node.destroyCache("cache" + (startIdx + idx));
                    if ((idx = (idx + 1) % 4) == 0)
                        start = !start;
                } catch (Exception e) {
                    if (ex.get() != null || !ex.compareAndSet(null, e))
                        ex.get().addSuppressed(e);
                    break;
                }
            }
        }
    }, 4, "cache-start-destroy");
    while (!fut.isDone()) hnd.handleAsync(req).get();
    if (ex.get() != null)
        throw ex.get();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture)

Example 89 with IgniteInternalFuture

use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.

the class IgniteCacheNodeJoinAbstractTest method testScanQuery.

/**
 * @throws Exception If failed.
 */
public void testScanQuery() throws Exception {
    final IgniteCache<Integer, Integer> cache = jcache(0);
    for (int i = 0; i < 5; i++) {
        log.info("Iteration: " + i);
        final IgniteInternalFuture fut = GridTestUtils.runAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                startGrid(1);
                return null;
            }
        });
        final AtomicBoolean stop = new AtomicBoolean();
        GridTestUtils.runMultiThreaded(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                ScanQuery qry = new ScanQuery();
                while (!stop.get() && !fut.isDone()) cache.query(qry).getAll();
                return null;
            }
        }, 10, "test-qry");
        try {
            fut.get(60_000);
        } finally {
            stop.set(true);
        }
        stopGrid(1);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture)

Example 90 with IgniteInternalFuture

use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.

the class CacheExchangeMergeTest method testDelayExchangeMessages.

/**
 * @throws Exception If failed.
 */
public void testDelayExchangeMessages() throws Exception {
    testDelaySpi = true;
    System.setProperty(IgniteSystemProperties.IGNITE_EXCHANGE_MERGE_DELAY, "2000");
    try {
        final int srvs = 6;
        final int clients = 3;
        startGridsMultiThreaded(srvs);
        for (int i = 0; i < clients; i++) {
            client.set(true);
            startGrid(srvs + i);
        }
        final int initNodes = srvs + clients;
        final AtomicInteger stopIdx = new AtomicInteger();
        IgniteInternalFuture stopFut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                Thread.sleep(ThreadLocalRandom.current().nextLong(500) + 1);
                stopGrid(stopIdx.incrementAndGet());
                return null;
            }
        }, 3, "stop-srv");
        final AtomicInteger startIdx = new AtomicInteger(initNodes);
        IgniteInternalFuture startFut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                int nodeIdx = startIdx.incrementAndGet();
                if (rnd.nextInt(3) == 0) {
                    log.info("Start client: " + nodeIdx);
                    client.set(true);
                } else
                    log.info("Start server: " + nodeIdx);
                startGrid(nodeIdx);
                if (rnd.nextBoolean()) {
                    log.info("Stop started node: " + nodeIdx);
                    stopGrid(nodeIdx);
                }
                return null;
            }
        }, 5, "start-node");
        stopFut.get();
        startFut.get();
        checkCaches();
    } finally {
        System.clearProperty(IgniteSystemProperties.IGNITE_EXCHANGE_MERGE_DELAY);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException)

Aggregations

IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)245 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)114 Ignite (org.apache.ignite.Ignite)71 ArrayList (java.util.ArrayList)52 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)46 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)46 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)43 IgniteException (org.apache.ignite.IgniteException)33 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)29 UUID (java.util.UUID)28 IgniteCache (org.apache.ignite.IgniteCache)28 ClusterNode (org.apache.ignite.cluster.ClusterNode)28 Callable (java.util.concurrent.Callable)27 HashMap (java.util.HashMap)25 Map (java.util.Map)25 CountDownLatch (java.util.concurrent.CountDownLatch)24 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)19 CacheException (javax.cache.CacheException)16 GridFinishedFuture (org.apache.ignite.internal.util.future.GridFinishedFuture)16 GridFutureAdapter (org.apache.ignite.internal.util.future.GridFutureAdapter)16