Search in sources :

Example 6 with Cluster

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

the class ObservedClusterMembersTest method membersShowsAsUnavailableWhenNewMasterElectedBeforeTheyBecomeAvailable.

@Test
public void membersShowsAsUnavailableWhenNewMasterElectedBeforeTheyBecomeAvailable() throws Exception {
    // given
    Cluster cluster = mock(Cluster.class);
    Heartbeat heartbeat = mock(Heartbeat.class);
    ClusterMemberEvents memberEvents = mock(ClusterMemberEvents.class);
    ObservedClusterMembers members = new ObservedClusterMembers(logProvider, cluster, heartbeat, memberEvents, null);
    ArgumentCaptor<ClusterListener> listener = ArgumentCaptor.forClass(ClusterListener.class);
    verify(cluster).addClusterListener(listener.capture());
    listener.getValue().enteredCluster(clusterConfiguration(clusterUri1, clusterUri2, clusterUri3));
    ArgumentCaptor<ClusterMemberListener> memberListener = ArgumentCaptor.forClass(ClusterMemberListener.class);
    verify(memberEvents).addClusterMemberListener(memberListener.capture());
    memberListener.getValue().memberIsAvailable(SLAVE, clusterId1, haUri1, StoreId.DEFAULT);
    // when
    memberListener.getValue().coordinatorIsElected(clusterId2);
    // then
    assertThat(members.getMembers(), hasItem(sameMemberAs(new ClusterMember(clusterId1))));
}
Also used : ClusterMemberEvents(org.neo4j.cluster.member.ClusterMemberEvents) ClusterListener(org.neo4j.cluster.protocol.cluster.ClusterListener) Heartbeat(org.neo4j.cluster.protocol.heartbeat.Heartbeat) Cluster(org.neo4j.cluster.protocol.cluster.Cluster) ClusterMemberListener(org.neo4j.cluster.member.ClusterMemberListener) Test(org.junit.Test)

Example 7 with Cluster

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

the class ObservedClusterMembersTest method failedMemberShowsAsSuch.

@Test
public void failedMemberShowsAsSuch() throws Exception {
    // given
    Cluster cluster = mock(Cluster.class);
    Heartbeat heartbeat = mock(Heartbeat.class);
    ClusterMemberEvents memberEvents = mock(ClusterMemberEvents.class);
    ObservedClusterMembers members = new ObservedClusterMembers(logProvider, cluster, heartbeat, memberEvents, null);
    ArgumentCaptor<ClusterListener> listener = ArgumentCaptor.forClass(ClusterListener.class);
    verify(cluster).addClusterListener(listener.capture());
    listener.getValue().enteredCluster(clusterConfiguration(clusterUri1, clusterUri2, clusterUri3));
    ArgumentCaptor<HeartbeatListener> heartBeatListener = ArgumentCaptor.forClass(HeartbeatListener.class);
    verify(heartbeat).addHeartbeatListener(heartBeatListener.capture());
    // when
    heartBeatListener.getValue().failed(clusterId1);
    // then
    assertThat(members.getMembers(), hasItem(sameMemberAs(new ClusterMember(clusterId1).failed())));
}
Also used : ClusterMemberEvents(org.neo4j.cluster.member.ClusterMemberEvents) HeartbeatListener(org.neo4j.cluster.protocol.heartbeat.HeartbeatListener) ClusterListener(org.neo4j.cluster.protocol.cluster.ClusterListener) Heartbeat(org.neo4j.cluster.protocol.heartbeat.Heartbeat) Cluster(org.neo4j.cluster.protocol.cluster.Cluster) Test(org.junit.Test)

Example 8 with Cluster

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

the class ObservedClusterMembersTest method missingSlaveUnavailabilityEventDoesNotClobberState.

@Test
public void missingSlaveUnavailabilityEventDoesNotClobberState() throws Exception {
    // given
    Cluster cluster = mock(Cluster.class);
    Heartbeat heartbeat = mock(Heartbeat.class);
    ClusterMemberEvents memberEvents = mock(ClusterMemberEvents.class);
    ObservedClusterMembers members = new ObservedClusterMembers(logProvider, cluster, heartbeat, memberEvents, clusterId1);
    ArgumentCaptor<ClusterListener> listener = ArgumentCaptor.forClass(ClusterListener.class);
    verify(cluster).addClusterListener(listener.capture());
    listener.getValue().enteredCluster(clusterConfiguration(clusterUri1, clusterUri2, clusterUri3));
    ArgumentCaptor<ClusterMemberListener> memberListener = ArgumentCaptor.forClass(ClusterMemberListener.class);
    verify(memberEvents).addClusterMemberListener(memberListener.capture());
    // when
    // first we are available as master
    memberListener.getValue().memberIsAvailable(MASTER, clusterId1, haUri1, StoreId.DEFAULT);
    // and then for some reason as slave, without an unavailable message in between
    memberListener.getValue().memberIsAvailable(SLAVE, clusterId1, haUri1, StoreId.DEFAULT);
    // then
    assertThat(members.getCurrentMember().getHARole(), equalTo(SLAVE));
}
Also used : ClusterMemberEvents(org.neo4j.cluster.member.ClusterMemberEvents) ClusterListener(org.neo4j.cluster.protocol.cluster.ClusterListener) Heartbeat(org.neo4j.cluster.protocol.heartbeat.Heartbeat) Cluster(org.neo4j.cluster.protocol.cluster.Cluster) ClusterMemberListener(org.neo4j.cluster.member.ClusterMemberListener) Test(org.junit.Test)

