Search in sources :

Example 71 with Executor

use of java.util.concurrent.Executor in project weave by continuuity.

the class DiscoveryServiceTestBase method testCancelChangeListener.

@Test
public void testCancelChangeListener() throws InterruptedException {
    Map.Entry<DiscoveryService, DiscoveryServiceClient> entry = create();
    DiscoveryService discoveryService = entry.getKey();
    DiscoveryServiceClient discoveryServiceClient = entry.getValue();
    String serviceName = "cancel_listener";
    ServiceDiscovered serviceDiscovered = discoveryServiceClient.discover(serviceName);
    // An executor that delay execute a Runnable. It's for testing race because listener cancel and discovery changes.
    Executor delayExecutor = new Executor() {

        @Override
        public void execute(final Runnable command) {
            Thread t = new Thread() {

                @Override
                public void run() {
                    try {
                        TimeUnit.SECONDS.sleep(2);
                        command.run();
                    } catch (InterruptedException e) {
                        throw Throwables.propagate(e);
                    }
                }
            };
            t.start();
        }
    };
    final BlockingQueue<List<Discoverable>> events = new ArrayBlockingQueue<List<Discoverable>>(10);
    Cancellable cancelWatch = serviceDiscovered.watchChanges(new ServiceDiscovered.ChangeListener() {

        @Override
        public void onChange(ServiceDiscovered serviceDiscovered) {
            events.add(ImmutableList.copyOf(serviceDiscovered));
        }
    }, delayExecutor);
    // Wait for the init event call
    Assert.assertNotNull(events.poll(3, TimeUnit.SECONDS));
    // Register a new service endpoint, wait a short while and then cancel the listener
    register(discoveryService, serviceName, "localhost", 1);
    TimeUnit.SECONDS.sleep(1);
    cancelWatch.cancel();
    // The change listener shouldn't get any event, since the invocation is delayed by the executor.
    Assert.assertNull(events.poll(3, TimeUnit.SECONDS));
}
Also used : Cancellable(com.continuuity.weave.common.Cancellable) Executor(java.util.concurrent.Executor) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Test(org.junit.Test)

Example 72 with Executor

use of java.util.concurrent.Executor in project hadoop by apache.

the class FsDatasetImpl method cacheBlock.

/**
   * Asynchronously attempts to cache a single block via {@link FsDatasetCache}.
   */
private void cacheBlock(String bpid, long blockId) {
    FsVolumeImpl volume;
    String blockFileName;
    long length, genstamp;
    Executor volumeExecutor;
    try (AutoCloseableLock lock = datasetLock.acquire()) {
        ReplicaInfo info = volumeMap.get(bpid, blockId);
        boolean success = false;
        try {
            if (info == null) {
                LOG.warn("Failed to cache block with id " + blockId + ", pool " + bpid + ": ReplicaInfo not found.");
                return;
            }
            if (info.getState() != ReplicaState.FINALIZED) {
                LOG.warn("Failed to cache block with id " + blockId + ", pool " + bpid + ": replica is not finalized; it is in state " + info.getState());
                return;
            }
            try {
                volume = (FsVolumeImpl) info.getVolume();
                if (volume == null) {
                    LOG.warn("Failed to cache block with id " + blockId + ", pool " + bpid + ": volume not found.");
                    return;
                }
            } catch (ClassCastException e) {
                LOG.warn("Failed to cache block with id " + blockId + ": volume was not an instance of FsVolumeImpl.");
                return;
            }
            if (volume.isTransientStorage()) {
                LOG.warn("Caching not supported on block with id " + blockId + " since the volume is backed by RAM.");
                return;
            }
            success = true;
        } finally {
            if (!success) {
                cacheManager.numBlocksFailedToCache.incrementAndGet();
            }
        }
        blockFileName = info.getBlockURI().toString();
        length = info.getVisibleLength();
        genstamp = info.getGenerationStamp();
        volumeExecutor = volume.getCacheExecutor();
    }
    cacheManager.cacheBlock(blockId, bpid, blockFileName, length, genstamp, volumeExecutor);
}
Also used : Executor(java.util.concurrent.Executor) ReplicaInfo(org.apache.hadoop.hdfs.server.datanode.ReplicaInfo) AutoCloseableLock(org.apache.hadoop.util.AutoCloseableLock)

