Skip to content

Node & NodeBuilder

xnnyygn edited this page Aug 27, 2018 · 1 revision

Node

Node is the core component in xraft. Service could use node to append log, listen for role change of current node, etc.

Node API

  • registerStateMachine(StateMachine)
  • start()
  • getRoleNameAndLeaderId()
  • addNodeRoleListener(NodeRoleListener)
  • appendLog(byte[])
  • addNode(NodeEndpoint)
  • removeNode(NodeId)
  • stop()

Service should call registerStateMachine before appendLog.

start and stop should be called when service starts or stops.

getRoleNameAndLeaderId is the API to get current role and leader id, or you can add a node role listener.

For service who wants to add or remove node, addNode or removeNode can be used.

NodeBuilder

NodeBuilder is the only way to create Node.

NodeBuilder API

  • NodeBuilder(Collection, NodeId)
  • NodeBuilder(NodeEndpoint)
  • build()
  • setConfig(NodeConfig)
  • setDataDir(String)
  • setStandby(boolean)
  • setWorkerNioEventLoopGroup(NioEventLoopGroup)

Service should provide a list of node endpoints to start as a member of cluster, or only one endpoint to start in standalone mode, in other word, single server mode.

Besides cluster member and standalone mode, there is a standby mode which will skip the leader election when standalone. It maybe useful for a new node to be added to cluster. Service could create NodeBuilder with one node endpoint and set standby to true in NodeBuilder to enable this mode. After receiving the cluster config including self from leader, the new node will be the member of cluster.

xraft uses nio powered by netty as the default rpc mechanism, for service who also use netty as rpc framework, worker event loop group can be reused by setWorkerNioEventLoopGroup using services's worker event loop group.

NodeBuilder Example

Standalone, memory log

Node node = new NodeBuilder(new NodeEndpoint("A", "localhost", 2333)).build();

Cluster, 3 nodes, node A of (A, B, C), file log

Node node = new NodeBuilder(
  Arrays.as(
    new NodeEndpoint("A", "localhost", 2333),
    new NodeEndpoint("B", "localhost", 2334),
    new NodeEndpoint("C", "localhost", 2335)
  ), NodeId.of("A"))
  .setDataDir("/path/to/data/dir")
  .build();

Standby

Node node = new NodeBuilder(new NodeEndpoint("A", "localhost", 2333)).setStandby(true).build();
Clone this wiki locally