2013-04-14 21:37:33 +00:00
package raft
import (
2013-04-16 02:47:59 +00:00
"bufio"
2013-04-14 21:37:33 +00:00
"errors"
"fmt"
"io"
"os"
"reflect"
2013-04-16 04:19:29 +00:00
"sync"
2013-04-14 21:37:33 +00:00
)
//------------------------------------------------------------------------------
//
// Typedefs
//
//------------------------------------------------------------------------------
// A log is a collection of log entries that are persisted to durable storage.
type Log struct {
2013-04-28 22:36:46 +00:00
ApplyFunc func ( Command )
2013-04-28 21:23:21 +00:00
file * os . File
2013-04-14 21:37:33 +00:00
entries [ ] * LogEntry
commitIndex uint64
commandTypes map [ string ] Command
2013-04-28 21:23:21 +00:00
mutex sync . Mutex
2013-04-14 21:37:33 +00:00
}
//------------------------------------------------------------------------------
//
// Constructor
//
//------------------------------------------------------------------------------
// Creates a new log.
func NewLog ( ) * Log {
return & Log {
commandTypes : make ( map [ string ] Command ) ,
}
}
2013-04-28 04:51:17 +00:00
//------------------------------------------------------------------------------
//
// Accessors
//
//------------------------------------------------------------------------------
//--------------------------------------
// Log Indices
//--------------------------------------
// The current index in the log.
func ( l * Log ) CurrentIndex ( ) uint64 {
l . mutex . Lock ( )
defer l . mutex . Unlock ( )
2013-04-28 21:23:21 +00:00
2013-04-28 04:51:17 +00:00
if len ( l . entries ) == 0 {
return 0
}
return l . entries [ len ( l . entries ) - 1 ] . index
}
// The next index in the log.
func ( l * Log ) NextIndex ( ) uint64 {
return l . CurrentIndex ( ) + 1
}
// The last committed index in the log.
func ( l * Log ) CommitIndex ( ) uint64 {
return l . commitIndex
}
//--------------------------------------
// Log Terms
//--------------------------------------
// The current term in the log.
func ( l * Log ) CurrentTerm ( ) uint64 {
l . mutex . Lock ( )
defer l . mutex . Unlock ( )
2013-04-28 21:23:21 +00:00
2013-04-28 04:51:17 +00:00
if len ( l . entries ) == 0 {
return 0
}
return l . entries [ len ( l . entries ) - 1 ] . term
}
2013-04-14 21:37:33 +00:00
//------------------------------------------------------------------------------
//
// Methods
//
//------------------------------------------------------------------------------
//--------------------------------------
// Commands
//--------------------------------------
// Instantiates a new command by type name. Returns an error if the command type
// has not been registered already.
func ( l * Log ) NewCommand ( name string ) ( Command , error ) {
// Find the registered command.
command := l . commandTypes [ name ]
if command == nil {
return nil , fmt . Errorf ( "raft.Log: Unregistered command type: %s" , name )
}
// Make a copy of the command.
2013-04-16 02:47:59 +00:00
v := reflect . New ( reflect . Indirect ( reflect . ValueOf ( command ) ) . Type ( ) ) . Interface ( )
copy , ok := v . ( Command )
2013-04-14 21:37:33 +00:00
if ! ok {
2013-04-28 04:51:17 +00:00
panic ( fmt . Sprintf ( "raft.Log: Unable to copy command: %s (%v)" , command . CommandName ( ) , reflect . ValueOf ( v ) . Kind ( ) . String ( ) ) )
2013-04-14 21:37:33 +00:00
}
return copy , nil
}
// Adds a command type to the log. The instance passed in will be copied and
// deserialized each time a new log entry is read. This function will panic
// if a command type with the same name already exists.
func ( l * Log ) AddCommandType ( command Command ) {
2013-05-01 05:11:23 +00:00
l . mutex . Lock ( )
defer l . mutex . Unlock ( )
2013-04-14 21:37:33 +00:00
if command == nil {
panic ( fmt . Sprintf ( "raft.Log: Command type cannot be nil" ) )
2013-04-28 04:51:17 +00:00
} else if l . commandTypes [ command . CommandName ( ) ] != nil {
panic ( fmt . Sprintf ( "raft.Log: Command type already exists: %s" , command . CommandName ( ) ) )
2013-04-14 21:37:33 +00:00
}
2013-04-28 04:51:17 +00:00
l . commandTypes [ command . CommandName ( ) ] = command
2013-04-14 21:37:33 +00:00
}
//--------------------------------------
// State
//--------------------------------------
// Opens the log file and reads existing entries. The log can remain open and
// continue to append entries to the end of the log.
func ( l * Log ) Open ( path string ) error {
2013-04-16 04:19:29 +00:00
l . mutex . Lock ( )
defer l . mutex . Unlock ( )
2013-04-28 21:23:21 +00:00
2013-04-14 21:37:33 +00:00
// Read all the entries from the log if one exists.
2013-04-16 04:02:08 +00:00
var lastIndex int = 0
2013-04-14 21:37:33 +00:00
if _ , err := os . Stat ( path ) ; ! os . IsNotExist ( err ) {
// Open the log file.
file , err := os . Open ( path )
if err != nil {
return err
}
defer file . Close ( )
2013-04-16 02:47:59 +00:00
reader := bufio . NewReader ( file )
2013-04-28 21:23:21 +00:00
2013-04-14 21:37:33 +00:00
// Read the file and decode entries.
2013-04-16 02:47:59 +00:00
for {
if _ , err := reader . Peek ( 1 ) ; err == io . EOF {
break
}
2013-04-14 21:37:33 +00:00
// Instantiate log entry and decode into it.
entry := NewLogEntry ( l , 0 , 0 , nil )
2013-04-16 04:02:08 +00:00
n , err := entry . Decode ( reader )
2013-04-16 02:47:59 +00:00
if err != nil {
2013-04-16 04:02:08 +00:00
warn ( "raft.Log: %v" , err )
warn ( "raft.Log: Recovering (%d)" , lastIndex )
file . Close ( )
if err = os . Truncate ( path , int64 ( lastIndex ) ) ; err != nil {
return fmt . Errorf ( "raft.Log: Unable to recover: %v" , err )
}
break
2013-04-14 21:37:33 +00:00
}
2013-04-16 04:19:29 +00:00
l . commitIndex = entry . index
2013-04-16 04:02:08 +00:00
lastIndex += n
2013-04-14 21:37:33 +00:00
// Append entry.
l . entries = append ( l . entries , entry )
}
2013-04-16 02:47:59 +00:00
file . Close ( )
2013-04-14 21:37:33 +00:00
}
// Open the file for appending.
var err error
2013-04-28 21:23:21 +00:00
l . file , err = os . OpenFile ( path , os . O_APPEND | os . O_CREATE | os . O_WRONLY , 0600 )
2013-04-14 21:37:33 +00:00
if err != nil {
return err
}
return nil
}
// Closes the log file.
func ( l * Log ) Close ( ) {
2013-04-16 04:19:29 +00:00
l . mutex . Lock ( )
defer l . mutex . Unlock ( )
2013-04-14 21:37:33 +00:00
if l . file != nil {
l . file . Close ( )
l . file = nil
}
l . entries = make ( [ ] * LogEntry , 0 )
}
//--------------------------------------
2013-04-28 04:51:17 +00:00
// Entries
2013-04-14 21:37:33 +00:00
//--------------------------------------
2013-04-28 04:51:17 +00:00
// Creates a log entry associated with this log.
2013-04-28 21:23:21 +00:00
func ( l * Log ) CreateEntry ( term uint64 , command Command ) * LogEntry {
2013-04-28 04:51:17 +00:00
return NewLogEntry ( l , l . NextIndex ( ) , term , command )
}
2013-04-30 04:13:50 +00:00
// Checks if the log contains a given index/term combination.
func ( l * Log ) ContainsEntry ( index uint64 , term uint64 ) bool {
l . mutex . Lock ( )
defer l . mutex . Unlock ( )
if index == 0 || index > uint64 ( len ( l . entries ) ) {
return false
}
return ( l . entries [ index - 1 ] . term == term )
}
2013-05-05 19:36:23 +00:00
// Retrieves a list of entries after a given index. This function also returns
// the term of the index provided.
func ( l * Log ) GetEntriesAfter ( index uint64 ) ( [ ] * LogEntry , uint64 ) {
l . mutex . Lock ( )
defer l . mutex . Unlock ( )
// Return an error if the index doesn't exist.
if index > uint64 ( len ( l . entries ) ) {
panic ( fmt . Sprintf ( "raft.Log: Index is beyond end of log: %v" , index ) )
}
// If we're going from the beginning of the log then return the whole log.
if index == 0 {
return l . entries , 0
}
2013-05-05 20:26:04 +00:00
2013-05-05 19:36:23 +00:00
// Determine the term at the given entry and return a subslice.
term := l . entries [ index - 1 ] . term
return l . entries [ index : ] , term
}
2013-04-28 22:36:46 +00:00
//--------------------------------------
// Commit
//--------------------------------------
// Retrieves the last index and term that has been committed to the log.
func ( l * Log ) CommitInfo ( ) ( index uint64 , term uint64 ) {
l . mutex . Lock ( )
defer l . mutex . Unlock ( )
// If we don't have any entries then just return zeros.
if l . commitIndex == 0 {
return 0 , 0
}
// Return the last index & term from the last committed entry.
lastCommitEntry := l . entries [ l . commitIndex - 1 ]
return lastCommitEntry . index , lastCommitEntry . term
}
// Updates the commit index and writes entries after that index to the stable storage.
2013-04-16 04:19:29 +00:00
func ( l * Log ) SetCommitIndex ( index uint64 ) error {
l . mutex . Lock ( )
defer l . mutex . Unlock ( )
2013-04-28 22:36:46 +00:00
// Panic if we don't have any way to apply commands.
if l . ApplyFunc == nil {
panic ( "raft.Log: Apply function not set" )
}
2013-04-16 04:19:29 +00:00
// Do not allow previous indices to be committed again.
if index < l . commitIndex {
return fmt . Errorf ( "raft.Log: Commit index (%d) ahead of requested commit index (%d)" , l . commitIndex , index )
}
2013-04-28 22:36:46 +00:00
if index > uint64 ( len ( l . entries ) ) {
return fmt . Errorf ( "raft.Log: Commit index (%d) out of range (%d)" , index , len ( l . entries ) )
}
2013-04-16 04:19:29 +00:00
// Find all entries whose index is between the previous index and the current index.
2013-04-28 22:36:46 +00:00
for i := l . commitIndex + 1 ; i <= index ; i ++ {
entry := l . entries [ i - 1 ]
2013-04-28 21:23:21 +00:00
2013-04-28 22:36:46 +00:00
// Write to storage.
if err := entry . Encode ( l . file ) ; err != nil {
return err
2013-04-16 04:19:29 +00:00
}
2013-04-28 22:36:46 +00:00
// Apply the changes to the state machine.
l . ApplyFunc ( entry . command )
// Update commit index.
l . commitIndex = entry . index
2013-04-16 04:19:29 +00:00
}
2013-04-28 21:23:21 +00:00
2013-04-16 04:19:29 +00:00
return nil
}
2013-05-01 04:44:16 +00:00
//--------------------------------------
// Truncation
//--------------------------------------
// Truncates the log to the given index and term. This only works if the log
// at the index has not been committed.
func ( l * Log ) Truncate ( index uint64 , term uint64 ) error {
l . mutex . Lock ( )
defer l . mutex . Unlock ( )
2013-05-01 05:11:23 +00:00
2013-05-01 04:44:16 +00:00
// Do not allow committed entries to be truncated.
if index < l . CommitIndex ( ) {
return fmt . Errorf ( "raft.Log: Index is already committed (%v): (IDX=%v, TERM=%v)" , l . CommitIndex ( ) , index , term )
}
// Do not truncate past end of entries.
if index > uint64 ( len ( l . entries ) ) {
return fmt . Errorf ( "raft.Log: Entry index does not exist (MAX=%v): (IDX=%v, TERM=%v)" , len ( l . entries ) , index , term )
}
// If we're truncating everything then just clear the entries.
if index == 0 {
l . entries = [ ] * LogEntry { }
} else {
// Do not truncate if the entry at index does not have the matching term.
entry := l . entries [ index - 1 ]
if len ( l . entries ) > 0 && entry . term != term {
return fmt . Errorf ( "raft.Log: Entry at index does not have matching term (%v): (IDX=%v, TERM=%v)" , entry . term , index , term )
}
// Otherwise truncate up to the desired entry.
if index < uint64 ( len ( l . entries ) ) {
l . entries = l . entries [ 0 : index ]
}
}
2013-05-01 05:11:23 +00:00
2013-05-01 04:44:16 +00:00
return nil
}
2013-04-16 04:19:29 +00:00
//--------------------------------------
// Append
//--------------------------------------
2013-04-30 04:13:50 +00:00
// Appends a series of entries to the log. These entries are not written to
// disk until SetCommitIndex() is called.
func ( l * Log ) AppendEntries ( entries [ ] * LogEntry ) error {
2013-04-16 04:19:29 +00:00
l . mutex . Lock ( )
defer l . mutex . Unlock ( )
2013-04-30 04:13:50 +00:00
// Append each entry but exit if we hit an error.
for _ , entry := range entries {
if err := l . appendEntry ( entry ) ; err != nil {
return err
}
}
return nil
}
// Appends a single entry to the log.
func ( l * Log ) AppendEntry ( entry * LogEntry ) error {
l . mutex . Lock ( )
defer l . mutex . Unlock ( )
return l . appendEntry ( entry )
}
// Writes a single log entry to the end of the log. This function does not
// obtain a lock and should only be used internally. Use AppendEntries() and
// AppendEntry() to use it externally.
func ( l * Log ) appendEntry ( entry * LogEntry ) error {
2013-04-14 21:37:33 +00:00
if l . file == nil {
return errors . New ( "raft.Log: Log is not open" )
}
// Make sure the term and index are greater than the previous.
if len ( l . entries ) > 0 {
lastEntry := l . entries [ len ( l . entries ) - 1 ]
if entry . term < lastEntry . term {
2013-05-01 02:45:05 +00:00
return fmt . Errorf ( "raft.Log: Cannot append entry with earlier term (%x:%x <= %x:%x)" , entry . term , entry . index , lastEntry . term , lastEntry . index )
2013-04-14 21:37:33 +00:00
} else if entry . index == lastEntry . index && entry . index <= lastEntry . index {
2013-05-01 02:45:05 +00:00
return fmt . Errorf ( "raft.Log: Cannot append entry with earlier index in the same term (%x:%x <= %x:%x)" , entry . term , entry . index , lastEntry . term , lastEntry . index )
2013-04-14 21:37:33 +00:00
}
}
2013-04-28 21:23:21 +00:00
2013-04-14 21:37:33 +00:00
// Append to entries list if stored on disk.
l . entries = append ( l . entries , entry )
return nil
}