Search in sources :

Example 11 with GridCloseableIterator

use of org.apache.ignite.internal.util.lang.GridCloseableIterator in project ignite by apache.

the class GridOffHeapMapAbstractSelfTest method testIteratorMultithreaded.

/**
 * @throws Exception If failed.
 */
@Test
public void testIteratorMultithreaded() throws Exception {
    initCap = 10;
    map = newMap();
    final AtomicInteger rehashes = new AtomicInteger();
    final AtomicInteger releases = new AtomicInteger();
    map.eventListener(new GridOffHeapEventListener() {

        @Override
        public void onEvent(GridOffHeapEvent evt) {
            switch(evt) {
                case REHASH:
                    rehashes.incrementAndGet();
                    break;
                case RELEASE:
                    releases.incrementAndGet();
                    break;
                // No-op.
                default:
            }
        }
    });
    final int max = 1024;
    int threads = 5;
    final Map<String, String> m = new ConcurrentHashMap<>(max);
    multithreaded(new Callable() {

        @Override
        public Object call() throws Exception {
            for (int i = 0; i < max; i++) {
                String key = string();
                String val = string();
                // info("Storing [i=" + i + ", key=" + key + ", val=" + val + ']');
                m.put(key, val);
                assertTrue(map.put(hash(key), key.getBytes(), val.getBytes()));
                assertTrue(map.contains(hash(key), key.getBytes()));
                assertNotNull(map.get(hash(key), key.getBytes()));
                assertEquals(new String(map.get(hash(key), key.getBytes())), val);
                try (GridCloseableIterator<IgniteBiTuple<byte[], byte[]>> it = map.iterator()) {
                    while (it.hasNext()) {
                        IgniteBiTuple<byte[], byte[]> t = it.next();
                        String k = new String(t.get1());
                        String v = new String(t.get2());
                        // info("Entry [k=" + k + ", v=" + v + ']');
                        assertEquals(m.get(k), v);
                    }
                }
            }
            return null;
        }
    }, threads);
    assertEquals(max * threads, map.totalSize());
    info("Stats [size=" + map.totalSize() + ", rehashes=" + rehashes + ", releases=" + releases + ']');
    assertTrue(rehashes.get() > 0);
    assertEquals(rehashes.get(), releases.get());
}
Also used : GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Callable(java.util.concurrent.Callable) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 12 with GridCloseableIterator

use of org.apache.ignite.internal.util.lang.GridCloseableIterator in project ignite by apache.

the class GridOffHeapMapAbstractSelfTest method testIteratorAfterRehash.

/**
 * @throws Exception If failed.
 */
@Test
public void testIteratorAfterRehash() throws Exception {
    mem = 0;
    initCap = 10;
    concurrency = 2;
    map = newMap();
    final CountDownLatch startLatch = new CountDownLatch(1);
    final AtomicBoolean run = new AtomicBoolean(true);
    IgniteInternalFuture<?> itFut = multithreadedAsync(new Runnable() {

        @Override
        public void run() {
            try {
                startLatch.await();
                while (run.get()) {
                    GridCloseableIterator<IgniteBiTuple<byte[], byte[]>> it = map.iterator();
                    while (it.hasNext()) it.next();
                    it.close();
                }
            } catch (IgniteCheckedException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
                Thread.currentThread().interrupt();
            }
        }
    }, 1);
    IgniteInternalFuture<?> putFut = multithreadedAsync(new Runnable() {

        @Override
        public void run() {
            try {
                startLatch.await();
                Random rnd = new Random();
                for (int size = 50; size < 5000; size++) {
                    int valSize = rnd.nextInt(50) + 1;
                    for (int i = 0; i < size; i++) map.put(i, U.intToBytes(i), new byte[valSize]);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                Thread.currentThread().interrupt();
            }
        }
    }, 1);
    startLatch.countDown();
    putFut.get();
    run.set(false);
    itFut.get();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) Random(java.util.Random) CountDownLatch(java.util.concurrent.CountDownLatch) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 13 with GridCloseableIterator

use of org.apache.ignite.internal.util.lang.GridCloseableIterator in project ignite by apache.

the class GridOffHeapMapAbstractSelfTest method testMultithreadedOps.

/**
 * @throws Exception If failed.
 */
@Test
public void testMultithreadedOps() throws Exception {
    // Small enough for evictions.
    mem = 1512;
    lruStripes = 3;
    concurrency = 8;
    initCap = 256;
    map = newMap();
    long zeroAllocated = map.allocatedSize();
    X.println("Empty map offheap size: " + zeroAllocated);
    final AtomicBoolean stop = new AtomicBoolean();
    final byte[][] keys = new byte[127][16];
    Random rnd = new Random();
    for (int i = 0; i < keys.length; i++) {
        rnd.nextBytes(keys[i]);
        // hash
        keys[i][0] = (byte) i;
    }
    IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws IgniteCheckedException {
            Random rnd = new Random();
            while (!stop.get()) {
                byte[] key = keys[rnd.nextInt(keys.length)];
                int hash = key[0];
                byte[] val = new byte[1 + rnd.nextInt(11)];
                rnd.nextBytes(val);
                switch(rnd.nextInt(5)) {
                    case 0:
                        map.put(hash, key, val);
                        break;
                    case 1:
                        map.remove(hash, key);
                        break;
                    case 2:
                        map.contains(hash, key);
                        break;
                    case 3:
                        map.get(hash, key);
                        break;
                    case 4:
                    // break;
                    case 5:
                        GridCloseableIterator<IgniteBiTuple<byte[], byte[]>> iter = map.iterator();
                        while (iter.hasNext()) assertNotNull(iter.next());
                        iter.close();
                        break;
                }
            }
            return null;
        }
    }, 49);
    Thread.sleep(60000);
    stop.set(true);
    fut.get();
    for (byte[] key : keys) map.remove(key[0], key);
    assertEquals(0, map.totalSize());
    assertEquals(0, ((GridUnsafeMap) map).lruSize());
    assertEquals(zeroAllocated, map.allocatedSize());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Random(java.util.Random) GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 14 with GridCloseableIterator

