Skip to content

Commit

Permalink
ISPN-720 Replace CacheEntryEvictedEvent with CacheEntriesEvictedEvent…
Browse files Browse the repository at this point in the history
… (note the plural)

* Revert some changes on CacheEntryPassivated
* Remove unnecessary imports, etc
* Only loop and generate eviction map if necessary
* Only fire eviction as a post-event
  • Loading branch information
maniksurtani committed Jun 10, 2011
1 parent 708386f commit e5537be
Show file tree
Hide file tree
Showing 20 changed files with 222 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
import org.infinispan.context.InvocationContext;
import org.infinispan.notifications.cachelistener.CacheNotifier;

import java.util.Collections;
import java.util.Map;

/**
* @author Mircea.Markus@jboss.com
* @since 4.0
Expand Down Expand Up @@ -61,8 +58,9 @@ public Object perform(InvocationContext ctx) throws Throwable {

@Override
public void notify(InvocationContext ctx, Object value, boolean isPre) {
Map<Object,Object> entries = Collections.singletonMap(key, value);
notifier.notifyCacheEntriesEvicted(entries, isPre, ctx);
if (!isPre) {
notifier.notifyCacheEntryEvicted(key, value, ctx);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@
*/
package org.infinispan.eviction;

import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import net.jcip.annotations.ThreadSafe;

import org.infinispan.config.Configuration;
import org.infinispan.container.DataContainer;
import org.infinispan.container.entries.InternalCacheEntry;
Expand All @@ -40,18 +34,19 @@
import org.infinispan.factories.annotations.Inject;
import org.infinispan.factories.annotations.Start;
import org.infinispan.factories.annotations.Stop;
import org.infinispan.loaders.CacheLoaderException;
import org.infinispan.loaders.CacheLoaderManager;
import org.infinispan.loaders.CacheStore;
import org.infinispan.marshall.MarshalledValue;
import org.infinispan.notifications.cachelistener.CacheNotifier;
import org.infinispan.util.InfinispanCollections;
import org.infinispan.util.Util;
import org.infinispan.util.concurrent.TimeoutException;
import org.infinispan.util.concurrent.locks.LockManager;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;

import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import static org.infinispan.util.InfinispanCollections.transformMapValue;

@ThreadSafe
Expand All @@ -67,24 +62,20 @@ public class EvictionManagerImpl implements EvictionManager {
private DataContainer dataContainer;
private CacheStore cacheStore;
private CacheNotifier cacheNotifier;
private LockManager lockManager;
private PassivationManager passivator;
private InvocationContextContainer ctxContainer;
private boolean enabled;

@Inject
public void initialize(@ComponentName(KnownComponentNames.EVICTION_SCHEDULED_EXECUTOR) ScheduledExecutorService executor,
Configuration configuration, DataContainer dataContainer,
CacheLoaderManager cacheLoaderManager, CacheNotifier cacheNotifier,
LockManager lockManager, PassivationManager passivator, InvocationContextContainer ctxContainer) {
PassivationManager passivator) {
this.executor = executor;
this.configuration = configuration;
this.dataContainer = dataContainer;
this.cacheLoaderManager = cacheLoaderManager;
this.cacheNotifier = cacheNotifier;
this.lockManager = lockManager;
this.passivator = passivator;
this.ctxContainer = ctxContainer;
}

@Start(priority = 55)
Expand Down Expand Up @@ -165,14 +156,6 @@ public void run() {

@Override
public void onEntryEviction(Map<Object, InternalCacheEntry> evicted) {
Map<Object, Object> evictedCopy = transformMapValue(evicted,
new InfinispanCollections.Function<InternalCacheEntry, Object>() {
public Object transform(InternalCacheEntry input) {
return input.getValue();
}
}
);

// don't reuse the threadlocal context as we don't want to include eviction
// operations in any ongoing transaction, nor be affected by flags
// especially see ISPN-1154: it's illegal to acquire locks in a committing transaction
Expand All @@ -183,11 +166,10 @@ public Object transform(InternalCacheEntry input) {
// However, when a user calls cache.evict(), you do want to carry over the
// contextual information, hence it makes sense for the notifyyCacheEntriesEvicted()
// call to carry on taking an InvocationContext object.
cacheNotifier.notifyCacheEntriesEvicted(evictedCopy, true, ctx);
// To avoid re-calculation, pass the naked eviction entries to the
// passivator, so that it can use them in its notifications
passivator.passivate(evicted, evictedCopy, ctx);
cacheNotifier.notifyCacheEntriesEvicted(evictedCopy, false, ctx);
passivator.passivate(evicted.values(), ctx);
cacheNotifier.notifyCacheEntriesEvicted(evicted.values(), ctx);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.infinispan.factories.scopes.Scopes;
import org.infinispan.loaders.CacheLoaderException;

import java.util.Map;
import java.util.Collection;

/**
* A passivation manager
Expand All @@ -43,8 +43,7 @@ public interface PassivationManager {

boolean isEnabled();

void passivate(Map<Object, InternalCacheEntry> entries,
Map<Object, Object> nakedEntries, InvocationContext ctx);
void passivate(Collection<InternalCacheEntry> entries, InvocationContext ctx);

void passivateAll() throws CacheLoaderException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.infinispan.container.DataContainer;
import org.infinispan.container.entries.InternalCacheEntry;
import org.infinispan.context.InvocationContext;
import org.infinispan.context.impl.ImmutableContext;
import org.infinispan.factories.annotations.Inject;
import org.infinispan.factories.annotations.Start;
import org.infinispan.factories.annotations.Stop;
Expand All @@ -42,7 +41,7 @@
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;

import java.util.Map;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicLong;

public class PassivationManagerImpl implements PassivationManager {
Expand Down Expand Up @@ -89,12 +88,11 @@ public boolean isEnabled() {
}

@Override
public void passivate(Map<Object, InternalCacheEntry> entries,
Map<Object, Object> nakedEntries, InvocationContext ctx) {
public void passivate(Collection<InternalCacheEntry> entries, InvocationContext ctx) {
if (enabled) {
notifier.notifyCacheEntriesPassivated(nakedEntries, true, ctx);
for (Map.Entry<Object, InternalCacheEntry> entry : entries.entrySet()) {
for (InternalCacheEntry entry : entries) {
Object key = entry.getKey();
notifier.notifyCacheEntryPassivated(key, entry.getValue(), true, ctx);
boolean locked = false;
try {
locked = acquireLock(ctx, key);
Expand All @@ -104,10 +102,11 @@ public void passivate(Map<Object, InternalCacheEntry> entries,
try {
// notify listeners that this entry is about to be passivated
if (trace) log.tracef("Passivating entry %s", key);
cacheStore.store(entry.getValue());
cacheStore.store(entry);
if (statsEnabled && entry.getValue() != null) {
passivations.getAndIncrement();
}
notifier.notifyCacheEntryPassivated(key, null, false, ctx);
} catch (CacheLoaderException e) {
log.unableToPassivateEntry(key, e);
}
Expand All @@ -117,7 +116,6 @@ public void passivate(Map<Object, InternalCacheEntry> entries,
}
}
}
notifier.notifyCacheEntriesPassivated(nakedEntries, false, ctx);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@
import org.rhq.helpers.pluginAnnotations.agent.Metric;
import org.rhq.helpers.pluginAnnotations.agent.Operation;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;

/**
* Writes evicted entries back to the store on the way in through the CacheStore
Expand All @@ -62,10 +60,7 @@ public void setDependencies(PassivationManager passivator, DataContainer dataCon
public Object visitEvictCommand(InvocationContext ctx, EvictCommand command) throws Throwable {
Object key = command.getKey();
InternalCacheEntry value = dataContainer.get(key);
Map<Object, InternalCacheEntry> entries = Collections.singletonMap(key, value);
Map<Object, Object> nakedEntries =
Collections.singletonMap(key, value == null ? null : value.getValue());
passivator.passivate(entries, nakedEntries, ctx);
if (value != null) passivator.passivate(Collections.singleton(value), ctx);
return invokeNextInterceptor(ctx, command);
}

Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/org/infinispan/notifications/Listener.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@
* cache entries were evicted</td> </tr> <tr> <td valign="top">{@link org.infinispan.notifications.cachelistener.annotation.CacheEntryActivated}</td>
* <td valign="top">{@link org.infinispan.notifications.cachelistener.event.CacheEntryActivatedEvent}</td> <td
* valign="top">A cache entry was activated</td> </tr> <tr> <td valign="top">{@link
* org.infinispan.notifications.cachelistener.annotation.CacheEntriesPassivated}</td> <td valign="top">{@link
* org.infinispan.notifications.cachelistener.event.CacheEntriesPassivatedEvent}</td> <td valign="top">One or more cache entries were
* org.infinispan.notifications.cachelistener.annotation.CacheEntryPassivated}</td> <td valign="top">{@link
* org.infinispan.notifications.cachelistener.event.CacheEntryPassivatedEvent}</td> <td valign="top">One or more cache entries were
* passivated</td> </tr> <tr> <td valign="top">{@link org.infinispan.notifications.cachemanagerlistener.annotation.ViewChanged}</td>
* <td valign="top">{@link org.infinispan.notifications.cachemanagerlistener.event.ViewChangedEvent}</td> <td
* valign="top">A view change event was detected</td> </tr> <tr> <td valign="top">{@link
Expand Down Expand Up @@ -203,7 +203,7 @@
* @see org.infinispan.notifications.cachelistener.annotation.CacheEntryLoaded
* @see org.infinispan.notifications.cachelistener.annotation.CacheEntriesEvicted
* @see org.infinispan.notifications.cachelistener.annotation.CacheEntryActivated
* @see org.infinispan.notifications.cachelistener.annotation.CacheEntriesPassivated
* @see org.infinispan.notifications.cachelistener.annotation.CacheEntryPassivated
* @see org.infinispan.notifications.cachemanagerlistener.annotation.ViewChanged
* @see org.infinispan.notifications.cachelistener.annotation.TransactionCompleted
* @see org.infinispan.notifications.cachelistener.annotation.TransactionRegistered
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
package org.infinispan.notifications.cachelistener;

import org.infinispan.container.entries.InternalCacheEntry;
import org.infinispan.context.InvocationContext;
import org.infinispan.distribution.ch.ConsistentHash;
import org.infinispan.factories.scopes.Scope;
Expand All @@ -31,7 +32,6 @@
import org.infinispan.transaction.xa.GlobalTransaction;

import java.util.Collection;
import java.util.Map;

/**
* Public interface with all allowed notifications.
Expand Down Expand Up @@ -64,7 +64,15 @@ public interface CacheNotifier extends Listenable {
/**
* Notifies all registered listeners of a CacheEntriesEvicted event.
*/
void notifyCacheEntriesEvicted(Map<Object, Object> entries, boolean pre, InvocationContext ctx);
void notifyCacheEntriesEvicted(Collection<InternalCacheEntry> entries, InvocationContext ctx);

/**
* Syntactic sugar
* @param key key evicted
* @param value value evicted
* @param ctx context
*/
void notifyCacheEntryEvicted(Object key, Object value, InvocationContext ctx);

/**
* Notifies all registered listeners of a CacheEntryInvalidated event.
Expand All @@ -82,9 +90,9 @@ public interface CacheNotifier extends Listenable {
void notifyCacheEntryActivated(Object key, Object value, boolean pre, InvocationContext ctx);

/**
* Notifies all registered listeners of a CacheEntriesPassivated event.
* Notifies all registered listeners of a CacheEntryPassivated event.
*/
void notifyCacheEntriesPassivated(Map<Object, Object> entries, boolean pre, InvocationContext ctx);
void notifyCacheEntryPassivated(Object key, Object value, boolean pre, InvocationContext ctx);

/**
* Notifies all registered listeners of a transaction completion event.
Expand All @@ -104,4 +112,6 @@ public interface CacheNotifier extends Listenable {
void notifyDataRehashed(Collection<Address> oldView, Collection<Address> newView, long newViewId, boolean pre);

void notifyTopologyChanged(ConsistentHash oldConsistentHash, ConsistentHash newConsistentHash, boolean pre);


}
Loading

0 comments on commit e5537be

Please sign in to comment.