JBoss Cache SVN: r6480 - in core/trunk/src: main/java/org/jboss/cache/eviction and 1 other directories.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2008-08-01 09:12:46 -0400 (Fri, 01 Aug 2008)
New Revision: 6480
Added:
core/trunk/src/test/java/org/jboss/cache/eviction/DisabledEvictionThreadTest.java
Modified:
core/trunk/src/main/java/org/jboss/cache/RegionImpl.java
core/trunk/src/main/java/org/jboss/cache/eviction/EvictionTimerTask.java
Log:
JBCACHE-1268 - allow disabling of eviction thread
Modified: core/trunk/src/main/java/org/jboss/cache/RegionImpl.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/RegionImpl.java 2008-08-01 11:03:23 UTC (rev 6479)
+++ core/trunk/src/main/java/org/jboss/cache/RegionImpl.java 2008-08-01 13:12:46 UTC (rev 6480)
@@ -203,7 +203,7 @@
}
catch (InterruptedException e)
{
- log.debug("trace", e);
+ Thread.currentThread().interrupt();
}
return null;
}
Modified: core/trunk/src/main/java/org/jboss/cache/eviction/EvictionTimerTask.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/eviction/EvictionTimerTask.java 2008-08-01 11:03:23 UTC (rev 6479)
+++ core/trunk/src/main/java/org/jboss/cache/eviction/EvictionTimerTask.java 2008-08-01 13:12:46 UTC (rev 6480)
@@ -10,12 +10,13 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.Region;
+import org.jboss.cache.util.concurrent.ConcurrentHashSet;
-import java.util.Collections;
-import java.util.HashSet;
import java.util.Set;
-import java.util.Timer;
-import java.util.TimerTask;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
@@ -32,13 +33,13 @@
private final Set<Region> processedRegions;
private static AtomicInteger tcount = new AtomicInteger();
private long wakeupInterval;
- private Timer evictionThread;
+ ScheduledExecutorService scheduledExecutor;
public EvictionTimerTask()
{
// synchronized set because we need to maintain thread safety
// for dynamic configuration purposes.
- processedRegions = Collections.synchronizedSet(new HashSet<Region>());
+ processedRegions = new ConcurrentHashSet<Region>();
}
public void init(long wakeupInterval)
@@ -81,32 +82,40 @@
public void stop()
{
- log.debug("Stopping eviction timer");
+ if (log.isDebugEnabled()) log.debug("Stopping eviction timer");
- if (evictionThread != null)
+ if (scheduledExecutor != null)
{
- evictionThread.cancel();
+ scheduledExecutor.shutdownNow();
}
- evictionThread = null;
+ scheduledExecutor = null;
}
private void start()
{
- evictionThread = new Timer("EvictionTimer-" + tcount.getAndIncrement(), true);
- TimerTask tt = new TimerTask()
+ if (wakeupInterval < 1)
{
- /**
- * Run the eviction thread.
- * <p/>
- * This thread will synchronize the set of regions and iterate through every MarshRegion registered w/ the
- * Eviction thread. It also synchronizes on each individual region as it is being processed.
- */
+ if (log.isInfoEnabled())
+ log.info("Wakeup Interval set to " + wakeupInterval + ". Not starting an eviction thread!");
+ return;
+ }
+ scheduledExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory()
+ {
+ public Thread newThread(Runnable r)
+ {
+ return new Thread(r, "EvictionTimer-" + tcount.getAndIncrement());
+ }
+ });
+ scheduledExecutor.scheduleWithFixedDelay(new Runnable()
+ {
public void run()
{
+ // Run the eviction thread.
+ // This thread will synchronize the set of regions and iterate through every MarshRegion registered w/ the
+ // Eviction thread. It also synchronizes on each individual region as it is being processed.
processRegions();
}
- };
- evictionThread.schedule(tt, wakeupInterval, wakeupInterval);
+ }, wakeupInterval, wakeupInterval, TimeUnit.MILLISECONDS);
}
private void processRegions()
Added: core/trunk/src/test/java/org/jboss/cache/eviction/DisabledEvictionThreadTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/eviction/DisabledEvictionThreadTest.java (rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/eviction/DisabledEvictionThreadTest.java 2008-08-01 13:12:46 UTC (rev 6480)
@@ -0,0 +1,53 @@
+package org.jboss.cache.eviction;
+
+import org.jboss.cache.Cache;
+import org.jboss.cache.DefaultCacheFactory;
+import org.jboss.cache.RegionManager;
+import org.jboss.cache.config.Configuration;
+import org.jboss.cache.config.Configuration.CacheMode;
+import org.jboss.cache.factories.ComponentRegistry;
+import org.jboss.cache.factories.UnitTestCacheConfigurationFactory;
+import org.jboss.cache.util.TestingUtil;
+import org.testng.annotations.Test;
+
+@Test(groups = "functional")
+public class DisabledEvictionThreadTest
+{
+ public void testDisabledEvictionTimer()
+ {
+ Cache<String, String> c = null;
+ try
+ {
+ Configuration cfg = UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.LOCAL, true);
+ cfg.getEvictionConfig().setWakeupInterval(0);
+ c = new DefaultCacheFactory<String, String>().createCache(cfg);
+ ComponentRegistry cr = TestingUtil.extractComponentRegistry(c);
+ RegionManager rm = cr.getComponent(RegionManager.class);
+ EvictionTimerTask ett = rm.getEvictionTimerTask();
+ assert ett.scheduledExecutor == null;
+ }
+ finally
+ {
+ TestingUtil.killCaches(c);
+ }
+ }
+
+ public void testControl()
+ {
+ Cache<String, String> c = null;
+ try
+ {
+ Configuration cfg = UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.LOCAL, true);
+ cfg.getEvictionConfig().setWakeupInterval(10);
+ c = new DefaultCacheFactory<String, String>().createCache(cfg);
+ ComponentRegistry cr = TestingUtil.extractComponentRegistry(c);
+ RegionManager rm = cr.getComponent(RegionManager.class);
+ EvictionTimerTask ett = rm.getEvictionTimerTask();
+ assert ett.scheduledExecutor != null;
+ }
+ finally
+ {
+ TestingUtil.killCaches(c);
+ }
+ }
+}
16 years, 7 months
JBoss Cache SVN: r6479 - in core/trunk/src: main/java/org/jboss/cache/commands/write and 4 other directories.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2008-08-01 07:03:23 -0400 (Fri, 01 Aug 2008)
New Revision: 6479
Added:
core/trunk/src/main/java/org/jboss/cache/notifications/annotation/NodeInvalidated.java
core/trunk/src/main/java/org/jboss/cache/notifications/event/NodeInvalidatedEvent.java
core/trunk/src/test/java/org/jboss/cache/notifications/NotifyNodeInvalidatedTest.java
Modified:
core/trunk/src/main/java/org/jboss/cache/commands/legacy/write/VersionedInvalidateCommand.java
core/trunk/src/main/java/org/jboss/cache/commands/write/InvalidateCommand.java
core/trunk/src/main/java/org/jboss/cache/notifications/Notifier.java
core/trunk/src/main/java/org/jboss/cache/notifications/NotifierImpl.java
core/trunk/src/main/java/org/jboss/cache/notifications/annotation/CacheListener.java
core/trunk/src/main/java/org/jboss/cache/notifications/event/Event.java
core/trunk/src/main/java/org/jboss/cache/notifications/event/EventImpl.java
core/trunk/src/test/java/org/jboss/cache/notifications/EventLog.java
core/trunk/src/test/java/org/jboss/cache/notifications/NotifierAnnotationsTest.java
core/trunk/src/test/java/org/jboss/cache/notifications/NotifierTest.java
core/trunk/src/test/java/org/jboss/cache/notifications/RemoteCacheListenerTest.java
Log:
JBCACHE-86 - a NodeInvalidated callback to listeners
Modified: core/trunk/src/main/java/org/jboss/cache/commands/legacy/write/VersionedInvalidateCommand.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/commands/legacy/write/VersionedInvalidateCommand.java 2008-08-01 09:04:40 UTC (rev 6478)
+++ core/trunk/src/main/java/org/jboss/cache/commands/legacy/write/VersionedInvalidateCommand.java 2008-08-01 11:03:23 UTC (rev 6479)
@@ -129,10 +129,10 @@
protected void removeData(NodeSPI n, InvocationContext ctx) throws CacheException
{
- notifier.notifyNodeEvicted(fqn, true, ctx);
+ notifier.notifyNodeInvalidated(fqn, true, ctx);
n.clearDataDirect();
n.setDataLoaded(false);
- notifier.notifyNodeEvicted(fqn, false, ctx);
+ notifier.notifyNodeInvalidated(fqn, false, ctx);
}
public DataVersion getDataVersion()
Modified: core/trunk/src/main/java/org/jboss/cache/commands/write/InvalidateCommand.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/commands/write/InvalidateCommand.java 2008-08-01 09:04:40 UTC (rev 6478)
+++ core/trunk/src/main/java/org/jboss/cache/commands/write/InvalidateCommand.java 2008-08-01 11:03:23 UTC (rev 6479)
@@ -65,14 +65,14 @@
boolean evictNode(Fqn fqn, InvocationContext ctx)
{
- notifier.notifyNodeEvicted(fqn, true, ctx);
+ notifier.notifyNodeInvalidated(fqn, true, ctx);
try
{
return dataContainer.evict(fqn);
}
finally
{
- notifier.notifyNodeEvicted(fqn, false, ctx);
+ notifier.notifyNodeInvalidated(fqn, false, ctx);
}
}
Modified: core/trunk/src/main/java/org/jboss/cache/notifications/Notifier.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/notifications/Notifier.java 2008-08-01 09:04:40 UTC (rev 6478)
+++ core/trunk/src/main/java/org/jboss/cache/notifications/Notifier.java 2008-08-01 11:03:23 UTC (rev 6479)
@@ -56,6 +56,11 @@
void notifyNodeEvicted(Fqn fqn, boolean pre, InvocationContext ctx);
/**
+ * Notifies all registered listeners of a nodeInvalidated event.
+ */
+ void notifyNodeInvalidated(Fqn fqn, boolean pre, InvocationContext ctx);
+
+ /**
* Notifies all registered listeners of a nodeLoaded event.
*/
void notifyNodeLoaded(Fqn fqn, boolean pre, Map data, InvocationContext ctx);
Modified: core/trunk/src/main/java/org/jboss/cache/notifications/NotifierImpl.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/notifications/NotifierImpl.java 2008-08-01 09:04:40 UTC (rev 6478)
+++ core/trunk/src/main/java/org/jboss/cache/notifications/NotifierImpl.java 2008-08-01 11:03:23 UTC (rev 6479)
@@ -28,11 +28,12 @@
import org.jgroups.View;
import javax.transaction.Transaction;
+import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
-import java.util.Collection;
import java.util.Collections;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
@@ -55,31 +56,35 @@
private static final Class[] allowedMethodAnnotations =
{
CacheStarted.class, CacheStopped.class, CacheBlocked.class, CacheUnblocked.class, NodeCreated.class, NodeRemoved.class, NodeVisited.class, NodeModified.class, NodeMoved.class,
- NodeActivated.class, NodePassivated.class, NodeLoaded.class, NodeEvicted.class, TransactionRegistered.class, TransactionCompleted.class, ViewChanged.class, BuddyGroupChanged.class
+ NodeActivated.class, NodePassivated.class, NodeLoaded.class, NodeEvicted.class, TransactionRegistered.class, TransactionCompleted.class, ViewChanged.class, BuddyGroupChanged.class,
+ NodeInvalidated.class
};
private static final Class[] parameterTypes =
{
CacheStartedEvent.class, CacheStoppedEvent.class, CacheBlockedEvent.class, CacheUnblockedEvent.class, NodeCreatedEvent.class, NodeRemovedEvent.class, NodeVisitedEvent.class, NodeModifiedEvent.class, NodeMovedEvent.class,
- NodeActivatedEvent.class, NodePassivatedEvent.class, NodeLoadedEvent.class, NodeEvictedEvent.class, TransactionRegisteredEvent.class, TransactionCompletedEvent.class, ViewChangedEvent.class, BuddyGroupChangedEvent.class
+ NodeActivatedEvent.class, NodePassivatedEvent.class, NodeLoadedEvent.class, NodeEvictedEvent.class, TransactionRegisteredEvent.class, TransactionCompletedEvent.class, ViewChangedEvent.class, BuddyGroupChangedEvent.class,
+ NodeInvalidatedEvent.class
};
- private final List<ListenerInvocation> cacheStartedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
- private final List<ListenerInvocation> cacheStoppedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
- private final List<ListenerInvocation> cacheBlockedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
- private final List<ListenerInvocation> cacheUnblockedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
- private final List<ListenerInvocation> nodeCreatedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
- private final List<ListenerInvocation> nodeRemovedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
- private final List<ListenerInvocation> nodeVisitedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
- private final List<ListenerInvocation> nodeModifiedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
- private final List<ListenerInvocation> nodeMovedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
- private final List<ListenerInvocation> nodeActivatedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
- private final List<ListenerInvocation> nodePassivatedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
- private final List<ListenerInvocation> nodeLoadedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
- private final List<ListenerInvocation> nodeEvictedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
- private final List<ListenerInvocation> transactionRegisteredListeners = new CopyOnWriteArrayList<ListenerInvocation>();
- private final List<ListenerInvocation> transactionCompletedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
- private final List<ListenerInvocation> viewChangedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
- private final List<ListenerInvocation> buddyGroupChangedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
+ final Map<Class<? extends Annotation>, List<ListenerInvocation>> listenersMap = new HashMap<Class<? extends Annotation>, List<ListenerInvocation>>(32);
+ final List<ListenerInvocation> cacheStartedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
+ final List<ListenerInvocation> cacheStoppedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
+ final List<ListenerInvocation> cacheBlockedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
+ final List<ListenerInvocation> cacheUnblockedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
+ final List<ListenerInvocation> nodeCreatedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
+ final List<ListenerInvocation> nodeRemovedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
+ final List<ListenerInvocation> nodeVisitedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
+ final List<ListenerInvocation> nodeModifiedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
+ final List<ListenerInvocation> nodeMovedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
+ final List<ListenerInvocation> nodeActivatedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
+ final List<ListenerInvocation> nodePassivatedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
+ final List<ListenerInvocation> nodeLoadedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
+ final List<ListenerInvocation> nodeInvalidatedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
+ final List<ListenerInvocation> nodeEvictedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
+ final List<ListenerInvocation> transactionRegisteredListeners = new CopyOnWriteArrayList<ListenerInvocation>();
+ final List<ListenerInvocation> transactionCompletedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
+ final List<ListenerInvocation> viewChangedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
+ final List<ListenerInvocation> buddyGroupChangedListeners = new CopyOnWriteArrayList<ListenerInvocation>();
// final Map<Class, List<ListenerInvocation>> listenerInvocations = new ConcurrentHashMap<Class, List<ListenerInvocation>>();
private Cache cache;
@@ -88,10 +93,29 @@
public NotifierImpl()
{
+ listenersMap.put(CacheStarted.class, cacheStartedListeners);
+ listenersMap.put(CacheStopped.class, cacheStoppedListeners);
+ listenersMap.put(CacheBlocked.class, cacheBlockedListeners);
+ listenersMap.put(CacheUnblocked.class, cacheUnblockedListeners);
+ listenersMap.put(NodeCreated.class, nodeCreatedListeners);
+ listenersMap.put(NodeRemoved.class, nodeRemovedListeners);
+ listenersMap.put(NodeVisited.class, nodeVisitedListeners);
+ listenersMap.put(NodeModified.class, nodeModifiedListeners);
+ listenersMap.put(NodeMoved.class, nodeMovedListeners);
+ listenersMap.put(NodeActivated.class, nodeActivatedListeners);
+ listenersMap.put(NodePassivated.class, nodePassivatedListeners);
+ listenersMap.put(NodeLoaded.class, nodeLoadedListeners);
+ listenersMap.put(NodeEvicted.class, nodeEvictedListeners);
+ listenersMap.put(TransactionRegistered.class, transactionRegisteredListeners);
+ listenersMap.put(TransactionCompleted.class, transactionCompletedListeners);
+ listenersMap.put(ViewChanged.class, viewChangedListeners);
+ listenersMap.put(BuddyGroupChanged.class, buddyGroupChangedListeners);
+ listenersMap.put(NodeInvalidated.class, nodeInvalidatedListeners);
}
public NotifierImpl(Cache cache)
{
+ this();
this.cache = cache;
}
@@ -120,6 +144,7 @@
*
* @param listener object to be considered as a listener.
*/
+ @SuppressWarnings("unchecked")
private void validateAndAddListenerInvocation(Object listener)
{
testListenerClassValidity(listener.getClass());
@@ -162,7 +187,7 @@
private void addListenerInvocation(Class annotation, ListenerInvocation li)
{
- List<ListenerInvocation> result = getListenersForAnnotation(annotation);
+ List<ListenerInvocation> result = getListenerCollectionForAnnotation(annotation);
result.add(li);
}
@@ -179,7 +204,7 @@
private void removeListenerInvocation(Class annotation, Object listener)
{
if (listener == null) return;
- List<ListenerInvocation> l = getListenersForAnnotation(annotation);
+ List<ListenerInvocation> l = getListenerCollectionForAnnotation(annotation);
Set<Object> markedForRemoval = new HashSet<Object>();
for (ListenerInvocation li : l)
{
@@ -216,23 +241,10 @@
public Set<Object> getCacheListeners()
{
Set<Object> result = new HashSet<Object>();
- result.addAll(getListeningObjects(cacheStartedListeners));
- result.addAll(getListeningObjects(cacheStoppedListeners));
- result.addAll(getListeningObjects(cacheBlockedListeners));
- result.addAll(getListeningObjects(cacheUnblockedListeners));
- result.addAll(getListeningObjects(nodeCreatedListeners));
- result.addAll(getListeningObjects(nodeRemovedListeners));
- result.addAll(getListeningObjects(nodeVisitedListeners));
- result.addAll(getListeningObjects(nodeModifiedListeners));
- result.addAll(getListeningObjects(nodeMovedListeners));
- result.addAll(getListeningObjects(nodeActivatedListeners));
- result.addAll(getListeningObjects(nodePassivatedListeners));
- result.addAll(getListeningObjects(nodeLoadedListeners));
- result.addAll(getListeningObjects(nodeEvictedListeners));
- result.addAll(getListeningObjects(transactionRegisteredListeners));
- result.addAll(getListeningObjects(transactionCompletedListeners));
- result.addAll(getListeningObjects(viewChangedListeners));
- result.addAll(getListeningObjects(buddyGroupChangedListeners));
+ for (List<ListenerInvocation> list : listenersMap.values())
+ {
+ for (ListenerInvocation li : list) result.add(li.target);
+ }
return Collections.unmodifiableSet(result);
}
@@ -359,6 +371,25 @@
}
}
+ public void notifyNodeInvalidated(final Fqn fqn, final boolean pre, InvocationContext ctx)
+ {
+ if (!nodeInvalidatedListeners.isEmpty())
+ {
+ final boolean originLocal = ctx.isOriginLocal();
+ Transaction tx = ctx.getTransaction();
+ InvocationContext backup = resetInvocationContext(ctx);
+ EventImpl e = new EventImpl();
+ e.setCache(cache);
+ e.setOriginLocal(originLocal);
+ e.setPre(pre);
+ e.setFqn(fqn);
+ e.setTransaction(tx);
+ e.setType(NODE_INVALIDATED);
+ for (ListenerInvocation listener : nodeInvalidatedListeners) listener.invoke(e);
+ restoreInvocationContext(backup);
+ }
+ }
+
public void notifyNodeLoaded(Fqn fqn, boolean pre, Map data, InvocationContext ctx)
{
if (!nodeLoadedListeners.isEmpty())
@@ -619,174 +650,10 @@
}
- private List<ListenerInvocation> getListenersForAnnotation(Class annotation)
+ private List<ListenerInvocation> getListenerCollectionForAnnotation(Class<? extends Annotation> annotation)
{
- if (annotation == CacheStarted.class)
- {
- return cacheStartedListeners;
- }
- else if (annotation == CacheStopped.class)
- {
- return cacheStoppedListeners;
- }
- else if (annotation == CacheBlocked.class)
- {
- return cacheBlockedListeners;
- }
- else if (annotation == CacheUnblocked.class)
- {
- return cacheUnblockedListeners;
- }
- else if (annotation == NodeCreated.class)
- {
- return nodeCreatedListeners;
- }
- else if (annotation == NodeRemoved.class)
- {
- return nodeRemovedListeners;
- }
- else if (annotation == NodeVisited.class)
- {
- return nodeVisitedListeners;
- }
- else if (annotation == NodeModified.class)
- {
- return nodeModifiedListeners;
- }
- else if (annotation == NodeMoved.class)
- {
- return nodeMovedListeners;
- }
- else if (annotation == NodeActivated.class)
- {
- return nodeActivatedListeners;
- }
- else if (annotation == NodePassivated.class)
- {
- return nodePassivatedListeners;
- }
- else if (annotation == NodeLoaded.class)
- {
- return nodeLoadedListeners;
- }
- else if (annotation == NodeEvicted.class)
- {
- return nodeEvictedListeners;
- }
- else if (annotation == TransactionRegistered.class)
- {
- return transactionRegisteredListeners;
- }
- else if (annotation == TransactionCompleted.class)
- {
- return transactionCompletedListeners;
- }
- else if (annotation == ViewChanged.class)
- {
- return viewChangedListeners;
- }
- else if (annotation == BuddyGroupChanged.class)
- {
- return buddyGroupChangedListeners;
- }
- else
- {
- throw new RuntimeException("Unknown listener class: " + annotation);
- }
+ List<ListenerInvocation> list = listenersMap.get(annotation);
+ if (list == null) throw new CacheException("Unknown listener annotation: " + annotation);
+ return list;
}
-
- private Collection getListeningObjects(List<ListenerInvocation> cacheStartedListeners)
- {
- Set result = new HashSet();
- for (ListenerInvocation li : cacheStartedListeners)
- {
- result.add(li.target);
- }
- return result;
- }
-
- public List<ListenerInvocation> getCacheStartedListeners()
- {
- return cacheStartedListeners;
- }
-
- public List<ListenerInvocation> getCacheStoppedListeners()
- {
- return cacheStoppedListeners;
- }
-
- public List<ListenerInvocation> getCacheBlockedListeners()
- {
- return cacheBlockedListeners;
- }
-
- public List<ListenerInvocation> getCacheUnblockedListeners()
- {
- return cacheUnblockedListeners;
- }
-
- public List<ListenerInvocation> getNodeCreatedListeners()
- {
- return nodeCreatedListeners;
- }
-
- public List<ListenerInvocation> getNodeRemovedListeners()
- {
- return nodeRemovedListeners;
- }
-
- public List<ListenerInvocation> getNodeVisitedListeners()
- {
- return nodeVisitedListeners;
- }
-
- public List<ListenerInvocation> getNodeModifiedListeners()
- {
- return nodeModifiedListeners;
- }
-
- public List<ListenerInvocation> getNodeMovedListeners()
- {
- return nodeMovedListeners;
- }
-
- public List<ListenerInvocation> getNodeActivatedListeners()
- {
- return nodeActivatedListeners;
- }
-
- public List<ListenerInvocation> getNodePassivatedListeners()
- {
- return nodePassivatedListeners;
- }
-
- public List<ListenerInvocation> getNodeLoadedListeners()
- {
- return nodeLoadedListeners;
- }
-
- public List<ListenerInvocation> getNodeEvictedListeners()
- {
- return nodeEvictedListeners;
- }
-
- public List<ListenerInvocation> getTransactionRegisteredListeners()
- {
- return transactionRegisteredListeners;
- }
-
- public List<ListenerInvocation> getTransactionCompletedListeners()
- {
- return transactionCompletedListeners;
- }
-
- public List<ListenerInvocation> getViewChangedListeners()
- {
- return viewChangedListeners;
- }
-
- public List<ListenerInvocation> getBuddyGroupChangedListeners()
- {
- return buddyGroupChangedListeners;
- }
}
Modified: core/trunk/src/main/java/org/jboss/cache/notifications/annotation/CacheListener.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/notifications/annotation/CacheListener.java 2008-08-01 09:04:40 UTC (rev 6478)
+++ core/trunk/src/main/java/org/jboss/cache/notifications/annotation/CacheListener.java 2008-08-01 11:03:23 UTC (rev 6479)
@@ -152,6 +152,11 @@
* <td valign=@"top">{@link org.jboss.cache.notifications.event.BuddyGroupChangedEvent}</td>
* <td valign="top">Buddy replication is enabled and one of the buddy groups that the instance is a member of has changed it's membership.</td>
* </tr>
+ * <tr>
+ * <td valign="top">{@link NodeInvalidated}</td>
+ * <td valign=@"top">{@link org.jboss.cache.notifications.event.NodeInvalidatedEvent}</td>
+ * <td valign="top">A node was invalidated by a remote cache. Only if cache mode is INVALIDATION_SYNC or INVALIDATION_ASYNC.</td>
+ * </tr>
* <p/>
* </table>
* <p/>
@@ -283,12 +288,10 @@
* @see TransactionCompleted
* @see TransactionRegistered
* @see BuddyGroupChanged
+ * @see NodeInvalidated
* @see org.jboss.cache.Cache#addCacheListener(Object)
- * @see org.jboss.cache.Cache#addCacheListener(org.jboss.cache.Fqn,Object)
* @see org.jboss.cache.Cache#removeCacheListener(Object)
- * @see org.jboss.cache.Cache#removeCacheListener(org.jboss.cache.Fqn,Object)
* @see org.jboss.cache.Cache#getCacheListeners()
- * @see org.jboss.cache.Cache#getCacheListeners(org.jboss.cache.Fqn)
* @since 2.0.0
*/
@Retention(RetentionPolicy.RUNTIME)
Copied: core/trunk/src/main/java/org/jboss/cache/notifications/annotation/NodeInvalidated.java (from rev 6476, core/trunk/src/main/java/org/jboss/cache/notifications/annotation/NodeEvicted.java)
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/notifications/annotation/NodeInvalidated.java (rev 0)
+++ core/trunk/src/main/java/org/jboss/cache/notifications/annotation/NodeInvalidated.java 2008-08-01 11:03:23 UTC (rev 6479)
@@ -0,0 +1,23 @@
+package org.jboss.cache.notifications.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * This annotation should be used on methods that need to be notified when a node is invalidated.
+ * <p/>
+ * Methods annotated with this annotation should be public and take in a single parameter, a {@link org.jboss.cache.notifications.event.NodeInvalidatedEvent}
+ * otherwise an {@link org.jboss.cache.notifications.IncorrectCacheListenerException} will be thrown when registering
+ * your cache listener.
+ *
+ * @author <a href="mailto:manik@jboss.org">Manik Surtani</a>
+ * @see org.jboss.cache.notifications.annotation.CacheListener
+ * @since 3.0
+ */
+(a)Retention(RetentionPolicy.RUNTIME)
+(a)Target(ElementType.METHOD)
+public @interface NodeInvalidated
+{
+}
\ No newline at end of file
Modified: core/trunk/src/main/java/org/jboss/cache/notifications/event/Event.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/notifications/event/Event.java 2008-08-01 09:04:40 UTC (rev 6478)
+++ core/trunk/src/main/java/org/jboss/cache/notifications/event/Event.java 2008-08-01 11:03:23 UTC (rev 6479)
@@ -14,7 +14,7 @@
{
CACHE_STARTED, CACHE_STOPPED, CACHE_BLOCKED, CACHE_UNBLOCKED, NODE_ACTIVATED, NODE_PASSIVATED,
NODE_LOADED, NODE_EVICTED, NODE_CREATED, NODE_REMOVED, NODE_MODIFIED, NODE_MOVED, NODE_VISITED,
- TRANSACTION_COMPLETED, TRANSACTION_REGISTERED, VIEW_CHANGED, BUDDY_GROUP_CHANGED
+ TRANSACTION_COMPLETED, TRANSACTION_REGISTERED, VIEW_CHANGED, BUDDY_GROUP_CHANGED, NODE_INVALIDATED
}
/**
Modified: core/trunk/src/main/java/org/jboss/cache/notifications/event/EventImpl.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/notifications/event/EventImpl.java 2008-08-01 09:04:40 UTC (rev 6478)
+++ core/trunk/src/main/java/org/jboss/cache/notifications/event/EventImpl.java 2008-08-01 11:03:23 UTC (rev 6479)
@@ -17,7 +17,7 @@
public class EventImpl implements CacheBlockedEvent, CacheUnblockedEvent, CacheStartedEvent, CacheStoppedEvent,
NodeActivatedEvent, NodeCreatedEvent, NodeEvictedEvent, NodeLoadedEvent, NodeModifiedEvent, NodeMovedEvent,
NodePassivatedEvent, NodeRemovedEvent, NodeVisitedEvent, TransactionCompletedEvent, TransactionRegisteredEvent,
- ViewChangedEvent, BuddyGroupChangedEvent
+ ViewChangedEvent, BuddyGroupChangedEvent, NodeInvalidatedEvent
{
private boolean pre = false; // by default events are after the fact
private Cache cache;
Added: core/trunk/src/main/java/org/jboss/cache/notifications/event/NodeInvalidatedEvent.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/notifications/event/NodeInvalidatedEvent.java (rev 0)
+++ core/trunk/src/main/java/org/jboss/cache/notifications/event/NodeInvalidatedEvent.java 2008-08-01 11:03:23 UTC (rev 6479)
@@ -0,0 +1,11 @@
+package org.jboss.cache.notifications.event;
+
+/**
+ * Notifies a listener of an invalidation event
+ *
+ * @author Manik Surtani (<a href="mailto:manik@jboss.org">manik(a)jboss.org</a>)
+ * @since 3.0
+ */
+public interface NodeInvalidatedEvent extends NodeEvent
+{
+}
Modified: core/trunk/src/test/java/org/jboss/cache/notifications/EventLog.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/notifications/EventLog.java 2008-08-01 09:04:40 UTC (rev 6478)
+++ core/trunk/src/test/java/org/jboss/cache/notifications/EventLog.java 2008-08-01 11:03:23 UTC (rev 6479)
@@ -1,6 +1,17 @@
package org.jboss.cache.notifications;
-import org.jboss.cache.notifications.annotation.*;
+import org.jboss.cache.notifications.annotation.CacheListener;
+import org.jboss.cache.notifications.annotation.NodeActivated;
+import org.jboss.cache.notifications.annotation.NodeCreated;
+import org.jboss.cache.notifications.annotation.NodeEvicted;
+import org.jboss.cache.notifications.annotation.NodeInvalidated;
+import org.jboss.cache.notifications.annotation.NodeModified;
+import org.jboss.cache.notifications.annotation.NodeMoved;
+import org.jboss.cache.notifications.annotation.NodePassivated;
+import org.jboss.cache.notifications.annotation.NodeRemoved;
+import org.jboss.cache.notifications.annotation.NodeVisited;
+import org.jboss.cache.notifications.annotation.TransactionCompleted;
+import org.jboss.cache.notifications.annotation.TransactionRegistered;
import org.jboss.cache.notifications.event.Event;
import org.jboss.cache.notifications.event.EventImpl;
@@ -22,6 +33,7 @@
@NodeEvicted
@NodePassivated
@NodeActivated
+ @NodeInvalidated
public void callback(Event e)
{
events.add(e);
Modified: core/trunk/src/test/java/org/jboss/cache/notifications/NotifierAnnotationsTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/notifications/NotifierAnnotationsTest.java 2008-08-01 09:04:40 UTC (rev 6478)
+++ core/trunk/src/test/java/org/jboss/cache/notifications/NotifierAnnotationsTest.java 2008-08-01 11:03:23 UTC (rev 6479)
@@ -180,9 +180,9 @@
{
Object l = new TestMultipleMethodsListener();
n.addCacheListener(l);
- List invocations = n.getCacheStartedListeners();
+ List invocations = n.cacheStartedListeners;
assertEquals(1, invocations.size());
- invocations = n.getCacheStoppedListeners();
+ invocations = n.cacheStoppedListeners;
assertEquals(1, invocations.size());
assertEquals(1, n.getCacheListeners().size());
}
@@ -191,9 +191,9 @@
{
Object l = new TestMultipleAnnotationsOneMethodListener();
n.addCacheListener(l);
- List invocations = n.getCacheStartedListeners();
+ List invocations = n.cacheStartedListeners;
assertEquals(1, invocations.size());
- invocations = n.getCacheStoppedListeners();
+ invocations = n.cacheStoppedListeners;
assertEquals(1, invocations.size());
assertEquals(1, n.getCacheListeners().size());
}
@@ -202,7 +202,7 @@
{
Object l = new TestMultipleMethodsOneAnnotationListener();
n.addCacheListener(l);
- List invocations = n.getCacheStartedListeners();
+ List invocations = n.cacheStartedListeners;
assertEquals(2, invocations.size());
assertEquals(1, n.getCacheListeners().size());
}
Modified: core/trunk/src/test/java/org/jboss/cache/notifications/NotifierTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/notifications/NotifierTest.java 2008-08-01 09:04:40 UTC (rev 6478)
+++ core/trunk/src/test/java/org/jboss/cache/notifications/NotifierTest.java 2008-08-01 11:03:23 UTC (rev 6479)
@@ -63,7 +63,6 @@
Map expected = new HashMap();
expected.put("k", "v");
notifier.notifyNodeModified(fqn, true, NodeModifiedEvent.ModificationType.PUT_DATA, expected, ctx);
- assert notifier.getNodeModifiedListeners().size() == 1;
assert allEventsListener.nodeModifiedEvent != null;
assert allEventsListener.nodeModifiedEvent.getData().equals(expected);
assert allEventsListener.nodeModifiedEvent.getModificationType() == NodeModifiedEvent.ModificationType.PUT_DATA;
Added: core/trunk/src/test/java/org/jboss/cache/notifications/NotifyNodeInvalidatedTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/notifications/NotifyNodeInvalidatedTest.java (rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/notifications/NotifyNodeInvalidatedTest.java 2008-08-01 11:03:23 UTC (rev 6479)
@@ -0,0 +1,49 @@
+package org.jboss.cache.notifications;
+
+import org.jboss.cache.Cache;
+import org.jboss.cache.DefaultCacheFactory;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.config.Configuration;
+import org.jboss.cache.config.Configuration.CacheMode;
+import org.jboss.cache.config.Configuration.NodeLockingScheme;
+import org.jboss.cache.factories.UnitTestCacheConfigurationFactory;
+import org.jboss.cache.notifications.event.Event;
+import static org.jboss.cache.notifications.event.Event.Type.NODE_INVALIDATED;
+import org.jboss.cache.notifications.event.EventImpl;
+import org.jboss.cache.util.TestingUtil;
+import org.testng.annotations.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Test(groups = "functional")
+public class NotifyNodeInvalidatedTest
+{
+ public void testInvalidatedCallback() throws CloneNotSupportedException
+ {
+ Cache<String, String> c1 = null, c2 = null;
+ try
+ {
+ Configuration cfg = UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.INVALIDATION_SYNC, false);
+ cfg.setNodeLockingScheme(NodeLockingScheme.MVCC);
+ c1 = new DefaultCacheFactory<String, String>().createCache(cfg.clone());
+ c2 = new DefaultCacheFactory<String, String>().createCache(cfg.clone());
+ EventLog eventLog = new EventLog();
+ c2.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
+ c2.put("/a/b/c", "x", "y");
+ c2.addCacheListener(eventLog);
+ c1.put("/a/b/c", "k", "v");
+
+ List<Event> expected = new ArrayList<Event>();
+ expected.add(new EventImpl(true, c2, null, null, Fqn.fromString("/a/b/c"), null, false, null, false, null, NODE_INVALIDATED));
+ expected.add(new EventImpl(false, c2, null, null, Fqn.fromString("/a/b/c"), null, false, null, false, null, NODE_INVALIDATED));
+
+ assert expected.equals(eventLog.events) : "Expected " + expected + " but got " + eventLog.events;
+ assert c2.getNode("/a/b/c") == null;
+ }
+ finally
+ {
+ TestingUtil.killCaches(c1, c2);
+ }
+ }
+}
Modified: core/trunk/src/test/java/org/jboss/cache/notifications/RemoteCacheListenerTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/notifications/RemoteCacheListenerTest.java 2008-08-01 09:04:40 UTC (rev 6478)
+++ core/trunk/src/test/java/org/jboss/cache/notifications/RemoteCacheListenerTest.java 2008-08-01 11:03:23 UTC (rev 6479)
@@ -63,7 +63,7 @@
private Cache<String, String> cache1, cache2;
@SuppressWarnings("unused")
- private TransactionManager tm1, tm2;
+ private TransactionManager tm1;
private EventLog eventLog1 = new EventLog(), eventLog2 = new EventLog();
private Fqn fqn = Fqn.fromString("/test");
@@ -95,7 +95,6 @@
cache2.addCacheListener(eventLog2);
tm1 = cache1.getConfiguration().getRuntimeConfig().getTransactionManager();
- tm2 = cache2.getConfiguration().getRuntimeConfig().getTransactionManager();
}
@AfterMethod(alwaysRun = true)
16 years, 7 months
JBoss Cache SVN: r6478 - core/trunk/src/test/java/org/jboss/cache/profiling.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2008-08-01 05:04:40 -0400 (Fri, 01 Aug 2008)
New Revision: 6478
Added:
core/trunk/src/test/java/org/jboss/cache/profiling/ProfileMapViewTest.java
Log:
added test
Copied: core/trunk/src/test/java/org/jboss/cache/profiling/ProfileMapViewTest.java (from rev 6476, core/trunk/src/test/java/org/jboss/cache/profiling/ProfileTest.java)
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/profiling/ProfileMapViewTest.java (rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/profiling/ProfileMapViewTest.java 2008-08-01 09:04:40 UTC (rev 6478)
@@ -0,0 +1,305 @@
+package org.jboss.cache.profiling;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.jboss.cache.Cache;
+import org.jboss.cache.DefaultCacheFactory;
+import org.jboss.cache.config.Configuration;
+import org.jboss.cache.config.Configuration.NodeLockingScheme;
+import org.jboss.cache.factories.UnitTestCacheConfigurationFactory;
+import org.jboss.cache.lock.IsolationLevel;
+import org.jboss.cache.util.Caches;
+import org.jboss.cache.util.Caches.HashKeySelector;
+import org.jboss.cache.util.TestingUtil;
+import org.testng.annotations.AfterTest;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
+@Test(groups = "profiling")
+public class ProfileMapViewTest
+{
+ /*
+ Test configuration options
+ */
+ protected static final long DURATION = 60 * 1000; // 1 min of GENERATION = a lot of processing. :-)
+ protected static final int NUM_THREADS = 15;
+ protected static final int MAX_RANDOM_SLEEP_MILLIS = 1;
+ protected static final int MAX_ENTRIES = 200;
+ protected static final int WARMUP_LOOPS = 20000;
+ protected static final boolean USE_SLEEP = false; // throttle generation a bit
+
+ private List<String> keys = new ArrayList<String>(MAX_ENTRIES);
+ private Random r = new Random();
+ private Cache<String, String> cache;
+ private Map<String, String> map;
+
+
+ private Log log = LogFactory.getLog(ProfileTest.class);
+
+ @BeforeTest
+ public void setUp()
+ {
+ Configuration cfg = UnitTestCacheConfigurationFactory.createConfiguration(Configuration.CacheMode.LOCAL);
+ cfg.setNodeLockingScheme(NodeLockingScheme.MVCC);
+ cfg.setConcurrencyLevel(500);
+ cache = new DefaultCacheFactory<String, String>().createCache(cfg, false);
+ }
+
+ @AfterTest
+ public void tearDown()
+ {
+ cache.stop();
+ }
+
+
+ public void testLocalModeMVCC_RC() throws Exception
+ {
+ cache.getConfiguration().setIsolationLevel(IsolationLevel.READ_COMMITTED);
+ runCompleteTest();
+ }
+
+ public void testLocalModeMVCC_RR() throws Exception
+ {
+ cache.getConfiguration().setIsolationLevel(IsolationLevel.REPEATABLE_READ);
+ runCompleteTest();
+ }
+
+
+ private void runCompleteTest() throws Exception
+ {
+ init();
+ startup();
+ warmup();
+ doTest();
+
+ // wait for user exit
+ System.in.read();
+ }
+
+ /**
+ * Thr following test phases can be profiled individually using triggers in JProfiler.
+ */
+
+ protected void init()
+ {
+ long startTime = System.currentTimeMillis();
+ log.warn("Starting init() phase");
+ keys.clear();
+ for (int i = 0; i < MAX_ENTRIES; i++)
+ {
+ String key = createRandomKey(r);
+ while (keys.contains(key)) key = createRandomKey(r);
+ if (i % 10 == 0)
+ {
+ log.warn("Generated " + i + " fqns");
+ }
+ keys.add(key);
+ }
+ System.gc();
+ long duration = System.currentTimeMillis() - startTime;
+ log.warn("Finished init() phase. " + printDuration(duration));
+ }
+
+ private String createRandomKey(Random r)
+ {
+ StringBuilder s = new StringBuilder("/");
+ int depth = r.nextInt(3);
+ for (int i = 0; i < depth; i++)
+ {
+ s.append(r.nextInt(Integer.MAX_VALUE)).append("/");
+ }
+
+ return s.toString();
+ }
+
+ private Map<String, String> createMap(Cache<String, String> cache)
+ {
+ return Caches.asPartitionedMap(cache.getRoot(), new HashKeySelector(128));
+ }
+
+ private void startup()
+ {
+ long startTime = System.currentTimeMillis();
+ log.warn("Starting cache");
+ cache.start();
+ map = createMap(cache);
+ long duration = System.currentTimeMillis() - startTime;
+ log.warn("Started cache. " + printDuration(duration));
+ }
+
+ private void warmup() throws InterruptedException
+ {
+ long startTime = System.currentTimeMillis();
+ ExecutorService exec = Executors.newFixedThreadPool(NUM_THREADS);
+ log.warn("Starting warmup");
+ // creates all the Fqns since this can be expensive and we don't really want to measure this (for now)
+ for (final String key : keys)
+ {
+ exec.execute(new Runnable()
+ {
+ public void run()
+ {
+ // this will create the necessary nodes.
+// cache.put(f, Collections.emptyMap());
+ map.put(key, "value");
+ }
+ });
+ }
+
+ // loop through WARMUP_LOOPS gets and puts for JVM optimisation
+ for (int i = 0; i < WARMUP_LOOPS; i++)
+ {
+ exec.execute(new Runnable()
+ {
+ public void run()
+ {
+// Fqn f = fqns.get(r.nextInt(MAX_ENTRIES));
+ String key = keys.get(r.nextInt(MAX_ENTRIES));
+// cache.get(f, "");
+// cache.put(f, "k", "v");
+// cache.remove(f, "k");
+ map.get(key);
+ map.put(key, "value");
+ map.remove(key);
+ }
+ });
+ }
+
+ exec.shutdown();
+ exec.awaitTermination(360, TimeUnit.SECONDS);
+
+ long duration = System.currentTimeMillis() - startTime;
+ log.warn("Finished warmup. " + printDuration(duration));
+ //cache.removeNode(Fqn.ROOT);
+ cache.stop();
+ cache.start();
+ map = createMap(cache);
+ }
+
+ private void doTest() throws Exception
+ {
+ ExecutorService exec = Executors.newFixedThreadPool(NUM_THREADS);
+ long end = System.currentTimeMillis() + DURATION;
+ long startTime = System.currentTimeMillis();
+ log.warn("Starting test");
+ int i = 0;
+ while (System.currentTimeMillis() < end)
+ {
+ MyRunnable r = null;
+ switch (i % 3)
+ {
+ case 0:
+ r = new Putter(i++);
+ break;
+ case 1:
+ r = new Getter(i++);
+ break;
+ case 2:
+ r = new Remover(i++);
+ break;
+ }
+ exec.execute(r);
+// if (USE_SLEEP) TestingUtil.sleepRandom(MAX_RANDOM_SLEEP_MILLIS);
+ if (USE_SLEEP) TestingUtil.sleepThread(MAX_RANDOM_SLEEP_MILLIS);
+ }
+ log.warn("Finished generating runnables; awaiting executor completion");
+ // wait for executors to complete!
+ exec.shutdown();
+ exec.awaitTermination(((long) i), TimeUnit.SECONDS); // wait up to 1 sec for each call?
+ long duration = System.currentTimeMillis() - startTime;
+ log.warn("Finished test. " + printDuration(duration));
+ }
+
+ enum Mode
+ {
+ PUT, GET, REMOVE
+ }
+
+ private abstract class MyRunnable implements Runnable
+ {
+ int id;
+ Mode mode;
+
+ public void run()
+ {
+ if (id % 100 == 0) log.warn("Processing iteration " + id);
+ String k = getRandomString();
+ String key = keys.get(r.nextInt(MAX_ENTRIES));
+ switch (mode)
+ {
+ case PUT:
+// cache.put(f, k, getRandomString());
+ map.put(key, getRandomString());
+ break;
+ case GET:
+// cache.get(f, k);
+ map.get(key);
+ break;
+ case REMOVE:
+// cache.remove(f, k);
+ map.remove(key);
+ break;
+ }
+ }
+ }
+
+ private class Putter extends MyRunnable
+ {
+ private Putter(int id)
+ {
+ this.id = id;
+ mode = Mode.PUT;
+ }
+ }
+
+ private class Getter extends MyRunnable
+ {
+ private Getter(int id)
+ {
+ this.id = id;
+ mode = Mode.GET;
+ }
+ }
+
+ private class Remover extends MyRunnable
+ {
+ private Remover(int id)
+ {
+ this.id = id;
+ mode = Mode.REMOVE;
+ }
+ }
+
+ private String getRandomString()
+ {
+ StringBuilder sb = new StringBuilder();
+ int len = r.nextInt(10);
+
+ for (int i = 0; i < len; i++)
+ {
+ sb.append((char) (63 + r.nextInt(26)));
+ }
+ return sb.toString();
+ }
+
+ private String printDuration(long duration)
+ {
+ if (duration > 2000)
+ {
+ double dSecs = ((double) duration / (double) 1000);
+ return "Duration: " + dSecs + " seconds";
+ }
+ else
+ {
+ return "Duration: " + duration + " millis";
+ }
+ }
+}
\ No newline at end of file
16 years, 7 months
JBoss Cache SVN: r6477 - core/trunk/src/main/java/org/jboss/cache/commands.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2008-08-01 04:39:15 -0400 (Fri, 01 Aug 2008)
New Revision: 6477
Modified:
core/trunk/src/main/java/org/jboss/cache/commands/OptimisticCommandsFactoryImpl.java
core/trunk/src/main/java/org/jboss/cache/commands/PessimisticCommandsFactoryImpl.java
Log:
added createNodeCommand for opt locking
deprecated opt commands factory
Modified: core/trunk/src/main/java/org/jboss/cache/commands/OptimisticCommandsFactoryImpl.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/commands/OptimisticCommandsFactoryImpl.java 2008-07-31 18:08:14 UTC (rev 6476)
+++ core/trunk/src/main/java/org/jboss/cache/commands/OptimisticCommandsFactoryImpl.java 2008-08-01 08:39:15 UTC (rev 6477)
@@ -2,6 +2,7 @@
import org.jboss.cache.Fqn;
import org.jboss.cache.commands.legacy.read.LegacyGravitateDataCommand;
+import org.jboss.cache.commands.legacy.write.CreateNodeCommand;
import org.jboss.cache.commands.legacy.write.LegacyEvictCommand;
import org.jboss.cache.commands.legacy.write.VersionedInvalidateCommand;
import org.jboss.cache.commands.read.GravitateDataCommand;
@@ -13,7 +14,10 @@
*
* @author Manik Surtani (<a href="mailto:manik@jboss.org">manik(a)jboss.org</a>)
* @since 3.0
+ * @deprecated will be removed with opt locking
*/
+@Deprecated
+@SuppressWarnings("deprecation")
public class OptimisticCommandsFactoryImpl extends CommandsFactoryImpl
{
@Override
@@ -42,12 +46,28 @@
}
@Override
+ public CreateNodeCommand buildCreateNodeCommand(Fqn fqn)
+ {
+ CreateNodeCommand command = new CreateNodeCommand(fqn);
+ command.initialize(dataContainer);
+ return command;
+ }
+
+
+ @Override
public ReplicableCommand fromStream(int id, Object[] parameters)
{
ReplicableCommand command;
boolean skipSetParams = false;
switch (id)
{
+ case CreateNodeCommand.METHOD_ID:
+ {
+ CreateNodeCommand returnValue = new CreateNodeCommand(null);
+ returnValue.initialize(dataContainer);
+ command = returnValue;
+ break;
+ }
case GravitateDataCommand.METHOD_ID:
{
LegacyGravitateDataCommand returnValue = new LegacyGravitateDataCommand(rpcManager.getLocalAddress());
Modified: core/trunk/src/main/java/org/jboss/cache/commands/PessimisticCommandsFactoryImpl.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/commands/PessimisticCommandsFactoryImpl.java 2008-07-31 18:08:14 UTC (rev 6476)
+++ core/trunk/src/main/java/org/jboss/cache/commands/PessimisticCommandsFactoryImpl.java 2008-08-01 08:39:15 UTC (rev 6477)
@@ -2,7 +2,6 @@
import org.jboss.cache.Fqn;
import org.jboss.cache.commands.legacy.read.PessGetChildrenNamesCommand;
-import org.jboss.cache.commands.legacy.write.CreateNodeCommand;
import org.jboss.cache.commands.legacy.write.PessClearDataCommand;
import org.jboss.cache.commands.legacy.write.PessMoveCommand;
import org.jboss.cache.commands.legacy.write.PessPutDataMapCommand;
@@ -30,7 +29,10 @@
* @author Manik Surtani (<a href="mailto:manik@jboss.org">manik(a)jboss.org</a>)
* @see org.jboss.cache.commands.legacy.ReversibleCommand
* @since 3.0
+ * @deprecated will be removed with possimistic locking
*/
+@Deprecated
+@SuppressWarnings("deprecation")
public class PessimisticCommandsFactoryImpl extends OptimisticCommandsFactoryImpl
{
@Override
@@ -98,14 +100,6 @@
}
@Override
- public CreateNodeCommand buildCreateNodeCommand(Fqn fqn)
- {
- CreateNodeCommand command = new CreateNodeCommand(fqn);
- command.initialize(dataContainer);
- return command;
- }
-
- @Override
public InvalidateCommand buildInvalidateCommand(Fqn fqn)
{
InvalidateCommand command = new InvalidateCommand(fqn);
@@ -185,13 +179,6 @@
command = returnValue;
break;
}
- case CreateNodeCommand.METHOD_ID:
- {
- CreateNodeCommand returnValue = new CreateNodeCommand(null);
- returnValue.initialize(dataContainer);
- command = returnValue;
- break;
- }
case InvalidateCommand.METHOD_ID:
{
InvalidateCommand returnValue = new InvalidateCommand(null);
16 years, 7 months