Search in sources :

Example 6 with CommandDispatcherException

use of org.wildfly.clustering.dispatcher.CommandDispatcherException in project wildfly by wildfly.

the class CommandDispatcherTransport method broadcast.

private void broadcast(Command<Void, CommandDispatcherTransport> command) throws WorkException {
    CommandDispatcher<CommandDispatcherTransport> dispatcher = this.dispatcher;
    ExceptionRunnable<WorkException> task = new ExceptionRunnable<WorkException>() {

        @Override
        public void run() throws WorkException {
            try {
                for (Map.Entry<Node, CompletionStage<Void>> entry : dispatcher.executeOnGroup(command).entrySet()) {
                    // Verify that command executed successfully on all nodes
                    try {
                        entry.getValue().toCompletableFuture().join();
                    } catch (CancellationException e) {
                    // Ignore
                    } catch (CompletionException e) {
                        throw new WorkException(e);
                    }
                }
            } catch (CommandDispatcherException e) {
                throw new WorkException(e);
            }
        }
    };
    this.executor.execute(task);
}
Also used : ExceptionRunnable(org.wildfly.common.function.ExceptionRunnable) CancellationException(java.util.concurrent.CancellationException) CommandDispatcherException(org.wildfly.clustering.dispatcher.CommandDispatcherException) WorkException(javax.resource.spi.work.WorkException) Node(org.wildfly.clustering.group.Node) CompletionException(java.util.concurrent.CompletionException) HashMap(java.util.HashMap) Map(java.util.Map) CompletionStage(java.util.concurrent.CompletionStage)

Example 7 with CommandDispatcherException

use of org.wildfly.clustering.dispatcher.CommandDispatcherException in project wildfly by wildfly.

the class CommandDispatcherTransport method join.

private void join(Membership membership) {
    Map<Node, CompletionStage<Set<Address>>> futures = new HashMap<>();
    for (Node member : membership.getMembers()) {
        if (!this.getOwnAddress().equals(member) && !this.nodes.containsValue(member)) {
            try {
                futures.put(member, this.dispatcher.executeOnMember(new GetWorkManagersCommand(), member));
            } catch (CommandDispatcherException e) {
                ConnectorLogger.ROOT_LOGGER.warn(e.getLocalizedMessage(), e);
            }
        }
    }
    for (Map.Entry<Node, CompletionStage<Set<Address>>> entry : futures.entrySet()) {
        Node member = entry.getKey();
        try {
            Set<Address> addresses = entry.getValue().toCompletableFuture().join();
            for (Address address : addresses) {
                this.join(address, member);
                this.localUpdateLongRunningFree(address, this.getShortRunningFree(address));
                this.localUpdateShortRunningFree(address, this.getShortRunningFree(address));
            }
        } catch (CancellationException e) {
        // Ignore
        } catch (CompletionException e) {
            ConnectorLogger.ROOT_LOGGER.warn(e.getLocalizedMessage(), e);
        }
    }
}
Also used : Address(org.jboss.jca.core.spi.workmanager.Address) HashMap(java.util.HashMap) CommandDispatcherException(org.wildfly.clustering.dispatcher.CommandDispatcherException) CancellationException(java.util.concurrent.CancellationException) Node(org.wildfly.clustering.group.Node) CompletionException(java.util.concurrent.CompletionException) HashMap(java.util.HashMap) Map(java.util.Map) CompletionStage(java.util.concurrent.CompletionStage)

Example 8 with CommandDispatcherException

use of org.wildfly.clustering.dispatcher.CommandDispatcherException in project wildfly by wildfly.

the class ClusterTopologyRetrieverBean method getClusterTopology.

@Override
public ClusterTopology getClusterTopology() {
    try {
        Collection<CompletionStage<String>> responses = this.dispatcher.executeOnGroup(this.command).values();
        List<String> nodes = new ArrayList<>(responses.size());
        for (CompletionStage<String> response : responses) {
            try {
                nodes.add(response.toCompletableFuture().join());
            } catch (CancellationException e) {
            // Ignore
            }
        }
        Node localNode = this.factory.getGroup().getLocalMember();
        String local = this.dispatcher.executeOnMember(this.command, localNode).toCompletableFuture().join();
        responses = this.dispatcher.executeOnGroup(this.command, localNode).values();
        List<String> remote = new ArrayList<>(responses.size());
        for (CompletionStage<String> response : responses) {
            try {
                remote.add(response.toCompletableFuture().join());
            } catch (CancellationException e) {
            // Ignore
            }
        }
        for (CompletionStage<Void> response : this.dispatcher.executeOnGroup(this.exceptionCommand).values()) {
            try {
                response.toCompletableFuture().join();
                throw new IllegalStateException("Exception expected");
            } catch (CancellationException e) {
            // Ignore
            } catch (CompletionException e) {
                e.printStackTrace(System.err);
                assert Exception.class.equals(e.getCause().getClass()) : e.getCause().getClass().getName();
            }
        }
        return new ClusterTopology(nodes, local, remote);
    } catch (CommandDispatcherException e) {
        throw new IllegalStateException(e.getCause());
    }
}
Also used : CommandDispatcherException(org.wildfly.clustering.dispatcher.CommandDispatcherException) Node(org.wildfly.clustering.group.Node) ArrayList(java.util.ArrayList) CancellationException(java.util.concurrent.CancellationException) CompletionException(java.util.concurrent.CompletionException) CommandDispatcherException(org.wildfly.clustering.dispatcher.CommandDispatcherException) CancellationException(java.util.concurrent.CancellationException) CompletionException(java.util.concurrent.CompletionException) CompletionStage(java.util.concurrent.CompletionStage)