Example 9 with Cluster

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

the class ClusterJoin method joinByConfig.

private void joinByConfig() throws TimeoutException {
    List<HostnamePort> hosts = config.getInitialHosts();
    cluster.addClusterListener(new UnknownJoiningMemberWarning(hosts));
    if (hosts == null || hosts.size() == 0) {
        userLog.info("No cluster hosts specified. Creating cluster %s", config.getClusterName());
        cluster.create(config.getClusterName());
    } else {
        URI[] memberURIs = hosts.stream().map(member -> URI.create("cluster://" + resolvePortOnlyHost(member))).toArray(URI[]::new);
        while (true) {
            userLog.info("Attempting to join cluster of %s", hosts.toString());
            Future<ClusterConfiguration> clusterConfig = cluster.join(this.config.getClusterName(), memberURIs);
            try {
                ClusterConfiguration clusterConf = config.getClusterJoinTimeout() > 0 ? clusterConfig.get(config.getClusterJoinTimeout(), TimeUnit.MILLISECONDS) : clusterConfig.get();
                userLog.info("Joined cluster: %s", clusterConf);
                return;
            } catch (InterruptedException e) {
                userLog.warn("Could not join cluster, interrupted. Retrying...");
            } catch (ExecutionException e) {
                messagesLog.debug("Could not join cluster " + this.config.getClusterName());
                if (e.getCause() instanceof IllegalStateException) {
                    throw (IllegalStateException) e.getCause();
                }
                if (config.isAllowedToCreateCluster()) {
                    // Failed to join cluster, create new one
                    userLog.info("Could not join cluster of %s", hosts.toString());
                    userLog.info("Creating new cluster with name [%s]...", config.getClusterName());
                    cluster.create(config.getClusterName());
                    break;
                }
                userLog.warn("Could not join cluster, timed out. Retrying...");
            }
        }
    }
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) ProtocolServer(org.neo4j.cluster.ProtocolServer) LifecycleAdapter(org.neo4j.kernel.lifecycle.LifecycleAdapter) Log(org.neo4j.logging.Log) Cluster(org.neo4j.cluster.protocol.cluster.Cluster) Semaphore(java.util.concurrent.Semaphore) TimeoutException(java.util.concurrent.TimeoutException) ClusterListener(org.neo4j.cluster.protocol.cluster.ClusterListener) LogService(org.neo4j.kernel.impl.logging.LogService) UnknownHostException(java.net.UnknownHostException) InetAddress(java.net.InetAddress) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Future(java.util.concurrent.Future) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) HostnamePort(org.neo4j.helpers.HostnamePort) URI(java.net.URI) HostnamePort(org.neo4j.helpers.HostnamePort) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI)

Example 10 with Cluster

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

Aggregations

Cluster (org.neo4j.cluster.protocol.cluster.Cluster)22 Test (org.junit.Test)19 ClusterListener (org.neo4j.cluster.protocol.cluster.ClusterListener)17 Heartbeat (org.neo4j.cluster.protocol.heartbeat.Heartbeat)15 ClusterMemberEvents (org.neo4j.cluster.member.ClusterMemberEvents)14 ClusterMemberListener (org.neo4j.cluster.member.ClusterMemberListener)7 HostnamePort (org.neo4j.helpers.HostnamePort)5 URI (java.net.URI)4 DefaultSlaveFactory (org.neo4j.kernel.ha.com.master.DefaultSlaveFactory)4 SlaveFactory (org.neo4j.kernel.ha.com.master.SlaveFactory)4 Slave (org.neo4j.kernel.ha.com.master.Slave)3 LifeSupport (org.neo4j.kernel.lifecycle.LifeSupport)3 InstanceId (org.neo4j.cluster.InstanceId)2 AtomicBroadcast (org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcast)2 AtomicBroadcastSerializer (org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastSerializer)2 ObjectStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectStreamFactory)2 ClusterConfiguration (org.neo4j.cluster.protocol.cluster.ClusterConfiguration)2 HeartbeatListener (org.neo4j.cluster.protocol.heartbeat.HeartbeatListener)2 FixedTimeoutStrategy (org.neo4j.cluster.timeout.FixedTimeoutStrategy)2 MessageTimeoutStrategy (org.neo4j.cluster.timeout.MessageTimeoutStrategy)2