ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sboi...@apache.org
Subject [01/53] [abbrv] [partial] incubator-ignite git commit: # ignite-164 : GridAbstractTest -> IgniteAbstractTest - auto-renaming with all suggested options + rename all methods and fields inside AbstractTest
Date Wed, 04 Feb 2015 14:37:35 GMT
Repository: incubator-ignite
Updated Branches:
  refs/heads/ignite-164 1989b2c9c -> c1212c3a1


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/6563e8a9/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffHeapMultiThreadedUpdateAbstractSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffHeapMultiThreadedUpdateAbstractSelfTest.java
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffHeapMultiThreadedUpdateAbstractSelfTest.java
new file mode 100644
index 0000000..b46a181
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffHeapMultiThreadedUpdateAbstractSelfTest.java
@@ -0,0 +1,399 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.cache;
+
+import org.apache.ignite.*;
+import org.apache.ignite.cache.*;
+import org.apache.ignite.internal.*;
+import org.apache.ignite.lang.*;
+import org.apache.ignite.testframework.*;
+
+import javax.cache.processor.*;
+import java.io.*;
+import java.util.concurrent.*;
+import java.util.concurrent.atomic.*;
+
+import static org.apache.ignite.cache.CacheAtomicWriteOrderMode.*;
+import static org.apache.ignite.cache.CacheMemoryMode.*;
+import static org.apache.ignite.cache.CacheMode.*;
+import static org.apache.ignite.cache.CacheWriteSynchronizationMode.*;
+
+/**
+ * Multithreaded update test with off heap enabled.
+ */
+public abstract class IgniteCacheOffHeapMultiThreadedUpdateAbstractSelfTest extends IgniteCacheAbstractSelfTest
{
+    /** */
+    protected static volatile boolean failed;
+
+    /** {@inheritDoc} */
+    @Override protected int gridCount() {
+        return 2;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected CacheConfiguration cacheConfiguration(String gridName) throws Exception
{
+        CacheConfiguration ccfg = new CacheConfiguration();
+
+        ccfg.setAtomicityMode(atomicityMode());
+        ccfg.setCacheMode(PARTITIONED);
+        ccfg.setBackups(1);
+        ccfg.setMemoryMode(OFFHEAP_TIERED);
+        ccfg.setOffHeapMaxMemory(1024 * 1024);
+        ccfg.setWriteSynchronizationMode(FULL_SYNC);
+        ccfg.setAtomicWriteOrderMode(PRIMARY);
+        ccfg.setPortableEnabled(portableEnabled());
+
+        return ccfg;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected long getTestTimeout() {
+        return 3 * 60_000;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTest() throws Exception {
+        super.beforeTest();
+
+        failed = false;
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testTransform() throws Exception {
+        testTransform(keyForNode(0));
+
+        if (gridCount() > 1)
+            testTransform(keyForNode(1));
+    }
+
+    /**
+     * @param key Key.
+     * @throws Exception If failed.
+     */
+    private void testTransform(final Integer key) throws Exception {
+        final IgniteCache<Integer, Integer> cache = igniteEx(0).jcache(null);
+
+        cache.put(key, 0);
+
+        final int THREADS = 5;
+        final int ITERATIONS_PER_THREAD = 10_000;
+
+        GridTestUtils.runMultiThreaded(new Callable<Void>() {
+            @Override public Void call() throws Exception {
+                for (int i = 0; i < ITERATIONS_PER_THREAD && !failed; i++) {
+                    if (i % 500 == 0)
+                        log.info("Iteration " + i);
+
+                    cache.invoke(key, new IncProcessor());
+                }
+
+                return null;
+            }
+        }, THREADS, "transform");
+
+        for (int i = 0; i < gridCount(); i++) {
+            Integer val = (Integer) igniteEx(i).cache(null).get(key);
+
+            assertEquals("Unexpected value for grid " + i, (Integer)(ITERATIONS_PER_THREAD
* THREADS), val);
+        }
+
+        assertFalse(failed);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testPut() throws Exception {
+        testPut(keyForNode(0));
+
+        if (gridCount() > 1)
+            testPut(keyForNode(1));
+    }
+
+    /**
+     * @param key Key.
+     * @throws Exception If failed.
+     */
+    private void testPut(final Integer key) throws Exception {
+        final GridCache<Integer, Integer> cache = igniteEx(0).cache(null);
+
+        cache.put(key, 0);
+
+        final int THREADS = 5;
+        final int ITERATIONS_PER_THREAD = iterations();
+
+        GridTestUtils.runMultiThreaded(new Callable<Void>() {
+            @Override public Void call() throws Exception {
+                for (int i = 0; i < ITERATIONS_PER_THREAD && !failed; i++) {
+                    if (i % 500 == 0)
+                        log.info("Iteration " + i);
+
+                    Integer val = cache.put(key, i);
+
+                    assertNotNull(val);
+                }
+
+                return null;
+            }
+        }, THREADS, "put");
+
+        for (int i = 0; i < gridCount(); i++) {
+            Integer val = (Integer) igniteEx(i).cache(null).get(key);
+
+            assertNotNull("Unexpected value for grid " + i, val);
+        }
+
+        assertFalse(failed);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testPutxIfAbsent() throws Exception {
+        testPutxIfAbsent(keyForNode(0));
+
+        if (gridCount() > 1)
+            testPutxIfAbsent(keyForNode(1));
+    }
+
+    /**
+     * @param key Key.
+     * @throws Exception If failed.
+     */
+    private void testPutxIfAbsent(final Integer key) throws Exception {
+        final GridCache<Integer, Integer> cache = igniteEx(0).cache(null);
+
+        cache.put(key, 0);
+
+        final int THREADS = 5;
+        final int ITERATIONS_PER_THREAD = iterations();
+
+        GridTestUtils.runMultiThreaded(new Callable<Void>() {
+            @Override public Void call() throws Exception {
+                for (int i = 0; i < ITERATIONS_PER_THREAD && !failed; i++) {
+                    if (i % 500 == 0)
+                        log.info("Iteration " + i);
+
+                    assertFalse(cache.putxIfAbsent(key, 100));
+                }
+
+                return null;
+            }
+        }, THREADS, "putxIfAbsent");
+
+        for (int i = 0; i < gridCount(); i++) {
+            Integer val = (Integer) igniteEx(i).cache(null).get(key);
+
+            assertEquals("Unexpected value for grid " + i, (Integer)0, val);
+        }
+
+        assertFalse(failed);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testPutWithFilter() throws Exception {
+        testPutWithFilter(keyForNode(0));
+
+        if (gridCount() > 1)
+            testPutWithFilter(keyForNode(1));
+    }
+
+    /**
+     * @param key Key.
+     * @throws Exception If failed.
+     */
+    private void testPutWithFilter(final Integer key) throws Exception {
+        final GridCache<Integer, Integer> cache = igniteEx(0).cache(null);
+
+        cache.put(key, 0);
+
+        final int THREADS = 5;
+        final int ITERATIONS_PER_THREAD = iterations();
+
+        GridTestUtils.runMultiThreaded(new Callable<Void>() {
+            @Override public Void call() throws Exception {
+                for (int i = 0; i < ITERATIONS_PER_THREAD && !failed; i++) {
+                    if (i % 500 == 0)
+                        log.info("Iteration " + i);
+
+                    assertTrue(cache.putx(key, i, new TestFilter()));
+                }
+
+                return null;
+            }
+        }, THREADS, "putWithFilter");
+
+        for (int i = 0; i < gridCount(); i++) {
+            Integer val = (Integer) igniteEx(i).cache(null).get(key);
+
+            assertNotNull("Unexpected value for grid " + i, val);
+        }
+
+        assertFalse(failed);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testPutGet() throws Exception {
+        testPutGet(keyForNode(0));
+
+        if (gridCount() > 1)
+            testPutGet(keyForNode(1));
+    }
+
+    /**
+     * @param key Key.
+     * @throws Exception If failed.
+     */
+    private void testPutGet(final Integer key) throws Exception {
+        final GridCache<Integer, Integer> cache = igniteEx(0).cache(null);
+
+        cache.put(key, 0);
+
+        final int THREADS = 5;
+        final int ITERATIONS_PER_THREAD = iterations();
+
+        IgniteInternalFuture<Long> putFut = GridTestUtils.runMultiThreadedAsync(new
Callable<Void>() {
+            @Override public Void call() throws Exception {
+                for (int i = 0; i < ITERATIONS_PER_THREAD; i++) {
+                    if (i % 1000 == 0)
+                        log.info("Put iteration " + i);
+
+                    assertTrue(cache.putx(key, i));
+                }
+
+                return null;
+            }
+        }, THREADS, "put");
+
+        final AtomicBoolean stop = new AtomicBoolean();
+
+        IgniteInternalFuture<Long> getFut;
+
+        try {
+            getFut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {
+                @Override public Void call() throws Exception {
+                    int cnt = 0;
+
+                    while (!stop.get()) {
+                        if (++cnt % 5000 == 0)
+                            log.info("Get iteration " + cnt);
+
+                        assertNotNull(cache.get(key));
+                    }
+
+                    return null;
+                }
+            }, THREADS, "get");
+
+            putFut.get();
+        }
+        finally {
+            stop.set(true);
+        }
+
+        getFut.get();
+
+        for (int i = 0; i < gridCount(); i++) {
+            Integer val = (Integer) igniteEx(i).cache(null).get(key);
+
+            assertNotNull("Unexpected value for grid " + i, val);
+        }
+    }
+
+    /**
+     * @param idx Node index.
+     * @return Primary key for node.
+     */
+    protected Integer keyForNode(int idx) {
+        Integer key0 = null;
+
+        GridCache<Integer, Integer> cache = igniteEx(0).cache(null);
+
+        for (int i = 0; i < 10_000; i++) {
+            if (cache.affinity().isPrimary(igniteEx(idx).localNode(), i)) {
+                key0 = i;
+
+                break;
+            }
+        }
+
+        assertNotNull(key0);
+
+        return key0;
+    }
+
+    /**
+     * @return Number of iterations.
+     */
+    protected int iterations() {
+        return 10_000;
+    }
+
+    /**
+     *
+     */
+    protected static class IncProcessor implements EntryProcessor<Integer, Integer, Void>,
Serializable {
+        /** {@inheritDoc} */
+        @Override public Void process(MutableEntry<Integer, Integer> e, Object... args)
{
+            Integer val = e.getValue();
+
+            if (val == null) {
+                failed = true;
+
+                System.out.println(Thread.currentThread() + " got null in processor: " +
val);
+
+                return null;
+            }
+
+            e.setValue(val + 1);
+
+            return null;
+        }
+    }
+
+    /**
+     *
+     */
+    protected static class TestFilter implements IgnitePredicate<CacheEntry<Integer,
Integer>> {
+        /** {@inheritDoc} */
+        @Override public boolean apply(CacheEntry<Integer, Integer> e) {
+            if (e == null) {
+                failed = true;
+
+                System.out.println(Thread.currentThread() + " got null entry in filter: "
+ e);
+
+                return false;
+            }
+            else if (e.peek() == null) {
+                failed = true;
+
+                System.out.println(Thread.currentThread() + " got null value in filter: "
+ e);
+
+                return false;
+            }
+
+            return true;
+        }
+    }
+}


Mime
View raw message