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);
}
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);
}
}
}
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());
}
}
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);
}
}
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);
}
}
}
Aggregations