Skip to content
This repository has been archived by the owner on Sep 6, 2018. It is now read-only.

Commit

Permalink
change the do struct and add commit center
Browse files Browse the repository at this point in the history
  • Loading branch information
xiang90 committed Jun 23, 2013
1 parent 5bef656 commit d8884cb
Show file tree
Hide file tree
Showing 10 changed files with 609 additions and 849 deletions.
3 changes: 3 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ func init() {
type Command interface {
CommandName() string
Apply(server *Server) ([]byte, error)
Join() ([]byte, error)
Init()
Finish()
}

//------------------------------------------------------------------------------
Expand Down
37 changes: 32 additions & 5 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (l *Log) CurrentTerm() uint64 {
func (l *Log) Open(path string) error {
l.mutex.Lock()
defer l.mutex.Unlock()

// Read all the entries from the log if one exists.
var lastIndex int = 0
if _, err := os.Stat(path); !os.IsNotExist(err) {
Expand Down Expand Up @@ -167,21 +167,22 @@ func (l *Log) Open(path string) error {
}
break
}

// Append entry.
l.entries = append(l.entries, entry)
l.commitIndex = entry.Index

// Apply the command.
entry.result, err = l.ApplyFunc(entry.Command)

l.errors = append(l.errors, err)

lastIndex += n
}

file.Close()
}

// Open the file for appending.
var err error
l.file, err = os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
Expand Down Expand Up @@ -230,15 +231,19 @@ func (l *Log) ContainsEntry(index uint64, term uint64) bool {
func (l *Log) GetEntriesAfter(index uint64) ([]*LogEntry, uint64) {
// Return an error if the index doesn't exist.
if index > (uint64(len(l.entries)) + l.startIndex) {
panic(fmt.Sprintf("raft: Index is beyond end of log: %v", index))
panic(fmt.Sprintf("raft: Index is beyond end of log: % v%v", len(l.entries), index))
}

// If we're going from the beginning of the log then return the whole log.
if index == l.startIndex {
return l.entries, l.startTerm
}

fmt.Println("[GetEntries] index ", index, "lastIndex", l.entries[len(l.entries) - 1].Index)

// Determine the term at the given entry and return a subslice.
term := l.entries[index-1-l.startIndex].Term

return l.entries[index-l.startIndex:], term
}

Expand Down Expand Up @@ -283,6 +288,22 @@ func (l *Log) CommitInfo() (index uint64, term uint64) {
return lastCommitEntry.Index, lastCommitEntry.Term
}

// Retrieves the last index and term that has been committed to the log.
func (l *Log) LastInfo() (index uint64, term uint64) {
l.mutex.Lock()
defer l.mutex.Unlock()

// If we don't have any entries then just return zeros.
if len(l.entries) == 0 {
return l.startIndex, l.startTerm
}

// Return the last index & term
lastEntry := l.entries[len(l.entries) - 1]
return lastEntry.Index, lastEntry.Term
}


// Updates the commit index
func (l *Log) UpdateCommitIndex(index uint64) {
l.mutex.Lock()
Expand Down Expand Up @@ -318,6 +339,7 @@ func (l *Log) SetCommitIndex(index uint64) error {

// Apply the changes to the state machine and store the error code.
entry.result, l.errors[entryIndex] = l.ApplyFunc(entry.Command)

}
return nil
}
Expand All @@ -331,13 +353,16 @@ func (l *Log) SetCommitIndex(index uint64) error {
func (l *Log) Truncate(index uint64, term uint64) error {
l.mutex.Lock()
defer l.mutex.Unlock()
fmt.Println("[Truncate] truncate to ", index)
// Do not allow committed entries to be truncated.
if index < l.CommitIndex() {
fmt.Println("[Truncate] error 1")
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 > l.startIndex+uint64(len(l.entries)) {
fmt.Println("[Truncate] error 2")
return fmt.Errorf("raft.Log: Entry index does not exist (MAX=%v): (IDX=%v, TERM=%v)", len(l.entries), index, term)
}

Expand All @@ -348,11 +373,13 @@ func (l *Log) Truncate(index uint64, term uint64) error {
// Do not truncate if the entry at index does not have the matching term.
entry := l.entries[index-l.startIndex-1]
if len(l.entries) > 0 && entry.Term != term {
fmt.Println("[Truncate] error 3")
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 < l.startIndex+uint64(len(l.entries)) {
fmt.Println("[Truncate] truncate to ", index)
l.entries = l.entries[0 : index-l.startIndex]
}
}
Expand Down
225 changes: 0 additions & 225 deletions log_test.go

This file was deleted.

Loading

0 comments on commit d8884cb

Please sign in to comment.