Search in sources :

Example 16 with Node

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

the class SimpleSingletonElectionPolicyTestCase method elect.

@Test
public void elect() {
    Node node1 = mock(Node.class);
    Node node2 = mock(Node.class);
    Node node3 = mock(Node.class);
    List<Node> nodes = Arrays.asList(node1, node2, node3);
    assertSame(node1, new SimpleSingletonElectionPolicy().elect(nodes));
    assertSame(node1, new SimpleSingletonElectionPolicy(0).elect(nodes));
    assertSame(node2, new SimpleSingletonElectionPolicy(1).elect(nodes));
    assertSame(node3, new SimpleSingletonElectionPolicy(2).elect(nodes));
    assertSame(node1, new SimpleSingletonElectionPolicy(3).elect(nodes));
    assertNull(new SimpleSingletonElectionPolicy().elect(Collections.<Node>emptyList()));
}
Also used : Node(org.wildfly.clustering.group.Node) SimpleSingletonElectionPolicy(org.wildfly.clustering.singleton.election.SimpleSingletonElectionPolicy) Test(org.junit.Test)

Example 17 with Node

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

the class ChannelCommandDispatcher method submitOnCluster.

@Override
public <R> Map<Node, Future<R>> submitOnCluster(Command<R, ? super C> command, Node... excludedNodes) throws CommandDispatcherException {
    Map<Node, Future<R>> results = new ConcurrentHashMap<>();
    FutureListener<RspList<R>> listener = future -> {
        try {
            future.get().keySet().stream().map(address -> this.factory.createNode(address)).forEach(node -> results.remove(node));
        } catch (CancellationException e) {
        } catch (ExecutionException e) {
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    };
    Message message = this.createMessage(command);
    RequestOptions options = this.createRequestOptions(excludedNodes);
    try {
        Future<? extends Map<Address, Rsp<R>>> futureResponses = this.dispatcher.castMessageWithFuture(null, message, options, listener);
        Set<Node> excluded = (excludedNodes != null) ? new HashSet<>(Arrays.asList(excludedNodes)) : Collections.<Node>emptySet();
        for (Address address : this.dispatcher.getChannel().getView().getMembers()) {
            Node node = this.factory.createNode(address);
            if (!excluded.contains(node)) {
                Future<R> future = new Future<R>() {

                    @Override
                    public boolean cancel(boolean mayInterruptIfRunning) {
                        return futureResponses.cancel(mayInterruptIfRunning);
                    }

                    @Override
                    public R get() throws InterruptedException, ExecutionException {
                        Map<Address, Rsp<R>> responses = futureResponses.get();
                        Rsp<R> response = responses.get(address);
                        if (response == null) {
                            throw new CancellationException();
                        }
                        return createCommandResponse(response).get();
                    }

                    @Override
                    public R get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
                        Map<Address, Rsp<R>> responses = futureResponses.get(timeout, unit);
                        Rsp<R> response = responses.get(address);
                        if (response == null) {
                            throw new CancellationException();
                        }
                        return createCommandResponse(response).get();
                    }

                    @Override
                    public boolean isCancelled() {
                        return futureResponses.isCancelled();
                    }

                    @Override
                    public boolean isDone() {
                        return futureResponses.isDone();
                    }
                };
                results.put(node, future);
            }
        }
        return results;
    } catch (Exception e) {
        throw new CommandDispatcherException(e);
    }
}
Also used : Arrays(java.util.Arrays) Rsp(org.jgroups.util.Rsp) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) FutureListener(org.jgroups.util.FutureListener) HashSet(java.util.HashSet) CommandDispatcher(org.wildfly.clustering.dispatcher.CommandDispatcher) Command(org.wildfly.clustering.dispatcher.Command) Future(java.util.concurrent.Future) RspFilter(org.jgroups.blocks.RspFilter) RequestOptions(org.jgroups.blocks.RequestOptions) Map(java.util.Map) RspList(org.jgroups.util.RspList) Address(org.jgroups.Address) NodeFactory(org.wildfly.clustering.group.NodeFactory) CancellationException(java.util.concurrent.CancellationException) ResponseMode(org.jgroups.blocks.ResponseMode) CommandResponse(org.wildfly.clustering.dispatcher.CommandResponse) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Addressable(org.wildfly.clustering.server.Addressable) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Message(org.jgroups.Message) Node(org.wildfly.clustering.group.Node) CommandDispatcherException(org.wildfly.clustering.dispatcher.CommandDispatcherException) Collections(java.util.Collections) MessageDispatcher(org.jgroups.blocks.MessageDispatcher) Message(org.jgroups.Message) Address(org.jgroups.Address) RequestOptions(org.jgroups.blocks.RequestOptions) CommandDispatcherException(org.wildfly.clustering.dispatcher.CommandDispatcherException) Node(org.wildfly.clustering.group.Node) RspList(org.jgroups.util.RspList) Rsp(org.jgroups.util.Rsp) TimeoutException(java.util.concurrent.TimeoutException) CancellationException(java.util.concurrent.CancellationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) CommandDispatcherException(org.wildfly.clustering.dispatcher.CommandDispatcherException) CancellationException(java.util.concurrent.CancellationException) Future(java.util.concurrent.Future) TimeUnit(java.util.concurrent.TimeUnit) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ExecutionException(java.util.concurrent.ExecutionException)

