Search in sources :

Example 76 with InstanceId

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

the class MultiPaxosServer method start.

public void start() throws IOException {
    broadcastSerializer = new AtomicBroadcastSerializer(new ObjectStreamFactory(), new ObjectStreamFactory());
    final LifeSupport life = new LifeSupport();
    try {
        MessageTimeoutStrategy timeoutStrategy = new MessageTimeoutStrategy(new FixedTimeoutStrategy(5000)).timeout(HeartbeatMessage.sendHeartbeat, 200);
        Monitors monitors = new Monitors();
        NetworkedServerFactory serverFactory = new NetworkedServerFactory(life, new MultiPaxosServerFactory(new ClusterConfiguration("default", NullLogProvider.getInstance()), NullLogProvider.getInstance(), monitors.newMonitor(StateMachines.Monitor.class)), timeoutStrategy, NullLogProvider.getInstance(), new ObjectStreamFactory(), new ObjectStreamFactory(), monitors.newMonitor(NetworkReceiver.Monitor.class), monitors.newMonitor(NetworkSender.Monitor.class), monitors.newMonitor(NamedThreadFactory.Monitor.class));
        ServerIdElectionCredentialsProvider electionCredentialsProvider = new ServerIdElectionCredentialsProvider();
        server = serverFactory.newNetworkedServer(Config.embeddedDefaults(), new InMemoryAcceptorInstanceStore(), electionCredentialsProvider);
        server.addBindingListener(electionCredentialsProvider);
        server.addBindingListener(new BindingListener() {

            @Override
            public void listeningAt(URI me) {
                System.out.println("Listening at:" + me);
            }
        });
        cluster = server.newClient(Cluster.class);
        cluster.addClusterListener(new ClusterListener() {

            @Override
            public void enteredCluster(ClusterConfiguration clusterConfiguration) {
                System.out.println("Entered cluster:" + clusterConfiguration);
            }

            @Override
            public void joinedCluster(InstanceId instanceId, URI member) {
                System.out.println("Joined cluster:" + instanceId + " (at URI " + member + ")");
            }

            @Override
            public void leftCluster(InstanceId instanceId, URI member) {
                System.out.println("Left cluster:" + instanceId);
            }

            @Override
            public void leftCluster() {
                System.out.println("Left cluster");
            }

            @Override
            public void elected(String role, InstanceId instanceId, URI electedMember) {
                System.out.println(instanceId + " at URI " + electedMember + " was elected as " + role);
            }

            @Override
            public void unelected(String role, InstanceId instanceId, URI electedMember) {
                System.out.println(instanceId + " at URI " + electedMember + " was removed from " + role);
            }
        });
        Heartbeat heartbeat = server.newClient(Heartbeat.class);
        heartbeat.addHeartbeatListener(new HeartbeatListener() {

            @Override
            public void failed(InstanceId server) {
                System.out.println(server + " failed");
            }

            @Override
            public void alive(InstanceId server) {
                System.out.println(server + " alive");
            }
        });
        broadcast = server.newClient(AtomicBroadcast.class);
        broadcast.addAtomicBroadcastListener(new AtomicBroadcastListener() {

            @Override
            public void receive(Payload value) {
                try {
                    System.out.println(broadcastSerializer.receive(value));
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
        life.start();
        String command;
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        while (!(command = reader.readLine()).equals("quit")) {
            String[] arguments = command.split(" ");
            Method method = getCommandMethod(arguments[0]);
            if (method != null) {
                String[] realArgs = new String[arguments.length - 1];
                System.arraycopy(arguments, 1, realArgs, 0, realArgs.length);
                try {
                    method.invoke(this, (Object[]) realArgs);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        cluster.leave();
    } finally {
        life.shutdown();
        System.out.println("Done");
    }
}
Also used : FixedTimeoutStrategy(org.neo4j.cluster.timeout.FixedTimeoutStrategy) NetworkedServerFactory(org.neo4j.cluster.NetworkedServerFactory) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) URI(java.net.URI) AtomicBroadcastListener(org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastListener) BindingListener(org.neo4j.cluster.BindingListener) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) ServerIdElectionCredentialsProvider(org.neo4j.cluster.protocol.election.ServerIdElectionCredentialsProvider) AtomicBroadcastSerializer(org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastSerializer) InputStreamReader(java.io.InputStreamReader) InstanceId(org.neo4j.cluster.InstanceId) AtomicBroadcast(org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcast) StateMachines(org.neo4j.cluster.StateMachines) Cluster(org.neo4j.cluster.protocol.cluster.Cluster) IOException(java.io.IOException) Method(java.lang.reflect.Method) ObjectStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectStreamFactory) InvocationTargetException(java.lang.reflect.InvocationTargetException) HeartbeatListener(org.neo4j.cluster.protocol.heartbeat.HeartbeatListener) ClusterListener(org.neo4j.cluster.protocol.cluster.ClusterListener) Heartbeat(org.neo4j.cluster.protocol.heartbeat.Heartbeat) Monitors(org.neo4j.kernel.monitoring.Monitors) MultiPaxosServerFactory(org.neo4j.cluster.MultiPaxosServerFactory) BufferedReader(java.io.BufferedReader) MessageTimeoutStrategy(org.neo4j.cluster.timeout.MessageTimeoutStrategy) Payload(org.neo4j.cluster.protocol.atomicbroadcast.Payload)

Example 77 with InstanceId

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

the class HeartbeatStateTest method shouldIgnoreSuspicionsForOurselvesButKeepTheRest.

@Test
public void shouldIgnoreSuspicionsForOurselvesButKeepTheRest() throws Throwable {
    // Given
    InstanceId myId = new InstanceId(1);
    InstanceId foreignId = new InstanceId(3);
    HeartbeatState heartbeat = HeartbeatState.heartbeat;
    ClusterConfiguration configuration = new ClusterConfiguration("whatever", NullLogProvider.getInstance(), "cluster://1", "cluster://2");
    configuration.joined(myId, URI.create("cluster://1"));
    configuration.joined(new InstanceId(2), URI.create("cluster://2"));
    Config config = mock(Config.class);
    when(config.get(ClusterSettings.max_acceptors)).thenReturn(10);
    MultiPaxosContext context = new MultiPaxosContext(myId, iterable(new ElectionRole("coordinator")), configuration, Mockito.mock(Executor.class), NullLogProvider.getInstance(), Mockito.mock(ObjectInputStreamFactory.class), Mockito.mock(ObjectOutputStreamFactory.class), Mockito.mock(AcceptorInstanceStore.class), Mockito.mock(Timeouts.class), mock(ElectionCredentialsProvider.class), config);
    HeartbeatContext heartbeatContext = context.getHeartbeatContext();
    Message received = Message.internal(HeartbeatMessage.suspicions, new HeartbeatMessage.SuspicionsState(asSet(iterable(myId, foreignId))));
    received.setHeader(Message.FROM, "cluster://2").setHeader(Message.INSTANCE_ID, "2");
    // When
    heartbeat.handle(heartbeatContext, received, mock(MessageHolder.class));
    // Then
    assertThat(heartbeatContext.getSuspicionsOf(myId).size(), equalTo(0));
    assertThat(heartbeatContext.getSuspicionsOf(foreignId).size(), equalTo(1));
}
Also used : LearnerMessage(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerMessage) Message(org.neo4j.cluster.com.message.Message) InstanceId(org.neo4j.cluster.InstanceId) Config(org.neo4j.kernel.configuration.Config) Timeouts(org.neo4j.cluster.timeout.Timeouts) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) ElectionRole(org.neo4j.cluster.protocol.election.ElectionRole) MessageHolder(org.neo4j.cluster.com.message.MessageHolder) Executor(java.util.concurrent.Executor) DelayedDirectExecutor(org.neo4j.cluster.DelayedDirectExecutor) ElectionCredentialsProvider(org.neo4j.cluster.protocol.election.ElectionCredentialsProvider) MultiPaxosContext(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext) ObjectInputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory) ObjectOutputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory) AcceptorInstanceStore(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AcceptorInstanceStore) Test(org.junit.Test)

Example 78 with InstanceId

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

the class SnapshotStateTest method baseNoSendTest.

public void baseNoSendTest(Map<InstanceId, URI> extraMembers) throws Throwable {
    URI me = URI.create("cluster://me");
    Map<InstanceId, URI> members = new HashMap<InstanceId, URI>();
    final InstanceId myId = new InstanceId(1);
    members.put(myId, me);
    members.putAll(extraMembers);
    ClusterConfiguration clusterConfiguration = mock(ClusterConfiguration.class);
    when(clusterConfiguration.getMembers()).thenReturn(members);
    when(clusterConfiguration.getElected(ClusterConfiguration.COORDINATOR)).thenReturn(myId);
    when(clusterConfiguration.getUriForId(myId)).thenReturn(me);
    ClusterContext clusterContext = mock(ClusterContext.class);
    when(clusterContext.getConfiguration()).thenReturn(clusterConfiguration);
    when(clusterContext.getMyId()).thenReturn(myId);
    SnapshotContext context = mock(SnapshotContext.class);
    when(context.getClusterContext()).thenReturn(clusterContext);
    when(context.getSnapshotProvider()).thenReturn(mock(SnapshotProvider.class));
    Message<SnapshotMessage> message = Message.to(SnapshotMessage.refreshSnapshot, me);
    MessageHolder outgoing = mock(MessageHolder.class);
    SnapshotState newState = (SnapshotState) SnapshotState.ready.handle(context, message, outgoing);
    assertThat(newState, equalTo(SnapshotState.ready));
    Mockito.verifyZeroInteractions(outgoing);
}
Also used : MessageHolder(org.neo4j.cluster.com.message.MessageHolder) HashMap(java.util.HashMap) InstanceId(org.neo4j.cluster.InstanceId) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) URI(java.net.URI)

