use of org.neo4j.cluster.InstanceId 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 org.neo4j.cluster.InstanceId 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());
}
use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.
the class ClusterContextImplTest method electorLeavingTheClusterMustBeRemovedAsElector.
/*
* This test ensures that an instance that cleanly leaves the cluster is no longer assumed to be an elector. This
* has the effect that when it rejoins its elector version will be reset and its results will go through
*/
@Test
public void electorLeavingTheClusterMustBeRemovedAsElector() throws Throwable {
// Given
InstanceId me = new InstanceId(1);
InstanceId elector = new InstanceId(2);
ClusterConfiguration clusterConfiguration = mock(ClusterConfiguration.class);
when(clusterConfiguration.getUriForId(elector)).thenReturn(URI.create("cluster://instance2"));
CommonContextState commonContextState = mock(CommonContextState.class);
when(commonContextState.configuration()).thenReturn(clusterConfiguration);
ClusterContext context = new ClusterContextImpl(me, commonContextState, NullLogProvider.getInstance(), mock(Timeouts.class), mock(Executor.class), mock(ObjectOutputStreamFactory.class), mock(ObjectInputStreamFactory.class), mock(LearnerContext.class), mock(HeartbeatContext.class), mock(Config.class));
// This means instance 2 was the elector at version 8
context.setLastElector(elector);
context.setLastElectorVersion(8);
// When
context.left(elector);
// Then
assertEquals(context.getLastElector(), InstanceId.NONE);
assertEquals(context.getLastElectorVersion(), -1);
}
use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.
the class ClusterContextImplTest method instanceEnteringTheClusterMustBeNotAffectElectorStatusIfItWasNotElectorBefore.
/*
* This test ensures that a joining instance that was not marked as elector before does not affect the
* current elector version. This is the complement of the previous test.
*/
@Test
public void instanceEnteringTheClusterMustBeNotAffectElectorStatusIfItWasNotElectorBefore() throws Exception {
// Given
InstanceId me = new InstanceId(1);
InstanceId elector = new InstanceId(2);
InstanceId other = new InstanceId(3);
CommonContextState commonContextState = mock(CommonContextState.class, RETURNS_MOCKS);
ClusterContext context = new ClusterContextImpl(me, commonContextState, NullLogProvider.getInstance(), mock(Timeouts.class), mock(Executor.class), mock(ObjectOutputStreamFactory.class), mock(ObjectInputStreamFactory.class), mock(LearnerContext.class), mock(HeartbeatContext.class), mock(Config.class));
// This means instance 2 was the elector at version 8
context.setLastElector(elector);
context.setLastElectorVersion(8);
// When
context.joined(other, URI.create("cluster://other"));
// Then
assertEquals(context.getLastElector(), elector);
assertEquals(context.getLastElectorVersion(), 8);
}
use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.
the class ClusterContextImplTest method shouldKeepTrackOfInstancesWeHaveContacted.
@Test
public void shouldKeepTrackOfInstancesWeHaveContacted() 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 two contacts us but we are not in the header
context.addContactingInstance(requestOne, "4, 5");
// Then we haven't contacted instance 2
assertFalse(context.haveWeContactedInstance(requestOne));
// When
// Instance 2 reports that we have contacted it after all
context.addContactingInstance(requestOne, "4, 5, 1");
// Then
assertTrue(context.haveWeContactedInstance(requestOne));
// When
// Instance 3 says we have contacted it
context.addContactingInstance(requestTwo, "2, 5, 1");
// Then
assertTrue(context.haveWeContactedInstance(requestTwo));
// When
// For some reason we are not in the header of 3 in subsequent responses (a delayed one, for example)
context.addContactingInstance(requestTwo, "2, 5");
// Then
// The state should still keep the fact we've contacted it already
assertTrue(context.haveWeContactedInstance(requestTwo));
}
Aggregations