Example 18 with Node

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

the class ChannelCommandDispatcherFactory method viewAccepted.

@Override
public void viewAccepted(View view) {
    View oldView = this.view.getAndSet(view);
    if (oldView != null) {
        List<Node> oldNodes = this.getNodes(oldView);
        List<Node> newNodes = this.getNodes(view);
        List<Address> leftMembers = View.leftMembers(oldView, view);
        if (leftMembers != null) {
            this.nodeFactory.invalidate(leftMembers);
        }
        for (Map.Entry<Listener, ExecutorService> entry : this.listeners.entrySet()) {
            try {
                Listener listener = entry.getKey();
                ExecutorService executor = entry.getValue();
                executor.submit(() -> {
                    try {
                        listener.membershipChanged(oldNodes, newNodes, view instanceof MergeView);
                    } catch (Throwable e) {
                        ClusteringLogger.ROOT_LOGGER.warn(e.getLocalizedMessage(), e);
                    }
                });
            } catch (RejectedExecutionException e) {
            // Executor was shutdown
            }
        }
    }
}
Also used : MergeView(org.jgroups.MergeView) MembershipListener(org.jgroups.MembershipListener) Address(org.jgroups.Address) Node(org.wildfly.clustering.group.Node) ExecutorService(java.util.concurrent.ExecutorService) MergeView(org.jgroups.MergeView) View(org.jgroups.View) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 19 with Node

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

the class InfinispanBeanManager method getWeakAffinity.

@Override
public Affinity getWeakAffinity(I id) {
    if (this.cache.getCacheConfiguration().clustering().cacheMode().isClustered()) {
        Node node = this.locatePrimaryOwner(id);
        Map.Entry<String, ?> entry = this.registry.getEntry(node);
        if (entry != null) {
            return new NodeAffinity(entry.getKey());
        }
    }
    return Affinity.NONE;
}
Also used : NodeAffinity(org.jboss.ejb.client.NodeAffinity) Node(org.wildfly.clustering.group.Node) Map(java.util.Map)

Example 20 with Node

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

the class ServiceProviderRetrieverBean method getProviders.

@Override
public Collection<String> getProviders() {
    Set<Node> nodes = this.registration.getProviders();
    List<String> result = new ArrayList<>(nodes.size());
    for (Node node : nodes) {
        result.add(node.getName());
    }
    return result;
}
Also used : Node(org.wildfly.clustering.group.Node) ArrayList(java.util.ArrayList)

Aggregations

Node (org.wildfly.clustering.group.Node)22 Map (java.util.Map)8 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 HashSet (java.util.HashSet)5 Set (java.util.Set)4 Test (org.junit.Test)4 AbstractMap (java.util.AbstractMap)3 ArrayList (java.util.ArrayList)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