Author: manik.surtani(a)jboss.com
Date: 2008-06-26 10:44:44 -0400 (Thu, 26 Jun 2008)
New Revision: 6056
Added:
core/trunk/src/main/java/org/jboss/cache/commands/write/VersionedInvalidateCommand.java
core/trunk/src/test/java/org/jboss/cache/commands/write/VersionedInvalidateCommandTest.java
Modified:
core/trunk/src/main/java/org/jboss/cache/DataContainerImpl.java
core/trunk/src/main/java/org/jboss/cache/config/Configuration.java
core/trunk/src/main/java/org/jboss/cache/eviction/BaseEvictionAlgorithm.java
core/trunk/src/main/java/org/jboss/cache/factories/CommandsFactory.java
core/trunk/src/main/java/org/jboss/cache/factories/InterceptorChainFactory.java
core/trunk/src/main/java/org/jboss/cache/interceptors/CacheLoaderInterceptor.java
core/trunk/src/main/java/org/jboss/cache/interceptors/CacheStoreInterceptor.java
core/trunk/src/main/java/org/jboss/cache/interceptors/CallInterceptor.java
core/trunk/src/main/java/org/jboss/cache/interceptors/InvalidationInterceptor.java
core/trunk/src/main/java/org/jboss/cache/lock/LockStrategyFactory.java
core/trunk/src/main/java/org/jboss/cache/statetransfer/DefaultStateTransferIntegrator.java
core/trunk/src/test/java/org/jboss/cache/config/ConfigurationTransformerTest.java
Log:
Removed use of deprecated Configuration.isNodeLockingOptimistic
Modified: core/trunk/src/main/java/org/jboss/cache/DataContainerImpl.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/DataContainerImpl.java 2008-06-26 14:18:49
UTC (rev 6055)
+++ core/trunk/src/main/java/org/jboss/cache/DataContainerImpl.java 2008-06-26 14:44:44
UTC (rev 6056)
@@ -165,7 +165,7 @@
NodeSPI toReturn = peek(fqn, false, includeInvalidNodes);
- if (toReturn != null && version != null &&
configuration.isNodeLockingOptimistic())
+ if (toReturn != null && version != null &&
configuration.getNodeLockingScheme().isVersionedScheme())
{
// we need to check the version of the data node...
DataVersion nodeVersion = toReturn.getVersion();
Copied:
core/trunk/src/main/java/org/jboss/cache/commands/write/VersionedInvalidateCommand.java
(from rev 6055,
core/trunk/src/main/java/org/jboss/cache/commands/write/OptimisticInvalidateCommand.java)
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/commands/write/VersionedInvalidateCommand.java
(rev 0)
+++
core/trunk/src/main/java/org/jboss/cache/commands/write/VersionedInvalidateCommand.java 2008-06-26
14:44:44 UTC (rev 6056)
@@ -0,0 +1,188 @@
+package org.jboss.cache.commands.write;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.jboss.cache.CacheException;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.NodeSPI;
+import org.jboss.cache.commands.VersionedDataCommand;
+import org.jboss.cache.config.Option;
+import org.jboss.cache.invocation.InvocationContext;
+import org.jboss.cache.optimistic.DataVersion;
+import org.jboss.cache.transaction.GlobalTransaction;
+
+import javax.transaction.Transaction;
+import javax.transaction.TransactionManager;
+import java.util.Collections;
+
+/**
+ * Behaves like {@link org.jboss.cache.commands.write.InvalidateCommand}. Also,
potentially throws a cache exception if
+ * data versioning is used and the node in memory has a newer data version than what is
passed in.
+ * <p/>
+ * Finally, the data version of the in-memory node is updated to the version being
evicted to prevent versions
+ * going out of sync.
+ * <p/>
+ *
+ * @author Mircea.Markus(a)jboss.com
+ * @since 2.2
+ */
+public class VersionedInvalidateCommand extends InvalidateCommand implements
VersionedDataCommand
+{
+ private static final Log log = LogFactory.getLog(VersionedInvalidateCommand.class);
+ private static boolean trace = log.isTraceEnabled();
+
+ /*
+ dependencies
+ */
+ private TransactionManager transactionManager;
+
+ /**
+ * Params.
+ */
+ protected GlobalTransaction globalTransaction;
+ private DataVersion dataVersion;
+
+ public VersionedInvalidateCommand(Fqn fqn)
+ {
+ super(fqn);
+ }
+
+ public VersionedInvalidateCommand()
+ {
+ }
+
+ public void initialize(TransactionManager txManager)
+ {
+ this.transactionManager = txManager;
+ }
+
+ @Override
+ public Object perform(InvocationContext ctx)
+ {
+ NodeSPI node = enforceNodeLoading();
+ if (trace) log.trace("Invalidating fqn:" + fqn);
+ if (node == null)
+ {
+ // check if a tombstone already exists
+ NodeSPI nodeSPI = dataContainer.peek(fqn, false, true);
+ if (nodeSPI == null)
+ {
+ if (dataVersion == null)
+ {
+ if (trace)
+ log.trace("Would have created a tombstone since the node
doesn't exist, but the version to invalidate is null and hence cannot create a
tombstone!");
+ return null;
+ }
+ createTombstone(ctx);
+ nodeSPI = (NodeSPI) dataContainer.getRoot().getChild(fqn);
+ }
+ node = nodeSPI;
+ }
+ removeData(ctx);
+ invalidateNode(node);
+ updateDataVersion();
+ return null;
+ }
+
+ protected void createTombstone(InvocationContext ctx)
+ {
+ if (trace)
+ log.trace("Node doesn't exist; creating a tombstone with data version
" + dataVersion);
+ // create the node we need.
+ Option o = ctx.getOptionOverrides();
+ boolean origCacheModeLocal = o.isCacheModeLocal();
+ o.setCacheModeLocal(true);
+ o.setDataVersion(dataVersion);
+ // if we are in a tx this call should happen outside of any tx
+ try
+ {
+ Transaction suspended = null;
+ if (transactionManager != null)
+ {
+ suspended = transactionManager.suspend();
+ }
+ spi.put(fqn, Collections.emptyMap());
+ if (suspended != null) transactionManager.resume(suspended);
+ ctx.getOptionOverrides().setCacheModeLocal(origCacheModeLocal);
+ }
+ catch (Exception e)
+ {
+ log.error("Unable to create tombstone!", e);
+ }
+ }
+
+ private void updateDataVersion()
+ {
+ if (dataVersion != null)
+ {
+ NodeSPI n = dataContainer.peek(fqn, false, true);
+ n.setVersion(dataVersion);
+ }
+ }
+
+ protected void removeData(InvocationContext ctx) throws CacheException
+ {
+ NodeSPI n = dataContainer.peekVersioned(fqn, dataVersion);
+ if (n == null)
+ {
+ log.warn("node " + fqn + " not found");
+ return;
+ }
+ notifier.notifyNodeEvicted(fqn, true, ctx);
+ n.clearDataDirect();
+ n.setDataLoaded(false);
+ notifier.notifyNodeEvicted(fqn, false, ctx);
+ }
+
+ public DataVersion getDataVersion()
+ {
+ return dataVersion;
+ }
+
+ public void setDataVersion(DataVersion dataVersion)
+ {
+ this.dataVersion = dataVersion;
+ }
+
+ public GlobalTransaction getGlobalTransaction()
+ {
+ return globalTransaction;
+ }
+
+ public void setGlobalTransaction(GlobalTransaction gtx)
+ {
+ this.globalTransaction = gtx;
+ }
+
+ public boolean isVersioned()
+ {
+ return dataVersion != null;
+ }
+
+ @Override
+ public String toString()
+ {
+ return "OptimisticInvalidateCommand{" +
+ "dataVersion=" + dataVersion +
+ " ,fqn=" + fqn +
+ '}';
+ }
+
+ @Override
+ public Object[] getParameters()
+ {
+ return new Object[]{fqn, dataVersion};
+ }
+
+ @Override
+ public void setParameters(int commandId, Object[] args)
+ {
+ fqn = (Fqn) args[0];
+ dataVersion = (DataVersion) args[1];
+ }
+
+ public void rollback()
+ {
+ //no op
+ }
+}
Modified: core/trunk/src/main/java/org/jboss/cache/config/Configuration.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/config/Configuration.java 2008-06-26 14:18:49
UTC (rev 6055)
+++ core/trunk/src/main/java/org/jboss/cache/config/Configuration.java 2008-06-26 14:44:44
UTC (rev 6056)
@@ -148,7 +148,15 @@
*
* @see <a
href="http://en.wikipedia.org/wiki/Optimistic_concurrency_control&qu...
*/
- OPTIMISTIC
+ OPTIMISTIC;
+
+ /**
+ * @return true if the node locking scheme uses versioning.
+ */
+ public boolean isVersionedScheme()
+ {
+ return this == MVCC || this == OPTIMISTIC;
+ }
}
/**
@@ -541,6 +549,12 @@
return this.shutdownHookBehavior;
}
+ /**
+ * This helper method is deprecated and will be removed when optimistic and
pessimistic locking support is dropped.
+ *
+ * @return true if node locking scheme is optimistic.
+ * @deprecated use {@link #getNodeLockingScheme()} to determine node locking scheme
used.
+ */
@Deprecated
public boolean isNodeLockingOptimistic()
{
Modified: core/trunk/src/main/java/org/jboss/cache/eviction/BaseEvictionAlgorithm.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/eviction/BaseEvictionAlgorithm.java 2008-06-26
14:18:49 UTC (rev 6055)
+++
core/trunk/src/main/java/org/jboss/cache/eviction/BaseEvictionAlgorithm.java 2008-06-26
14:44:44 UTC (rev 6056)
@@ -82,7 +82,7 @@
// hacky temp solution till we have an ioc fwk to inject configuration elements as
needed
Configuration c = region.getCacheConfiguration();
Configuration.CacheMode cm = c != null ? c.getCacheMode() :
Configuration.CacheMode.LOCAL;
- allowTombstones = c != null && c.isNodeLockingOptimistic() &&
+ allowTombstones = c != null && c.getNodeLockingScheme().isVersionedScheme()
&&
(cm == Configuration.CacheMode.INVALIDATION_ASYNC || cm ==
Configuration.CacheMode.INVALIDATION_SYNC);
}
Modified: core/trunk/src/main/java/org/jboss/cache/factories/CommandsFactory.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/factories/CommandsFactory.java 2008-06-26
14:18:49 UTC (rev 6055)
+++ core/trunk/src/main/java/org/jboss/cache/factories/CommandsFactory.java 2008-06-26
14:44:44 UTC (rev 6056)
@@ -177,9 +177,9 @@
public InvalidateCommand buildInvalidateCommand(Fqn fqn)
{
- if (configuration.isNodeLockingOptimistic())
+ if (configuration.getNodeLockingScheme().isVersionedScheme())
{
- OptimisticInvalidateCommand command = new OptimisticInvalidateCommand(fqn);
+ VersionedInvalidateCommand command = new VersionedInvalidateCommand(fqn);
command.initialize(txManager);
command.initialize(cacheSpi, dataContainer, notifier);
return command;
@@ -450,9 +450,9 @@
case InvalidateCommand.METHOD_ID:
{
- if (configuration.isNodeLockingOptimistic())
+ if (configuration.getNodeLockingScheme().isVersionedScheme())
{
- OptimisticInvalidateCommand returnValue = new
OptimisticInvalidateCommand();
+ VersionedInvalidateCommand returnValue = new
VersionedInvalidateCommand();
returnValue.initialize(txManager);
returnValue.initialize(cacheSpi, dataContainer, notifier);
command = returnValue;
Modified: core/trunk/src/main/java/org/jboss/cache/factories/InterceptorChainFactory.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/factories/InterceptorChainFactory.java 2008-06-26
14:18:49 UTC (rev 6055)
+++
core/trunk/src/main/java/org/jboss/cache/factories/InterceptorChainFactory.java 2008-06-26
14:44:44 UTC (rev 6056)
@@ -52,7 +52,7 @@
public InterceptorChain buildInterceptorChain() throws IllegalAccessException,
InstantiationException, ClassNotFoundException
{
- boolean optimistic = configuration.isNodeLockingOptimistic();
+ boolean optimistic = configuration.getNodeLockingScheme() ==
NodeLockingScheme.OPTIMISTIC;
// load the icInterceptor first
CommandInterceptor first = createInterceptor(InvocationContextInterceptor.class);
InterceptorChain interceptorChain = new InterceptorChain(first);
Modified:
core/trunk/src/main/java/org/jboss/cache/interceptors/CacheLoaderInterceptor.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/interceptors/CacheLoaderInterceptor.java 2008-06-26
14:18:49 UTC (rev 6055)
+++
core/trunk/src/main/java/org/jboss/cache/interceptors/CacheLoaderInterceptor.java 2008-06-26
14:44:44 UTC (rev 6056)
@@ -20,6 +20,7 @@
import org.jboss.cache.commands.write.RemoveNodeCommand;
import org.jboss.cache.config.Configuration;
import static org.jboss.cache.config.Configuration.CacheMode;
+import org.jboss.cache.config.Configuration.NodeLockingScheme;
import org.jboss.cache.factories.annotations.Inject;
import org.jboss.cache.factories.annotations.Start;
import org.jboss.cache.interceptors.base.CommandInterceptor;
@@ -59,7 +60,7 @@
protected Notifier notifier;
protected boolean isActivation = false;
- protected boolean usingOptimisticInvalidation = false;
+ protected boolean usingVersionedInvalidation = false;
/**
@@ -76,7 +77,7 @@
this.txTable = txTable;
this.clm = clm;
CacheMode mode = configuration.getCacheMode();
- usingOptimisticInvalidation = configuration.isNodeLockingOptimistic() &&
mode.isInvalidation();
+ usingVersionedInvalidation =
configuration.getNodeLockingScheme().isVersionedScheme() &&
mode.isInvalidation();
this.dataContainer = dataContainer;
this.lockManager = lockManager;
this.notifier = notifier;
@@ -144,7 +145,7 @@
{
if (command.getFqn() != null)
{
- loadIfNeeded(ctx, command.getFqn(), null, false, false, true,
ctx.getTransactionContext(), false, false, !usingOptimisticInvalidation);
+ loadIfNeeded(ctx, command.getFqn(), null, false, false, true,
ctx.getTransactionContext(), false, false, !usingVersionedInvalidation);
}
return invokeNextInterceptor(ctx, command);
}
@@ -213,7 +214,7 @@
@Override
public Object visitRemoveNodeCommand(InvocationContext ctx, RemoveNodeCommand command)
throws Throwable
{
- if (configuration.isNodeLockingOptimistic() && command.getFqn() != null)
+ if (configuration.getNodeLockingScheme() == NodeLockingScheme.OPTIMISTIC &&
command.getFqn() != null)
{
loadIfNeeded(ctx, command.getFqn(), null, false, false, false,
ctx.getTransactionContext(), false, false, false);
}
@@ -397,7 +398,7 @@
}
// check this first!!!
- if (!n.isValid() && configuration.isNodeLockingOptimistic())
+ if (!n.isValid() &&
configuration.getNodeLockingScheme().isVersionedScheme())
{
// attempt to load again; this only happens if we have tombstones lying around,
or we are using invalidation.
if (trace) log.trace("loading again from cache loader since in-memory node
is marked as invalid");
@@ -455,7 +456,7 @@
protected void lock(Fqn fqn, LockType lockType, boolean recursive, InvocationContext
ctx) throws Throwable
{
- if (configuration.isNodeLockingOptimistic()) return;
+ if (configuration.getNodeLockingScheme() == NodeLockingScheme.OPTIMISTIC) return;
if (recursive)
lockManager.lockAllAndRecord(fqn, lockType, ctx);
@@ -504,7 +505,7 @@
n.setInternalState(nodeData);
// set this node as valid?
- if (usingOptimisticInvalidation) n.setValid(true, false);
+ if (usingVersionedInvalidation) n.setValid(true, false);
notifier.notifyNodeLoaded(fqn, false, nodeData, ctx);
if (isActivation)
Modified:
core/trunk/src/main/java/org/jboss/cache/interceptors/CacheStoreInterceptor.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/interceptors/CacheStoreInterceptor.java 2008-06-26
14:18:49 UTC (rev 6055)
+++
core/trunk/src/main/java/org/jboss/cache/interceptors/CacheStoreInterceptor.java 2008-06-26
14:44:44 UTC (rev 6056)
@@ -285,7 +285,7 @@
private void storeInternalState(Set<Fqn> affectedFqns) throws Exception
{
- if (configuration.isNodeLockingOptimistic())
+ if (configuration.getNodeLockingScheme().isVersionedScheme())
{
for (Fqn f : affectedFqns)
{
Modified: core/trunk/src/main/java/org/jboss/cache/interceptors/CallInterceptor.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/interceptors/CallInterceptor.java 2008-06-26
14:18:49 UTC (rev 6055)
+++ core/trunk/src/main/java/org/jboss/cache/interceptors/CallInterceptor.java 2008-06-26
14:44:44 UTC (rev 6056)
@@ -14,6 +14,8 @@
import org.jboss.cache.commands.write.PutKeyValueCommand;
import org.jboss.cache.commands.write.RemoveKeyCommand;
import org.jboss.cache.commands.write.RemoveNodeCommand;
+import org.jboss.cache.config.Configuration.NodeLockingScheme;
+import org.jboss.cache.factories.annotations.Start;
import org.jboss.cache.interceptors.base.CommandInterceptor;
import org.jboss.cache.invocation.InvocationContext;
import org.jboss.cache.transaction.GlobalTransaction;
@@ -33,6 +35,14 @@
*/
public class CallInterceptor extends CommandInterceptor
{
+ private boolean notOptimisticLocking;
+
+ @Start
+ protected void start()
+ {
+ notOptimisticLocking = configuration.getNodeLockingScheme() !=
NodeLockingScheme.OPTIMISTIC;
+ }
+
@Override
public Object visitPrepareCommand(InvocationContext ctx, PrepareCommand command)
throws Throwable
{
@@ -140,7 +150,7 @@
throws Throwable
{
Object result = invokeCommand(ctx, command);
- if (ctx.isValidTransaction() && !configuration.isNodeLockingOptimistic())
+ if (notOptimisticLocking && ctx.isValidTransaction())
{
GlobalTransaction gtx = ctx.getGlobalTransaction();
if (gtx == null)
Modified:
core/trunk/src/main/java/org/jboss/cache/interceptors/InvalidationInterceptor.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/interceptors/InvalidationInterceptor.java 2008-06-26
14:18:49 UTC (rev 6055)
+++
core/trunk/src/main/java/org/jboss/cache/interceptors/InvalidationInterceptor.java 2008-06-26
14:44:44 UTC (rev 6056)
@@ -17,12 +17,13 @@
import org.jboss.cache.commands.write.ClearDataCommand;
import org.jboss.cache.commands.write.InvalidateCommand;
import org.jboss.cache.commands.write.MoveCommand;
-import org.jboss.cache.commands.write.OptimisticInvalidateCommand;
import org.jboss.cache.commands.write.PutDataMapCommand;
import org.jboss.cache.commands.write.PutForExternalReadCommand;
import org.jboss.cache.commands.write.PutKeyValueCommand;
import org.jboss.cache.commands.write.RemoveKeyCommand;
import org.jboss.cache.commands.write.RemoveNodeCommand;
+import org.jboss.cache.commands.write.VersionedInvalidateCommand;
+import org.jboss.cache.config.Configuration.NodeLockingScheme;
import org.jboss.cache.config.Option;
import org.jboss.cache.factories.CommandsFactory;
import org.jboss.cache.factories.annotations.Inject;
@@ -75,7 +76,7 @@
@Start
private void initTxMap()
{
- optimistic = configuration.isNodeLockingOptimistic();
+ optimistic = configuration.getNodeLockingScheme() == NodeLockingScheme.OPTIMISTIC;
if (optimistic) txMods = new ConcurrentHashMap<GlobalTransaction,
List<ReversibleCommand>>();
}
@@ -261,7 +262,7 @@
{
try
{
- TransactionWorkspace workspace = configuration.isNodeLockingOptimistic() ?
getWorkspace(ctx) : null;
+ TransactionWorkspace workspace = optimistic ? getWorkspace(ctx) : null;
for (Fqn fqn : filterVisitor.result) invalidateAcrossCluster(fqn,
workspace, defaultSynchronous, ctx);
}
catch (Throwable t)
@@ -376,7 +377,7 @@
incrementInvalidations();
InvalidateCommand command = commandsFactory.buildInvalidateCommand(fqn);
DataVersion dataVersion = getNodeVersion(workspace, fqn);
- if (dataVersion != null) ((OptimisticInvalidateCommand)
command).setDataVersion(dataVersion);
+ if (dataVersion != null) ((VersionedInvalidateCommand)
command).setDataVersion(dataVersion);
if (log.isDebugEnabled()) log.debug("Cache [" +
rpcManager.getLocalAddress() + "] replicating " + command);
// voila, invalidated!
replicateCall(ctx, command, synchronous, ctx.getOptionOverrides());
Modified: core/trunk/src/main/java/org/jboss/cache/lock/LockStrategyFactory.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/lock/LockStrategyFactory.java 2008-06-26
14:18:49 UTC (rev 6055)
+++ core/trunk/src/main/java/org/jboss/cache/lock/LockStrategyFactory.java 2008-06-26
14:44:44 UTC (rev 6056)
@@ -7,6 +7,7 @@
package org.jboss.cache.lock;
import org.jboss.cache.config.Configuration;
+import org.jboss.cache.config.Configuration.NodeLockingScheme;
import org.jboss.cache.factories.annotations.Inject;
import org.jboss.cache.factories.annotations.NonVolatile;
import org.jboss.cache.factories.annotations.Start;
@@ -36,7 +37,7 @@
@Start(priority = 1)
public void start()
{
- lockingLevel = configuration.isNodeLockingOptimistic() ?
IsolationLevel.REPEATABLE_READ : configuration.getIsolationLevel();
+ lockingLevel = configuration.getNodeLockingScheme() == NodeLockingScheme.OPTIMISTIC
? IsolationLevel.REPEATABLE_READ : configuration.getIsolationLevel();
}
public LockStrategy getLockStrategy()
Modified:
core/trunk/src/main/java/org/jboss/cache/statetransfer/DefaultStateTransferIntegrator.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/statetransfer/DefaultStateTransferIntegrator.java 2008-06-26
14:18:49 UTC (rev 6055)
+++
core/trunk/src/main/java/org/jboss/cache/statetransfer/DefaultStateTransferIntegrator.java 2008-06-26
14:44:44 UTC (rev 6056)
@@ -50,12 +50,12 @@
private Set<Fqn> internalFqns;
- public DefaultStateTransferIntegrator(Fqn targetFqn, CacheSPI cache)
+ public DefaultStateTransferIntegrator(Fqn targetFqn, CacheSPI<?, ?> cache)
{
this.targetFqn = targetFqn;
this.cache = cache;
this.factory = cache.getConfiguration().getRuntimeConfig().getNodeFactory();
- this.nodeType = cache.getConfiguration().isNodeLockingOptimistic()
+ this.nodeType =
cache.getConfiguration().getNodeLockingScheme().isVersionedScheme()
? NodeFactory.NodeType.VERSIONED_NODE
: NodeFactory.NodeType.UNVERSIONED_NODE;
this.internalFqns = cache.getInternalFqns();
Copied:
core/trunk/src/test/java/org/jboss/cache/commands/write/VersionedInvalidateCommandTest.java
(from rev 6055,
core/trunk/src/test/java/org/jboss/cache/commands/write/OptimisticInvalidateCommandTest.java)
===================================================================
---
core/trunk/src/test/java/org/jboss/cache/commands/write/VersionedInvalidateCommandTest.java
(rev 0)
+++
core/trunk/src/test/java/org/jboss/cache/commands/write/VersionedInvalidateCommandTest.java 2008-06-26
14:44:44 UTC (rev 6056)
@@ -0,0 +1,139 @@
+package org.jboss.cache.commands.write;
+
+import static org.easymock.EasyMock.createStrictControl;
+import static org.easymock.EasyMock.expect;
+import org.easymock.IMocksControl;
+import org.jboss.cache.CacheSPI;
+import org.jboss.cache.DataContainer;
+import org.jboss.cache.commands.read.AbstractDataCommandTest;
+import org.jboss.cache.mock.MockNodesFixture;
+import org.jboss.cache.notifications.Notifier;
+import org.jboss.cache.optimistic.DataVersion;
+import org.jboss.cache.optimistic.DefaultDataVersion;
+import org.testng.annotations.Test;
+
+import javax.transaction.Transaction;
+import javax.transaction.TransactionManager;
+import java.util.Collections;
+
+/**
+ * tester class for {@link VersionedInvalidateCommand}.
+ *
+ * @author Mircea.Markus(a)jboss.com
+ * @since 2.2
+ */
+@Test(groups = "unit")
+public class VersionedInvalidateCommandTest extends AbstractDataCommandTest
+{
+ DataVersion dataVersion;
+ VersionedInvalidateCommand command;
+ IMocksControl control;
+
+ Notifier notifier;
+ TransactionManager tmMock;
+
+ MockNodesFixture nodes;
+ CacheSPI spiMock;
+
+ protected void moreSetup()
+ {
+ control = createStrictControl();
+ notifier = control.createMock(Notifier.class);
+ container = control.createMock(DataContainer.class);
+ tmMock = control.createMock(TransactionManager.class);
+ spiMock = control.createMock(CacheSPI.class);
+ nodes = new MockNodesFixture();
+
+ command = new VersionedInvalidateCommand(testFqn);
+ dataVersion = new DefaultDataVersion(10);
+ command.setDataVersion(dataVersion);
+ command.initialize(spiMock, container, notifier);
+ command.initialize(tmMock);
+ }
+
+ public void testWithExistingNode()
+ {
+ nodes.adfNode.put("key", "value");
+ nodes.adfNode.setDataLoaded(true);
+ expect(spiMock.getNode(testFqn)).andReturn(nodes.adfNode);
+ expect(container.peekVersioned(testFqn, dataVersion)).andReturn(nodes.adfNode);
+ notifier.notifyNodeEvicted(testFqn, true, ctx);
+ notifier.notifyNodeEvicted(testFqn, false, ctx);
+ expect(container.peek(testFqn, false, true)).andReturn(nodes.adfNode);
+
+ control.replay();
+ assert null == command.perform(ctx);
+ assert nodes.adfNode.getData().isEmpty();
+ assert !nodes.adfNode.isDataLoaded();
+ assert !nodes.adfNode.isValid();
+ assert nodes.adfNode.getVersion().equals(dataVersion);
+
+ control.verify();
+ }
+
+ public void testWithExistingNodeInvalidVersion()
+ {
+ nodes.adfNode.put("key", "value");
+ nodes.adfNode.setDataLoaded(true);
+ expect(spiMock.getNode(testFqn)).andReturn(nodes.adfNode);
+ expect(container.peekVersioned(testFqn, dataVersion)).andThrow(new
RuntimeException());
+ control.replay();
+
+ try
+ {
+ command.perform(ctx);
+ assert false : "exception expected";
+ }
+ catch (Exception e)
+ {
+ //expected as there is a version mismatch
+ }
+ assert !nodes.adfNode.getData().isEmpty();
+ assert nodes.adfNode.isDataLoaded();
+ assert nodes.adfNode.isValid();
+ assert !dataVersion.equals(nodes.adfNode.getVersion());
+
+ control.verify();
+ }
+
+ public void testExistingTumbstone()
+ {
+ nodes.adfNode.setValid(false, true);
+ expect(spiMock.getNode(testFqn)).andReturn(null);
+ expect(container.peek(testFqn, false, true)).andReturn(nodes.adfNode);
+ expect(container.peekVersioned(testFqn, dataVersion)).andReturn(nodes.adfNode);
+ notifier.notifyNodeEvicted(testFqn, true, ctx);
+ notifier.notifyNodeEvicted(testFqn, false, ctx);
+ expect(container.peek(testFqn, false, true)).andReturn(nodes.adfNode);
+
+ control.replay();
+ assert null == command.perform(ctx);
+ assert nodes.adfNode.getData().isEmpty();
+ assert !nodes.adfNode.isDataLoaded();
+ assert !nodes.adfNode.isValid();
+ assert nodes.adfNode.getVersion().equals(dataVersion);
+ control.verify();
+ }
+
+ public void testCreateTumbstone() throws Exception
+ {
+ Transaction tx = control.createMock(Transaction.class);
+ expect(tmMock.suspend()).andReturn(tx);
+ spiMock.put(testFqn, Collections.emptyMap());
+ tmMock.resume(tx);
+
+ control.replay();
+ command.createTombstone(ctx);
+ control.verify();
+ }
+
+ public void testCreateTumbstoneNoTx() throws Exception
+ {
+ expect(tmMock.suspend()).andReturn(null);
+ spiMock.put(testFqn, Collections.EMPTY_MAP);
+
+ control.replay();
+ command.createTombstone(ctx);
+ control.verify();
+ }
+}
Modified:
core/trunk/src/test/java/org/jboss/cache/config/ConfigurationTransformerTest.java
===================================================================
---
core/trunk/src/test/java/org/jboss/cache/config/ConfigurationTransformerTest.java 2008-06-26
14:18:49 UTC (rev 6055)
+++
core/trunk/src/test/java/org/jboss/cache/config/ConfigurationTransformerTest.java 2008-06-26
14:44:44 UTC (rev 6056)
@@ -1,14 +1,14 @@
package org.jboss.cache.config;
-import org.testng.annotations.Test;
-import org.testng.annotations.BeforeMethod;
import org.jboss.cache.config.parsing.ConfigFilesConvertor;
import org.jboss.cache.config.parsing.XmlConfigurationParser;
import org.jboss.cache.config.parsing.XmlConfigurationParser2x;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
-import java.io.File;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
+import java.io.File;
/**
* Test how xsl for migrating config files from 2.x to 3.x works.
@@ -55,15 +55,15 @@
Configuration oldConfig = oldParser.parseFile(fileName);
assert oldConfig.getNodeLockingScheme().equals(newConfig.getNodeLockingScheme());
- assert oldConfig.isNodeLockingOptimistic() == newConfig.isNodeLockingOptimistic();
+ assert oldConfig.getNodeLockingScheme() == newConfig.getNodeLockingScheme();
assert oldConfig.equals(newConfig);
}
public void testEqualityOnTransformedFiles() throws Exception
{
String[] fileNames = {"buddy-replication-cache-service.xml",
"cacheloader-enabled-cache-service.xml",
- "eviction-enabled-cache-service.xml",
"local-cache-service.xml", "multiplexer-enabled-cache-service.xml",
- "optimistically-locked-cache-service.xml",
"total-replication-cache-service.xml"};
+ "eviction-enabled-cache-service.xml",
"local-cache-service.xml", "multiplexer-enabled-cache-service.xml",
+ "optimistically-locked-cache-service.xml",
"total-replication-cache-service.xml"};
for (String file : fileNames)
{
System.out.println("Processing file = " + file);