# sprint-2 test and fix for IGNITE-280
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/2ef337cc Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/2ef337cc Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/2ef337cc Branch: refs/heads/ignite-283 Commit: 2ef337cc7d35407f7541b1035de46de40df37e98 Parents: 786690c Author: sboikov <sboi...@gridgain.com> Authored: Wed Feb 18 16:50:18 2015 +0300 Committer: sboikov <sboi...@gridgain.com> Committed: Wed Feb 18 16:50:18 2015 +0300 ---------------------------------------------------------------------- .../processors/cache/GridCacheAdapter.java | 33 +++- .../distributed/dht/GridDhtCacheAdapter.java | 6 +- .../IgniteCacheExpiryStoreLoadSelfTest.java | 166 +++++++++++++++++++ .../IgniteCacheExpiryPolicyTestSuite.java | 3 + 4 files changed, 204 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2ef337cc/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 cba8b1c..4359a6e 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 @@ -3825,7 +3825,11 @@ public abstract class GridCacheAdapter<K, V> implements GridCache<K, V>, final boolean replicate = ctx.isDrEnabled(); final long topVer = ctx.affinity().affinityTopologyVersion(); - final ExpiryPolicy plc = ctx.expiry(); + GridCacheProjectionImpl<K, V> prj = ctx.projectionPerCall(); + + ExpiryPolicy plc0 = prj != null ? prj.expiry() : null; + + final ExpiryPolicy plc = plc0 != null ? plc0 : ctx.expiry(); if (ctx.store().isLocalStore()) { IgniteDataLoaderImpl<K, V> ldr = ctx.kernalContext().<K, V>dataLoad().dataLoader(ctx.namex(), false); @@ -4090,8 +4094,12 @@ public abstract class GridCacheAdapter<K, V> implements GridCache<K, V>, ctx.kernalContext().task().setThreadContext(TC_NO_FAILOVER, true); + GridCacheProjectionImpl<K, V> prj = ctx.projectionPerCall(); + + ExpiryPolicy plc = prj != null ? prj.expiry() : null; + return ctx.kernalContext().closure().callAsync(BROADCAST, - Arrays.asList(new LoadCacheClosure<>(ctx.name(), p, args)), + Arrays.asList(new LoadCacheClosure<>(ctx.name(), p, args, plc)), nodes.nodes()); } @@ -6153,6 +6161,9 @@ public abstract class GridCacheAdapter<K, V> implements GridCache<K, V>, @IgniteInstanceResource private Ignite ignite; + /** */ + private ExpiryPolicy plc; + /** * Required by {@link Externalizable}. */ @@ -6164,11 +6175,17 @@ public abstract class GridCacheAdapter<K, V> implements GridCache<K, V>, * @param cacheName Cache name. * @param p Predicate. * @param args Arguments. + * @param plc Explicitly specified expiry policy. */ - private LoadCacheClosure(String cacheName, IgniteBiPredicate<K, V> p, Object[] args) { + private LoadCacheClosure(String cacheName, + IgniteBiPredicate<K, V> p, + Object[] args, + @Nullable ExpiryPolicy plc) + { this.cacheName = cacheName; this.p = p; this.args = args; + this.plc = plc; } /** {@inheritDoc} */ @@ -6177,6 +6194,9 @@ public abstract class GridCacheAdapter<K, V> implements GridCache<K, V>, assert cache != null : cacheName; + if (plc != null) + cache = cache.withExpiryPolicy(plc); + cache.localLoadCache(p, args); return null; @@ -6189,6 +6209,11 @@ public abstract class GridCacheAdapter<K, V> implements GridCache<K, V>, out.writeObject(args); U.writeString(out, cacheName); + + if (plc != null) + out.writeObject(new IgniteExternalizableExpiryPolicy(plc)); + else + out.writeObject(null); } /** {@inheritDoc} */ @@ -6199,6 +6224,8 @@ public abstract class GridCacheAdapter<K, V> implements GridCache<K, V>, args = (Object[])in.readObject(); cacheName = U.readString(in); + + plc = (ExpiryPolicy)in.readObject(); } /** {@inheritDoc} */ http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2ef337cc/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtCacheAdapter.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtCacheAdapter.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtCacheAdapter.java index 43653c7..6e828fb 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtCacheAdapter.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtCacheAdapter.java @@ -398,7 +398,11 @@ public abstract class GridDhtCacheAdapter<K, V> extends GridDistributedCacheAdap final long topVer = ctx.affinity().affinityTopologyVersion(); - final ExpiryPolicy plc = ctx.expiry(); + GridCacheProjectionImpl<K, V> prj = ctx.projectionPerCall(); + + ExpiryPolicy plc0 = prj != null ? prj.expiry() : null; + + final ExpiryPolicy plc = plc0 != null ? plc0 : ctx.expiry(); ctx.store().loadCache(new CI3<K, V, GridCacheVersion>() { @Override public void apply(K key, V val, @Nullable GridCacheVersion ver) { http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2ef337cc/modules/core/src/test/java/org/apache/ignite/cache/store/IgniteCacheExpiryStoreLoadSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/cache/store/IgniteCacheExpiryStoreLoadSelfTest.java b/modules/core/src/test/java/org/apache/ignite/cache/store/IgniteCacheExpiryStoreLoadSelfTest.java new file mode 100644 index 0000000..64c76a8 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/cache/store/IgniteCacheExpiryStoreLoadSelfTest.java @@ -0,0 +1,166 @@ +/* + * 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.cache.store; + +import org.apache.ignite.*; +import org.apache.ignite.cache.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.internal.processors.cache.*; +import org.apache.ignite.lang.*; +import org.jetbrains.annotations.*; + +import javax.cache.*; +import javax.cache.configuration.*; +import javax.cache.expiry.*; +import javax.cache.integration.*; +import java.util.*; +import java.util.concurrent.*; + +import static org.apache.ignite.cache.CacheMode.*; + +/** + * Test check that cache values removes from cache on expiry. + */ +public class IgniteCacheExpiryStoreLoadSelfTest extends GridCacheAbstractSelfTest { + /** Expected time to live in milliseconds. */ + private static final int TIME_TO_LIVE = 1000; + + /** Additional time to wait expiry process in milliseconds. */ + private static final int WAIT_TIME = 500; + + /** {@inheritDoc} */ + @Override protected int gridCount() { + return 3; + } + + /** {@inheritDoc} */ + @Override protected CacheMode cacheMode() { + return PARTITIONED; + } + + /** {@inheritDoc} */ + @SuppressWarnings("unchecked") + @Override protected CacheConfiguration cacheConfiguration(String gridName) throws Exception { + CacheConfiguration cfg = super.cacheConfiguration(gridName); + + cfg.setCacheStoreFactory(new FactoryBuilder.SingletonFactory(new TestStore())); + cfg.setReadThrough(true); + cfg.setWriteThrough(true); + cfg.setLoadPreviousValue(true); + + return cfg; + } + + /** + * @throws Exception If failed. + */ + public void testLoadCacheWithExpiry() throws Exception { + checkLoad(false); + } + + /** + * @throws Exception If failed. + */ + public void testLoadCacheWithExpiryAsync() throws Exception { + checkLoad(true); + } + + /** + * @param async If {@code true} uses asynchronous method. + * @throws Exception If failed. + */ + private void checkLoad(boolean async) throws Exception { + IgniteCache<String, Integer> cache = jcache(0) + .withExpiryPolicy(new CreatedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, TIME_TO_LIVE))); + + List<Integer> keys = new ArrayList<>(); + + keys.add(primaryKey(jcache(0))); + keys.add(primaryKey(jcache(1))); + keys.add(primaryKey(jcache(2))); + + if (async) { + IgniteCache<String, Integer> asyncCache = cache.withAsync(); + + asyncCache.loadCache(null, keys.toArray(new Integer[3])); + + asyncCache.future().get(); + } + else + cache.loadCache(null, keys.toArray(new Integer[3])); + + assertEquals(3, cache.size(CachePeekMode.PRIMARY)); + + Thread.sleep(TIME_TO_LIVE + WAIT_TIME); + + assertEquals(0, cache.size(CachePeekMode.PRIMARY)); + } + + /** + * Test cache store. + */ + private static class TestStore implements CacheStore<Integer, Integer> { + /** {@inheritDoc} */ + @Override public void loadCache(IgniteBiInClosure<Integer, Integer> clo, + @Nullable Object... args) throws CacheLoaderException { + assertNotNull(args); + assertTrue(args.length > 0); + + for (Object arg : args) { + Integer k = (Integer)arg; + + clo.apply(k, k); + } + } + + /** {@inheritDoc} */ + @Override public void txEnd(boolean commit) throws CacheWriterException { + // No-op. + } + + /** {@inheritDoc} */ + @Override public Integer load(Integer key) throws CacheLoaderException { + return null; + } + + /** {@inheritDoc} */ + @Override public Map<Integer, Integer> loadAll(Iterable<? extends Integer> keys) { + return null; + } + + /** {@inheritDoc} */ + @Override public void write(Cache.Entry<? extends Integer, ? extends Integer> entry) { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void writeAll(Collection<Cache.Entry<? extends Integer, ? extends Integer>> entries) { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void delete(Object key) { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void deleteAll(Collection<?> keys) { + // No-op. + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2ef337cc/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/expiry/IgniteCacheExpiryPolicyTestSuite.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/expiry/IgniteCacheExpiryPolicyTestSuite.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/expiry/IgniteCacheExpiryPolicyTestSuite.java index 233ae2b..3a87d6d 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/expiry/IgniteCacheExpiryPolicyTestSuite.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/expiry/IgniteCacheExpiryPolicyTestSuite.java @@ -18,6 +18,7 @@ package org.apache.ignite.internal.processors.cache.expiry; import junit.framework.*; +import org.apache.ignite.cache.store.*; /** * @@ -45,6 +46,8 @@ public class IgniteCacheExpiryPolicyTestSuite extends TestSuite { suite.addTestSuite(IgniteCacheAtomicExpiryPolicyWithStoreTest.class); suite.addTestSuite(IgniteCacheTxExpiryPolicyWithStoreTest.class); + suite.addTestSuite(IgniteCacheExpiryStoreLoadSelfTest.class); + return suite; } }