<commit_before>require 'rubygems'
require 'bud'
require '../lib/votecounter'
require '../src/serverstate'
require '../src/orderedstatemachine'

# Quite the mouthful, eh?
module StronglyConsistentDistributedStateMachineProto
  state do
    interface input, :execute_command, [:reqid] => [:command]
    interface output, :execute_command_resp [:reqid] => [:command, :new_state]
  end
end

module RaftLog
  include StronglyConsistentDistributedStateMachineProto
  include ServerStateImpl
  import VoteCounterImpl => :vc
  import OrderedStateMachine => :sm


  state do
    channel :append_entries_request_chan, [:@dest, :leader_id, :term, :prev_log_index, :prev_log_term, :entry, :commit_index]
    channel :append_entries_response_chan, [:@dest, :candidate_id, :term, :success]

    table :log, [:term, :index] => [:command]
    table :committed_entries, log.schema
    table :next_indices, [:client_id] => [:next_index]

    # the index of the highest log entry committed so far
    lmax :max_index_committed
    scratch :to_commit, log.schema

    # scratches for master logic
    scratch :prev_index_temp, [:client_id] => [:term, :index]
    scratch :highest_log_entry, log.schema
    scratch :tracked_members, [:client_id]
    scratch :untracked_members, [:client_id]

    # scratches for follower logic
    scratch :append_entry_success, [:leader_id, :command, :prev_term, :prev_index, :commit_index, :success]
  end

  # when log entries are committed, they can be applied to the state machine
  bloom :commit_to_state_machine do
    # find entries ready to commit that have not yet been committed
    to_commit <= (committed_entries * log).outer(:term => :term, :index => :index) do |ce, l|
      l if ce == [nil, nil, nil] and l.index <= max_index_comitted.reveal
    end

    # commit them
    committed_entries <+ to_commit
    sm.execute_command <= to_commit {|tc| [tc.index, tc.command]}
  end

  # LEADER RULES

  # When a leader first comes into power it initializes all
  # next_index values to the index just after the last one in its log
  bloom :bootstrap_leader do
    # get the highest log entry in the log
    highest_log_entry <= log.argmax([:term, :index, :command], :index)
<commit_msg>don't need to store the command twice<commit_after>require 'rubygems'
require 'bud'
require '../lib/votecounter'
require '../src/serverstate'
require '../src/orderedstatemachine'

# Quite the mouthful, eh?
module StronglyConsistentDistributedStateMachineProto
  state do
    interface input, :execute_command, [:reqid] => [:command]
    interface output, :execute_command_resp [:reqid] => [:command, :new_state]
  end
end

module RaftLog
  include StronglyConsistentDistributedStateMachineProto
  include ServerStateImpl
  import VoteCounterImpl => :vc
  import OrderedStateMachine => :sm


  state do
    channel :append_entries_request_chan, [:@dest, :leader_id, :term, :prev_log_index, :prev_log_term, :entry, :commit_index]
    channel :append_entries_response_chan, [:@dest, :candidate_id, :term, :success]

    # the log
    table :log, [:term, :index] => [:command]
    # entries to the log that have been committed
    table :committed_entries, [:term, :index] => []

    # the index of the highest log entry committed so far
    lmax :max_index_committed
    scratch :to_commit, log.schema

    table :next_indices, [:client_id] => [:next_index]



    # scratches for master logic
    scratch :prev_index_temp, [:client_id] => [:term, :index]
    scratch :highest_log_entry, log.schema
    scratch :tracked_members, [:client_id]
    scratch :untracked_members, [:client_id]

    # scratches for follower logic
    scratch :append_entry_success, [:leader_id, :command, :prev_term, :prev_index, :commit_index, :success]
  end

  # when log entries are committed, they can be applied to the state machine
  bloom :commit_to_state_machine do
    # find entries ready to commit that have not yet been committed
    to_commit <= (committed_entries * log).outer(:term => :term, :index => :index) do |ce, l|
      l if ce == [nil, nil] and l.index <= max_index_comitted.reveal
    end

    # commit them
    committed_entries <+ to_commit {|tc| [tc.term, tc.index]}
    sm.execute_command <= to_commit {|tc| [tc.index, tc.command]}
  end

  # LEADER RULES

  # When a leader first comes into power it initializes all
  # next_index values to the index just after the last one in its log
  bloom :bootstrap_leader do
    # get the highest log entry in the log
    highest_log_entry <= log.argmax([:term, :index, :command], :index)

{'fraction_non_alphanumeric': 0.10418502202643172, 'fraction_numerical': 0.0, 'mean_word_length': 4.280232558139535, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '590576', 'n_tokens_mistral': 1473, 'n_tokens_neox': 1439, 'n_words': 449}