Example 79 with InstanceId

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

the class InstanceIdTest method substituteFailedNode.

@Test
public void substituteFailedNode() throws InterruptedException, ExecutionException, TimeoutException, URISyntaxException {
    List<URI> correctMembers = new ArrayList<>();
    correctMembers.add(URI.create("server1"));
    correctMembers.add(URI.create("server2"));
    correctMembers.add(URI.create("server4"));
    List<URI> wrongMembers = new ArrayList<>();
    wrongMembers.add(URI.create("server1"));
    wrongMembers.add(URI.create("server2"));
    wrongMembers.add(URI.create("server3"));
    Map<String, InstanceId> roles = new HashMap<>();
    roles.put("coordinator", new InstanceId(1));
    Set<InstanceId> clusterMemberFailed = new HashSet<>();
    Set<InstanceId> isolatedMemberFailed = new HashSet<>();
    // will never receive heartbeats again from 1,2 so they are failed
    isolatedMemberFailed.add(new InstanceId(1));
    isolatedMemberFailed.add(new InstanceId(2));
    testCluster(new int[] { 1, 2, 3, 3 }, new VerifyInstanceConfiguration[] { new VerifyInstanceConfiguration(correctMembers, roles, clusterMemberFailed), new VerifyInstanceConfiguration(correctMembers, roles, clusterMemberFailed), new VerifyInstanceConfiguration(wrongMembers, roles, isolatedMemberFailed), new VerifyInstanceConfiguration(correctMembers, roles, clusterMemberFailed) }, DEFAULT_NETWORK(), new ClusterTestScriptDSL().rounds(8000).join(100, 1, 1).join(100, 2, 1).join(100, 3, 1).down(3000, 3).join(1000, 4, 1, 2, 3));
}
Also used : VerifyInstanceConfiguration(org.neo4j.cluster.VerifyInstanceConfiguration) HashMap(java.util.HashMap) InstanceId(org.neo4j.cluster.InstanceId) ArrayList(java.util.ArrayList) URI(java.net.URI) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 80 with InstanceId

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

