/* * JBoss, Home of Professional Open Source * Copyright 2011 Red Hat Inc. and/or its affiliates and other * contributors as indicated by the @author tags. All rights reserved. * See the copyright.txt in the distribution for a full listing of * individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.infinispan.lock.singlelock; import org.infinispan.commands.tx.CommitCommand; import org.infinispan.commands.tx.PrepareCommand; import org.infinispan.config.Configuration; import org.infinispan.context.impl.TxInvocationContext; import org.infinispan.interceptors.base.CommandInterceptor; import org.infinispan.test.MultipleCacheManagersTest; import org.infinispan.transaction.lookup.DummyTransactionManagerLookup; import org.infinispan.transaction.tm.DummyTransaction; import javax.transaction.Status; import java.util.concurrent.CountDownLatch; import static org.testng.Assert.assertEquals; /** * @author Mircea Markus * @since 5.1 */ public class AbstractCrashTest extends MultipleCacheManagersTest { private DummyTransaction transaction; @Override protected void createCacheManagers() throws Throwable { final Configuration c = getDefaultClusteredConfig(Configuration.CacheMode.DIST_SYNC); c.fluent().transaction().transactionManagerLookup(new DummyTransactionManagerLookup()); c.fluent().transaction().useSynchronization(true);//no recovery c.fluent().hash().rehashEnabled(false); c.fluent().hash().numOwners(3); createCluster(c, 3); waitForClusterToForm(); } protected Object beginAndPrepareTx(final Object k, final int cacheIndex) { fork(new Runnable() { @Override public void run() { try { tm(1).begin(); cache(1).put(k,"v"); final DummyTransaction transaction = (DummyTransaction) tm(cacheIndex).getTransaction(); transaction.notifyBeforeCompletion(); transaction.runPrepare(); } catch (Throwable e) { e.printStackTrace(); } } }, false); return k; } protected Object beginAndCommitTx(final Object k, final int cacheIndex) { fork(new Runnable() { @Override public void run() { try { tm(cacheIndex).begin(); cache(cacheIndex).put(k,"v"); tm(cacheIndex).commit(); } catch (Throwable e) { e.printStackTrace(); } } }, false); return k; } public static class TxControlInterceptor extends CommandInterceptor { public CountDownLatch prepareProgress = new CountDownLatch(1); public CountDownLatch preparedReceived = new CountDownLatch(1); public CountDownLatch commitReceived = new CountDownLatch(1); public CountDownLatch commitProgress = new CountDownLatch(1); @Override public Object visitPrepareCommand(TxInvocationContext ctx, PrepareCommand command) throws Throwable { final Object result = super.visitPrepareCommand(ctx, command); preparedReceived.countDown(); prepareProgress.await(); return result; } @Override public Object visitCommitCommand(TxInvocationContext ctx, CommitCommand command) throws Throwable { commitReceived.countDown(); commitProgress.await(); return super.visitCommitCommand(ctx, command); } } //todo create JIRA for this use case public void testLockOwnerCrashesDuringPrepare() throws Exception { TxControlInterceptor c0ControlInterceptor = new TxControlInterceptor(); TxControlInterceptor c2ControlInterceptor = new TxControlInterceptor(); advancedCache(0).addInterceptor(c0ControlInterceptor, 1); advancedCache(2).addInterceptor(c2ControlInterceptor, 1); final Object k = getKeyForCache(2); fork(new Runnable() { @Override public void run() { try { tm(1).begin(); cache(1).put(k, "v"); transaction = (DummyTransaction) tm(1).getTransaction(); transaction.notifyBeforeCompletion(); transaction.runPrepare(); } catch (Throwable e) { e.printStackTrace(); } } }, false); c0ControlInterceptor.preparedReceived.await(); c2ControlInterceptor.preparedReceived.await(); killMember(2); cacheManagers.remove(2); assert caches().size() == 2; c0ControlInterceptor.prepareProgress.countDown(); Thread.sleep(1000); log.trace("here is the end"); tm(1).resume(transaction); transaction.commit(); transaction.notifyAfterCompletion(Status.STATUS_COMMITTED); assertEquals(cache(0).get(k), "v"); assertEquals(cache(1).get(k), "v"); assertNotLocked(k); eventually(new Condition() { @Override public boolean isSatisfied() throws Exception { return checkTxCount(0, 0, 0) && checkTxCount(1, 0, 0); } }); } }