Search in sources :

Example 1 with FutureListener

use of org.jgroups.util.FutureListener in project JGroups by belaban.

the class ExecutingServiceTest2 method testDisconnect.

@Test
public void testDisconnect() throws Exception {
    JChannel channel1 = new JChannel(Util.getTestStack(new CENTRAL_EXECUTOR()));
    JChannel channel2 = new JChannel(Util.getTestStack(new CENTRAL_EXECUTOR()));
    channels.add(channel1);
    channels.add(channel2);
    channel1.connect("test-cluster");
    channel2.connect("test-cluster");
    Util.waitUntilAllChannelsHaveSameView(20000, 1000, channel1, channel2);
    final ExecutionService executionService = new ExecutionService(channel1);
    ExecutionRunner executionRunner1 = new ExecutionRunner(channel1);
    ExecutionRunner executionRunner2 = new ExecutionRunner(channel2);
    Thread runner1 = new Thread(executionRunner1);
    threads.add(runner1);
    runner1.start();
    Thread runner2 = new Thread(executionRunner2);
    threads.add(runner2);
    runner2.start();
    final AtomicInteger submittedTasks = new AtomicInteger();
    final AtomicInteger finishedTasks = new AtomicInteger();
    final FutureListener<Void> listener = future -> {
        finishedTasks.incrementAndGet();
        synchronized (ExecutingServiceTest2.this) {
            ExecutingServiceTest2.this.notify();
        }
    };
    Thread submitter = new Thread(new Runnable() {

        @Override
        public void run() {
            // Two long running tasks that should be sent to each runner
            submit(true);
            submit(true);
            while (!Thread.interrupted()) {
                submit(false);
                // Throttle
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        }

        private void submit(boolean wait) {
            Callable<Void> task = new Wait(wait);
            NotifyingFuture<Void> future = executionService.submit(task);
            submittedTasks.incrementAndGet();
            future.setListener(listener);
        }
    });
    threads.add(submitter);
    submitter.start();
    // Run for 2 seconds
    Thread.sleep(500);
    // Close channel
    channel2.close();
    // Stop submitting
    submitter.interrupt();
    submitter.join();
    // Wait for running tasks to finish
    synchronized (this) {
        int lastFinished = finishedTasks.get();
        while (submittedTasks.get() > finishedTasks.get()) {
            wait(10000);
            if (lastFinished == finishedTasks.get()) {
                assert false : "Tasks still outstanding, none finished in the last 10s";
            }
            lastFinished = finishedTasks.get();
        }
    }
    Assert.assertEquals(submittedTasks.get(), finishedTasks.get(), "Tasks not finished");
}
Also used : NotifyingFuture(org.jgroups.util.NotifyingFuture) Util(org.jgroups.util.Util) ExecutionService(org.jgroups.blocks.executor.ExecutionService) Set(java.util.Set) Test(org.testng.annotations.Test) Callable(java.util.concurrent.Callable) FutureListener(org.jgroups.util.FutureListener) AfterMethod(org.testng.annotations.AfterMethod) Serializable(java.io.Serializable) HashSet(java.util.HashSet) CENTRAL_EXECUTOR(org.jgroups.protocols.CENTRAL_EXECUTOR) Assert(org.testng.Assert) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Global(org.jgroups.Global) ExecutionRunner(org.jgroups.blocks.executor.ExecutionRunner) JChannel(org.jgroups.JChannel) JChannel(org.jgroups.JChannel) CENTRAL_EXECUTOR(org.jgroups.protocols.CENTRAL_EXECUTOR) ExecutionService(org.jgroups.blocks.executor.ExecutionService) Callable(java.util.concurrent.Callable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutionRunner(org.jgroups.blocks.executor.ExecutionRunner) NotifyingFuture(org.jgroups.util.NotifyingFuture) Test(org.testng.annotations.Test)

Example 2 with FutureListener

use of org.jgroups.util.FutureListener 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)

Aggregations

HashSet (java.util.HashSet)2 Set (java.util.Set)2 FutureListener (org.jgroups.util.FutureListener)2 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Callable (java.util.concurrent.Callable)1 CancellationException (java.util.concurrent.CancellationException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Address (org.jgroups.Address)1 Global (org.jgroups.Global)1 JChannel (org.jgroups.JChannel)1