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