IGNITE-45 - Fixing tests, added getOrCreate
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/a88eff96 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/a88eff96 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/a88eff96 Branch: refs/heads/ignite-45 Commit: a88eff96572996c681c08dfe398d59690af0c420 Parents: dfdfc6c Author: Alexey Goncharuk <agoncha...@gridgain.com> Authored: Wed Mar 18 22:47:06 2015 -0700 Committer: Alexey Goncharuk <agoncha...@gridgain.com> Committed: Wed Mar 18 22:47:06 2015 -0700 ---------------------------------------------------------------------- .../src/main/java/org/apache/ignite/Ignite.java | 8 ++++ .../apache/ignite/internal/IgniteKernal.java | 24 ++++++++++ .../processors/cache/GridCacheProcessor.java | 8 ++-- .../cache/IgniteCacheExistsException.java | 48 ++++++++++++++++++++ .../GridCacheDistributedEvictionsSelfTest.java | 34 -------------- .../ignite/testframework/junits/IgniteMock.java | 5 ++ .../org/apache/ignite/IgniteSpringBean.java | 7 +++ 7 files changed, 96 insertions(+), 38 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a88eff96/modules/core/src/main/java/org/apache/ignite/Ignite.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/Ignite.java b/modules/core/src/main/java/org/apache/ignite/Ignite.java index ee68a5f..ce21beb 100644 --- a/modules/core/src/main/java/org/apache/ignite/Ignite.java +++ b/modules/core/src/main/java/org/apache/ignite/Ignite.java @@ -198,6 +198,14 @@ public interface Ignite extends AutoCloseable { public <K, V> IgniteCache<K, V> createCache(CacheConfiguration<K, V> cacheCfg); /** + * Gets existing cache with the given name or creates new one with the given configuration. + * + * @param cacheCfg Cache configuration to use. + * @return Existing or newly created cache. + */ + public <K, V> IgniteCache<K, V> getOrCreateCache(CacheConfiguration<K, V> cacheCfg); + + /** * Dynamically starts new cache with the given cache configuration. * <p> * If local node is an affinity node, this method will return the instance of started cache. http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a88eff96/modules/core/src/main/java/org/apache/ignite/internal/IgniteKernal.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/IgniteKernal.java b/modules/core/src/main/java/org/apache/ignite/internal/IgniteKernal.java index f62c678..db6e535 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/IgniteKernal.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/IgniteKernal.java @@ -2270,6 +2270,30 @@ public class IgniteKernal implements IgniteEx, IgniteMXBean, Externalizable { } /** {@inheritDoc} */ + @Override public <K, V> IgniteCache<K, V> getOrCreateCache(CacheConfiguration<K, V> cacheCfg) { + A.notNull(cacheCfg, "cacheCfg"); + + guard(); + + try { + try { + ctx.cache().dynamicStartCache(cacheCfg, null).get(); + } + catch (IgniteCacheExistsException ignore) { + // Ignore the error if cache already exists. + } + + return ctx.cache().publicJCache(cacheCfg.getName()); + } + catch (IgniteCheckedException e) { + throw CU.convertToCacheException(e); + } + finally { + unguard(); + } + } + + /** {@inheritDoc} */ @Override public <K, V> IgniteCache<K, V> createCache( CacheConfiguration<K, V> cacheCfg, NearCacheConfiguration<K, V> nearCfg http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a88eff96/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java index ab00cd2..46fef6b 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java @@ -1537,7 +1537,7 @@ public class GridCacheProcessor extends GridProcessorAdapter { if (ccfg != null) { if (desc != null) - return new GridFinishedFuture<>(new IgniteCheckedException("Failed to start cache " + + return new GridFinishedFuture<>(new IgniteCacheExistsException("Failed to start cache " + "(a cache with the same name is already started): " + cacheName)); req.deploymentId(IgniteUuid.randomUuid()); @@ -1565,7 +1565,7 @@ public class GridCacheProcessor extends GridProcessorAdapter { ccfg = desc.cacheConfiguration(); if (ccfg == null) - return new GridFinishedFuture<>(new IgniteCheckedException("Failed to start near cache " + + return new GridFinishedFuture<>(new IgniteCacheExistsException("Failed to start near cache " + "(a cache with the given name is not started): " + nearCfg.getName())); if (ccfg.getNodeFilter().apply(ctx.discovery().localNode())) @@ -1628,7 +1628,7 @@ public class GridCacheProcessor extends GridProcessorAdapter { if (old != null) { if (req.isStart() && !req.clientStartOnly()) { - fut.onDone(new IgniteCheckedException("Failed to start cache " + + fut.onDone(new IgniteCacheExistsException("Failed to start cache " + "(a cache with the same name is already being started or stopped): " + req.cacheName())); } else { @@ -1676,7 +1676,7 @@ public class GridCacheProcessor extends GridProcessorAdapter { if (!req.clientStartOnly()) { // If local node initiated start, fail the start future. if (startFut != null && startFut.deploymentId().equals(req.deploymentId())) { - startFut.onDone(new IgniteCheckedException("Failed to start cache " + + startFut.onDone(new IgniteCacheExistsException("Failed to start cache " + "(a cache with the same name is already started): " + ccfg.getName())); } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a88eff96/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheExistsException.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheExistsException.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheExistsException.java new file mode 100644 index 0000000..f905ae7 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheExistsException.java @@ -0,0 +1,48 @@ +/* + * 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.jetbrains.annotations.*; + +/** + * Exception thrown when cache already exists. + */ +public class IgniteCacheExistsException extends IgniteCheckedException { + /** + * @param msg Error message. + */ + public IgniteCacheExistsException(String msg) { + super(msg); + } + + /** + * @param cause Error cause. + */ + public IgniteCacheExistsException(Throwable cause) { + super(cause); + } + + /** + * @param msg Error message. + * @param cause Error cause. + */ + public IgniteCacheExistsException(String msg, @Nullable Throwable cause) { + super(msg, cause); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a88eff96/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheDistributedEvictionsSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheDistributedEvictionsSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheDistributedEvictionsSelfTest.java index 591a6c8..1b09f96 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheDistributedEvictionsSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheDistributedEvictionsSelfTest.java @@ -57,9 +57,6 @@ public class GridCacheDistributedEvictionsSelfTest extends GridCommonAbstractTes private boolean evictSync; /** */ - private boolean evictNearSync; - - /** */ private final AtomicInteger idxGen = new AtomicInteger(); /** {@inheritDoc} */ @@ -116,52 +113,21 @@ public class GridCacheDistributedEvictionsSelfTest extends GridCommonAbstractTes } /** @throws Throwable If failed. */ - public void testNearSyncBackupUnsync() throws Throwable { - gridCnt = 3; - mode = PARTITIONED; - evictNearSync = true; - evictSync = false; - nearEnabled = true; - - checkEvictions(); - } - - /** @throws Throwable If failed. */ public void testNearSyncBackupSync() throws Throwable { gridCnt = 3; mode = PARTITIONED; - evictNearSync = true; evictSync = true; nearEnabled = true; checkEvictions(); } - /** @throws Throwable If failed. */ - public void testNearUnsyncBackupSync() throws Throwable { - gridCnt = 1; - mode = PARTITIONED; - evictNearSync = false; - evictSync = true; - nearEnabled = true; - - try { - startGrid(0); - - assert false : "Grid was started with illegal configuration."; - } - catch (IgniteCheckedException e) { - info("Caught expected exception: " + e); - } - } - /** * @throws Throwable If failed. */ public void testLocalSync() throws Throwable { gridCnt = 1; mode = LOCAL; - evictNearSync = true; evictSync = true; nearEnabled = true; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a88eff96/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteMock.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteMock.java b/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteMock.java index ada8123..947af96 100644 --- a/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteMock.java +++ b/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteMock.java @@ -171,6 +171,11 @@ public class IgniteMock implements Ignite { } /** {@inheritDoc} */ + @Override public <K, V> IgniteCache<K, V> getOrCreateCache(CacheConfiguration<K, V> cacheCfg) { + return null; + } + + /** {@inheritDoc} */ @Override public <K, V> IgniteCache<K, V> createCache(CacheConfiguration<K, V> cacheCfg, @Nullable NearCacheConfiguration<K, V> nearCfg) { return null; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a88eff96/modules/spring/src/main/java/org/apache/ignite/IgniteSpringBean.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/main/java/org/apache/ignite/IgniteSpringBean.java b/modules/spring/src/main/java/org/apache/ignite/IgniteSpringBean.java index 53e36de..b21ef9b 100644 --- a/modules/spring/src/main/java/org/apache/ignite/IgniteSpringBean.java +++ b/modules/spring/src/main/java/org/apache/ignite/IgniteSpringBean.java @@ -247,6 +247,13 @@ public class IgniteSpringBean implements Ignite, DisposableBean, InitializingBea } /** {@inheritDoc} */ + @Override public <K, V> IgniteCache<K, V> getOrCreateCache(CacheConfiguration<K, V> cacheCfg) { + assert g != null; + + return g.getOrCreateCache(cacheCfg); + } + + /** {@inheritDoc} */ @Override public <K, V> IgniteCache<K, V> createCache(CacheConfiguration<K, V> cacheCfg, NearCacheConfiguration<K, V> nearCfg) { assert g != null;