Repository: incubator-ignite Updated Branches: refs/heads/ignite-45 95537b58c -> 87a941a3b
IGNITE-541 - Dynamic caching in SpringCacheManager Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/be25aa36 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/be25aa36 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/be25aa36 Branch: refs/heads/ignite-45 Commit: be25aa36787e1b0b5a04104d726fcb6c328a649a Parents: dbfaf0a Author: Valentin Kulichenko <vkuliche...@gridgain.com> Authored: Fri Mar 20 16:53:19 2015 -0700 Committer: Valentin Kulichenko <vkuliche...@gridgain.com> Committed: Fri Mar 20 16:53:19 2015 -0700 ---------------------------------------------------------------------- .../apache/ignite/cache/spring/SpringCache.java | 142 ++------ .../ignite/cache/spring/SpringCacheManager.java | 103 ++++-- .../cache/spring/SpringDynamicCacheManager.java | 339 ------------------- .../spring/GridSpringCacheManagerSelfTest.java | 107 +++++- .../GridSpringDynamicCacheManagerSelfTest.java | 219 ------------ .../GridSpringDynamicCacheTestService.java | 34 +- .../org/apache/ignite/spring/spring-caching.xml | 10 + .../ignite/spring/spring-dynamic-caching.xml | 43 --- .../testsuites/IgniteSpringTestSuite.java | 1 - 9 files changed, 243 insertions(+), 755 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be25aa36/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringCache.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringCache.java b/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringCache.java index 07e38a1..7651fbe 100644 --- a/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringCache.java +++ b/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringCache.java @@ -18,52 +18,28 @@ package org.apache.ignite.cache.spring; import org.apache.ignite.*; -import org.apache.ignite.internal.processors.cache.*; -import org.apache.ignite.internal.util.typedef.*; -import org.apache.ignite.lang.*; import org.springframework.cache.*; import org.springframework.cache.support.*; -import java.io.*; - /** * Spring cache implementation. */ -class SpringCache implements Cache, Serializable { - /** */ - private String name; - - /** */ - private Ignite ignite; - - /** */ - private CacheProjection<Object, Object> cache; - +class SpringCache implements Cache { /** */ - private IgniteClosure<Object, Object> keyFactory; + private final IgniteCache<Object, Object> cache; /** - * @param name Cache name. - * @param ignite Ignite instance. * @param cache Cache. - * @param keyFactory Key factory. */ - SpringCache(String name, - Ignite ignite, - CacheProjection<?, ?> cache, - IgniteClosure<Object, Object> keyFactory) - { + SpringCache(IgniteCache<Object, Object> cache) { assert cache != null; - this.name = name; - this.ignite = ignite; - this.cache = (CacheProjection<Object, Object>)cache; - this.keyFactory = keyFactory != null ? keyFactory : F.identity(); + this.cache = cache; } /** {@inheritDoc} */ @Override public String getName() { - return name; + return cache.getName(); } /** {@inheritDoc} */ @@ -73,116 +49,42 @@ class SpringCache implements Cache, Serializable { /** {@inheritDoc} */ @Override public Cache.ValueWrapper get(Object key) { - try { - Object val = cache.get(keyFactory.apply(key)); - - return val != null ? new SimpleValueWrapper(val) : null; - } - catch (IgniteCheckedException e) { - throw new IgniteException("Failed to get value from cache [cacheName=" + cache.name() + - ", key=" + key + ']', e); - } + Object val = cache.get(key); + + return val != null ? new SimpleValueWrapper(val) : null; } /** {@inheritDoc} */ + @SuppressWarnings("unchecked") @Override public <T> T get(Object key, Class<T> type) { - try { - Object val = cache.get(keyFactory.apply(key)); - - if (val != null && type != null && !type.isInstance(val)) - throw new IllegalStateException("Cached value is not of required type [cacheName=" + cache.name() + - ", key=" + key + ", val=" + val + ", requiredType=" + type + ']'); - - return (T)val; - } - catch (IgniteCheckedException e) { - throw new IgniteException("Failed to get value from cache [cacheName=" + cache.name() + - ", key=" + key + ']', e); - } + Object val = cache.get(key); + + if (val != null && type != null && !type.isInstance(val)) + throw new IllegalStateException("Cached value is not of required type [cacheName=" + cache.getName() + + ", key=" + key + ", val=" + val + ", requiredType=" + type + ']'); + + return (T)val; } /** {@inheritDoc} */ @Override public void put(Object key, Object val) { - try { - cache.putx(keyFactory.apply(key), val); - } - catch (IgniteCheckedException e) { - throw new IgniteException("Failed to put value to cache [cacheName=" + cache.name() + - ", key=" + key + ", val=" + val + ']', e); - } + cache.put(key, val); } /** {@inheritDoc} */ @Override public ValueWrapper putIfAbsent(Object key, Object val) { - try { - Object old = cache.putIfAbsent(keyFactory.apply(key), val); - - return old != null ? new SimpleValueWrapper(old) : null; - } - catch (IgniteCheckedException e) { - throw new IgniteException("Failed to put value to cache [cacheName=" + cache.name() + - ", key=" + key + ", val=" + val + ']', e); - } + Object old = cache.putIfAbsent(key, val); + + return old != null ? new SimpleValueWrapper(old) : null; } /** {@inheritDoc} */ @Override public void evict(Object key) { - try { - cache.removex(keyFactory.apply(key)); - } - catch (IgniteCheckedException e) { - throw new IgniteException("Failed to remove value from cache [cacheName=" + cache.name() + - ", key=" + key + ']', e); - } + cache.remove(key); } /** {@inheritDoc} */ @Override public void clear() { - try { - ignite.compute(cache.gridProjection()).broadcast(new ClearClosure(cache)); - } - catch (IgniteException e) { - throw new IgniteException("Failed to clear cache [cacheName=" + cache.name() + ']', e); - } - } - - /** - * Closure that removes all entries from cache. - */ - private static class ClearClosure extends CAX implements Externalizable { - /** */ - private static final long serialVersionUID = 0L; - - /** Cache projection. */ - private CacheProjection<Object, Object> cache; - - /** - * For {@link Externalizable}. - */ - public ClearClosure() { - // No-op. - } - - /** - * @param cache Cache projection. - */ - private ClearClosure(CacheProjection<Object, Object> cache) { - this.cache = cache; - } - - /** {@inheritDoc} */ - @Override public void applyx() throws IgniteCheckedException { - cache.localRemoveAll(); - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeObject(cache); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - cache = (CacheProjection<Object, Object>)in.readObject(); - } + cache.removeAll(); } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be25aa36/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringCacheManager.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringCacheManager.java b/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringCacheManager.java index b8e21ff..2d6c5ae 100644 --- a/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringCacheManager.java +++ b/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringCacheManager.java @@ -19,14 +19,12 @@ package org.apache.ignite.cache.spring; import org.apache.ignite.*; import org.apache.ignite.configuration.*; -import org.apache.ignite.internal.*; -import org.apache.ignite.internal.processors.cache.*; -import org.apache.ignite.internal.util.typedef.*; -import org.apache.ignite.lang.*; +import org.jdk8.backport.*; import org.springframework.beans.factory.*; -import org.springframework.cache.CacheManager; +import org.springframework.cache.*; import java.util.*; +import java.util.concurrent.*; /** * Implementation of Spring cache abstraction based on Ignite cache. @@ -124,13 +122,16 @@ import java.util.*; * will try to use default Grid instance (the one with the {@code null} * name). If it doesn't exist, exception will be thrown. * <h1>Starting Remote Nodes</h1> - * Remember that the node started inside your application is an entry point + * Keep in mind that the node started inside your application is an entry point * to the whole topology it connects to. You can start as many remote standalone * nodes as you need using {@code bin/ignite.{sh|bat}} scripts provided in * Ignite distribution, and all these nodes will participate - * in caching data. + * in caching the data. */ public class SpringCacheManager implements CacheManager, InitializingBean { + /** Caches map. */ + private final ConcurrentMap<String, SpringCache> caches = new ConcurrentHashMap8<>(); + /** Grid configuration file path. */ private String cfgPath; @@ -140,8 +141,14 @@ public class SpringCacheManager implements CacheManager, InitializingBean { /** Grid name. */ private String gridName; + /** Dynamic cache configuration template. */ + private CacheConfiguration<Object, Object> dynamicCacheCfg; + + /** Dynamic near cache configuration template. */ + private NearCacheConfiguration<Object, Object> dynamicNearCacheCfg; + /** Ignite instance. */ - protected Ignite grid; + private Ignite ignite; /** * Gets configuration file path. @@ -197,10 +204,45 @@ public class SpringCacheManager implements CacheManager, InitializingBean { this.gridName = gridName; } + /** + * Gets dynamic cache configuration template. + * + * @return Dynamic cache configuration template. + */ + public CacheConfiguration<Object, Object> getDynamicCacheConfiguration() { + return dynamicCacheCfg; + } + + /** + * Sets dynamic cache configuration template. + * + * @param dynamicCacheCfg Dynamic cache configuration template. + */ + public void setDynamicCacheConfiguration(CacheConfiguration<Object, Object> dynamicCacheCfg) { + this.dynamicCacheCfg = dynamicCacheCfg; + } + + /** + * Gets dynamic near cache configuration template. + * + * @return Dynamic near cache configuration template. + */ + public NearCacheConfiguration<Object, Object> getDynamicNearCacheConfiguration() { + return dynamicNearCacheCfg; + } + + /** + * Sets dynamic cache configuration template. + * + * @param dynamicNearCacheCfg Dynamic cache configuration template. + */ + public void setDynamicNearCacheConfiguration(NearCacheConfiguration<Object, Object> dynamicNearCacheCfg) { + this.dynamicNearCacheCfg = dynamicNearCacheCfg; + } + /** {@inheritDoc} */ - @SuppressWarnings("IfMayBeConditional") @Override public void afterPropertiesSet() throws Exception { - assert grid == null; + assert ignite == null; if (cfgPath != null && cfg != null) { throw new IllegalArgumentException("Both 'configurationPath' and 'configuration' are " + @@ -210,33 +252,44 @@ public class SpringCacheManager implements CacheManager, InitializingBean { } if (cfgPath != null) - grid = Ignition.start(cfgPath); + ignite = Ignition.start(cfgPath); else if (cfg != null) - grid = Ignition.start(cfg); + ignite = Ignition.start(cfg); else - grid = Ignition.ignite(gridName); + ignite = Ignition.ignite(gridName); } /** {@inheritDoc} */ @Override public org.springframework.cache.Cache getCache(String name) { - assert grid != null; + assert ignite != null; - try { - return new SpringCache(name, grid, ((IgniteKernal)grid).cache(name), null); - } - catch (IllegalArgumentException ignored) { - return null; + SpringCache cache = caches.get(name); + + if (cache == null) { + CacheConfiguration<Object, Object> cacheCfg = dynamicCacheCfg != null ? + new CacheConfiguration<>(dynamicCacheCfg) : new CacheConfiguration<>(); + + NearCacheConfiguration<Object, Object> nearCacheCfg = dynamicNearCacheCfg != null ? + new NearCacheConfiguration<>(dynamicNearCacheCfg) : null; + + cacheCfg.setName(name); + + // TODO: IGNITE-541 - Provide near cfg. + cache = new SpringCache(ignite.getOrCreateCache(cacheCfg)); + + SpringCache old = caches.putIfAbsent(name, cache); + + if (old != null) + cache = old; } + + return cache; } /** {@inheritDoc} */ @Override public Collection<String> getCacheNames() { - assert grid != null; + assert ignite != null; - return F.viewReadOnly(((IgniteKernal)grid).caches(), new IgniteClosure<IgniteCache<?,?>, String>() { - @Override public String apply(IgniteCache<?, ?> c) { - return c.getName(); - } - }); + return new ArrayList<>(caches.keySet()); } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be25aa36/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringDynamicCacheManager.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringDynamicCacheManager.java b/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringDynamicCacheManager.java deleted file mode 100644 index dd03b42..0000000 --- a/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringDynamicCacheManager.java +++ /dev/null @@ -1,339 +0,0 @@ -/* - * 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.spring; - -import org.apache.ignite.*; -import org.apache.ignite.cache.*; -import org.apache.ignite.internal.*; -import org.apache.ignite.internal.processors.cache.*; -import org.apache.ignite.internal.util.tostring.*; -import org.apache.ignite.internal.util.typedef.*; -import org.apache.ignite.internal.util.typedef.internal.*; -import org.apache.ignite.lang.*; - -import java.io.*; -import java.util.*; - -/** - * Extension of {@link SpringCacheManager} that adds an option to - * emulate dynamic cache creation for you Spring-based applications. - * <p> - * All the data will be actually cached in one Ignite cache. It's - * name should be provided to this cache manager via - * {@link #setDataCacheName(String)} configuration property. - * <p> - * Under the hood, this cache manager will create a cache projection - * for each cache name provided in {@ignitelink org.springframework.cache.annotation.Cacheable}, - * {@ignitelink org.springframework.cache.annotation.CachePut}, - * etc. annotations. Note that you're still able to use caches configured in - * Ignite configuration. Cache projection will be created only - * cache with provided name doesn't exist. - * <h1 class="header">Configuration</h1> - * {@link SpringDynamicCacheManager} inherits all configuration - * properties from {@link SpringCacheManager} (see it's JavaDoc - * for more information on how to enable Ignite-based caching in - * a Spring application). - * <p> - * Additionally you will need to set a Ignite cache name where the data for - * all dynamically created caches will be stored. By default its name - * is {@code null}, which refers to default cache. Here is the example - * of how to configure a named cache: - * <pre name="code" class="xml"> - * <beans xmlns="http://www.springframework.org/schema/beans" - * xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - * xmlns:cache="http://www.springframework.org/schema/cache" - * xsi:schemaLocation=" - * http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd - * http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> - * <-- Provide configuration file path --> - * <bean id="cacheManager" class="org.apache.ignite.cache.spring.GridSpringCacheManager"> - * <property name="dataCacheName" value="myDataCache"/> - * </bean> - * - * ... - * </beans> - * </pre> - * - * @see SpringCacheManager - */ -public class SpringDynamicCacheManager extends SpringCacheManager { - /** Data cache name. */ - private String dataCacheName; - - /** Meta cache. */ - private GridCacheProjectionEx<MetaKey, org.springframework.cache.Cache> metaCache; - - /** Data cache. */ - private GridCache<DataKey, Object> dataCache; - - /** - * Sets data cache name. - * - * @return Data cache name. - */ - public String getDataCacheName() { - return dataCacheName; - } - - /** - * Gets data cache name. - * - * @param dataCacheName Data cache name. - */ - public void setDataCacheName(String dataCacheName) { - this.dataCacheName = dataCacheName; - } - - /** {@inheritDoc} */ - @Override public void afterPropertiesSet() throws Exception { - super.afterPropertiesSet(); - - metaCache = ((IgniteEx)grid).utilityCache(MetaKey.class, org.springframework.cache.Cache.class); - dataCache = ((IgniteKernal)grid).cache(dataCacheName); - } - - /** {@inheritDoc} */ - @Override public org.springframework.cache.Cache getCache(final String name) { - org.springframework.cache.Cache cache = super.getCache(name); - - if (cache != null) - return cache; - - try { - MetaKey key = new MetaKey(name); - - cache = metaCache.get(key); - - if (cache == null) { - cache = new SpringCache(name, - grid, - dataCache.projection(new CacheEntrySerializablePredicate(new ProjectionFilter(name))), - new DataKeyFactory(name)); - - org.springframework.cache.Cache old = metaCache.putIfAbsent(key, cache); - - if (old != null) - cache = old; - } - - return cache; - } - catch (IgniteCheckedException e) { - throw new IgniteException(e); - } - } - - /** {@inheritDoc} */ - @Override public Collection<String> getCacheNames() { - Collection<String> names = F.view(super.getCacheNames(), new IgnitePredicate<String>() { - @Override public boolean apply(String name) { - return !F.eq(name, dataCacheName); - } - }); - - return F.concat( - false, - names, - F.transform( - metaCache.entrySetx(), - new IgniteClosure<javax.cache.Cache.Entry<MetaKey, org.springframework.cache.Cache>, String>() { - @Override public String apply(javax.cache.Cache.Entry<MetaKey, org.springframework.cache.Cache> e) { - return e.getKey().name; - } - })); - } - - /** - * - */ - private static class DataKeyFactory implements IgniteClosure<Object, Object> { - /** */ - private String name; - - /** - * @param name Name. - */ - public DataKeyFactory(String name) { - this.name = name; - } - - /** {@inheritDoc} */ - @Override public Object apply(Object o) { - return new DataKey(name, o); - } - } - - /** - * Meta key. - */ - private static class MetaKey extends GridCacheUtilityKey<MetaKey> implements Externalizable { - /** Cache name. */ - private String name; - - /** - * For {@link Externalizable}. - */ - public MetaKey() { - // No-op. - } - - /** - * @param name Cache name. - */ - private MetaKey(String name) { - this.name = name; - } - - /** {@inheritDoc} */ - @Override protected boolean equalsx(MetaKey key) { - return name != null ? name.equals(key.name) : key.name == null; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - return name.hashCode(); - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - U.writeString(out, name); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - name = U.readString(in); - } - - /** {@inheritDoc} */ - @Override public String toString() { - return S.toString(MetaKey.class, this); - } - } - - /** - * Data key. - */ - private static class DataKey implements Externalizable { - /** Cache name. */ - private String name; - - /** Key. */ - @GridToStringInclude - private Object key; - - /** - * @param name Cache name. - * @param key Key. - */ - private DataKey(String name, Object key) { - this.name = name; - this.key = key; - } - - /** - * For {@link Externalizable}. - */ - public DataKey() { - // No-op. - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (o == null || getClass() != o.getClass()) - return false; - - DataKey key0 = (DataKey)o; - - return name != null ? name.equals(key0.name) : key0.name == null && - key != null ? key.equals(key0.key) : key0.key == null; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = name != null ? name.hashCode() : 0; - - res = 31 * res + (key != null ? key.hashCode() : 0); - - return res; - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - U.writeString(out, name); - out.writeObject(key); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - name = U.readString(in); - key = in.readObject(); - } - - /** {@inheritDoc} */ - @Override public String toString() { - return S.toString(DataKey.class, this); - } - } - - /** - * Projection filter. - */ - private static class ProjectionFilter extends CacheEntryPredicateAdapter implements Externalizable { - /** Cache name. */ - private String name; - - /** - * For {@link Externalizable}. - */ - public ProjectionFilter() { - // No-op. - } - - /** - * @param name Cache name. - */ - private ProjectionFilter(String name) { - this.name = name; - } - - /** {@inheritDoc} */ - @Override public boolean apply(GridCacheEntryEx e) { - DataKey key = e.key().value(e.context().cacheObjectContext(), false); - - return name != null ? name.equals(key.name) : key.name == null; - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - U.writeString(out, name); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - name = U.readString(in); - } - - /** {@inheritDoc} */ - @Override public String toString() { - return S.toString(ProjectionFilter.class, this); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be25aa36/modules/spring/src/test/java/org/apache/ignite/spring/GridSpringCacheManagerSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/test/java/org/apache/ignite/spring/GridSpringCacheManagerSelfTest.java b/modules/spring/src/test/java/org/apache/ignite/spring/GridSpringCacheManagerSelfTest.java index 926f575c..3e2f544 100644 --- a/modules/spring/src/test/java/org/apache/ignite/spring/GridSpringCacheManagerSelfTest.java +++ b/modules/spring/src/test/java/org/apache/ignite/spring/GridSpringCacheManagerSelfTest.java @@ -17,8 +17,7 @@ package org.apache.ignite.spring; -import org.apache.ignite.IgniteCache; -import org.apache.ignite.cache.*; +import org.apache.ignite.*; import org.apache.ignite.configuration.*; import org.apache.ignite.spi.discovery.tcp.*; import org.apache.ignite.spi.discovery.tcp.ipfinder.*; @@ -38,8 +37,14 @@ public class GridSpringCacheManagerSelfTest extends GridCommonAbstractTest { private static final String CACHE_NAME = "testCache"; /** */ + private static final String DYNAMIC_CACHE_NAME = "dynamicCache"; + + /** */ private GridSpringCacheTestService svc; + /** */ + private GridSpringDynamicCacheTestService dynamicSvc; + /** {@inheritDoc} */ @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { IgniteConfiguration cfg = super.getConfiguration(gridName); @@ -79,13 +84,17 @@ public class GridSpringCacheManagerSelfTest extends GridCommonAbstractTest { BeanFactory factory = new ClassPathXmlApplicationContext("org/apache/ignite/spring/spring-caching.xml"); svc = (GridSpringCacheTestService)factory.getBean("testService"); + dynamicSvc = (GridSpringDynamicCacheTestService)factory.getBean("dynamicTestService"); svc.reset(); + dynamicSvc.reset(); } /** {@inheritDoc} */ @Override protected void afterTest() throws Exception { grid().jcache(CACHE_NAME).removeAll(); + + grid().destroyCache(DYNAMIC_CACHE_NAME); } /** @@ -235,4 +244,98 @@ public class GridSpringCacheManagerSelfTest extends GridCommonAbstractTest { assertEquals(0, c.size()); } + + /** + * @throws Exception If failed. + */ + public void testDynamicCache() throws Exception { + for (int i = 0; i < 3; i++) { + assertEquals("value" + i, dynamicSvc.cacheable(i)); + assertEquals("value" + i, dynamicSvc.cacheable(i)); + } + + assertEquals(3, dynamicSvc.called()); + + IgniteCache<Integer, String> c = grid().jcache(DYNAMIC_CACHE_NAME); + + // Check that correct config is used. + assertEquals(2, c.getConfiguration(CacheConfiguration.class).getBackups()); + + assertEquals(3, c.size()); + + for (int i = 0; i < 3; i++) + assertEquals("value" + i, c.get(i)); + } + + /** + * @throws Exception If failed. + */ + public void testDynamicCachePut() throws Exception { + for (int i = 0; i < 3; i++) { + assertEquals("value" + i, dynamicSvc.cachePut(i)); + assertEquals("value" + i, dynamicSvc.cachePut(i)); + } + + assertEquals(6, dynamicSvc.called()); + + IgniteCache<Integer, String> c = grid().jcache(DYNAMIC_CACHE_NAME); + + // Check that correct config is used. + assertEquals(2, c.getConfiguration(CacheConfiguration.class).getBackups()); + + assertEquals(3, c.size()); + + for (int i = 0; i < 3; i++) + assertEquals("value" + i, c.get(i)); + } + + /** + * @throws Exception If failed. + */ + public void testDynamicCacheEvict() throws Exception { + CacheConfiguration<Integer, String> cacheCfg = new CacheConfiguration<>(); + + cacheCfg.setName(DYNAMIC_CACHE_NAME); + + IgniteCache<Integer, String> c = grid().createCache(cacheCfg); + + for (int i = 0; i < 3; i++) + c.put(i, "value" + i); + + assertEquals(3, c.size()); + + for (int i = 0; i < 2; i++) { + dynamicSvc.cacheEvict(i); + dynamicSvc.cacheEvict(i); + } + + assertEquals(4, dynamicSvc.called()); + + assertEquals(1, c.size()); + + assertEquals("value2", c.get(2)); + } + + /** + * @throws Exception If failed. + */ + public void testDynamicCacheEvictAll() throws Exception { + CacheConfiguration<Integer, String> cacheCfg = new CacheConfiguration<>(); + + cacheCfg.setName(DYNAMIC_CACHE_NAME); + + IgniteCache<Integer, String> c = grid().createCache(cacheCfg); + + for (int i = 0; i < 3; i++) + c.put(i, "value" + i); + + assertEquals(3, c.size()); + + dynamicSvc.cacheEvictAll(); + dynamicSvc.cacheEvictAll(); + + assertEquals(2, dynamicSvc.called()); + + assertEquals(0, c.size()); + } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be25aa36/modules/spring/src/test/java/org/apache/ignite/spring/GridSpringDynamicCacheManagerSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/test/java/org/apache/ignite/spring/GridSpringDynamicCacheManagerSelfTest.java b/modules/spring/src/test/java/org/apache/ignite/spring/GridSpringDynamicCacheManagerSelfTest.java deleted file mode 100644 index dd3bf27..0000000 --- a/modules/spring/src/test/java/org/apache/ignite/spring/GridSpringDynamicCacheManagerSelfTest.java +++ /dev/null @@ -1,219 +0,0 @@ -/* - * 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.spring; - -import org.apache.ignite.IgniteCache; -import org.apache.ignite.cache.*; -import org.apache.ignite.cache.spring.*; -import org.apache.ignite.configuration.*; -import org.apache.ignite.spi.discovery.tcp.*; -import org.apache.ignite.spi.discovery.tcp.ipfinder.*; -import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.*; -import org.apache.ignite.testframework.junits.common.*; -import org.springframework.beans.factory.*; -import org.springframework.context.support.*; - -import java.lang.reflect.*; -import java.util.*; - -/** - * Spring cache test. - */ -public class GridSpringDynamicCacheManagerSelfTest extends GridCommonAbstractTest { - /** */ - private static final TcpDiscoveryIpFinder IP_FINDER = new TcpDiscoveryVmIpFinder(true); - - /** */ - private static final String DATA_CACHE_NAME = "data"; - - /** */ - private GridSpringDynamicCacheTestService svc; - - /** */ - private org.springframework.cache.CacheManager mgr; - - /** {@inheritDoc} */ - @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { - IgniteConfiguration cfg = super.getConfiguration(gridName); - - CacheConfiguration cache = new CacheConfiguration(); - - cache.setName(DATA_CACHE_NAME); - - cfg.setCacheConfiguration(cache); - - TcpDiscoverySpi disco = new TcpDiscoverySpi(); - - disco.setIpFinder(IP_FINDER); - - cfg.setDiscoverySpi(disco); - - return cfg; - } - - /** {@inheritDoc} */ - @Override public String getTestGridName() { - return "testGrid"; - } - - /** {@inheritDoc} */ - @Override protected void beforeTestsStarted() throws Exception { - startGrid(); - } - - /** {@inheritDoc} */ - @Override protected void afterTestsStopped() throws Exception { - stopAllGrids(); - } - - /** {@inheritDoc} */ - @Override protected void beforeTest() throws Exception { - BeanFactory factory = new ClassPathXmlApplicationContext( - "org/apache/ignite/spring/spring-dynamic-caching.xml"); - - svc = (GridSpringDynamicCacheTestService)factory.getBean("testService"); - mgr = (org.springframework.cache.CacheManager)factory.getBean("cacheManager"); - } - - /** {@inheritDoc} */ - @Override protected void afterTest() throws Exception { - grid().jcache(DATA_CACHE_NAME).removeAll(); - } - - /** - * @throws Exception If failed. - */ - public void testNames() throws Exception { - assertEquals("value1", svc.cacheable(1)); - - Collection<String> names = mgr.getCacheNames(); - - assertEquals(names.toString(), 2, names.size()); - } - - /** - * @throws Exception If failed. - */ - public void testCacheAndEvict() throws Exception { - IgniteCache<Object, String> c = grid().jcache(DATA_CACHE_NAME); - - assertEquals("value1", svc.cacheable(1)); - - assertEquals(2, c.size()); - - assertEquals("value1", c.get(key("testCache1", 1))); - assertEquals("value1", c.get(key("testCache2", 1))); - - svc.cacheEvict(1); - - assertEquals(1, c.size()); - - assertEquals(null, c.get(key("testCache1", 1))); - assertEquals("value1", c.get(key("testCache2", 1))); - } - - /** - * @throws Exception If failed. - */ - public void testPutAndEvict() throws Exception { - IgniteCache<Object, String> c = grid().jcache(DATA_CACHE_NAME); - - assertEquals("value1", svc.cachePut(1)); - - assertEquals(2, c.size()); - - assertEquals("value1", c.get(key("testCache1", 1))); - assertEquals("value1", c.get(key("testCache2", 1))); - - svc.cacheEvict(1); - - assertEquals(1, c.size()); - - assertEquals(null, c.get(key("testCache1", 1))); - assertEquals("value1", c.get(key("testCache2", 1))); - } - - /** - * @throws Exception If failed. - */ - public void testCacheAndEvictAll() throws Exception { - IgniteCache<Object, String> c = grid().jcache(DATA_CACHE_NAME); - - assertEquals("value1", svc.cacheable(1)); - assertEquals("value2", svc.cacheable(2)); - - assertEquals(4, c.size()); - - assertEquals("value1", c.get(key("testCache1", 1))); - assertEquals("value1", c.get(key("testCache2", 1))); - assertEquals("value2", c.get(key("testCache1", 2))); - assertEquals("value2", c.get(key("testCache2", 2))); - - svc.cacheEvictAll(); - - assertEquals(2, c.size()); - - assertEquals(null, c.get(key("testCache1", 1))); - assertEquals("value1", c.get(key("testCache2", 1))); - assertEquals(null, c.get(key("testCache1", 2))); - assertEquals("value2", c.get(key("testCache2", 2))); - } - - - /** - * @throws Exception If failed. - */ - public void testPutAndEvictAll() throws Exception { - IgniteCache<Object, String> c = grid().jcache(DATA_CACHE_NAME); - - assertEquals("value1", svc.cachePut(1)); - assertEquals("value2", svc.cachePut(2)); - - assertEquals(4, c.size()); - - assertEquals("value1", c.get(key("testCache1", 1))); - assertEquals("value1", c.get(key("testCache2", 1))); - assertEquals("value2", c.get(key("testCache1", 2))); - assertEquals("value2", c.get(key("testCache2", 2))); - - svc.cacheEvictAll(); - - assertEquals(2, c.size()); - - assertEquals(null, c.get(key("testCache1", 1))); - assertEquals("value1", c.get(key("testCache2", 1))); - assertEquals(null, c.get(key("testCache1", 2))); - assertEquals("value2", c.get(key("testCache2", 2))); - } - - /** - * @param cacheName Cache name. - * @param key Key. - * @return Data key. - * @throws Exception In case of error. - */ - private Object key(String cacheName, int key) throws Exception { - Class<?> cls = Class.forName(SpringDynamicCacheManager.class.getName() + "$DataKey"); - - Constructor<?> cons = cls.getDeclaredConstructor(String.class, Object.class); - - cons.setAccessible(true); - - return cons.newInstance(cacheName, key); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be25aa36/modules/spring/src/test/java/org/apache/ignite/spring/GridSpringDynamicCacheTestService.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/test/java/org/apache/ignite/spring/GridSpringDynamicCacheTestService.java b/modules/spring/src/test/java/org/apache/ignite/spring/GridSpringDynamicCacheTestService.java index c6043c6..985c546 100644 --- a/modules/spring/src/test/java/org/apache/ignite/spring/GridSpringDynamicCacheTestService.java +++ b/modules/spring/src/test/java/org/apache/ignite/spring/GridSpringDynamicCacheTestService.java @@ -19,18 +19,25 @@ package org.apache.ignite.spring; import org.springframework.cache.annotation.*; +import java.util.concurrent.atomic.*; + /** * Test service. */ public class GridSpringDynamicCacheTestService { + /** */ + private final AtomicInteger cnt = new AtomicInteger(); + /** * @param key Key. * @return Value. */ - @Cacheable({"testCache1", "testCache2"}) + @Cacheable("dynamicCache") public String cacheable(Integer key) { assert key != null; + cnt.incrementAndGet(); + return "value" + key; } @@ -38,25 +45,40 @@ public class GridSpringDynamicCacheTestService { * @param key Key. * @return Value. */ - @CachePut({"testCache1", "testCache2"}) + @CachePut("dynamicCache") public String cachePut(Integer key) { assert key != null; + cnt.incrementAndGet(); + return "value" + key; } /** * @param key Key. */ - @CacheEvict("testCache1") + @CacheEvict("dynamicCache") public void cacheEvict(Integer key) { - // No-op. + cnt.incrementAndGet(); } /** */ - @CacheEvict(value = "testCache1", allEntries = true) + @CacheEvict(value = "dynamicCache", allEntries = true) public void cacheEvictAll() { - // No-op. + cnt.incrementAndGet(); + } + + /** + * @return Calls count. + */ + public int called() { + return cnt.get(); + } + + /** + */ + public void reset() { + cnt.set(0); } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be25aa36/modules/spring/src/test/java/org/apache/ignite/spring/spring-caching.xml ---------------------------------------------------------------------- diff --git a/modules/spring/src/test/java/org/apache/ignite/spring/spring-caching.xml b/modules/spring/src/test/java/org/apache/ignite/spring/spring-caching.xml index 96f3848..784cf01 100644 --- a/modules/spring/src/test/java/org/apache/ignite/spring/spring-caching.xml +++ b/modules/spring/src/test/java/org/apache/ignite/spring/spring-caching.xml @@ -29,10 +29,20 @@ <bean id="testService" class="org.apache.ignite.spring.GridSpringCacheTestService"/> <!-- + Test service with cacheable methods (dynamic cache). + --> + <bean id="dynamicTestService" class="org.apache.ignite.spring.GridSpringDynamicCacheTestService"/> + + <!-- Cache manager. --> <bean id="cacheManager" class="org.apache.ignite.cache.spring.SpringCacheManager"> <property name="gridName" value="testGrid"/> + <property name="dynamicCacheConfiguration"> + <bean class="org.apache.ignite.configuration.CacheConfiguration"> + <property name="backups" value="2"/> + </bean> + </property> </bean> <!-- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be25aa36/modules/spring/src/test/java/org/apache/ignite/spring/spring-dynamic-caching.xml ---------------------------------------------------------------------- diff --git a/modules/spring/src/test/java/org/apache/ignite/spring/spring-dynamic-caching.xml b/modules/spring/src/test/java/org/apache/ignite/spring/spring-dynamic-caching.xml deleted file mode 100644 index d20dfb4..0000000 --- a/modules/spring/src/test/java/org/apache/ignite/spring/spring-dynamic-caching.xml +++ /dev/null @@ -1,43 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> - -<!-- - 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. ---> - -<beans xmlns="http://www.springframework.org/schema/beans" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xmlns:cache="http://www.springframework.org/schema/cache" - xsi:schemaLocation=" - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd - http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> - <!-- - Test service with cacheable methods. - --> - <bean id="testService" class="org.apache.ignite.spring.GridSpringDynamicCacheTestService"/> - - <!-- - Cache manager. - --> - <bean id="cacheManager" class="org.apache.ignite.cache.spring.SpringDynamicCacheManager"> - <property name="gridName" value="testGrid"/> - <property name="dataCacheName" value="data"/> - </bean> - - <!-- - Enable annotation-driver configuration for caching. - --> - <cache:annotation-driven/> -</beans> http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be25aa36/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java b/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java index 117c4bb..212e841 100644 --- a/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java +++ b/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java @@ -42,7 +42,6 @@ public class IgniteSpringTestSuite extends TestSuite { suite.addTest(new TestSuite(GridP2PUserVersionChangeSelfTest.class)); suite.addTest(new TestSuite(GridSpringCacheManagerSelfTest.class)); - suite.addTest(new TestSuite(GridSpringDynamicCacheManagerSelfTest.class)); return suite; }