Class: Raft::Node
- Inherits:
-
Object
- Object
- Raft::Node
- Defined in:
- lib/raft.rb
Constant Summary collapse
- FOLLOWER_ROLE =
0- CANDIDATE_ROLE =
1- LEADER_ROLE =
2
Instance Attribute Summary collapse
-
#cluster ⇒ Object
readonly
Returns the value of attribute cluster.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#election_timer ⇒ Object
readonly
Returns the value of attribute election_timer.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#persistent_state ⇒ Object
readonly
Returns the value of attribute persistent_state.
-
#role ⇒ Object
readonly
Returns the value of attribute role.
-
#temporary_state ⇒ Object
readonly
Returns the value of attribute temporary_state.
Instance Method Summary collapse
- #handle_append_entries(request) ⇒ Object
- #handle_command(request) ⇒ Object
- #handle_request_vote(request) ⇒ Object
-
#initialize(id, config, cluster) ⇒ Node
constructor
A new instance of Node.
- #update ⇒ Object
Constructor Details
#initialize(id, config, cluster) ⇒ Node
Returns a new instance of Node.
100 101 102 103 104 105 106 107 108 |
# File 'lib/raft.rb', line 100 def initialize(id, config, cluster) @id = id @role = FOLLOWER_ROLE @config = config @cluster = cluster @persistent_state = PersistentState.new(0, nil, []) @temporary_state = TemporaryState.new(nil, nil) @election_timer = Timer.new(config.election_timeout) end |
Instance Attribute Details
#cluster ⇒ Object (readonly)
Returns the value of attribute cluster.
91 92 93 |
# File 'lib/raft.rb', line 91 def cluster @cluster end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
90 91 92 |
# File 'lib/raft.rb', line 90 def config @config end |
#election_timer ⇒ Object (readonly)
Returns the value of attribute election_timer.
94 95 96 |
# File 'lib/raft.rb', line 94 def election_timer @election_timer end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
88 89 90 |
# File 'lib/raft.rb', line 88 def id @id end |
#persistent_state ⇒ Object (readonly)
Returns the value of attribute persistent_state.
92 93 94 |
# File 'lib/raft.rb', line 92 def persistent_state @persistent_state end |
#role ⇒ Object (readonly)
Returns the value of attribute role.
89 90 91 |
# File 'lib/raft.rb', line 89 def role @role end |
#temporary_state ⇒ Object (readonly)
Returns the value of attribute temporary_state.
93 94 95 |
# File 'lib/raft.rb', line 93 def temporary_state @temporary_state end |
Instance Method Details
#handle_append_entries(request) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/raft.rb', line 257 def handle_append_entries(request) response = AppendEntriesResponse.new response.term = @persistent_state.current_term response.success = false return response if request.term < @persistent_state.current_term step_down_if_new_term(request.term) reset_election_timeout @temporary_state.leader_id = request.leader_id abs_log_index = abs_log_index_for(request.prev_log_index, request.prev_log_term) return response if abs_log_index.nil? && !request.prev_log_index.nil? && !request.prev_log_term.nil? raise "Cannot truncate committed logs" if abs_log_index < @temporary_state.commit_index truncate_and_update_log(abs_log_index, request.entries) return response unless update_commit_index(request.commit_index) response.success = true response end |
#handle_command(request) ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/raft.rb', line 283 def handle_command(request) response = CommandResponse.new(false) case @role when FOLLOWER_ROLE response = @config.rpc_provider.command(request, @temporary_state.leader_id) when CANDIDATE_ROLE await_leader response = handle_command(request) when LEADER_ROLE log_entry = LogEntry.new(@persistent_state.current_term, @persistent_state.log.last.index + 1, request.command) @persistent_state.log << log_entry await_consensus(log_entry) end response end |
#handle_request_vote(request) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/raft.rb', line 225 def handle_request_vote(request) response = RequestVoteResponse.new response.term = @persistent_state.current_term response.vote_granted = false return response if request.term < @persistent_state.current_term @temporary_state.leader_id = nil if request.term > @persistent_state.current_term step_down_if_new_term(request.term) if FOLLOWER_ROLE == @role if @persistent_state.voted_for == request.candidate_id response.vote_granted = success elsif @persistent_state.voted_for.nil? if request.last_log_term == @persistent_state.log.last.term && request.last_log_index < @persistent_state.log.last.index # candidate's log is incomplete compared to this node elsif request.last_log_term < @persistent_state.log.last.term # candidate's log is incomplete compared to this node @persistent_state.voted_for = request.candidate_id else @persistent_state.voted_for = request.candidate_id response.vote_granted = true end end reset_election_timeout if response.vote_granted end response end |
#update ⇒ Object
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/raft.rb', line 110 def update case @role when FOLLOWER_ROLE follower_update when CANDIDATE_ROLE candidate_update when LEADER_ROLE leader_update end end |