Search in sources :

Example 51 with InstanceId

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);
}
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 52 with InstanceId

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

Example 53 with InstanceId

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);
}
Also used : Executor(java.util.concurrent.Executor) InstanceId(org.neo4j.cluster.InstanceId) Timeouts(org.neo4j.cluster.timeout.Timeouts) Config(org.neo4j.kernel.configuration.Config) HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) ObjectInputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory) LearnerContext(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerContext) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) ObjectOutputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory) Test(org.junit.Test)

Example 54 with InstanceId

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);
}
Also used : Executor(java.util.concurrent.Executor) InstanceId(org.neo4j.cluster.InstanceId) Timeouts(org.neo4j.cluster.timeout.Timeouts) Config(org.neo4j.kernel.configuration.Config) HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) ObjectInputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory) LearnerContext(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerContext) ObjectOutputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory) Test(org.junit.Test)

Example 55 with InstanceId

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

InstanceId (org.neo4j.cluster.InstanceId)154 Test (org.junit.Test)129 URI (java.net.URI)47 Config (org.neo4j.kernel.configuration.Config)37 ClusterConfiguration (org.neo4j.cluster.protocol.cluster.ClusterConfiguration)36 Timeouts (org.neo4j.cluster.timeout.Timeouts)32 Executor (java.util.concurrent.Executor)28 ClusterContext (org.neo4j.cluster.protocol.cluster.ClusterContext)28 ObjectInputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory)27 ObjectOutputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory)27 HeartbeatContext (org.neo4j.cluster.protocol.heartbeat.HeartbeatContext)27 MultiPaxosContext (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext)24 AcceptorInstanceStore (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AcceptorInstanceStore)18 MessageHolder (org.neo4j.cluster.com.message.MessageHolder)16 Message (org.neo4j.cluster.com.message.Message)15 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)13 ClusterMemberAvailability (org.neo4j.cluster.member.ClusterMemberAvailability)13 ClusterMemberEvents (org.neo4j.cluster.member.ClusterMemberEvents)12 ClusterMemberListener (org.neo4j.cluster.member.ClusterMemberListener)12