Search in sources :

Example 6 with ClusterContext

use of org.neo4j.cluster.protocol.cluster.ClusterContext 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 7 with ClusterContext

use of org.neo4j.cluster.protocol.cluster.ClusterContext 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 8 with ClusterContext

use of org.neo4j.cluster.protocol.cluster.ClusterContext 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 9 with ClusterContext

use of org.neo4j.cluster.protocol.cluster.ClusterContext 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)

Example 10 with ClusterContext

use of org.neo4j.cluster.protocol.cluster.ClusterContext in project neo4j by neo4j.

the class MultiPaxosServer method config.

public void config() {
    ClusterConfiguration configuration = ((ClusterContext) server.getStateMachines().getStateMachine(ClusterMessage.class).getContext()).getConfiguration();
    Collection<InstanceId> failed = ((HeartbeatContext) server.getStateMachines().getStateMachine(HeartbeatMessage.class).getContext()).getFailed();
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration)

Aggregations

ClusterContext (org.neo4j.cluster.protocol.cluster.ClusterContext)30 Test (org.junit.Test)27 InstanceId (org.neo4j.cluster.InstanceId)27 ClusterConfiguration (org.neo4j.cluster.protocol.cluster.ClusterConfiguration)17 Executor (java.util.concurrent.Executor)16 ObjectInputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory)16 ObjectOutputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory)16 Timeouts (org.neo4j.cluster.timeout.Timeouts)16 Config (org.neo4j.kernel.configuration.Config)16 URI (java.net.URI)13 HeartbeatContext (org.neo4j.cluster.protocol.heartbeat.HeartbeatContext)13 MessageHolder (org.neo4j.cluster.com.message.MessageHolder)10 LearnerContext (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerContext)9 HashMap (java.util.HashMap)7 AcceptorInstanceStore (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AcceptorInstanceStore)7 MultiPaxosContext (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext)7 Message (org.neo4j.cluster.com.message.Message)6 LinkedList (java.util.LinkedList)4 MessageType (org.neo4j.cluster.com.message.MessageType)4 HashSet (java.util.HashSet)3