use of org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastSerializer in project neo4j by neo4j.
the class NetworkMock method newTestProtocolServer.
protected TestProtocolServer newTestProtocolServer(int serverId, URI serverUri) {
ProtocolServerFactory protocolServerFactory = new MultiPaxosServerFactory(new ClusterConfiguration("default", logService.getInternalLogProvider()), logService.getInternalLogProvider(), monitors.newMonitor(StateMachines.Monitor.class));
ServerIdElectionCredentialsProvider electionCredentialsProvider = new ServerIdElectionCredentialsProvider();
electionCredentialsProvider.listeningAt(serverUri);
TestProtocolServer protocolServer = new TestProtocolServer(logService.getInternalLogProvider(), timeoutStrategy, protocolServerFactory, serverUri, new InstanceId(serverId), new InMemoryAcceptorInstanceStore(), electionCredentialsProvider);
protocolServer.addStateTransitionListener(new StateTransitionLogger(logService.getInternalLogProvider(), new AtomicBroadcastSerializer(new ObjectStreamFactory(), new ObjectStreamFactory())));
return protocolServer;
}
use of org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastSerializer 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.protocol.atomicbroadcast.AtomicBroadcastSerializer in project neo4j by neo4j.
the class MultiPaxosTest method testFailure.
@Test
public void testFailure() throws Exception {
ScriptableNetworkFailureLatencyStrategy networkLatency = new ScriptableNetworkFailureLatencyStrategy();
NetworkMock network = new NetworkMock(NullLogService.getInstance(), new Monitors(), 50, new MultipleFailureLatencyStrategy(networkLatency), new MessageTimeoutStrategy(new FixedTimeoutStrategy(1000)));
List<TestProtocolServer> nodes = new ArrayList<TestProtocolServer>();
TestProtocolServer server = network.addServer(1, URI.create("cluster://server1"));
server.newClient(Cluster.class).create("default");
network.tickUntilDone();
nodes.add(server);
for (int i = 1; i < 3; i++) {
TestProtocolServer protocolServer = network.addServer(i + 1, new URI("cluster://server" + (i + 1)));
protocolServer.newClient(Cluster.class).join("default", new URI("cluster://server1"));
network.tick(10);
nodes.add(protocolServer);
}
final AtomicBroadcast atomicBroadcast = nodes.get(0).newClient(AtomicBroadcast.class);
ObjectStreamFactory objectStreamFactory = new ObjectStreamFactory();
final AtomicBroadcastSerializer serializer = new AtomicBroadcastSerializer(objectStreamFactory, objectStreamFactory);
atomicBroadcast.broadcast(serializer.broadcast(new DaPayload()));
networkLatency.nodeIsDown("cluster://server2");
networkLatency.nodeIsDown("cluster://server3");
atomicBroadcast.broadcast(serializer.broadcast(new DaPayload()));
network.tick(100);
networkLatency.nodeIsUp("cluster://server3");
network.tick(1000);
for (TestProtocolServer node : nodes) {
node.newClient(Cluster.class).leave();
network.tick(10);
}
}
use of org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastSerializer in project neo4j by neo4j.
the class StateTransitionLoggerTest method shouldThrottle.
@Test
public void shouldThrottle() throws Exception {
// Given
AssertableLogProvider logProvider = new AssertableLogProvider(true);
StateTransitionLogger stateLogger = new StateTransitionLogger(logProvider, new AtomicBroadcastSerializer(new ObjectStreamFactory(), new ObjectStreamFactory()));
// When
stateLogger.stateTransition(new StateTransition(entered, Message.internal(join), joining));
stateLogger.stateTransition(new StateTransition(entered, Message.internal(join), joining));
stateLogger.stateTransition(new StateTransition(joining, Message.internal(join), entered));
stateLogger.stateTransition(new StateTransition(entered, Message.internal(join), joining));
// Then
logProvider.assertExactly(inLog(entered.getClass()).debug("ClusterState: entered-[join]->joining"), inLog(joining.getClass()).debug("ClusterState: joining-[join]->entered"), inLog(entered.getClass()).debug("ClusterState: entered-[join]->joining"));
}
use of org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastSerializer in project neo4j by neo4j.
the class NetworkedServerFactory method newNetworkedServer.
public ProtocolServer newNetworkedServer(final Config config, AcceptorInstanceStore acceptorInstanceStore, ElectionCredentialsProvider electionCredentialsProvider) {
final NetworkReceiver receiver = new NetworkReceiver(networkReceiverMonitor, new NetworkReceiver.Configuration() {
@Override
public HostnamePort clusterServer() {
return config.get(ClusterSettings.cluster_server);
}
@Override
public int defaultPort() {
return 5001;
}
@Override
public String name() {
return null;
}
}, logProvider);
final NetworkSender sender = new NetworkSender(networkSenderMonitor, new NetworkSender.Configuration() {
@Override
public int defaultPort() {
return 5001;
}
@Override
public int port() {
return config.get(ClusterSettings.cluster_server).getPort();
}
}, receiver, logProvider);
ExecutorLifecycleAdapter stateMachineExecutor = new ExecutorLifecycleAdapter(() -> Executors.newSingleThreadExecutor(new NamedThreadFactory("State machine", namedThreadFactoryMonitor)));
final ProtocolServer protocolServer = protocolServerFactory.newProtocolServer(config.get(ClusterSettings.server_id), timeoutStrategy, receiver, sender, acceptorInstanceStore, electionCredentialsProvider, stateMachineExecutor, objectInputStreamFactory, objectOutputStreamFactory, config);
receiver.addNetworkChannelsListener(new NetworkReceiver.NetworkChannelsListener() {
private StateTransitionLogger logger;
@Override
public void listeningAt(URI me) {
protocolServer.listeningAt(me);
if (logger == null) {
logger = new StateTransitionLogger(logProvider, new AtomicBroadcastSerializer(objectInputStreamFactory, objectOutputStreamFactory));
protocolServer.addStateTransitionListener(logger);
}
}
@Override
public void channelOpened(URI to) {
}
@Override
public void channelClosed(URI to) {
}
});
life.add(stateMachineExecutor);
// Timeout timer - triggers every 10 ms
life.add(new Lifecycle() {
private ScheduledExecutorService scheduler;
@Override
public void init() throws Throwable {
protocolServer.getTimeouts().tick(System.currentTimeMillis());
}
@Override
public void start() throws Throwable {
scheduler = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("timeout"));
scheduler.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
long now = System.currentTimeMillis();
protocolServer.getTimeouts().tick(now);
}
}, 0, 10, TimeUnit.MILLISECONDS);
}
@Override
public void stop() throws Throwable {
scheduler.shutdownNow();
}
@Override
public void shutdown() throws Throwable {
}
});
// Add this last to ensure that timeout service is setup first
life.add(sender);
life.add(receiver);
return protocolServer;
}
Aggregations