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, commit_handler = nil, &block) ⇒ Node
constructor
A new instance of Node.
- #update ⇒ Object
Constructor Details
#initialize(id, config, cluster, commit_handler = nil, &block) ⇒ Node
Returns a new instance of Node.
186 187 188 189 190 191 192 193 194 195 |
# File 'lib/raft.rb', line 186 def initialize(id, config, cluster, commit_handler=nil, &block) @id = id @role = FOLLOWER_ROLE @config = config @cluster = cluster @persistent_state = PersistentState.new @temporary_state = TemporaryState.new(nil, nil) @election_timer = Timer.new(config.election_timeout, config.election_splay) @commit_handler = commit_handler || (block.to_proc if block_given?) end |
Instance Attribute Details
#cluster ⇒ Object (readonly)
Returns the value of attribute cluster.
177 178 179 |
# File 'lib/raft.rb', line 177 def cluster @cluster end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
176 177 178 |
# File 'lib/raft.rb', line 176 def config @config end |
#election_timer ⇒ Object (readonly)
Returns the value of attribute election_timer.
180 181 182 |
# File 'lib/raft.rb', line 180 def election_timer @election_timer end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
174 175 176 |
# File 'lib/raft.rb', line 174 def id @id end |
#persistent_state ⇒ Object (readonly)
Returns the value of attribute persistent_state.
178 179 180 |
# File 'lib/raft.rb', line 178 def persistent_state @persistent_state end |
#role ⇒ Object (readonly)
Returns the value of attribute role.
175 176 177 |
# File 'lib/raft.rb', line 175 def role @role end |
#temporary_state ⇒ Object (readonly)
Returns the value of attribute temporary_state.
179 180 181 |
# File 'lib/raft.rb', line 179 def temporary_state @temporary_state end |
Instance Method Details
#handle_append_entries(request) ⇒ Object
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
# File 'lib/raft.rb', line 409 def handle_append_entries(request) #STDOUT.write("\n\nnode #{@id} handle_append_entries: #{request.entries.pretty_inspect}\n\n") #if request.prev_log_index.nil? response = AppendEntriesResponse.new response.term = @persistent_state.current_term response.success = false #STDOUT.write("\n\nnode #{@id} handle_append_entries for term #{request.term} (current is #{@persistent_state.current_term})\n")# if request.prev_log_index.nil? return response if request.term < @persistent_state.current_term #STDOUT.write("\n\nnode #{@id} handle_append_entries stage 2\n") if request.prev_log_index.nil? 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? #STDOUT.write("\n\nnode #{@id} handle_append_entries stage 3\n") if request.prev_log_index.nil? if @temporary_state.commit_index && abs_log_index && abs_log_index < @temporary_state.commit_index raise "Cannot truncate committed logs; @temporary_state.commit_index = #{@temporary_state.commit_index}; abs_log_index = #{abs_log_index}" end truncate_and_update_log(abs_log_index, request.entries) return response unless update_commit_index(request.commit_index) #STDOUT.write("\n\nnode #{@id} handle_append_entries stage 4\n") if request.prev_log_index.nil? response.success = true response end |
#handle_command(request) ⇒ Object
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
# File 'lib/raft.rb', line 443 def handle_command(request) response = CommandResponse.new(false) case @role when FOLLOWER_ROLE await_leader if @role == LEADER_ROLE handle_command(request) else # forward the command to the leader response = @config.rpc_provider.command(request, @temporary_state.leader_id) end when CANDIDATE_ROLE await_leader response = handle_command(request) when LEADER_ROLE last_log = @persistent_state.log.last log_entry = LogEntry.new(@persistent_state.current_term, last_log.index ? last_log.index + 1 : 0, request.command) @persistent_state.log << log_entry await_consensus(log_entry) response = CommandResponse.new(true) end response end |
#handle_request_vote(request) ⇒ Object
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/raft.rb', line 373 def handle_request_vote(request) #STDOUT.write("\nnode #{@id} handling vote request from #{request.candidate_id} (request.last_log_index: #{request.last_log_index}, vs #{@persistent_state.log.last.index}\n") 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 @persistent_state.log.empty? # this node has no log so it can't be ahead @persistent_state.voted_for = request.candidate_id response.vote_granted = true elsif request.last_log_term == @persistent_state.log.last.term && (request.last_log_index || -1) < @persistent_state.log.last.index # candidate's log is incomplete compared to this node elsif (request.last_log_term || -1) < @persistent_state.log.last.term # candidate's log is incomplete compared to this node 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
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/raft.rb', line 197 def update return if @updating @updating = true indent = "\t" * (@id.to_i % 3) #STDOUT.write("\n\n#{indent}update #{@id}, role #{@role}, log length #{@persistent_state.log.count}\n\n") case @role when FOLLOWER_ROLE follower_update when CANDIDATE_ROLE candidate_update when LEADER_ROLE leader_update end @updating = false end |