Home

Awesome

zraft_lib

Erlang raft consensus protocol implementation .

Supported features:

Erlang Architecture

schema

General Configuration

Example app configuration file.

[{zraft_lib,
     [{snapshot_listener_port,0},
      {election_timeout,500},
      {request_timeout,1000},
      {snapshot_listener_addr,"0,0,0,0"},
      {snapshot_backup,false},
      {log_dir,"./data"},
      {snapshot_dir,"./data"},
      {max_segment_size,10485760},
      {max_log_count,1000}]},
 {lager,
     [{error_logger_hwm,100},
      {error_logger_redirect,true},
      {crash_log_date,"$D0"},
      {crash_log_size,10485760},
      {crash_log_msg_size,65536},
      {handlers,
          [{lager_file_backend,
               [{file,"./log/console.log"},
                {level,info},
                {size,10485760},
                {date,"$D0"},
                {count,5}]},
           {lager_file_backend,
               [{file,"./log/error.log"},
                {level,error},
                {size,10485760},
                {date,"$D0"},
                {count,5}]}]},
      {crash_log,"./log/crash.log"},
      {crash_log_count,5}]},
 {sasl,[{sasl_error_logger,false}]}].

Create and Config RAFT Cluster.

zraft_client:create(Peers,BackEnd).

Parameters:

Possible return values:

Basic operations.

Light Session Object.

Light session object used to track current raft cluster state, e.g. leader,failed peers, etc...

Create session object by PeerID:

zraft_client:light_session(PeerID,FailTimeout,ElectionTimeout).

Parameters:

Possible return values:

Create session by list PeerID:

zraft_client:light_session(PeersList,FailTimeout,ElectionTimeout).

This function will not try read configuration from cluster.

Write operation.

zraft_client:write(PeerID,Data,Timeout).

Parameters:

Return:

Write using session object.

zraft_client:write(LightSessionObj,Data,Timeout).

Parameters:

Return:

WARNING: during this request Data may be applyed to backend module twice.

Read request:

zraft_client:query(PeerID,Query,Timeout).

Parameters:

Return:

Or read data using light session object:

zraft_client:query(LightSessionObj,Query,Timeout).

Return:

Change Configuration:

zraft_client:set_new_conf(Peer,NewPeers,OldPeers,Timeout).

Use Session:

You can create long lived session to RAFT cluster. It can be used triggers and temporary datas.

zraft_session:start_link(PeerOrPeers,SessionTimeout)->{ok,Session}.

If first parameter is PeerID other available Peer will be readed from that Peer.

Write Data and Ephemeral data.

zraft_session:write(Session,Data, Temporary, Timeout).

If Temporary is true then data will be deleted after session wil be expired.

Read Data and Set watchers

zraft_session:query(Session,Query,Watch,Timeout).

Watch is trigger reference that will be triggered after future changes.Trigger will be triggered only once, if you need new trigger you must data again.

Example:

zraft_session:query(S1,1,my_watcher,1000).
%%Result = not_found.
zraft_session:write(S2,{1,2},1000).
receive
     {swatch_trigger,my_watcher,Reason}->
          %%Data changed. Change Reason is data_chaged or leader chaged.
          ok
end.
zraft_session:query(S1,1,my_watcher,1000). %%watch again

##Standalone Server.

You can use it for tests from erlang console.

https://github.com/dreyk/zraft

TODO: