Search in sources :

Example 6 with Address

use of org.infinispan.remoting.transport.Address 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)

Example 7 with Address

use of org.infinispan.remoting.transport.Address in project wildfly by wildfly.

the class AffinityIdentityFactoryTestCase method createIdentifier.

@Test
public void createIdentifier() {
    String expected = "id";
    Address address = mock(Address.class);
    when(this.manager.getAddress()).thenReturn(address);
    when(this.affinity.getKeyForAddress(address)).thenReturn(new Key<>(expected));
    String result = this.subject.createIdentifier();
    assertSame(expected, result);
}
Also used : Address(org.infinispan.remoting.transport.Address) Test(org.junit.Test)

Example 8 with Address

use of org.infinispan.remoting.transport.Address in project hibernate-orm by hibernate.

the class NonTxPutFromLoadInterceptor method endInvalidating.

public void endInvalidating(Object key, Object lockOwner, boolean successful) {
    assert lockOwner != null;
    if (!putFromLoadValidator.endInvalidatingKey(lockOwner, key, successful)) {
        log.failedEndInvalidating(key, cacheName);
    }
    EndInvalidationCommand endInvalidationCommand = commandInitializer.buildEndInvalidationCommand(cacheName, new Object[] { key }, lockOwner);
    List<Address> members = stateTransferManager.getCacheTopology().getMembers();
    rpcManager.invokeRemotely(members, endInvalidationCommand, asyncUnordered);
}
Also used : Address(org.infinispan.remoting.transport.Address) EndInvalidationCommand(org.hibernate.cache.infinispan.util.EndInvalidationCommand)

Example 9 with Address

use of org.infinispan.remoting.transport.Address in project hibernate-orm by hibernate.

the class TxPutFromLoadInterceptor method endInvalidationAndInvokeNextInterceptor.

protected Object endInvalidationAndInvokeNextInterceptor(TxInvocationContext<?> ctx, VisitableCommand command) throws Throwable {
    try {
        if (ctx.isOriginLocal()) {
            // During evictAll inside transaction this would cause unnecessary invalidate command
            if (!ctx.getModifications().isEmpty()) {
                Object[] keys = ctx.getModifications().stream().flatMap(mod -> mod.getAffectedKeys().stream()).distinct().toArray();
                if (log.isTraceEnabled()) {
                    log.tracef("Sending end invalidation for keys %s asynchronously, modifications are %s", Arrays.toString(keys), ctx.getCacheTransaction().getModifications());
                }
                GlobalTransaction globalTransaction = ctx.getGlobalTransaction();
                EndInvalidationCommand commitCommand = cacheCommandInitializer.buildEndInvalidationCommand(cacheName, keys, globalTransaction);
                List<Address> members = stateTransferManager.getCacheTopology().getMembers();
                rpcManager.invokeRemotely(members, commitCommand, asyncUnordered);
                // we have to end invalidation from here manually (in successful case as well)
                for (Object key : keys) {
                    putFromLoadValidator.endInvalidatingKey(globalTransaction, key);
                }
            }
        }
    } finally {
        return invokeNextInterceptor(ctx, command);
    }
}
Also used : Address(org.infinispan.remoting.transport.Address) EndInvalidationCommand(org.hibernate.cache.infinispan.util.EndInvalidationCommand) GlobalTransaction(org.infinispan.transaction.xa.GlobalTransaction)

Example 10 with Address

use of org.infinispan.remoting.transport.Address in project hibernate-orm by hibernate.

the class UnorderedDistributionInterceptor method visitPutKeyValueCommand.

@Override
public Object visitPutKeyValueCommand(InvocationContext ctx, PutKeyValueCommand command) throws Throwable {
    if (command.hasFlag(Flag.CACHE_MODE_LOCAL)) {
        // for state-transfer related writes
        return invokeNextInterceptor(ctx, command);
    }
    int commandTopologyId = command.getTopologyId();
    int currentTopologyId = stateTransferManager.getCacheTopology().getTopologyId();
    if (commandTopologyId != -1 && currentTopologyId != commandTopologyId) {
        throw new OutdatedTopologyException("Cache topology changed while the command was executing: expected " + commandTopologyId + ", got " + currentTopologyId);
    }
    ConsistentHash writeCH = distributionManager.getWriteConsistentHash();
    List<Address> owners = null;
    if (writeCH.isReplicated()) {
        // local result is always ignored
        invokeNextInterceptor(ctx, command);
    } else {
        owners = writeCH.locateOwners(command.getKey());
        if (owners.contains(rpcManager.getAddress())) {
            invokeNextInterceptor(ctx, command);
        } else {
            log.tracef("Not invoking %s on %s since it is not an owner", command, rpcManager.getAddress());
        }
    }
    if (ctx.isOriginLocal() && command.isSuccessful()) {
        // unlocking (and committing) the entry.
        return rpcManager.invokeRemotelyAsync(owners, command, isSynchronous(command) ? syncRpcOptions : asyncRpcOptions);
    }
    return null;
}
Also used : ConsistentHash(org.infinispan.distribution.ch.ConsistentHash) Address(org.infinispan.remoting.transport.Address) OutdatedTopologyException(org.infinispan.statetransfer.OutdatedTopologyException)

Aggregations

Address (org.infinispan.remoting.transport.Address)15 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 Test (org.junit.Test)3 Map (java.util.Map)2 EndInvalidationCommand (org.hibernate.cache.infinispan.util.EndInvalidationCommand)2 DistributionManager (org.infinispan.distribution.DistributionManager)2 ConsistentHash (org.infinispan.distribution.ch.ConsistentHash)2 DataRehashed (org.infinispan.notifications.cachelistener.annotation.DataRehashed)2 CommandDispatcherFactory (org.wildfly.clustering.dispatcher.CommandDispatcherFactory)2 Node (org.wildfly.clustering.group.Node)2 ConsistentHashLocality (org.wildfly.clustering.infinispan.spi.distribution.ConsistentHashLocality)2 Locality (org.wildfly.clustering.infinispan.spi.distribution.Locality)2 SimpleLocality (org.wildfly.clustering.infinispan.spi.distribution.SimpleLocality)2 PrivilegedAction (java.security.PrivilegedAction)1 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1