Example 73 with Executor

use of java.util.concurrent.Executor in project neo4j by neo4j.

the class StateMachinesTest method shouldAlwaysAddItsInstanceIdToOutgoingMessages.

@Test
public void shouldAlwaysAddItsInstanceIdToOutgoingMessages() throws Exception {
    InstanceId me = new InstanceId(42);
    final List<Message> sentOut = new LinkedList<Message>();
    /*
         * Lots of setup required. Must have a sender that keeps messages so we can see what the machine sent out.
         * We must have the StateMachines actually delegate the incoming message and retrieve the generated outgoing.
         * That means we need an actual StateMachine with a registered MessageType. And most of those are void
         * methods, which means lots of Answer objects.
         */
    // Given
    MessageSender sender = mock(MessageSender.class);
    // The sender, which adds messages outgoing to the list above.
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            sentOut.addAll((Collection<? extends Message>) invocation.getArguments()[0]);
            return null;
        }
    }).when(sender).process(Matchers.<List<Message<? extends MessageType>>>any());
    StateMachines stateMachines = new StateMachines(NullLogProvider.getInstance(), mock(StateMachines.Monitor.class), mock(MessageSource.class), sender, mock(Timeouts.class), mock(DelayedDirectExecutor.class), new Executor() {

        @Override
        public void execute(Runnable command) {
            command.run();
        }
    }, me);
    // The state machine, which has a TestMessage message type and simply adds a TO header to the messages it
    // is handed to handle.
    StateMachine machine = mock(StateMachine.class);
    when(machine.getMessageType()).then(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return TestMessage.class;
        }
    });
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Message message = (Message) invocation.getArguments()[0];
            MessageHolder holder = (MessageHolder) invocation.getArguments()[1];
            message.setHeader(Message.TO, "to://neverland");
            holder.offer(message);
            return null;
        }
    }).when(machine).handle(any(Message.class), any(MessageHolder.class));
    stateMachines.addStateMachine(machine);
    // When
    stateMachines.process(Message.internal(TestMessage.message1));
    // Then
    assertEquals("StateMachines should not make up messages from thin air", 1, sentOut.size());
    Message sent = sentOut.get(0);
    assertTrue("StateMachines should add the instance-id header", sent.hasHeader(Message.INSTANCE_ID));
    assertEquals("StateMachines should add instance-id header that has the correct value", me.toString(), sent.getHeader(Message.INSTANCE_ID));
}
Also used : Message(org.neo4j.cluster.com.message.Message) MessageSender(org.neo4j.cluster.com.message.MessageSender) Timeouts(org.neo4j.cluster.timeout.Timeouts) StateMachine(org.neo4j.cluster.statemachine.StateMachine) MessageSource(org.neo4j.cluster.com.message.MessageSource) LinkedList(java.util.LinkedList) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) MessageHolder(org.neo4j.cluster.com.message.MessageHolder) Executor(java.util.concurrent.Executor) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Collection(java.util.Collection) MessageType(org.neo4j.cluster.com.message.MessageType) Test(org.junit.Test)

Example 74 with Executor

use of java.util.concurrent.Executor in project neo4j by neo4j.

the class ClusterContextImplTest method electorFailingMustCauseElectorVersionToBeReset.

/*
     * This test ensures that an instance that is marked as failed has its elector version reset. This means that
     * the instance, once it comes back, will still be able to do elections even if it lost state
     */
