Class: Raft::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/raft.rb

Constant Summary collapse

FOLLOWER_ROLE =
0
CANDIDATE_ROLE =
1
LEADER_ROLE =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, config, cluster, commit_handler = nil, &block) ⇒ Node



163
164
165
166
167
168
169
170
171
172
# File 'lib/raft.rb', line 163

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)
  @commit_handler = commit_handler || (block.to_proc if block_given?)
end

Instance Attribute Details

#clusterObject (readonly)

Returns the value of attribute cluster.



154
155
156
# File 'lib/raft.rb', line 154

def cluster
  @cluster
end

#configObject (readonly)

Returns the value of attribute config.



153
154
155
# File 'lib/raft.rb', line 153

def config
  @config
end

#election_timerObject (readonly)

Returns the value of attribute election_timer.



157
158
159
# File 'lib/raft.rb', line 157

def election_timer
  @election_timer
end

#idObject (readonly)

Returns the value of attribute id.



151
152
153
# File 'lib/raft.rb', line 151

def id
  @id
end

#persistent_stateObject (readonly)

Returns the value of attribute persistent_state.



155
156
157
# File 'lib/raft.rb', line 155

def persistent_state
  @persistent_state
end

#roleObject (readonly)

Returns the value of attribute role.



152
153
154
# File 'lib/raft.rb', line 152

def role
  @role
end

#temporary_stateObject (readonly)

Returns the value of attribute temporary_state.



156
157
158
# File 'lib/raft.rb', line 156

def temporary_state
  @temporary_state
end

Instance Method Details

#handle_append_entries(request) ⇒ Object



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
408
409
410
411
412
413
414
# File 'lib/raft.rb', line 383

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

  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



416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/raft.rb', line 416

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



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/raft.rb', line 348

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 @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 && request.last_log_index < @persistent_state.log.last.index
        # candidate's log is incomplete compared to this node
      elsif request.last_log_term && request.last_log_term < @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

#updateObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/raft.rb', line 174

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