use of org.neo4j.cluster.MultiPaxosServerFactory 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");
}
}
use of org.neo4j.cluster.MultiPaxosServerFactory in project neo4j by neo4j.
the class ClusterInstance method newClusterInstance.
public static ClusterInstance newClusterInstance(InstanceId id, URI uri, Monitors monitors, ClusterConfiguration configuration, int maxSurvivableFailedMembers, LogProvider logging) {
MultiPaxosServerFactory factory = new MultiPaxosServerFactory(configuration, logging, monitors.newMonitor(StateMachines.Monitor.class));
ClusterInstanceInput input = new ClusterInstanceInput();
ClusterInstanceOutput output = new ClusterInstanceOutput(uri);
ObjectStreamFactory objStreamFactory = new ObjectStreamFactory();
ProverTimeouts timeouts = new ProverTimeouts(uri);
InMemoryAcceptorInstanceStore acceptorInstances = new InMemoryAcceptorInstanceStore();
Config config = mock(Config.class);
when(config.get(ClusterSettings.max_acceptors)).thenReturn(maxSurvivableFailedMembers);
DelayedDirectExecutor executor = new DelayedDirectExecutor(logging);
final MultiPaxosContext context = new MultiPaxosContext(id, Iterables.<ElectionRole, ElectionRole>iterable(new ElectionRole(ClusterConfiguration.COORDINATOR)), new ClusterConfiguration(configuration.getName(), logging, configuration.getMemberURIs()), executor, logging, objStreamFactory, objStreamFactory, acceptorInstances, timeouts, new DefaultElectionCredentialsProvider(id, new StateVerifierLastTxIdGetter(), new MemberInfoProvider()), config);
context.getClusterContext().setBoundAt(uri);
SnapshotContext snapshotContext = new SnapshotContext(context.getClusterContext(), context.getLearnerContext());
DelayedDirectExecutor taskExecutor = new DelayedDirectExecutor(logging);
ProtocolServer ps = factory.newProtocolServer(id, input, output, DIRECT_EXECUTOR, taskExecutor, timeouts, context, snapshotContext);
return new ClusterInstance(DIRECT_EXECUTOR, logging, factory, ps, context, acceptorInstances, timeouts, input, output, uri);
}
Aggregations