Search in sources :

Example 21 with ClusterContext

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

the class ElectionStateTest method testElectionFromDemoteIsRejectedIfNoQuorum.

@Test
public void testElectionFromDemoteIsRejectedIfNoQuorum() throws Throwable {
    ElectionContext context = mock(ElectionContext.class);
    ClusterContext clusterContextMock = mock(ClusterContext.class);
    when(context.electionOk()).thenReturn(false);
    when(clusterContextMock.getLog(Matchers.<Class>any())).thenReturn(NullLog.getInstance());
    when(context.getLog(Matchers.<Class>any())).thenReturn(NullLog.getInstance());
    MessageHolder holder = mock(MessageHolder.class);
    election.handle(context, Message.<ElectionMessage>internal(demote), holder);
    verifyZeroInteractions(holder);
}
Also used : MessageHolder(org.neo4j.cluster.com.message.MessageHolder) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) Test(org.junit.Test)

Example 22 with ClusterContext

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

the class ClusterContextImplTest method instanceEnteringTheClusterMustBeRemovedAsElector.

/*
     * This test ensures that an instance that enters the cluster has its elector version reset. That means that
     * if it was the elector before its version is now reset so results can be applied. This and the previous tests
     * actually perform the same things at different events, one covering for the other.
     */
@Test
public void instanceEnteringTheClusterMustBeRemovedAsElector() throws Exception {
    // Given
    InstanceId me = new InstanceId(1);
    InstanceId elector = new InstanceId(2);
    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(elector, URI.create("cluster://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) ObjectOutputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory) Test(org.junit.Test)

Example 23 with ClusterContext

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

the class ClusterContextImplTest method nonElectorLeavingTheClusterMustNotAffectElectorInformation.

/*
     * This test ensures that an instance that cleanly leaves the cluster but is not the elector has no effect on
     * elector id and last version
     */
@Test
public void nonElectorLeavingTheClusterMustNotAffectElectorInformation() throws Throwable {
    // Given
    InstanceId me = new InstanceId(1);
    InstanceId elector = new InstanceId(2);
    InstanceId other = new InstanceId(3);
    ClusterConfiguration clusterConfiguration = mock(ClusterConfiguration.class);
    when(clusterConfiguration.getUriForId(other)).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(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) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) ObjectOutputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory) Test(org.junit.Test)

Example 24 with ClusterContext

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

the class ClusterContextImplTest method shouldGracefullyHandleEmptyDiscoveryHeader.

@Test
public void shouldGracefullyHandleEmptyDiscoveryHeader() throws Exception {
    // Given
    InstanceId me = new InstanceId(1);
    InstanceId joining = 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);
    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 request = mock(ClusterMessage.ConfigurationRequestState.class);
    when(request.getJoiningId()).thenReturn(joining);
    // When
    // Instance 2 contacts us with a request but it is empty
    context.addContactingInstance(request, "");
    // Then
    // The discovery header we generate should still contain that instance
    assertEquals("2", 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 25 with ClusterContext

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

the class ClusterContextImplTest method nonElectorFailingMustNotCauseElectorVersionToBeReset.

/*
     * 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 nonElectorFailingMustNotCauseElectorVersionToBeReset() 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(new InstanceId(3));
    // Then
    assertEquals(context.getLastElector(), elector);
    assertEquals(context.getLastElectorVersion(), 8);
}
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)

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