use of org.apache.ignite.internal.util.lang.GridCloseableIterator in project ignite by apache.

the class GridOffHeapPartitionedMapAbstractSelfTest method testIteratorMultithreaded.

/**
 * @throws Exception If failed.
 */
@Test
public void testIteratorMultithreaded() throws Exception {
    initCap = 10;
    map = newMap();
    final AtomicInteger rehashes = new AtomicInteger();
    final AtomicInteger releases = new AtomicInteger();
    map.eventListener(new GridOffHeapEventListener() {

        @Override
        public void onEvent(GridOffHeapEvent evt) {
            switch(evt) {
                case REHASH:
                    rehashes.incrementAndGet();
                    break;
                case RELEASE:
                    releases.incrementAndGet();
                    break;
                // No-op.
                default:
            }
        }
    });
    final int max = 1024;
    final ConcurrentMap<String, String> m = new ConcurrentHashMap<>(max * parts);
    final AtomicInteger part = new AtomicInteger();
    multithreaded(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            int p = part.getAndIncrement();
            for (int i = 0; i < max; i++) {
                String key = string();
                String val = string();
                // info("Storing [i=" + i + ", key=" + key + ", val=" + val + ']');
                String old = m.putIfAbsent(key, val);
                if (old != null)
                    val = old;
                assertTrue(map.put(p, hash(key), key.getBytes(), val.getBytes()));
                assertTrue(map.contains(p, hash(key), key.getBytes()));
                assertNotNull(map.get(p, hash(key), key.getBytes()));
                assertEquals(new String(map.get(p, hash(key), key.getBytes())), val);
            }
            try (GridCloseableIterator<IgniteBiTuple<byte[], byte[]>> it = map.iterator()) {
                while (it.hasNext()) {
                    IgniteBiTuple<byte[], byte[]> t = it.next();
                    String k = new String(t.get1());
                    String v = new String(t.get2());
                    // info("Entry [k=" + k + ", v=" + v + ']');
                    assertEquals(m.get(k), v);
                }
            }
            return null;
        }
    }, parts);
    int cnt = 0;
    try (GridCloseableIterator<IgniteBiTuple<byte[], byte[]>> it = map.iterator()) {
        while (it.hasNext()) {
            IgniteBiTuple<byte[], byte[]> t = it.next();
            String k = new String(t.get1());
            String v = new String(t.get2());
            // info("Entry [k=" + k + ", v=" + v + ']');
            assertEquals(m.get(k), v);
            cnt++;
        }
    }
    assertEquals(map.size(), cnt);
    assertEquals(max * parts, map.size());
    info("Stats [size=" + map.size() + ", rehashes=" + rehashes + ", releases=" + releases + ']');
    assertTrue(rehashes.get() > 0);
    assertEquals(rehashes.get(), releases.get());
}
Also used : GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 15 with GridCloseableIterator

use of org.apache.ignite.internal.util.lang.GridCloseableIterator in project ignite by apache.

the class GridOffHeapPartitionedMapAbstractSelfTest method testIteratorRemoveMultithreaded.

/**
 * @throws Exception If failed.
 */
@Test
public void testIteratorRemoveMultithreaded() throws Exception {
    initCap = 10;
    map = newMap();
    final int max = 1024;
    final Map<String, String> m = new ConcurrentHashMap<>(max * parts);
    for (int i = 0; i < max; i++) {
        String key = string();
        String val = string();
        m.put(key, val);
        map.put(0, hash(key), key.getBytes(), val.getBytes());
    }
    final AtomicBoolean running = new AtomicBoolean(true);
    IgniteInternalFuture<?> iterFut = multithreadedAsync(new Runnable() {

        @Override
        public void run() {
            try {
                while (running.get()) {
                    try (GridCloseableIterator<IgniteBiTuple<byte[], byte[]>> it = map.iterator()) {
                        while (it.hasNext()) {
                            IgniteBiTuple<byte[], byte[]> tup = it.next();
                            String key = new String(tup.get1());
                            String val = new String(tup.get2());
                            String exp = m.get(key);
                            assertEquals(exp, val);
                        }
                    }
                }
            } catch (IgniteCheckedException e) {
                fail("Unexpected exception caught: " + e);
            }
        }
    }, 1);
    for (String key : m.keySet()) map.remove(0, hash(key), key.getBytes());
    running.set(false);
    iterFut.get();
    map.destruct();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

GridCloseableIterator (org.apache.ignite.internal.util.lang.GridCloseableIterator)16 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)14 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)6 Test (org.junit.Test)6 NoSuchElementException (java.util.NoSuchElementException)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 ClusterNode (org.apache.ignite.cluster.ClusterNode)5 ArrayList (java.util.ArrayList)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 HashSet (java.util.HashSet)3 Iterator (java.util.Iterator)3 Map (java.util.Map)3 Callable (java.util.concurrent.Callable)3 CacheException (javax.cache.CacheException)3 IgniteException (org.apache.ignite.IgniteException)3 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 Collection (java.util.Collection)2