Search in sources :

Example 31 with InstanceId

use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.

the class ReadOnlySlaveTest method givenClusterWithReadOnlySlaveWhenAddNewRelTypeOnSlaveThenThrowException.

@Test
public void givenClusterWithReadOnlySlaveWhenAddNewRelTypeOnSlaveThenThrowException() throws Throwable {
    // Given
    ManagedCluster cluster = clusterRule.startCluster();
    Node node;
    Node node2;
    HighlyAvailableGraphDatabase master = cluster.getMaster();
    try (Transaction tx = master.beginTx()) {
        node = master.createNode();
        node2 = master.createNode();
        tx.success();
    }
    // When
    HighlyAvailableGraphDatabase readOnlySlave = cluster.getMemberByServerId(new InstanceId(2));
    try (Transaction tx = readOnlySlave.beginTx()) {
        Node slaveNode = readOnlySlave.getNodeById(node.getId());
        Node slaveNode2 = readOnlySlave.getNodeById(node2.getId());
        // Then
        slaveNode.createRelationshipTo(slaveNode2, RelationshipType.withName("KNOWS"));
        tx.success();
        fail("Should have thrown exception");
    } catch (WriteOperationsNotAllowedException ex) {
    // Ok!
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) HighlyAvailableGraphDatabase(org.neo4j.kernel.ha.HighlyAvailableGraphDatabase) InstanceId(org.neo4j.cluster.InstanceId) ManagedCluster(org.neo4j.kernel.impl.ha.ClusterManager.ManagedCluster) Node(org.neo4j.graphdb.Node) WriteOperationsNotAllowedException(org.neo4j.graphdb.security.WriteOperationsNotAllowedException) Test(org.junit.Test)

Example 32 with InstanceId

use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.

the class ElectionStateTest method shouldSendAtomicBroadcastOnJoiningAClusterWithAnEstablishedCoordinator.

@Test
public void shouldSendAtomicBroadcastOnJoiningAClusterWithAnEstablishedCoordinator() throws Throwable {
    // Given
    String winnerURI = "some://winner";
    InstanceId winner = new InstanceId(2);
    final List<Message<?>> messages = new ArrayList<>(1);
    MessageHolder holder = new MessageHolder() {

        @Override
        public void offer(Message<? extends MessageType> message) {
            messages.add(message);
        }
    };
    ElectionCredentials voteCredentialComparable = mock(ElectionCredentials.class);
    ElectionContext electionContext = mock(ElectionContext.class);
    when(electionContext.voted(eq(COORDINATOR), eq(new InstanceId(1)), eq(voteCredentialComparable), anyLong())).thenReturn(true);
    when(electionContext.getVoteCount(COORDINATOR)).thenReturn(3);
    when(electionContext.getNeededVoteCount()).thenReturn(3);
    when(electionContext.getElectionWinner(COORDINATOR)).thenReturn(winner);
    when(electionContext.getLog(any(Class.class))).thenReturn(NullLog.getInstance());
    when(electionContext.newConfigurationStateChange()).thenReturn(mock(ClusterMessage.VersionedConfigurationStateChange.class));
    when(electionContext.getUriForId(winner)).thenReturn(URI.create(winnerURI));
    // When
    Message<ElectionMessage> votedMessage = Message.to(ElectionMessage.voted, URI.create("some://instance"), new ElectionMessage.VotedData(COORDINATOR, new InstanceId(1), voteCredentialComparable));
    votedMessage.setHeader(Message.FROM, "some://other");
    election.handle(electionContext, votedMessage, holder);
    // Then
    assertEquals(1, messages.size());
    Message<?> message = messages.get(0);
    assertEquals(AtomicBroadcastMessage.broadcast, message.getMessageType());
}
Also used : AtomicBroadcastMessage(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AtomicBroadcastMessage) Message(org.neo4j.cluster.com.message.Message) ClusterMessage(org.neo4j.cluster.protocol.cluster.ClusterMessage) InstanceId(org.neo4j.cluster.InstanceId) ArrayList(java.util.ArrayList) MessageHolder(org.neo4j.cluster.com.message.MessageHolder) MessageType(org.neo4j.cluster.com.message.MessageType) Test(org.junit.Test)

Example 33 with InstanceId

use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.

the class ElectionStateTest method electionShouldRemainLocalIfStartedBySingleInstanceWhichIsTheRoleHolder.

@Test
public void electionShouldRemainLocalIfStartedBySingleInstanceWhichIsTheRoleHolder() throws Throwable {
    /*
         * Ensures that when an instance is alone in the cluster, elections for roles that it holds do not set
         * timeouts or try to reach other instances.
         */
    // Given
    ElectionContext context = mock(ElectionContext.class);
    ClusterContext clusterContextMock = mock(ClusterContext.class);
    when(clusterContextMock.getLog(Matchers.<Class>any())).thenReturn(NullLog.getInstance());
    MessageHolder holder = mock(MessageHolder.class);
    // These mean the election can proceed normally, by us
    when(context.electionOk()).thenReturn(true);
    when(context.isInCluster()).thenReturn(true);
    when(context.isElector()).thenReturn(true);
    // Like it says on the box, we are the only instance
    final InstanceId myInstanceId = new InstanceId(1);
    Map<InstanceId, URI> members = new HashMap<InstanceId, URI>();
    members.put(myInstanceId, URI.create("ha://me"));
    when(context.getMembers()).thenReturn(members);
    // Any role would do, just make sure we have it
    final String role = "master";
    ElectionContext.VoteRequest voteRequest = new ElectionContext.VoteRequest(role, 13);
    when(context.getPossibleRoles()).thenReturn(Collections.<ElectionRole>singletonList(new ElectionRole(role)));
    when(context.getElected(role)).thenReturn(myInstanceId);
    when(context.voteRequestForRole(new ElectionRole(role))).thenReturn(voteRequest);
    // Required for logging
    when(context.getLog(Mockito.<Class>any())).thenReturn(NullLog.getInstance());
    // When
    election.handle(context, Message.<ElectionMessage>internal(performRoleElections), holder);
    // Then
    // Make sure that we asked ourselves to vote for that role and that no timer was set
    verify(holder, times(1)).offer(Matchers.argThat(new MessageArgumentMatcher<ElectionMessage>().onMessageType(ElectionMessage.vote).withPayload(voteRequest)));
    verify(context, times(0)).setTimeout(Matchers.<String>any(), Matchers.<Message>any());
}
Also used : MessageHolder(org.neo4j.cluster.com.message.MessageHolder) MessageArgumentMatcher(org.neo4j.cluster.protocol.MessageArgumentMatcher) InstanceId(org.neo4j.cluster.InstanceId) HashMap(java.util.HashMap) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) URI(java.net.URI) Test(org.junit.Test)

