Search in sources :

Example 1 with Node

use of org.wildfly.clustering.group.Node in project wildfly by wildfly.

the class CacheServiceProviderRegistry method membershipChanged.

@Override
public void membershipChanged(List<Node> previousMembers, List<Node> members, final boolean merged) {
    if (this.getGroup().isCoordinator()) {
        Set<Node> deadNodes = new HashSet<>(previousMembers);
        deadNodes.removeAll(members);
        Set<Node> newNodes = new HashSet<>(members);
        newNodes.removeAll(previousMembers);
        if (!deadNodes.isEmpty()) {
            try (Batch batch = this.batcher.createBatch()) {
                try (CloseableIterator<Map.Entry<T, Set<Node>>> entries = this.cache.entrySet().iterator()) {
                    while (entries.hasNext()) {
                        Map.Entry<T, Set<Node>> entry = entries.next();
                        Set<Node> nodes = entry.getValue();
                        if (nodes.removeAll(deadNodes)) {
                            entry.setValue(nodes);
                        }
                    }
                }
            }
        }
        if (merged) {
            // Re-assert services for new members following merge since these may have been lost following split
            for (Node node : newNodes) {
                try {
                    Collection<T> services = this.dispatcher.executeOnNode(new GetLocalServicesCommand<>(), node).get();
                    try (Batch batch = this.batcher.createBatch()) {
                        services.forEach(service -> this.register(node, service));
                    }
                } catch (Exception e) {
                    ClusteringServerLogger.ROOT_LOGGER.warn(e.getLocalizedMessage(), e);
                }
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) Node(org.wildfly.clustering.group.Node) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Batch(org.wildfly.clustering.ee.Batch) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AbstractMap(java.util.AbstractMap) HashSet(java.util.HashSet)

Example 2 with Node

use of org.wildfly.clustering.group.Node in project wildfly by wildfly.

the class CacheRegistry method close.

@Override
public void close() {
    this.cache.removeListener(this);
    this.shutdown(this.topologyChangeExecutor);
    Node node = this.getGroup().getLocalNode();
    try (Batch batch = this.batcher.createBatch()) {
        // If this remove fails, the entry will be auto-removed on topology change by the new primary owner
        this.cache.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES, Flag.FAIL_SILENTLY).remove(node);
    } catch (CacheException e) {
        ClusteringLogger.ROOT_LOGGER.warn(e.getLocalizedMessage(), e);
    } finally {
        // Cleanup any unregistered listeners
        this.listeners.values().forEach(executor -> this.shutdown(executor));
        this.listeners.clear();
        this.closeTask.run();
    }
}
Also used : Batch(org.wildfly.clustering.ee.Batch) CacheException(org.infinispan.commons.CacheException) Node(org.wildfly.clustering.group.Node)

Example 3 with Node

use of org.wildfly.clustering.group.Node 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 4 with Node

use of org.wildfly.clustering.group.Node in project wildfly by wildfly.

the class DistributedSingletonService method providersChanged.

@Override
public void providersChanged(Set<Node> nodes) {
    Group group = this.registry.getValue().getGroup();
    List<Node> candidates = group.getNodes();
    candidates.retainAll(nodes);
    // Only run election on a single node
    if (candidates.isEmpty() || candidates.get(0).equals(group.getLocalNode())) {
        // First validate that quorum was met
        int size = candidates.size();
        boolean quorumMet = size >= this.quorum;
        if ((this.quorum > 1) && (size == this.quorum)) {
            // Log fragility of singleton availability
            ClusteringServerLogger.ROOT_LOGGER.quorumJustReached(this.serviceName.getCanonicalName(), this.quorum);
        }
        Node elected = quorumMet ? this.electionPolicy.elect(candidates) : null;
        try {
            if (elected != null) {
                ClusteringServerLogger.ROOT_LOGGER.elected(elected.getName(), this.serviceName.getCanonicalName());
                // Stop service on every node except elected node
                this.dispatcher.executeOnCluster(new StopCommand<>(), elected);
                // Start service on elected node
                this.dispatcher.executeOnNode(new StartCommand<>(), elected);
            } else {
                if (quorumMet) {
                    ClusteringServerLogger.ROOT_LOGGER.noPrimaryElected(this.serviceName.getCanonicalName());
                } else {
                    ClusteringServerLogger.ROOT_LOGGER.quorumNotReached(this.serviceName.getCanonicalName(), this.quorum);
                }
                // Stop service on every node
                this.dispatcher.executeOnCluster(new StopCommand<>());
            }
        } catch (CommandDispatcherException e) {
            throw new IllegalStateException(e);
        }
    }
}
Also used : Group(org.wildfly.clustering.group.Group) CommandDispatcherException(org.wildfly.clustering.dispatcher.CommandDispatcherException) Node(org.wildfly.clustering.group.Node)

Example 5 with Node

use of org.wildfly.clustering.group.Node in project wildfly by wildfly.

the class SocketAddressPreferenceTestCase method test.

@Test
public void test() throws UnknownHostException {
    InetSocketAddress preferredAddress = new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 1);
    InetSocketAddress otherAddress1 = new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 2);
    InetSocketAddress otherAddress2 = new InetSocketAddress(InetAddress.getByName("127.0.0.2"), 1);
    Preference preference = new SocketAddressPreference(preferredAddress);
    Node preferredNode = mock(Node.class);
    Node otherNode1 = mock(Node.class);
    Node otherNode2 = mock(Node.class);
    when(preferredNode.getSocketAddress()).thenReturn(preferredAddress);
    when(otherNode1.getSocketAddress()).thenReturn(otherAddress1);
    when(otherNode2.getSocketAddress()).thenReturn(otherAddress2);
    assertTrue(preference.preferred(preferredNode));
    assertFalse(preference.preferred(otherNode1));
    assertFalse(preference.preferred(otherNode2));
}
Also used : SocketAddressPreference(org.wildfly.clustering.singleton.election.SocketAddressPreference) SocketAddressPreference(org.wildfly.clustering.singleton.election.SocketAddressPreference) Preference(org.wildfly.clustering.singleton.election.Preference) InetSocketAddress(java.net.InetSocketAddress) Node(org.wildfly.clustering.group.Node) Test(org.junit.Test)

Aggregations

Node (org.wildfly.clustering.group.Node)23 Map (java.util.Map)8 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 HashSet (java.util.HashSet)5 ArrayList (java.util.ArrayList)4 Set (java.util.Set)4 Test (org.junit.Test)4 AbstractMap (java.util.AbstractMap)3 HashMap (java.util.HashMap)3 ExecutorService (java.util.concurrent.ExecutorService)3 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 TimeoutException (java.util.concurrent.TimeoutException)3 Batch (org.wildfly.clustering.ee.Batch)3 Group (org.wildfly.clustering.group.Group)3 IOException (java.io.IOException)2 InetSocketAddress (java.net.InetSocketAddress)2 Collections (java.util.Collections)2 List (java.util.List)2 CancellationException (java.util.concurrent.CancellationException)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2