Example 9 with CommandDispatcherException

use of org.wildfly.clustering.dispatcher.CommandDispatcherException 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 10 with CommandDispatcherException

use of org.wildfly.clustering.dispatcher.CommandDispatcherException in project wildfly by wildfly.

the class AbstractDistributedSingletonService method providersChanged.

@Override
public synchronized void providersChanged(Set<Node> nodes) {
    Group group = this.registry.get().getGroup();
    List<Node> candidates = new ArrayList<>(group.getMembership().getMembers());
    candidates.retainAll(nodes);
    // Only run election on a single node
    if (candidates.isEmpty() || candidates.get(0).equals(group.getLocalMember())) {
        // 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.name.getCanonicalName(), this.quorum);
        }
        Node elected = quorumMet ? this.electionPolicy.elect(candidates) : null;
        try {
            if (elected != null) {
                // Stop service on every node except elected node
                for (Map.Entry<Node, CompletionStage<Void>> entry : this.dispatcher.executeOnGroup(new StopCommand(), elected).entrySet()) {
                    try {
                        entry.getValue().toCompletableFuture().join();
                    } catch (CancellationException e) {
                        ClusteringServerLogger.ROOT_LOGGER.tracef("Singleton service %s is not installed on %s", this.name.getCanonicalName(), entry.getKey().getName());
                    } catch (CompletionException e) {
                        Throwable cause = e.getCause();
                        if ((cause instanceof IllegalStateException) && (cause.getCause() instanceof ServiceNotFoundException)) {
                            ClusteringServerLogger.ROOT_LOGGER.debugf("Singleton service %s is no longer installed on %s", this.name.getCanonicalName(), entry.getKey().getName());
                        } else {
                            throw e;
                        }
                    }
                }
                // Start service on elected node
                try {
                    this.dispatcher.executeOnMember(new StartCommand(), elected).toCompletableFuture().join();
                } catch (CancellationException e) {
                    ClusteringServerLogger.ROOT_LOGGER.debugf("Singleton service %s could not be started on the elected primary singleton provider (%s) because it left the cluster.  A new primary provider election will take place.", this.name.getCanonicalName(), elected.getName());
                } catch (CompletionException e) {
                    Throwable cause = e.getCause();
                    if ((cause instanceof IllegalStateException) && (cause.getCause() instanceof ServiceNotFoundException)) {
                        ClusteringServerLogger.ROOT_LOGGER.debugf("Service % is no longer installed on the elected primary singleton provider (%s). A new primary provider election will take place.", this.name.getCanonicalName(), elected.getName());
                    } else {
                        throw e;
                    }
                }
            } else {
                if (!quorumMet) {
                    ClusteringServerLogger.ROOT_LOGGER.quorumNotReached(this.name.getCanonicalName(), this.quorum);
                }
                // Stop service on every node
                for (Map.Entry<Node, CompletionStage<Void>> entry : this.dispatcher.executeOnGroup(new StopCommand()).entrySet()) {
                    try {
                        entry.getValue().toCompletableFuture().join();
                    } catch (CancellationException e) {
                        ClusteringServerLogger.ROOT_LOGGER.tracef("Singleton service %s is not installed on %s", this.name.getCanonicalName(), entry.getKey().getName());
                    } catch (CompletionException e) {
                        Throwable cause = e.getCause();
                        if ((cause instanceof IllegalStateException) && (cause.getCause() instanceof ServiceNotFoundException)) {
                            ClusteringServerLogger.ROOT_LOGGER.debugf("Singleton service %s is no longer installed on %s", this.name.getCanonicalName(), entry.getKey().getName());
                        } else {
                            throw e;
                        }
                    }
                }
            }
            if (this.electionListener != null) {
                for (CompletionStage<Void> stage : this.dispatcher.executeOnGroup(new SingletonElectionCommand(candidates, elected)).values()) {
                    try {
                        stage.toCompletableFuture().join();
                    } catch (CancellationException e) {
                    // Ignore
                    }
                }
            }
        } 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) ArrayList(java.util.ArrayList) CancellationException(java.util.concurrent.CancellationException) ServiceNotFoundException(org.jboss.msc.service.ServiceNotFoundException) CompletionException(java.util.concurrent.CompletionException) Map(java.util.Map) CompletionStage(java.util.concurrent.CompletionStage)

Aggregations

CommandDispatcherException (org.wildfly.clustering.dispatcher.CommandDispatcherException)12 CancellationException (java.util.concurrent.CancellationException)9 Node (org.wildfly.clustering.group.Node)9 CompletionStage (java.util.concurrent.CompletionStage)6 Map (java.util.Map)5 CompletionException (java.util.concurrent.CompletionException)5 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 ExecutionException (java.util.concurrent.ExecutionException)4 TimeoutException (java.util.concurrent.TimeoutException)4 Message (org.jgroups.Message)4 RequestOptions (org.jgroups.blocks.RequestOptions)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 Address (org.jgroups.Address)3 ArrayList (java.util.ArrayList)2 WorkException (javax.resource.spi.work.WorkException)2 Rsp (org.jgroups.util.Rsp)2 CommandResponse (org.wildfly.clustering.dispatcher.CommandResponse)2 Group (org.wildfly.clustering.group.Group)2 ExceptionSupplier (org.wildfly.common.function.ExceptionSupplier)2