Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 1C5E0200AF6 for ; Fri, 27 May 2016 13:30:43 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 1B2CA160A38; Fri, 27 May 2016 11:30:43 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 7C61D160A3D for ; Fri, 27 May 2016 13:30:41 +0200 (CEST) Received: (qmail 27370 invoked by uid 500); 27 May 2016 11:30:40 -0000 Mailing-List: contact commits-help@ignite.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ignite.apache.org Delivered-To: mailing list commits@ignite.apache.org Received: (qmail 27294 invoked by uid 99); 27 May 2016 11:30:40 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 27 May 2016 11:30:40 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 3BB03E962C; Fri, 27 May 2016 11:30:40 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: dmagda@apache.org To: commits@ignite.apache.org Date: Fri, 27 May 2016 11:30:47 -0000 Message-Id: In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [08/17] ignite git commit: ignite-2899 Fixed issues with 'keepBinary' handling, added test. (cherry picked from commit e47440d) archived-at: Fri, 27 May 2016 11:30:43 -0000 ignite-2899 Fixed issues with 'keepBinary' handling, added test. (cherry picked from commit e47440d) Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/54e432e9 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/54e432e9 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/54e432e9 Branch: refs/heads/master Commit: 54e432e9bb6927351ea45d85b94965925a8351c7 Parents: 07611bd Author: sboikov Authored: Mon May 23 21:08:14 2016 +0300 Committer: sboikov Committed: Tue May 24 11:21:11 2016 +0300 ---------------------------------------------------------------------- .../processors/cache/GridCacheAdapter.java | 71 +++- .../CacheKeepBinaryWithInterceptorTest.java | 419 +++++++++++++++++++ .../testsuites/IgniteBinaryCacheTestSuite.java | 2 + 3 files changed, 471 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/54e432e9/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java index b68a8ca..3a72ba2 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java @@ -1388,10 +1388,18 @@ public abstract class GridCacheAdapter implements IgniteInternalCache implements IgniteInternalCache t = (T2)get(key, !ctx.keepBinary(), true); + T2 t = (T2)get(key, !keepBinary, true); CacheEntry val = t != null ? new CacheEntryImplEx<>( - ctx.keepBinary() ? (K)ctx.unwrapBinaryIfNeeded(key, true, false) : key, + keepBinary ? (K)ctx.unwrapBinaryIfNeeded(key, true, false) : key, t.get1(), t.get2()) : null; if (ctx.config().getInterceptor() != null) { + key = keepBinary ? (K) ctx.unwrapBinaryIfNeeded(key, true, false) : key; + V val0 = (V)ctx.config().getInterceptor().onGet(key, t != null ? val.getValue() : null); val = (val0 != null) ? new CacheEntryImplEx<>(key, val0, t != null ? t.get2() : null) : null; @@ -1438,11 +1450,17 @@ public abstract class GridCacheAdapter implements IgniteInternalCache fut = getAsync(key, !ctx.keepBinary(), false); + final boolean keepBinary = ctx.keepBinary(); + + final K key0 = keepBinary ? (K)ctx.toCacheKeyObject(key) : key; + + IgniteInternalFuture fut = getAsync(key, !keepBinary, false); if (ctx.config().getInterceptor() != null) fut = fut.chain(new CX1, V>() { @Override public V applyx(IgniteInternalFuture f) throws IgniteCheckedException { + K key = keepBinary ? (K)ctx.unwrapBinaryIfNeeded(key0, true, false) : key0; + return (V)ctx.config().getInterceptor().onGet(key, f.get()); } }); @@ -1461,10 +1479,12 @@ public abstract class GridCacheAdapter implements IgniteInternalCache> fut = - (IgniteInternalFuture>)getAsync(key0, !ctx.keepBinary(), true); + (IgniteInternalFuture>)getAsync(key0, !keepBinary, true); final boolean intercept = ctx.config().getInterceptor() != null; @@ -1474,19 +1494,21 @@ public abstract class GridCacheAdapter implements IgniteInternalCache t = f.get(); - CacheEntry val = t != null ? new CacheEntryImplEx<>( - ctx.keepBinary() ? (K)ctx.unwrapBinaryIfNeeded(key0, true, false) : key0, - t.get1(), - t.get2()) - : null; + K key = keepBinary ? (K)ctx.unwrapBinaryIfNeeded(key0, true, false) : key0; - if (intercept) { - V val0 = (V)ctx.config().getInterceptor().onGet(key0, t != null ? val.getValue() : null); + CacheEntry val = t != null ? new CacheEntryImplEx<>( + key, + t.get1(), + t.get2()) + : null; - return val0 != null ? new CacheEntryImplEx(key0, val0, t != null ? t.get2() : null) : null; - } - else - return val; + if (intercept) { + V val0 = (V)ctx.config().getInterceptor().onGet(key, t != null ? val.getValue() : null); + + return val0 != null ? new CacheEntryImplEx(key, val0, t != null ? t.get2() : null) : null; + } + else + return val; } }); @@ -2870,17 +2892,24 @@ public abstract class GridCacheAdapter implements IgniteInternalCache(true) { @Override public V op(IgniteTxLocalAdapter tx) throws IgniteCheckedException { + K key0 = keepBinary ? (K)ctx.toCacheKeyObject(key) : key; + V ret = tx.removeAllAsync(ctx, null, - Collections.singletonList(key), + Collections.singletonList(key0), /*retval*/true, null, /*singleRmv*/false).get().value(); - if (ctx.config().getInterceptor() != null) + if (ctx.config().getInterceptor() != null) { + K key = keepBinary ? (K)ctx.unwrapBinaryIfNeeded(key0, true, false) : key0; + return (V)ctx.config().getInterceptor().onBeforeRemove(new CacheEntryImpl(key, ret)).get2(); + } return ret; } http://git-wip-us.apache.org/repos/asf/ignite/blob/54e432e9/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/binary/CacheKeepBinaryWithInterceptorTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/binary/CacheKeepBinaryWithInterceptorTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/binary/CacheKeepBinaryWithInterceptorTest.java new file mode 100644 index 0000000..60b48a8 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/binary/CacheKeepBinaryWithInterceptorTest.java @@ -0,0 +1,419 @@ +/* + * 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.binary; + +import org.apache.ignite.IgniteCache; +import org.apache.ignite.binary.BinaryObject; +import org.apache.ignite.cache.CacheAtomicityMode; +import org.apache.ignite.cache.CacheInterceptor; +import org.apache.ignite.cache.CacheWriteSynchronizationMode; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.lang.IgniteBiTuple; +import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi; +import org.apache.ignite.spi.discovery.tcp.ipfinder.TcpDiscoveryIpFinder; +import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.jetbrains.annotations.Nullable; + +import javax.cache.Cache; + +import static org.apache.ignite.cache.CacheAtomicityMode.*; +import static org.apache.ignite.cache.CacheWriteSynchronizationMode.*; + +/** + * + */ +@SuppressWarnings("unchecked") +public class CacheKeepBinaryWithInterceptorTest extends GridCommonAbstractTest { + /** */ + private static final TcpDiscoveryIpFinder IP_FINDER = new TcpDiscoveryVmIpFinder(true); + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(gridName); + + ((TcpDiscoverySpi)cfg.getDiscoverySpi()).setIpFinder(IP_FINDER); + + cfg.setMarshaller(null); + + return cfg; + } + + /** {@inheritDoc} */ + @Override protected void beforeTestsStarted() throws Exception { + super.beforeTestsStarted(); + + startGrid(0); + } + + /** {@inheritDoc} */ + @Override protected void afterTestsStopped() throws Exception { + stopAllGrids(); + + super.afterTestsStopped(); + } + + /** + * @throws Exception If failed. + */ + public void testKeepBinaryWithInterceptor() throws Exception { + keepBinaryWithInterceptor(cacheConfiguration(ATOMIC, false)); + keepBinaryWithInterceptor(cacheConfiguration(TRANSACTIONAL, false)); + + keepBinaryWithInterceptorPrimitives(cacheConfiguration(ATOMIC, true)); + keepBinaryWithInterceptorPrimitives(cacheConfiguration(TRANSACTIONAL, true)); + + startGridsMultiThreaded(1, 3); + + keepBinaryWithInterceptor(cacheConfiguration(ATOMIC, false)); + keepBinaryWithInterceptor(cacheConfiguration(TRANSACTIONAL, false)); + + keepBinaryWithInterceptorPrimitives(cacheConfiguration(ATOMIC, true)); + keepBinaryWithInterceptorPrimitives(cacheConfiguration(TRANSACTIONAL, true)); + } + + /** + * @param ccfg Cache configuration. + */ + private void keepBinaryWithInterceptor(CacheConfiguration ccfg) { + ignite(0).createCache(ccfg); + + try { + TestInterceptor1.onAfterRmv = 0; + TestInterceptor1.onBeforeRmv = 0; + TestInterceptor1.onAfterPut = 0; + TestInterceptor1.onBeforePut = 0; + TestInterceptor1.onGet = 0; + + IgniteCache cache = ignite(0).cache(null).withKeepBinary(); + + IgniteCache asyncCache = cache.withAsync(); + + cache.put(new TestKey(1), new TestValue(10)); + + cache.put(new TestKey(1), new TestValue(10)); + + BinaryObject obj = (BinaryObject)cache.get(new TestKey(1)); + assertEquals(10, obj.field("val")); + + asyncCache.get(new TestKey(1)); + obj = (BinaryObject)asyncCache.future().get(); + assertEquals(10, obj.field("val")); + + Cache.Entry e = (Cache.Entry)cache.getEntry(new TestKey(1)); + assertEquals(1, e.getKey().field("key")); + assertEquals(10, e.getValue().field("val")); + + asyncCache.getEntry(new TestKey(1)); + e = (Cache.Entry)asyncCache.future().get(); + assertEquals(1, e.getKey().field("key")); + assertEquals(10, e.getValue().field("val")); + + obj = (BinaryObject)cache.getAndRemove(new TestKey(1)); + assertEquals(10, obj.field("val")); + + cache.put(new TestKey(1), new TestValue(10)); + + assertTrue(cache.remove(new TestKey(1))); + + assertTrue(TestInterceptor1.onAfterRmv > 0); + assertTrue(TestInterceptor1.onBeforeRmv > 0); + assertTrue(TestInterceptor1.onAfterPut > 0); + assertTrue(TestInterceptor1.onBeforePut > 0); + assertTrue(TestInterceptor1.onGet > 0); + } + finally { + ignite(0).destroyCache(ccfg.getName()); + } + } + + /** + * @param ccfg Cache configuration. + */ + private void keepBinaryWithInterceptorPrimitives(CacheConfiguration ccfg) { + ignite(0).createCache(ccfg); + + try { + TestInterceptor2.onAfterRmv = 0; + TestInterceptor2.onBeforeRmv = 0; + TestInterceptor2.onAfterPut = 0; + TestInterceptor2.onBeforePut = 0; + TestInterceptor2.onGet = 0; + + IgniteCache cache = ignite(0).cache(null).withKeepBinary(); + + IgniteCache asyncCache = cache.withAsync(); + + cache.put(1, 10); + + cache.put(1, 10); + + Integer obj = (Integer)cache.get(1); + assertEquals((Integer)10, obj); + + asyncCache.get(1); + obj = (Integer)asyncCache.future().get(); + assertEquals((Integer)10, obj); + + Cache.Entry e = (Cache.Entry)cache.getEntry(1); + assertEquals((Integer)1, e.getKey()); + assertEquals((Integer)10, e.getValue()); + + asyncCache.getEntry(1); + e = (Cache.Entry)asyncCache.future().get(); + assertEquals((Integer)1, e.getKey()); + assertEquals((Integer)10, e.getValue()); + + obj = (Integer)cache.getAndRemove(1); + assertEquals((Integer)10, obj); + + cache.put(1, 10); + + assertTrue(cache.remove(1)); + + assertTrue(TestInterceptor2.onAfterRmv > 0); + assertTrue(TestInterceptor2.onBeforeRmv > 0); + assertTrue(TestInterceptor2.onAfterPut > 0); + assertTrue(TestInterceptor2.onBeforePut > 0); + assertTrue(TestInterceptor2.onGet > 0); + } + finally { + ignite(0).destroyCache(ccfg.getName()); + } + } + + /** + * @param atomicityMode Cache atomicity mode. + * @param testPrimitives {@code True} if test interceptor with primitive values. + * @return Cache configuration. + */ + private CacheConfiguration cacheConfiguration(CacheAtomicityMode atomicityMode, boolean testPrimitives) { + CacheConfiguration ccfg = new CacheConfiguration(); + + ccfg.setAtomicityMode(atomicityMode); + ccfg.setInterceptor(testPrimitives ? new TestInterceptor2() : new TestInterceptor1()); + ccfg.setWriteSynchronizationMode(FULL_SYNC); + ccfg.setBackups(1); + + return ccfg; + } + + /** + * + */ + static class TestInterceptor1 implements CacheInterceptor { + /** */ + static int onGet; + + /** */ + static int onBeforePut; + + /** */ + static int onAfterPut; + + /** */ + static int onBeforeRmv; + + /** */ + static int onAfterRmv; + + /** {@inheritDoc} */ + @Nullable @Override public BinaryObject onGet(BinaryObject key, @Nullable BinaryObject val) { + System.out.println("Get [key=" + key + ", val=" + val + ']'); + + onGet++; + + assertEquals(1, key.field("key")); + assertEquals(10, val.field("val")); + + return val; + } + + /** {@inheritDoc} */ + @Nullable @Override public BinaryObject onBeforePut(Cache.Entry entry, BinaryObject newVal) { + System.out.println("Before put [e=" + entry + ", newVal=" + newVal + ']'); + + onBeforePut++; + + if (entry.getValue() != null) + assertEquals(10, entry.getValue().field("val")); + + assertEquals(1, entry.getKey().field("key")); + assertEquals(10, newVal.field("val")); + + return newVal; + } + + /** {@inheritDoc} */ + @Override public void onAfterPut(Cache.Entry entry) { + System.out.println("After put [e=" + entry + ']'); + + onAfterPut++; + + assertEquals(1, entry.getKey().field("key")); + assertEquals(10, entry.getValue().field("val")); + } + + /** {@inheritDoc} */ + @Nullable @Override public IgniteBiTuple onBeforeRemove(Cache.Entry entry) { + assertEquals(1, entry.getKey().field("key")); + assertEquals(10, entry.getValue().field("val")); + + onBeforeRmv++; + + return new IgniteBiTuple<>(false, entry.getValue()); + } + + /** {@inheritDoc} */ + @Override public void onAfterRemove(Cache.Entry entry) { + System.out.println("After remove [e=" + entry + ']'); + + onAfterRmv++; + + assertEquals(1, entry.getKey().field("key")); + assertEquals(10, entry.getValue().field("val")); + } + } + + /** + * + */ + static class TestInterceptor2 implements CacheInterceptor { + /** */ + static int onGet; + + /** */ + static int onBeforePut; + + /** */ + static int onAfterPut; + + /** */ + static int onBeforeRmv; + + /** */ + static int onAfterRmv; + + /** {@inheritDoc} */ + @Nullable @Override public Integer onGet(Integer key, @Nullable Integer val) { + System.out.println("Get [key=" + key + ", val=" + val + ']'); + + onGet++; + + assertEquals((Integer)1, key); + assertEquals((Integer)10, val); + + return val; + } + + /** {@inheritDoc} */ + @Nullable @Override public Integer onBeforePut(Cache.Entry entry, Integer newVal) { + System.out.println("Before put [e=" + entry + ", newVal=" + newVal + ']'); + + onBeforePut++; + + if (entry.getValue() != null) + assertEquals((Integer)10, entry.getValue()); + + assertEquals((Integer)1, entry.getKey()); + assertEquals((Integer)10, newVal); + + return newVal; + } + + /** {@inheritDoc} */ + @Override public void onAfterPut(Cache.Entry entry) { + System.out.println("After put [e=" + entry + ']'); + + onAfterPut++; + + assertEquals((Integer)1, entry.getKey()); + assertEquals((Integer)10, entry.getValue()); + } + + /** {@inheritDoc} */ + @Nullable @Override public IgniteBiTuple onBeforeRemove(Cache.Entry entry) { + assertEquals((Integer)1, entry.getKey()); + assertEquals((Integer)10, entry.getValue()); + + onBeforeRmv++; + + return new IgniteBiTuple<>(false, entry.getValue()); + } + + /** {@inheritDoc} */ + @Override public void onAfterRemove(Cache.Entry entry) { + System.out.println("After remove [e=" + entry + ']'); + + onAfterRmv++; + + assertEquals((Integer)1, entry.getKey()); + assertEquals((Integer)10, entry.getValue()); + } + } + + /** + * + */ + static class TestKey { + /** */ + private int key; + + /** + * @param key Key. + */ + public TestKey(int key) { + this.key = key; + } + + /** {@inheritDoc} */ + @Override public boolean equals(Object o) { + if (this == o) + return true; + + if (o == null || getClass() != o.getClass()) + return false; + + TestKey testKey = (TestKey)o; + + return key == testKey.key; + + } + + /** {@inheritDoc} */ + @Override public int hashCode() { + return key; + } + } + + /** + * + */ + static class TestValue { + /** */ + private int val; + + /** + * @param val Value. + */ + public TestValue(int val) { + this.val = val; + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/54e432e9/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBinaryCacheTestSuite.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBinaryCacheTestSuite.java b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBinaryCacheTestSuite.java index 69ca67e..2668598 100644 --- a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBinaryCacheTestSuite.java +++ b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBinaryCacheTestSuite.java @@ -27,6 +27,7 @@ import org.apache.ignite.internal.processors.cache.GridCacheOffHeapTieredAtomicS import org.apache.ignite.internal.processors.cache.GridCacheOffHeapTieredEvictionAtomicSelfTest; import org.apache.ignite.internal.processors.cache.GridCacheOffHeapTieredEvictionSelfTest; import org.apache.ignite.internal.processors.cache.GridCacheOffHeapTieredSelfTest; +import org.apache.ignite.internal.processors.cache.binary.CacheKeepBinaryWithInterceptorTest; import org.apache.ignite.internal.processors.cache.expiry.IgniteCacheAtomicLocalExpiryPolicyTest; import org.apache.ignite.internal.processors.cache.binary.GridBinaryCacheEntryMemorySizeSelfTest; import org.apache.ignite.internal.processors.cache.binary.datastreaming.DataStreamProcessorBinarySelfTest; @@ -97,6 +98,7 @@ public class IgniteBinaryCacheTestSuite extends TestSuite { suite.addTestSuite(GridCacheAffinityRoutingBinarySelfTest.class); suite.addTestSuite(GridBinaryCacheEntryMemorySizeSelfTest.class); + suite.addTestSuite(CacheKeepBinaryWithInterceptorTest.class); return suite; }