use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.
the class HeartbeatContextTest method setup.
@Before
public void setup() {
Map<InstanceId, URI> members = new HashMap<InstanceId, URI>();
for (int i = 0; i < instanceIds.length; i++) {
members.put(instanceIds[i], URI.create(initialHosts[i]));
}
ClusterConfiguration config = new ClusterConfiguration("clusterName", NullLogProvider.getInstance(), initialHosts);
config.setMembers(members);
context = mock(ClusterContext.class);
Config configuration = mock(Config.class);
when(configuration.get(ClusterSettings.max_acceptors)).thenReturn(10);
when(context.getConfiguration()).thenReturn(config);
when(context.getMyId()).thenReturn(instanceIds[0]);
MultiPaxosContext context = new MultiPaxosContext(instanceIds[0], Iterables.iterable(new ElectionRole("coordinator")), config, Mockito.mock(Executor.class), NullLogProvider.getInstance(), Mockito.mock(ObjectInputStreamFactory.class), Mockito.mock(ObjectOutputStreamFactory.class), Mockito.mock(AcceptorInstanceStore.class), Mockito.mock(Timeouts.class), mock(ElectionCredentialsProvider.class), configuration);
toTest = context.getHeartbeatContext();
}
use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.
the class HeartbeatContextTest method testOneInstanceComesAliveAfterAllOtherFail.
/**
* Tests the following scenario:
* Instance A (the one this test simulates) sees instance C down. B agrees.
* Instance A sees instance B down.
* Instance C starts responding again.
* Instance A should now consider C alive.
*/
@Test
public void testOneInstanceComesAliveAfterAllOtherFail() {
InstanceId instanceB = instanceIds[1];
InstanceId instanceC = instanceIds[2];
// Both A and B consider C down
toTest.suspect(instanceC);
toTest.suspicions(instanceB, Collections.singleton(instanceC));
assertTrue(toTest.isFailed(instanceC));
// A sees B as down
toTest.suspect(instanceB);
assertTrue(toTest.isFailed(instanceB));
// C starts responding again
assertTrue(toTest.alive(instanceC));
assertFalse(toTest.isFailed(instanceC));
}
use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.
the class HeartbeatIAmAliveProcessorTest method shouldNotGenerateHeartbeatsForSuspicions.
@Test
public void shouldNotGenerateHeartbeatsForSuspicions() throws Exception {
URI to = URI.create("ha://1");
// GIVEN
MessageHolder outgoing = mock(MessageHolder.class);
ClusterContext mockContext = mock(ClusterContext.class);
ClusterConfiguration mockConfiguration = mock(ClusterConfiguration.class);
when(mockConfiguration.getMembers()).thenReturn(new HashMap<InstanceId, URI>() {
{
put(new InstanceId(1), URI.create("ha://1"));
put(new InstanceId(2), URI.create("ha://2"));
}
});
when(mockContext.getConfiguration()).thenReturn(mockConfiguration);
HeartbeatIAmAliveProcessor processor = new HeartbeatIAmAliveProcessor(outgoing, mockContext);
Message incoming = Message.to(HeartbeatMessage.suspicions, to).setHeader(Message.FROM, to.toASCIIString()).setHeader(Message.INSTANCE_ID, "1");
assertEquals(HeartbeatMessage.suspicions, incoming.getMessageType());
// WHEN
processor.process(incoming);
// THEN
verifyZeroInteractions(outgoing);
}
use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.
the class HeartbeatIAmAliveProcessorTest method shouldNotGenerateHeartbeatsForHeartbeats.
@Test
public void shouldNotGenerateHeartbeatsForHeartbeats() throws Exception {
URI to = URI.create("ha://1");
// GIVEN
MessageHolder outgoing = mock(MessageHolder.class);
ClusterContext mockContext = mock(ClusterContext.class);
ClusterConfiguration mockConfiguration = mock(ClusterConfiguration.class);
when(mockConfiguration.getMembers()).thenReturn(new HashMap<InstanceId, URI>() {
{
put(new InstanceId(1), URI.create("ha://1"));
put(new InstanceId(2), URI.create("ha://2"));
}
});
when(mockContext.getConfiguration()).thenReturn(mockConfiguration);
HeartbeatIAmAliveProcessor processor = new HeartbeatIAmAliveProcessor(outgoing, mockContext);
Message incoming = Message.to(HeartbeatMessage.i_am_alive, to).setHeader(Message.FROM, to.toASCIIString()).setHeader(Message.INSTANCE_ID, "1");
assertEquals(HeartbeatMessage.i_am_alive, incoming.getMessageType());
// WHEN
processor.process(incoming);
// THEN
verifyZeroInteractions(outgoing);
}
use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.
the class HeartbeatIAmAliveProcessorTest method shouldRevertToInverseUriLookupIfNoInstanceIdHeader.
/*
* This test is required to ensure compatibility with the previous version. If we fail on non existing INSTANCE_ID
* header then heartbeats may pause during rolling upgrades and cause timeouts, which we don't want.
*/
@Test
public void shouldRevertToInverseUriLookupIfNoInstanceIdHeader() throws Exception {
final List<Message> sentOut = new LinkedList<Message>();
String instance2UriString = "ha://2";
// Given
MessageHolder holder = mock(MessageHolder.class);
// The sender, which adds messages outgoing to the list above.
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
sentOut.add((Message) invocation.getArguments()[0]);
return null;
}
}).when(holder).offer(Matchers.<Message<MessageType>>any());
ClusterContext mockContext = mock(ClusterContext.class);
ClusterConfiguration mockConfiguration = mock(ClusterConfiguration.class);
when(mockConfiguration.getIdForUri(URI.create(instance2UriString))).thenReturn(new InstanceId(2));
when(mockConfiguration.getMembers()).thenReturn(new HashMap<InstanceId, URI>() {
{
put(new InstanceId(1), URI.create("ha://1"));
put(new InstanceId(2), URI.create("ha://2"));
}
});
when(mockContext.getConfiguration()).thenReturn(mockConfiguration);
HeartbeatIAmAliveProcessor processor = new HeartbeatIAmAliveProcessor(holder, mockContext);
Message incoming = Message.to(mock(MessageType.class), URI.create("ha://someAwesomeInstanceInJapan")).setHeader(Message.FROM, instance2UriString);
// WHEN
processor.process(incoming);
// THEN
assertEquals(1, sentOut.size());
assertEquals(HeartbeatMessage.i_am_alive, sentOut.get(0).getMessageType());
assertEquals(new InstanceId(2), ((HeartbeatMessage.IAmAliveState) sentOut.get(0).getPayload()).getServer());
}
Aggregations