Example 34 with InstanceId

use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.

the class HeartbeatContextTest method testSuspicions.

@Test
public void testSuspicions() {
    InstanceId suspect = instanceIds[1];
    toTest.suspect(suspect);
    assertEquals(Collections.singleton(suspect), toTest.getSuspicionsFor(context.getMyId()));
    assertEquals(Collections.singletonList(context.getMyId()), toTest.getSuspicionsOf(suspect));
    // Being suspected by just one (us) is not enough
    assertFalse(toTest.isFailed(suspect));
    // This resets the suspicion above
    assertTrue(toTest.alive(suspect));
    // If we suspect an instance twice in a row, it shouldn't change its status in any way.
    toTest.suspect(suspect);
    toTest.suspect(suspect);
    assertEquals(Collections.singleton(suspect), toTest.getSuspicionsFor(context.getMyId()));
    assertEquals(Collections.singletonList(context.getMyId()), toTest.getSuspicionsOf(suspect));
    assertFalse(toTest.isFailed(suspect));
    assertTrue(toTest.alive(suspect));
    // The other one sends suspicions too
    InstanceId newSuspiciousBastard = instanceIds[2];
    toTest.suspicions(newSuspiciousBastard, Collections.singleton(suspect));
    toTest.suspect(suspect);
    // Now two instances suspect it, it should be reported failed
    assertEquals(Collections.singleton(suspect), toTest.getSuspicionsFor(context.getMyId()));
    assertEquals(Collections.singleton(suspect), toTest.getSuspicionsFor(newSuspiciousBastard));
    List<InstanceId> suspiciousBastards = new ArrayList<InstanceId>(2);
    suspiciousBastards.add(context.getMyId());
    suspiciousBastards.add(newSuspiciousBastard);
    assertEquals(suspiciousBastards, toTest.getSuspicionsOf(suspect));
    assertTrue(toTest.isFailed(suspect));
    assertTrue(toTest.alive(suspect));
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 35 with InstanceId

use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.

the class HeartbeatContextTest method testFailedInstanceBecomingAlive.

@Test
public void testFailedInstanceBecomingAlive() {
    InstanceId suspect = instanceIds[1];
    InstanceId newSuspiciousBastard = instanceIds[2];
    toTest.suspicions(newSuspiciousBastard, Collections.singleton(suspect));
    toTest.suspect(suspect);
    // Just make sure
    assertTrue(toTest.isFailed(suspect));
    // Ok, here it is. We received a heartbeat, so it is alive.
    toTest.alive(suspect);
    // It must no longer be failed
    assertFalse(toTest.isFailed(suspect));
    // Simulate us stopping receiving heartbeats again
    toTest.suspect(suspect);
    assertTrue(toTest.isFailed(suspect));
    // Assume the other guy started receiving heartbeats first
    toTest.suspicions(newSuspiciousBastard, Collections.<InstanceId>emptySet());
    assertFalse(toTest.isFailed(suspect));
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) 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