use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.
the class ClusterMockTest method verifyConfigurations.
public void verifyConfigurations(String description) {
logger.getLogger().fine("Verify configurations");
List<URI> members = null;
Map<String, InstanceId> roles = null;
Set<InstanceId> failed = null;
List<AssertionError> errors = new LinkedList<AssertionError>();
List<TestProtocolServer> protocolServers = network.getServers();
for (int j = 0; j < protocolServers.size(); j++) {
StateMachines stateMachines = protocolServers.get(j).getServer().getStateMachines();
State<?, ?> clusterState = stateMachines.getStateMachine(ClusterMessage.class).getState();
if (!clusterState.equals(ClusterState.entered)) {
logger.getLogger().fine("Instance " + (j + 1) + " is not in the cluster (" + clusterState + ")");
continue;
}
ClusterContext context = (ClusterContext) stateMachines.getStateMachine(ClusterMessage.class).getContext();
HeartbeatContext heartbeatContext = (HeartbeatContext) stateMachines.getStateMachine(HeartbeatMessage.class).getContext();
ClusterConfiguration clusterConfiguration = context.getConfiguration();
if (!clusterConfiguration.getMemberURIs().isEmpty()) {
logger.getLogger().fine(" Server " + (j + 1) + ": Cluster:" + clusterConfiguration.getMemberURIs() + ", Roles:" + clusterConfiguration.getRoles() + ", Failed:" + heartbeatContext.getFailed());
if (members == null) {
members = clusterConfiguration.getMemberURIs();
roles = clusterConfiguration.getRoles();
failed = heartbeatContext.getFailed();
} else {
verifyConfigurations(stateMachines, members, roles, failed, errors);
}
}
}
assertEquals(description + ": In:" + in + ", Out:" + out, protocolServers.size(), Iterables.count(Iterables.<Cluster, List<Cluster>>flatten(in, out)));
if (!errors.isEmpty()) {
for (AssertionError error : errors) {
logger.getLogger().severe(error.toString());
}
throw errors.get(0);
}
}
use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.
the class ClusterStateTest method joinDeniedResponseShouldContainRespondersConfiguration.
@Test
public void joinDeniedResponseShouldContainRespondersConfiguration() throws Throwable {
// GIVEN
ClusterContext context = mock(ClusterContext.class);
Map<InstanceId, URI> existingMembers = members(1, 2);
when(context.isCurrentlyAlive(any(InstanceId.class))).thenReturn(true);
when(context.getMembers()).thenReturn(existingMembers);
when(context.getConfiguration()).thenReturn(clusterConfiguration(existingMembers));
when(context.getLog(any(Class.class))).thenReturn(NullLog.getInstance());
TrackingMessageHolder outgoing = new TrackingMessageHolder();
Message<ClusterMessage> message = to(configurationRequest, uri(1), configuration(2)).setHeader(Message.FROM, uri(2).toString());
// WHEN an instance responds to a join request, responding that the joining instance cannot join
ClusterState.entered.handle(context, message, outgoing);
// THEN assert that the responding instance sends its configuration along with the response
Message<ClusterMessage> response = outgoing.single();
assertTrue(response.getPayload() instanceof ConfigurationResponseState);
ConfigurationResponseState responseState = response.getPayload();
assertEquals(existingMembers, responseState.getMembers());
}
use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.
the class ClusterStateTest method shouldNotDenyJoinToInstanceThatRejoinsBeforeTimingOut.
@Test
public void shouldNotDenyJoinToInstanceThatRejoinsBeforeTimingOut() throws Throwable {
// GIVEN
ClusterContext context = mock(ClusterContext.class);
Map<InstanceId, URI> existingMembers = members(1, 2);
when(context.isCurrentlyAlive(id(2))).thenReturn(true);
when(context.getMembers()).thenReturn(existingMembers);
when(context.getConfiguration()).thenReturn(clusterConfiguration(existingMembers));
when(context.getLog(any(Class.class))).thenReturn(NullLog.getInstance());
when(context.getUriForId(id(2))).thenReturn(uri(2));
TrackingMessageHolder outgoing = new TrackingMessageHolder();
Message<ClusterMessage> message = to(configurationRequest, uri(1), configuration(2)).setHeader(Message.FROM, uri(2).toString());
// WHEN the join denial actually takes effect (signaled by a join timeout locally)
ClusterState.entered.handle(context, message, outgoing);
// THEN assert that the failure contains the received configuration
Message<? extends MessageType> response = outgoing.single();
assertEquals(ClusterMessage.configurationResponse, response.getMessageType());
}
use of org.neo4j.cluster.InstanceId 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);
}
use of org.neo4j.cluster.InstanceId 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);
}
Aggregations