Return-Path: X-Original-To: apmail-ignite-commits-archive@minotaur.apache.org Delivered-To: apmail-ignite-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 1AFCB17442 for ; Tue, 2 Jun 2015 08:58:10 +0000 (UTC) Received: (qmail 70117 invoked by uid 500); 2 Jun 2015 08:58:00 -0000 Delivered-To: apmail-ignite-commits-archive@ignite.apache.org Received: (qmail 70085 invoked by uid 500); 2 Jun 2015 08:58:00 -0000 Mailing-List: contact commits-help@ignite.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ignite.incubator.apache.org Delivered-To: mailing list commits@ignite.incubator.apache.org Received: (qmail 70076 invoked by uid 99); 2 Jun 2015 08:58:00 -0000 Received: from Unknown (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 02 Jun 2015 08:58:00 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id 2374C1A431B for ; Tue, 2 Jun 2015 08:58:00 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 1.79 X-Spam-Level: * X-Spam-Status: No, score=1.79 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, T_RP_MATCHES_RCVD=-0.01] autolearn=disabled Received: from mx1-us-east.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id 6MMRmsx5BQsG for ; Tue, 2 Jun 2015 08:57:44 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-us-east.apache.org (ASF Mail Server at mx1-us-east.apache.org) with SMTP id 994B847C2B for ; Tue, 2 Jun 2015 08:57:28 +0000 (UTC) Received: (qmail 68360 invoked by uid 99); 2 Jun 2015 08:57:27 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 02 Jun 2015 08:57:27 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 01692DF99C; Tue, 2 Jun 2015 08:57:26 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sboikov@apache.org To: commits@ignite.incubator.apache.org Date: Tue, 02 Jun 2015 08:57:52 -0000 Message-Id: <982caa397d7243fba9b753702146c9f7@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [27/39] incubator-ignite git commit: IGNITE-891 - Cache store improvements 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-876-2 Commit: e6cc139efa4bb33a334521b6ad0e463ff5b390e8 Parents: 9f88b05 Author: Valentin Kulichenko Authored: Sun May 24 23:36:21 2015 -0700 Committer: Valentin Kulichenko 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 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 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 cacheConfiguration(String name) { + CacheConfiguration 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 { + /** */ + 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 { + public Store() { + } + + /** {@inheritDoc} */ + @Override public Integer load(Integer key) throws CacheLoaderException { + return null; + } + + /** {@inheritDoc} */ + @Override public void write(Cache.Entry entry) + throws CacheWriterException { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void delete(Object key) throws CacheWriterException { + // No-op. + } + } +}