@Test
public void electorFailingMustCauseElectorVersionToBeReset() throws Exception {
    // Given
    InstanceId me = new InstanceId(1);
    InstanceId elector = new InstanceId(2);
    CommonContextState commonContextState = mock(CommonContextState.class, RETURNS_MOCKS);
    Timeouts timeouts = mock(Timeouts.class);
    Executor executor = mock(Executor.class);
    HeartbeatContext heartbeatContext = mock(HeartbeatContext.class);
    ArgumentCaptor<HeartbeatListener> listenerCaptor = ArgumentCaptor.forClass(HeartbeatListener.class);
    ClusterContext context = new ClusterContextImpl(me, commonContextState, NullLogProvider.getInstance(), timeouts, executor, mock(ObjectOutputStreamFactory.class), mock(ObjectInputStreamFactory.class), mock(LearnerContext.class), heartbeatContext, mock(Config.class));
    verify(heartbeatContext).addHeartbeatListener(listenerCaptor.capture());
    HeartbeatListener theListener = listenerCaptor.getValue();
    // This means instance 2 was the elector at version 8
    context.setLastElector(elector);
    context.setLastElectorVersion(8);
    // When
    theListener.failed(elector);
    // Then
    assertEquals(context.getLastElector(), InstanceId.NONE);
    assertEquals(context.getLastElectorVersion(), ClusterContextImpl.NO_ELECTOR_VERSION);
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) Timeouts(org.neo4j.cluster.timeout.Timeouts) Config(org.neo4j.kernel.configuration.Config) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) LearnerContext(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerContext) HeartbeatListener(org.neo4j.cluster.protocol.heartbeat.HeartbeatListener) Executor(java.util.concurrent.Executor) HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext) ObjectInputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory) ObjectOutputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory) Test(org.junit.Test)

Example 75 with Executor

use of java.util.concurrent.Executor in project neo4j by neo4j.

the class ClusterContextImplTest method shouldUpdateDiscoveryHeaderWithContactingInstances.

@Test
public void shouldUpdateDiscoveryHeaderWithContactingInstances() throws Exception {
    // Given
    InstanceId me = new InstanceId(1);
    InstanceId joiningOne = new InstanceId(2);
    InstanceId joiningTwo = new InstanceId(3);
    CommonContextState commonContextState = mock(CommonContextState.class, RETURNS_MOCKS);
    Timeouts timeouts = mock(Timeouts.class);
    Executor executor = mock(Executor.class);
    HeartbeatContext heartbeatContext = mock(HeartbeatContext.class);
    ClusterContext context = new ClusterContextImpl(me, commonContextState, NullLogProvider.getInstance(), timeouts, executor, mock(ObjectOutputStreamFactory.class), mock(ObjectInputStreamFactory.class), mock(LearnerContext.class), heartbeatContext, mock(Config.class));
    ClusterMessage.ConfigurationRequestState requestOne = mock(ClusterMessage.ConfigurationRequestState.class);
    when(requestOne.getJoiningId()).thenReturn(joiningOne);
    ClusterMessage.ConfigurationRequestState requestTwo = mock(ClusterMessage.ConfigurationRequestState.class);
    when(requestTwo.getJoiningId()).thenReturn(joiningTwo);
    // When
    // Instance 2 contacts us twice and Instance 3 contacts us once
    // discovery headers are random here
    context.addContactingInstance(requestOne, "4, 5");
    context.addContactingInstance(requestOne, "4, 5");
    context.addContactingInstance(requestTwo, "2, 5");
    // Then
    // The discovery header we generate should still contain one copy of each instance
    assertEquals("2,3", context.generateDiscoveryHeader());
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) Timeouts(org.neo4j.cluster.timeout.Timeouts) Config(org.neo4j.kernel.configuration.Config) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) LearnerContext(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerContext) Executor(java.util.concurrent.Executor) HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext) ObjectInputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory) ObjectOutputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory) ClusterMessage(org.neo4j.cluster.protocol.cluster.ClusterMessage) Test(org.junit.Test)

Aggregations

Executor (java.util.concurrent.Executor)302 Test (org.junit.Test)127 ArrayList (java.util.ArrayList)35 CountDownLatch (java.util.concurrent.CountDownLatch)32 IOException (java.io.IOException)29 List (java.util.List)27 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)22 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)17 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 Map (java.util.Map)15 Timeouts (org.neo4j.cluster.timeout.Timeouts)15 ExecutorService (java.util.concurrent.ExecutorService)13 InstanceId (org.neo4j.cluster.InstanceId)13 Config (org.neo4j.kernel.configuration.Config)13 File (java.io.File)12 ObjectInputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory)12 ObjectOutputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory)12 HeartbeatContext (org.neo4j.cluster.protocol.heartbeat.HeartbeatContext)12 InetSocketAddress (java.net.InetSocketAddress)10