Skip to content

Commit

Permalink
Byraft v0.3.0
Browse files Browse the repository at this point in the history
* Remove bundle dependencies / require relative

* Refactor: logger, executor

* Update proto schema

* Edit scripts
  • Loading branch information
taekop committed Aug 1, 2022
1 parent 3b0548e commit 1d607d6
Show file tree
Hide file tree
Showing 16 changed files with 256 additions and 151 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
byraft (0.2.0)
byraft (0.3.0)
grpc (~> 1)

GEM
Expand Down
56 changes: 40 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,66 @@ Byraft is an implementation of Raft consensus algorithm in Ruby using gRPC.
## Install

```shell
bundle install
gem install grpc
```

## Usage

```shell
script/byraft # run server
script/client # request as client
```

## Spec

[raft.proto](proto/raft.proto)

## Example

Three nodes communicates each other with the following configuration.
- `election timeout` between 1 and 2 sec
- `heartbeat period` as 0.1 sec
- `verbose` print DEBUG msg
- Write commited entries in log/log-node-<id>.txt
Configuration

- `election timeout` : between 1 and 2 sec
- `heartbeat period` : 0.1 sec
- Write committed commands in log/log-node-\<id\>.txt
- Nodes
- #1 on localhost:50051
- #2 on localhost:50052
- #3 on localhost:50053
- #1 on localhost:50051
- #2 on localhost:50052
- ...

Options

- `-n <number of nodes>` : default is 3
- `-c <command>` : request command as client
- `-v` : set logger level to DEBUG, otherwise INFO

Run examples in different terminal tabs.
Run servers in different terminals.

```shell
bin/example 1 # terminal 1
bin/example 2 # terminal 2
bin/example 3 # terminal 3
script/example 1 -n 5 # terminal 1
script/example 2 -n 5 # terminal 2
script/example 3 -n 5 # terminal 3
script/example 4 -n 5 # terminal 4
script/example 5 -n 5 # terminal 5
```

Run client to append log.

```shell
bundle exec bin/client localhost:50051 command
script/example 1 -c 'RUN COMMAND' # another terminal
```

## Test

```shell
# rspec
bin/test
bundle install
bundle exec rspec
```

## TODO

- [Liveness in the face of Network Faults](https://decentralizedthoughts.github.io/2020-12-12-raft-liveness-full-omission/)
- Make gem

## Reference

[paper](https://raft.github.io/raft.pdf)
19 changes: 0 additions & 19 deletions bin/example

This file was deleted.

2 changes: 1 addition & 1 deletion byraft.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'byraft'
s.version = '0.2.0'
s.version = '0.3.0'
s.platform = Gem::Platform::RUBY
s.authors = ["taekop"]
s.email = ["taekop@naver.com"]
Expand Down
29 changes: 9 additions & 20 deletions lib/byraft.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
dir = File.expand_path(File.dirname(__FILE__))
$LOAD_PATH.unshift(dir) unless $LOAD_PATH.include?(dir)
require 'byraft/node'

module Byraft
# @param id [String]
# @param port [Integer]
# @param nodes [Hash] id as key, and address as value
# @option election_timeout [Range] Randomize election timeout in range
# @option heartbeat_period [Float] Heartbeat period
Expand All @@ -11,32 +14,18 @@ module Byraft
#
# Byraft.start('1', 50051, { 1 => '0.0.0.0:50051', 2 => '0.0.0.0:50052', 3 => '0.0.0.0:50053' })
def self.start(id, port, nodes, **opts)
logger_level = opts.delete(:logger_level)
node = Node.new(id, nodes, **opts)
node.logger.level = logger_level || 0
address = "localhost:#{port}"
@server_thread = Thread.new do
node.logger.info(node.colorize) { "Running..." }
s = ::GRPC::RpcServer.new
s.add_http2_port(address, :this_port_is_insecure)
s.handle(node)
s.run_till_terminated_or_interrupted(['INT', 'TERM'])
end
@ping_thread = Thread.new do
loop do
sleep(node.heartbeat_period)
node.heartbeat
end
end
logger_level = opts.delete(:logger_level)
@node = Node.new(id, nodes, **opts)
@node.logger.level = logger_level || 0
@node.start(address)
end

def self.join
@server_thread.join
@ping_thread.kill
@node.join
end

def self.stop
@server_thread.kill
@ping_thread.kill
@node.stop
end
end
93 changes: 46 additions & 47 deletions lib/byraft/grpc/byraft_pb.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/byraft/grpc/byraft_services_pb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self.included klass

klass.marshal_class_method = :encode
klass.unmarshal_class_method = :decode
klass.service_name = 'byraft.RaftNode'
klass.service_name = 'raft.Raft'

klass.rpc :AppendEntries, AppendEntriesRequest, AppendEntriesResponse
klass.rpc :RequestVote, RequestVoteRequest, RequestVoteResponse
Expand Down
Loading

0 comments on commit 1d607d6

Please sign in to comment.