Search in sources :

Example 1 with TopologyChangedEvent

use of org.infinispan.notifications.cachelistener.event.TopologyChangedEvent in project wildfly by wildfly.

the class CacheRegistry method topologyChanged.

@TopologyChanged
public void topologyChanged(TopologyChangedEvent<Node, Map.Entry<K, V>> event) {
    if (event.isPre())
        return;
    ConsistentHash previousHash = event.getConsistentHashAtStart();
    List<Address> previousMembers = previousHash.getMembers();
    ConsistentHash hash = event.getConsistentHashAtEnd();
    List<Address> members = hash.getMembers();
    Address localAddress = event.getCache().getCacheManager().getAddress();
    // Determine which nodes have left the cache view
    Set<Address> addresses = new HashSet<>(previousMembers);
    addresses.removeAll(members);
    try {
        this.topologyChangeExecutor.submit(() -> {
            if (!addresses.isEmpty()) {
                // We're only interested in the entries for which we are the primary owner
                List<Node> nodes = addresses.stream().filter(address -> hash.locatePrimaryOwner(address).equals(localAddress)).map(address -> this.factory.createNode(address)).collect(Collectors.toList());
                if (!nodes.isEmpty()) {
                    Cache<Node, Map.Entry<K, V>> cache = this.cache.getAdvancedCache().withFlags(Flag.FORCE_SYNCHRONOUS);
                    Map<K, V> removed = new HashMap<>();
                    try (Batch batch = this.batcher.createBatch()) {
                        for (Node node : nodes) {
                            Map.Entry<K, V> old = cache.remove(node);
                            if (old != null) {
                                removed.put(old.getKey(), old.getValue());
                            }
                        }
                    } catch (CacheException e) {
                        ClusteringServerLogger.ROOT_LOGGER.registryPurgeFailed(e, this.cache.getCacheManager().toString(), this.cache.getName(), nodes);
                    }
                    // Invoke listeners outside above tx context
                    if (!removed.isEmpty()) {
                        this.notifyListeners(Event.Type.CACHE_ENTRY_REMOVED, removed);
                    }
                }
            } else {
                // This is a merge after cluster split: re-populate the cache registry with lost registry entries
                if (!previousMembers.contains(localAddress)) {
                    // If this node is not a member at merge start, its mapping is lost and needs to be recreated and listeners notified
                    try {
                        this.populateRegistry();
                        // Local cache events do not trigger notifications
                        this.notifyListeners(Event.Type.CACHE_ENTRY_CREATED, this.entry);
                    } catch (CacheException e) {
                        ClusteringServerLogger.ROOT_LOGGER.failedToRestoreLocalRegistryEntry(e, this.cache.getCacheManager().toString(), this.cache.getName());
                    }
                }
            }
        });
    } catch (RejectedExecutionException e) {
    // Executor was shutdown
    }
}
Also used : CacheEntryRemoved(org.infinispan.notifications.cachelistener.annotation.CacheEntryRemoved) TopologyChanged(org.infinispan.notifications.cachelistener.annotation.TopologyChanged) ClusteringLogger(org.jboss.as.clustering.logging.ClusteringLogger) HashMap(java.util.HashMap) Cache(org.infinispan.Cache) HashSet(java.util.HashSet) ClassLoaderThreadFactory(org.wildfly.clustering.service.concurrent.ClassLoaderThreadFactory) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Map(java.util.Map) CacheEntryModified(org.infinispan.notifications.cachelistener.annotation.CacheEntryModified) CacheEntryRemovedEvent(org.infinispan.notifications.cachelistener.event.CacheEntryRemovedEvent) ThreadFactory(java.util.concurrent.ThreadFactory) NodeFactory(org.wildfly.clustering.group.NodeFactory) ExecutorService(java.util.concurrent.ExecutorService) Address(org.infinispan.remoting.transport.Address) JBossThreadFactory(org.jboss.threads.JBossThreadFactory) CacheException(org.infinispan.commons.CacheException) ConsistentHash(org.infinispan.distribution.ch.ConsistentHash) Batcher(org.wildfly.clustering.ee.Batcher) Registry(org.wildfly.clustering.registry.Registry) Group(org.wildfly.clustering.group.Group) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) PrivilegedAction(java.security.PrivilegedAction) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) TopologyChangedEvent(org.infinispan.notifications.cachelistener.event.TopologyChangedEvent) TimeUnit(java.util.concurrent.TimeUnit) CacheEntryCreated(org.infinispan.notifications.cachelistener.annotation.CacheEntryCreated) AbstractMap(java.util.AbstractMap) List(java.util.List) WildFlySecurityManager(org.wildfly.security.manager.WildFlySecurityManager) Flag(org.infinispan.context.Flag) KeyFilter(org.infinispan.filter.KeyFilter) CacheEntryEvent(org.infinispan.notifications.cachelistener.event.CacheEntryEvent) Batch(org.wildfly.clustering.ee.Batch) Event(org.infinispan.notifications.cachelistener.event.Event) Node(org.wildfly.clustering.group.Node) ClusteringServerLogger(org.wildfly.clustering.server.logging.ClusteringServerLogger) Collections(java.util.Collections) ConsistentHash(org.infinispan.distribution.ch.ConsistentHash) Address(org.infinispan.remoting.transport.Address) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CacheException(org.infinispan.commons.CacheException) Node(org.wildfly.clustering.group.Node) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Batch(org.wildfly.clustering.ee.Batch) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AbstractMap(java.util.AbstractMap) HashSet(java.util.HashSet) TopologyChanged(org.infinispan.notifications.cachelistener.annotation.TopologyChanged)

Aggregations

PrivilegedAction (java.security.PrivilegedAction)1 AbstractMap (java.util.AbstractMap)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 Cache (org.infinispan.Cache)1 CacheException (org.infinispan.commons.CacheException)1 Flag (org.infinispan.context.Flag)1 ConsistentHash (org.infinispan.distribution.ch.ConsistentHash)1 KeyFilter (org.infinispan.filter.KeyFilter)1