the class InstanceIdTest method substituteFailedNodeAndFailedComesOnlineAgain.

@Test
public void substituteFailedNodeAndFailedComesOnlineAgain() throws InterruptedException, ExecutionException, TimeoutException, URISyntaxException {
    List<URI> correctMembers = new ArrayList<>();
    correctMembers.add(URI.create("server1"));
    correctMembers.add(URI.create("server2"));
    correctMembers.add(URI.create("server4"));
    List<URI> badMembers = new ArrayList<>();
    badMembers.add(URI.create("server1"));
    badMembers.add(URI.create("server2"));
    badMembers.add(URI.create("server3"));
    Map<String, InstanceId> roles = new HashMap<>();
    roles.put("coordinator", new InstanceId(1));
    // no failures
    Set<InstanceId> clusterMemberFailed = new HashSet<>();
    Set<InstanceId> isolatedMemberFailed = new HashSet<>();
    // will never receive heartbeats again from 1,2 so they are failed
    isolatedMemberFailed.add(new InstanceId(1));
    isolatedMemberFailed.add(new InstanceId(2));
    testCluster(new int[] { 1, 2, 3, 3 }, new VerifyInstanceConfiguration[] { new VerifyInstanceConfiguration(correctMembers, roles, clusterMemberFailed), new VerifyInstanceConfiguration(correctMembers, roles, clusterMemberFailed), new VerifyInstanceConfiguration(badMembers, roles, isolatedMemberFailed), new VerifyInstanceConfiguration(correctMembers, roles, clusterMemberFailed) }, DEFAULT_NETWORK(), new ClusterTestScriptDSL().rounds(800).join(100, 1, 1).join(100, 2, 1).join(100, 3, 1).down(3000, 3).join(1000, 4, 1, 2, 3).up(1000, 3));
}
Also used : VerifyInstanceConfiguration(org.neo4j.cluster.VerifyInstanceConfiguration) HashMap(java.util.HashMap) InstanceId(org.neo4j.cluster.InstanceId) ArrayList(java.util.ArrayList) URI(java.net.URI) HashSet(java.util.HashSet) 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