You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by vk...@apache.org on 2015/05/25 08:53:59 UTC
[1/3] incubator-ignite git commit: IGNITE-891 - Cache store
improvements
Repository: incubator-ignite
Updated Branches:
refs/heads/ignite-891 ada1b2a7c -> f0b980cb5
IGNITE-891 - Cache store improvements
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/9f88b05c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/9f88b05c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/9f88b05c
Branch: refs/heads/ignite-891
Commit: 9f88b05cc8be60e69d746330b0d6617ff5f424bd
Parents: ada1b2a
Author: Valentin Kulichenko <vk...@gridgain.com>
Authored: Sun May 24 23:35:44 2015 -0700
Committer: Valentin Kulichenko <vk...@gridgain.com>
Committed: Sun May 24 23:35:44 2015 -0700
----------------------------------------------------------------------
...heStoreSessionListenerLifeCycleSelfTest.java | 395 -------------------
1 file changed, 395 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/9f88b05c/modules/core/src/test/java/org/apache/ignite/cache/store/CacheStoreSessionListenerLifeCycleSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/cache/store/CacheStoreSessionListenerLifeCycleSelfTest.java b/modules/core/src/test/java/org/apache/ignite/cache/store/CacheStoreSessionListenerLifeCycleSelfTest.java
deleted file mode 100644
index 814c8a5..0000000
--- a/modules/core/src/test/java/org/apache/ignite/cache/store/CacheStoreSessionListenerLifeCycleSelfTest.java
+++ /dev/null
@@ -1,395 +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.store;
-
-import org.apache.ignite.*;
-import org.apache.ignite.configuration.*;
-import org.apache.ignite.lifecycle.*;
-import org.apache.ignite.resources.*;
-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.apache.ignite.transactions.*;
-
-import javax.cache.*;
-import javax.cache.configuration.*;
-import javax.cache.integration.*;
-import java.util.*;
-import java.util.concurrent.*;
-
-import static org.apache.ignite.cache.CacheAtomicityMode.*;
-
-/**
- * Store session listeners test.
- */
-public class CacheStoreSessionListenerLifecycleSelfTest extends GridCommonAbstractTest {
- /** */
- private static final TcpDiscoveryIpFinder IP_FINDER = new TcpDiscoveryVmIpFinder(true);
-
- /** */
- private static final Queue<String> evts = new ConcurrentLinkedDeque<>();
-
- /** {@inheritDoc} */
- @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception {
- IgniteConfiguration cfg = super.getConfiguration(gridName);
-
- cfg.setCacheStoreSessionListenerFactories(
- new SessionListenerFactory("Shared 1"),
- new SessionListenerFactory("Shared 2")
- );
-
- TcpDiscoverySpi disco = new TcpDiscoverySpi();
-
- disco.setIpFinder(IP_FINDER);
-
- cfg.setDiscoverySpi(disco);
-
- return cfg;
- }
-
- /** {@inheritDoc} */
- @Override protected void beforeTest() throws Exception {
- evts.clear();
- }
-
- /**
- * @throws Exception If failed.
- */
- public void testNoCaches() throws Exception {
- try {
- startGrid();
- }
- finally {
- stopGrid();
- }
-
- assertEqualsCollections(Arrays.asList("Shared 1 START", "Shared 2 START", "Shared 1 STOP", "Shared 2 STOP"),
- evts);
- }
-
- /**
- * @throws Exception If failed.
- */
- public void testNoOverride() throws Exception {
- try {
- Ignite ignite = startGrid();
-
- for (int i = 0; i < 2; i++) {
- CacheConfiguration<Integer, Integer> cacheCfg = cacheConfiguration("cache-" + i);
-
- cacheCfg.setAtomicityMode(TRANSACTIONAL);
-
- ignite.createCache(cacheCfg);
- }
-
- ignite.cache("cache-0").put(1, 1);
- ignite.cache("cache-1").put(1, 1);
-
- try (Transaction tx = ignite.transactions().txStart()) {
- ignite.cache("cache-0").put(2, 2);
- ignite.cache("cache-0").put(3, 3);
- ignite.cache("cache-1").put(2, 2);
- ignite.cache("cache-1").put(3, 3);
-
- tx.commit();
- }
- }
- finally {
- stopGrid();
- }
-
- assertEqualsCollections(Arrays.asList(
- "Shared 1 START",
- "Shared 2 START",
-
- // Put to cache-0.
- "Shared 1 SESSION START cache-0",
- "Shared 2 SESSION START cache-0",
- "Shared 1 SESSION END cache-0",
- "Shared 2 SESSION END cache-0",
-
- // Put to cache-1.
- "Shared 1 SESSION START cache-1",
- "Shared 2 SESSION START cache-1",
- "Shared 1 SESSION END cache-1",
- "Shared 2 SESSION END cache-1",
-
- // Transaction.
- "Shared 1 SESSION START cache-0",
- "Shared 2 SESSION START cache-0",
- "Shared 1 SESSION START cache-1",
- "Shared 2 SESSION START cache-1",
- "Shared 1 SESSION END cache-0",
- "Shared 2 SESSION END cache-0",
- "Shared 1 SESSION END cache-1",
- "Shared 2 SESSION END cache-1",
-
- "Shared 1 STOP",
- "Shared 2 STOP"
- ), evts);
- }
-
- /**
- * @throws Exception If failed.
- */
- public void testPartialOverride() throws Exception {
- try {
- Ignite ignite = startGrid();
-
- for (int i = 0; i < 2; i++) {
- String name = "cache-" + i;
-
- CacheConfiguration cacheCfg = cacheConfiguration(name);
-
- cacheCfg.setAtomicityMode(TRANSACTIONAL);
-
- if (i == 0) {
- cacheCfg.setCacheStoreSessionListenerFactories(
- new SessionListenerFactory(name + " 1"),
- new SessionListenerFactory(name + " 2")
- );
- }
-
- ignite.createCache(cacheCfg);
- }
-
- ignite.cache("cache-0").put(1, 1);
- ignite.cache("cache-1").put(1, 1);
-
- try (Transaction tx = ignite.transactions().txStart()) {
- ignite.cache("cache-0").put(2, 2);
- ignite.cache("cache-0").put(3, 3);
- ignite.cache("cache-1").put(2, 2);
- ignite.cache("cache-1").put(3, 3);
-
- tx.commit();
- }
- }
- finally {
- stopGrid();
- }
-
- assertEqualsCollections(Arrays.asList(
- "Shared 1 START",
- "Shared 2 START",
- "cache-0 1 START",
- "cache-0 2 START",
-
- // Put to cache-0.
- "cache-0 1 SESSION START cache-0",
- "cache-0 2 SESSION START cache-0",
- "cache-0 1 SESSION END cache-0",
- "cache-0 2 SESSION END cache-0",
-
- // Put to cache-1.
- "Shared 1 SESSION START cache-1",
- "Shared 2 SESSION START cache-1",
- "Shared 1 SESSION END cache-1",
- "Shared 2 SESSION END cache-1",
-
- // Transaction.
- "cache-0 1 SESSION START cache-0",
- "cache-0 2 SESSION START cache-0",
- "Shared 1 SESSION START cache-1",
- "Shared 2 SESSION START cache-1",
- "cache-0 1 SESSION END cache-0",
- "cache-0 2 SESSION END cache-0",
- "Shared 1 SESSION END cache-1",
- "Shared 2 SESSION END cache-1",
-
- "cache-0 1 STOP",
- "cache-0 2 STOP",
- "Shared 1 STOP",
- "Shared 2 STOP"
- ), evts);
- }
-
- /**
- * @throws Exception If failed.
- */
- public void testOverride() throws Exception {
- try {
- Ignite ignite = startGrid();
-
- for (int i = 0; i < 2; i++) {
- String name = "cache-" + i;
-
- CacheConfiguration cacheCfg = cacheConfiguration(name);
-
- cacheCfg.setCacheStoreSessionListenerFactories(new SessionListenerFactory(name + " 1"), new SessionListenerFactory(name + " 2"));
-
- ignite.createCache(cacheCfg);
- }
-
- ignite.cache("cache-0").put(1, 1);
- ignite.cache("cache-1").put(1, 1);
-
- try (Transaction tx = ignite.transactions().txStart()) {
- ignite.cache("cache-0").put(2, 2);
- ignite.cache("cache-0").put(3, 3);
- ignite.cache("cache-1").put(2, 2);
- ignite.cache("cache-1").put(3, 3);
-
- tx.commit();
- }
- }
- finally {
- stopGrid();
- }
-
- assertEqualsCollections(Arrays.asList(
- "Shared 1 START",
- "Shared 2 START",
- "cache-0 1 START",
- "cache-0 2 START",
- "cache-1 1 START",
- "cache-1 2 START",
-
- // Put to cache-0.
- "cache-0 1 SESSION START cache-0",
- "cache-0 2 SESSION START cache-0",
- "cache-0 1 SESSION END cache-0",
- "cache-0 2 SESSION END cache-0",
-
- // Put to cache-1.
- "cache-1 1 SESSION START cache-1",
- "cache-1 2 SESSION START cache-1",
- "cache-1 1 SESSION END cache-1",
- "cache-1 2 SESSION END cache-1",
-
- // Transaction.
- "cache-0 1 SESSION START cache-0",
- "cache-0 2 SESSION START cache-0",
- "cache-1 1 SESSION START cache-1",
- "cache-1 2 SESSION START cache-1",
- "cache-0 1 SESSION END cache-0",
- "cache-0 2 SESSION END cache-0",
- "cache-1 1 SESSION END cache-1",
- "cache-1 2 SESSION END cache-1",
-
- "cache-0 1 STOP",
- "cache-0 2 STOP",
- "cache-1 1 STOP",
- "cache-1 2 STOP",
- "Shared 1 STOP",
- "Shared 2 STOP"
- ), evts);
- }
-
- /**
- * @param name Cache name.
- * @return Cache configuration.
- */
- private CacheConfiguration<Integer, Integer> cacheConfiguration(String name) {
- CacheConfiguration<Integer, Integer> cacheCfg = new CacheConfiguration<>(name);
-
- cacheCfg.setAtomicityMode(TRANSACTIONAL);
- cacheCfg.setCacheStoreFactory(FactoryBuilder.factoryOf(Store.class));
- cacheCfg.setWriteThrough(true);
-
- return cacheCfg;
- }
-
- /**
- */
- private static class SessionListener implements CacheStoreSessionListener, LifecycleAware {
- /** */
- private final String name;
-
- /** */
- @IgniteInstanceResource
- private Ignite ignite;
-
- /**
- * @param name Name.
- */
- private SessionListener(String name) {
- this.name = name;
- }
-
- /** {@inheritDoc} */
- @Override public void start() throws IgniteException {
- assertNotNull(ignite);
-
- evts.add(name + " START");
- }
-
- /** {@inheritDoc} */
- @Override public void stop() throws IgniteException {
- assertNotNull(ignite);
-
- evts.add(name + " STOP");
- }
-
- /** {@inheritDoc} */
- @Override public void onSessionStart(CacheStoreSession ses) {
- assertNotNull(ignite);
-
- evts.add(name + " SESSION START " + ses.cacheName());
- }
-
- /** {@inheritDoc} */
- @Override public void onSessionEnd(CacheStoreSession ses, boolean commit) {
- assertNotNull(ignite);
-
- evts.add(name + " SESSION END " + ses.cacheName());
- }
- }
-
- /**
- */
- private static class SessionListenerFactory implements Factory<CacheStoreSessionListener> {
- /** */
- private String name;
-
- /**
- * @param name Name.
- */
- private SessionListenerFactory(String name) {
- this.name = name;
- }
-
- @Override public CacheStoreSessionListener create() {
- return new SessionListener(name);
- }
- }
-
- /**
- */
- public static class Store extends CacheStoreAdapter<Integer, Integer> {
- public Store() {
- }
-
- /** {@inheritDoc} */
- @Override public Integer load(Integer key) throws CacheLoaderException {
- return null;
- }
-
- /** {@inheritDoc} */
- @Override public void write(Cache.Entry<? extends Integer, ? extends Integer> entry)
- throws CacheWriterException {
- // No-op.
- }
-
- /** {@inheritDoc} */
- @Override public void delete(Object key) throws CacheWriterException {
- // No-op.
- }
- }
-}
[3/3] incubator-ignite git commit: IGNITE-891 - Cache store
improvements
Posted by vk...@apache.org.
IGNITE-891 - Cache store improvements
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/f0b980cb
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/f0b980cb
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/f0b980cb
Branch: refs/heads/ignite-891
Commit: f0b980cb500a77617cfe42fe166ada086d7464af
Parents: e6cc139
Author: Valentin Kulichenko <vk...@gridgain.com>
Authored: Sun May 24 23:53:44 2015 -0700
Committer: Valentin Kulichenko <vk...@gridgain.com>
Committed: Sun May 24 23:53:44 2015 -0700
----------------------------------------------------------------------
.../ignite/cache/store/CacheStoreSession.java | 19 ++++++++++++++++++-
.../jdbc/CacheJdbcStoreSessionListener.java | 4 +---
.../store/GridCacheStoreManagerAdapter.java | 11 +++++++----
.../junits/cache/TestCacheSession.java | 6 +++++-
.../cache/TestThreadLocalCacheSession.java | 5 ++---
.../CacheHibernateStoreSessionListener.java | 4 +---
.../spring/CacheSpringStoreSessionListener.java | 4 +---
7 files changed, 35 insertions(+), 18 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/f0b980cb/modules/core/src/main/java/org/apache/ignite/cache/store/CacheStoreSession.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/cache/store/CacheStoreSession.java b/modules/core/src/main/java/org/apache/ignite/cache/store/CacheStoreSession.java
index 980c6df..329e994 100644
--- a/modules/core/src/main/java/org/apache/ignite/cache/store/CacheStoreSession.java
+++ b/modules/core/src/main/java/org/apache/ignite/cache/store/CacheStoreSession.java
@@ -52,8 +52,25 @@ public interface CacheStoreSession {
*/
public boolean isWithinTransaction();
- public void attach(@Nullable Object attachment);
+ /**
+ * Attaches the given object to this session.
+ * <p>
+ * An attached object may later be retrieved via the {@link #attachment()}
+ * method. Invoking this method causes any previous attachment to be
+ * discarded. To attach additional objects use {@link #properties()} map.
+ * <p>
+ * The current attachment may be discarded by attaching {@code null}.
+ *
+ * @param attachment The object to be attached (or {@code null} to discard current attachment).
+ * @return Previously attached object, if any.
+ */
+ @Nullable public <T> T attach(@Nullable Object attachment);
+ /**
+ * Retrieves the current attachment or {@code null} if there is no attachment.
+ *
+ * @return Currently attached object, if any.
+ */
@Nullable public <T> T attachment();
/**
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/f0b980cb/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcStoreSessionListener.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcStoreSessionListener.java b/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcStoreSessionListener.java
index c59e86f..a20e535 100644
--- a/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcStoreSessionListener.java
+++ b/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcStoreSessionListener.java
@@ -121,11 +121,9 @@ public class CacheJdbcStoreSessionListener implements CacheStoreSessionListener,
/** {@inheritDoc} */
@Override public void onSessionEnd(CacheStoreSession ses, boolean commit) {
- Connection conn = ses.attachment();
+ Connection conn = ses.attach(null);
if (conn != null) {
- ses.attach(null);
-
try {
if (commit)
conn.commit();
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/f0b980cb/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/store/GridCacheStoreManagerAdapter.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/store/GridCacheStoreManagerAdapter.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/store/GridCacheStoreManagerAdapter.java
index bc5a0a2..b4a146a 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/store/GridCacheStoreManagerAdapter.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/store/GridCacheStoreManagerAdapter.java
@@ -889,8 +889,12 @@ public abstract class GridCacheStoreManagerAdapter extends GridCacheManagerAdapt
/**
* @param attachment Attachment.
*/
- private void attach(Object attachment) {
+ private Object attach(Object attachment) {
+ Object prev = this.attachment;
+
this.attachment = attachment;
+
+ return prev;
}
/**
@@ -962,11 +966,10 @@ public abstract class GridCacheStoreManagerAdapter extends GridCacheManagerAdapt
}
/** {@inheritDoc} */
- @Override public void attach(@Nullable Object attachment) {
+ @Override public Object attach(@Nullable Object attachment) {
SessionData ses0 = sesHolder.get();
- if (ses0 != null)
- ses0.attach(attachment);
+ return ses0 != null ? ses0.attach(attachment) : null;
}
/** {@inheritDoc} */
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/f0b980cb/modules/core/src/test/java/org/apache/ignite/testframework/junits/cache/TestCacheSession.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/testframework/junits/cache/TestCacheSession.java b/modules/core/src/test/java/org/apache/ignite/testframework/junits/cache/TestCacheSession.java
index bc7bf7d..31cbefa 100644
--- a/modules/core/src/test/java/org/apache/ignite/testframework/junits/cache/TestCacheSession.java
+++ b/modules/core/src/test/java/org/apache/ignite/testframework/junits/cache/TestCacheSession.java
@@ -58,8 +58,12 @@ public class TestCacheSession implements CacheStoreSession {
}
/** {@inheritDoc} */
- @Override public void attach(@Nullable Object attachment) {
+ @Override public Object attach(@Nullable Object attachment) {
+ Object prev = this.attachment;
+
this.attachment = attachment;
+
+ return prev;
}
/** {@inheritDoc} */
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/f0b980cb/modules/core/src/test/java/org/apache/ignite/testframework/junits/cache/TestThreadLocalCacheSession.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/testframework/junits/cache/TestThreadLocalCacheSession.java b/modules/core/src/test/java/org/apache/ignite/testframework/junits/cache/TestThreadLocalCacheSession.java
index 2047600..dc876d3 100644
--- a/modules/core/src/test/java/org/apache/ignite/testframework/junits/cache/TestThreadLocalCacheSession.java
+++ b/modules/core/src/test/java/org/apache/ignite/testframework/junits/cache/TestThreadLocalCacheSession.java
@@ -54,11 +54,10 @@ public class TestThreadLocalCacheSession implements CacheStoreSession {
}
/** {@inheritDoc} */
- @Override public void attach(@Nullable Object attachment) {
+ @Override public Object attach(@Nullable Object attachment) {
TestCacheSession ses = sesHolder.get();
- if (ses != null)
- ses.attach(attachment);
+ return ses != null ? ses.attach(attachment) : null;
}
/** {@inheritDoc} */
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/f0b980cb/modules/hibernate/src/main/java/org/apache/ignite/cache/store/hibernate/CacheHibernateStoreSessionListener.java
----------------------------------------------------------------------
diff --git a/modules/hibernate/src/main/java/org/apache/ignite/cache/store/hibernate/CacheHibernateStoreSessionListener.java b/modules/hibernate/src/main/java/org/apache/ignite/cache/store/hibernate/CacheHibernateStoreSessionListener.java
index 61f7265..cfad322 100644
--- a/modules/hibernate/src/main/java/org/apache/ignite/cache/store/hibernate/CacheHibernateStoreSessionListener.java
+++ b/modules/hibernate/src/main/java/org/apache/ignite/cache/store/hibernate/CacheHibernateStoreSessionListener.java
@@ -190,11 +190,9 @@ public class CacheHibernateStoreSessionListener implements CacheStoreSessionList
/** {@inheritDoc} */
@Override public void onSessionEnd(CacheStoreSession ses, boolean commit) {
- Session hibSes = ses.attachment();
+ Session hibSes = ses.attach(null);
if (hibSes != null) {
- ses.attach(null);
-
try {
Transaction tx = hibSes.getTransaction();
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/f0b980cb/modules/spring/src/main/java/org/apache/ignite/cache/store/spring/CacheSpringStoreSessionListener.java
----------------------------------------------------------------------
diff --git a/modules/spring/src/main/java/org/apache/ignite/cache/store/spring/CacheSpringStoreSessionListener.java b/modules/spring/src/main/java/org/apache/ignite/cache/store/spring/CacheSpringStoreSessionListener.java
index 90431d7..53fc599 100644
--- a/modules/spring/src/main/java/org/apache/ignite/cache/store/spring/CacheSpringStoreSessionListener.java
+++ b/modules/spring/src/main/java/org/apache/ignite/cache/store/spring/CacheSpringStoreSessionListener.java
@@ -169,11 +169,9 @@ public class CacheSpringStoreSessionListener implements CacheStoreSessionListene
/** {@inheritDoc} */
@Override public void onSessionEnd(CacheStoreSession ses, boolean commit) {
if (ses.isWithinTransaction()) {
- TransactionStatus tx = ses.attachment();
+ TransactionStatus tx = ses.attach(null);
if (tx != null) {
- ses.attach(null);
-
try {
if (commit)
txMgr.commit(tx);
[2/3] incubator-ignite git commit: IGNITE-891 - Cache store
improvements
Posted by vk...@apache.org.
IGNITE-891 - Cache store improvements
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/e6cc139e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/e6cc139e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/e6cc139e
Branch: refs/heads/ignite-891
Commit: e6cc139efa4bb33a334521b6ad0e463ff5b390e8
Parents: 9f88b05
Author: Valentin Kulichenko <vk...@gridgain.com>
Authored: Sun May 24 23:36:21 2015 -0700
Committer: Valentin Kulichenko <vk...@gridgain.com>
Committed: Sun May 24 23:36:21 2015 -0700
----------------------------------------------------------------------
...heStoreSessionListenerLifecycleSelfTest.java | 395 +++++++++++++++++++
1 file changed, 395 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e6cc139e/modules/core/src/test/java/org/apache/ignite/cache/store/CacheStoreSessionListenerLifecycleSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/cache/store/CacheStoreSessionListenerLifecycleSelfTest.java b/modules/core/src/test/java/org/apache/ignite/cache/store/CacheStoreSessionListenerLifecycleSelfTest.java
new file mode 100644
index 0000000..814c8a5
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/cache/store/CacheStoreSessionListenerLifecycleSelfTest.java
@@ -0,0 +1,395 @@
+/*
+ * 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.configuration.*;
+import org.apache.ignite.lifecycle.*;
+import org.apache.ignite.resources.*;
+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.apache.ignite.transactions.*;
+
+import javax.cache.*;
+import javax.cache.configuration.*;
+import javax.cache.integration.*;
+import java.util.*;
+import java.util.concurrent.*;
+
+import static org.apache.ignite.cache.CacheAtomicityMode.*;
+
+/**
+ * Store session listeners test.
+ */
+public class CacheStoreSessionListenerLifecycleSelfTest extends GridCommonAbstractTest {
+ /** */
+ private static final TcpDiscoveryIpFinder IP_FINDER = new TcpDiscoveryVmIpFinder(true);
+
+ /** */
+ private static final Queue<String> evts = new ConcurrentLinkedDeque<>();
+
+ /** {@inheritDoc} */
+ @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception {
+ IgniteConfiguration cfg = super.getConfiguration(gridName);
+
+ cfg.setCacheStoreSessionListenerFactories(
+ new SessionListenerFactory("Shared 1"),
+ new SessionListenerFactory("Shared 2")
+ );
+
+ TcpDiscoverySpi disco = new TcpDiscoverySpi();
+
+ disco.setIpFinder(IP_FINDER);
+
+ cfg.setDiscoverySpi(disco);
+
+ return cfg;
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void beforeTest() throws Exception {
+ evts.clear();
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ public void testNoCaches() throws Exception {
+ try {
+ startGrid();
+ }
+ finally {
+ stopGrid();
+ }
+
+ assertEqualsCollections(Arrays.asList("Shared 1 START", "Shared 2 START", "Shared 1 STOP", "Shared 2 STOP"),
+ evts);
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ public void testNoOverride() throws Exception {
+ try {
+ Ignite ignite = startGrid();
+
+ for (int i = 0; i < 2; i++) {
+ CacheConfiguration<Integer, Integer> cacheCfg = cacheConfiguration("cache-" + i);
+
+ cacheCfg.setAtomicityMode(TRANSACTIONAL);
+
+ ignite.createCache(cacheCfg);
+ }
+
+ ignite.cache("cache-0").put(1, 1);
+ ignite.cache("cache-1").put(1, 1);
+
+ try (Transaction tx = ignite.transactions().txStart()) {
+ ignite.cache("cache-0").put(2, 2);
+ ignite.cache("cache-0").put(3, 3);
+ ignite.cache("cache-1").put(2, 2);
+ ignite.cache("cache-1").put(3, 3);
+
+ tx.commit();
+ }
+ }
+ finally {
+ stopGrid();
+ }
+
+ assertEqualsCollections(Arrays.asList(
+ "Shared 1 START",
+ "Shared 2 START",
+
+ // Put to cache-0.
+ "Shared 1 SESSION START cache-0",
+ "Shared 2 SESSION START cache-0",
+ "Shared 1 SESSION END cache-0",
+ "Shared 2 SESSION END cache-0",
+
+ // Put to cache-1.
+ "Shared 1 SESSION START cache-1",
+ "Shared 2 SESSION START cache-1",
+ "Shared 1 SESSION END cache-1",
+ "Shared 2 SESSION END cache-1",
+
+ // Transaction.
+ "Shared 1 SESSION START cache-0",
+ "Shared 2 SESSION START cache-0",
+ "Shared 1 SESSION START cache-1",
+ "Shared 2 SESSION START cache-1",
+ "Shared 1 SESSION END cache-0",
+ "Shared 2 SESSION END cache-0",
+ "Shared 1 SESSION END cache-1",
+ "Shared 2 SESSION END cache-1",
+
+ "Shared 1 STOP",
+ "Shared 2 STOP"
+ ), evts);
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ public void testPartialOverride() throws Exception {
+ try {
+ Ignite ignite = startGrid();
+
+ for (int i = 0; i < 2; i++) {
+ String name = "cache-" + i;
+
+ CacheConfiguration cacheCfg = cacheConfiguration(name);
+
+ cacheCfg.setAtomicityMode(TRANSACTIONAL);
+
+ if (i == 0) {
+ cacheCfg.setCacheStoreSessionListenerFactories(
+ new SessionListenerFactory(name + " 1"),
+ new SessionListenerFactory(name + " 2")
+ );
+ }
+
+ ignite.createCache(cacheCfg);
+ }
+
+ ignite.cache("cache-0").put(1, 1);
+ ignite.cache("cache-1").put(1, 1);
+
+ try (Transaction tx = ignite.transactions().txStart()) {
+ ignite.cache("cache-0").put(2, 2);
+ ignite.cache("cache-0").put(3, 3);
+ ignite.cache("cache-1").put(2, 2);
+ ignite.cache("cache-1").put(3, 3);
+
+ tx.commit();
+ }
+ }
+ finally {
+ stopGrid();
+ }
+
+ assertEqualsCollections(Arrays.asList(
+ "Shared 1 START",
+ "Shared 2 START",
+ "cache-0 1 START",
+ "cache-0 2 START",
+
+ // Put to cache-0.
+ "cache-0 1 SESSION START cache-0",
+ "cache-0 2 SESSION START cache-0",
+ "cache-0 1 SESSION END cache-0",
+ "cache-0 2 SESSION END cache-0",
+
+ // Put to cache-1.
+ "Shared 1 SESSION START cache-1",
+ "Shared 2 SESSION START cache-1",
+ "Shared 1 SESSION END cache-1",
+ "Shared 2 SESSION END cache-1",
+
+ // Transaction.
+ "cache-0 1 SESSION START cache-0",
+ "cache-0 2 SESSION START cache-0",
+ "Shared 1 SESSION START cache-1",
+ "Shared 2 SESSION START cache-1",
+ "cache-0 1 SESSION END cache-0",
+ "cache-0 2 SESSION END cache-0",
+ "Shared 1 SESSION END cache-1",
+ "Shared 2 SESSION END cache-1",
+
+ "cache-0 1 STOP",
+ "cache-0 2 STOP",
+ "Shared 1 STOP",
+ "Shared 2 STOP"
+ ), evts);
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ public void testOverride() throws Exception {
+ try {
+ Ignite ignite = startGrid();
+
+ for (int i = 0; i < 2; i++) {
+ String name = "cache-" + i;
+
+ CacheConfiguration cacheCfg = cacheConfiguration(name);
+
+ cacheCfg.setCacheStoreSessionListenerFactories(new SessionListenerFactory(name + " 1"), new SessionListenerFactory(name + " 2"));
+
+ ignite.createCache(cacheCfg);
+ }
+
+ ignite.cache("cache-0").put(1, 1);
+ ignite.cache("cache-1").put(1, 1);
+
+ try (Transaction tx = ignite.transactions().txStart()) {
+ ignite.cache("cache-0").put(2, 2);
+ ignite.cache("cache-0").put(3, 3);
+ ignite.cache("cache-1").put(2, 2);
+ ignite.cache("cache-1").put(3, 3);
+
+ tx.commit();
+ }
+ }
+ finally {
+ stopGrid();
+ }
+
+ assertEqualsCollections(Arrays.asList(
+ "Shared 1 START",
+ "Shared 2 START",
+ "cache-0 1 START",
+ "cache-0 2 START",
+ "cache-1 1 START",
+ "cache-1 2 START",
+
+ // Put to cache-0.
+ "cache-0 1 SESSION START cache-0",
+ "cache-0 2 SESSION START cache-0",
+ "cache-0 1 SESSION END cache-0",
+ "cache-0 2 SESSION END cache-0",
+
+ // Put to cache-1.
+ "cache-1 1 SESSION START cache-1",
+ "cache-1 2 SESSION START cache-1",
+ "cache-1 1 SESSION END cache-1",
+ "cache-1 2 SESSION END cache-1",
+
+ // Transaction.
+ "cache-0 1 SESSION START cache-0",
+ "cache-0 2 SESSION START cache-0",
+ "cache-1 1 SESSION START cache-1",
+ "cache-1 2 SESSION START cache-1",
+ "cache-0 1 SESSION END cache-0",
+ "cache-0 2 SESSION END cache-0",
+ "cache-1 1 SESSION END cache-1",
+ "cache-1 2 SESSION END cache-1",
+
+ "cache-0 1 STOP",
+ "cache-0 2 STOP",
+ "cache-1 1 STOP",
+ "cache-1 2 STOP",
+ "Shared 1 STOP",
+ "Shared 2 STOP"
+ ), evts);
+ }
+
+ /**
+ * @param name Cache name.
+ * @return Cache configuration.
+ */
+ private CacheConfiguration<Integer, Integer> cacheConfiguration(String name) {
+ CacheConfiguration<Integer, Integer> cacheCfg = new CacheConfiguration<>(name);
+
+ cacheCfg.setAtomicityMode(TRANSACTIONAL);
+ cacheCfg.setCacheStoreFactory(FactoryBuilder.factoryOf(Store.class));
+ cacheCfg.setWriteThrough(true);
+
+ return cacheCfg;
+ }
+
+ /**
+ */
+ private static class SessionListener implements CacheStoreSessionListener, LifecycleAware {
+ /** */
+ private final String name;
+
+ /** */
+ @IgniteInstanceResource
+ private Ignite ignite;
+
+ /**
+ * @param name Name.
+ */
+ private SessionListener(String name) {
+ this.name = name;
+ }
+
+ /** {@inheritDoc} */
+ @Override public void start() throws IgniteException {
+ assertNotNull(ignite);
+
+ evts.add(name + " START");
+ }
+
+ /** {@inheritDoc} */
+ @Override public void stop() throws IgniteException {
+ assertNotNull(ignite);
+
+ evts.add(name + " STOP");
+ }
+
+ /** {@inheritDoc} */
+ @Override public void onSessionStart(CacheStoreSession ses) {
+ assertNotNull(ignite);
+
+ evts.add(name + " SESSION START " + ses.cacheName());
+ }
+
+ /** {@inheritDoc} */
+ @Override public void onSessionEnd(CacheStoreSession ses, boolean commit) {
+ assertNotNull(ignite);
+
+ evts.add(name + " SESSION END " + ses.cacheName());
+ }
+ }
+
+ /**
+ */
+ private static class SessionListenerFactory implements Factory<CacheStoreSessionListener> {
+ /** */
+ private String name;
+
+ /**
+ * @param name Name.
+ */
+ private SessionListenerFactory(String name) {
+ this.name = name;
+ }
+
+ @Override public CacheStoreSessionListener create() {
+ return new SessionListener(name);
+ }
+ }
+
+ /**
+ */
+ public static class Store extends CacheStoreAdapter<Integer, Integer> {
+ public Store() {
+ }
+
+ /** {@inheritDoc} */
+ @Override public Integer load(Integer key) throws CacheLoaderException {
+ return null;
+ }
+
+ /** {@inheritDoc} */
+ @Override public void write(Cache.Entry<? extends Integer, ? extends Integer> entry)
+ throws CacheWriterException {
+ // No-op.
+ }
+
+ /** {@inheritDoc} */
+ @Override public void delete(Object key) throws CacheWriterException {
+ // No-op.
+ }
+ }
+}