Skip to content

Commit

Permalink
Modified state machine to accept only size of the log
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsadair committed Sep 9, 2023
1 parent 461a8fa commit 8140a05
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 14 deletions.
14 changes: 2 additions & 12 deletions raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -1192,20 +1192,10 @@ func (r *Raft) fsmLoop() {
response := OperationResponse{Operation: *operation}

r.mu.Lock()
// Compute the size of all persisted state in case a snapshot is needed.
logSize, err := r.log.SizeInBytes()
logSizeInBytes, err := r.log.SizeInBytes()
if err != nil {
r.options.logger.Fatalf("failed to get log size: %s", err.Error())
}
snapshotStorageSize, err := r.snapshotStorage.SizeInBytes()
if err != nil {
r.options.logger.Fatalf("failed to get snapshot storage size: %s", err.Error())
}
storageSize, err := r.storage.SizeInBytes()
if err != nil {
r.options.logger.Fatalf("failed to get storage size: %s", err.Error())
}
stateSizeInBytes := logSize + storageSize + snapshotStorageSize

// Ensure that the lease is valid before performing read-only operation.
// This check should be as close as possible to where the operation is
Expand Down Expand Up @@ -1233,7 +1223,7 @@ func (r *Raft) fsmLoop() {
r.sendResponseWithoutBlocking(operation.ResponseCh, response)

// Take a snapshot of the state machine if necessary.
if !operation.IsReadOnly && r.fsm.NeedSnapshot(stateSizeInBytes) {
if !operation.IsReadOnly && r.fsm.NeedSnapshot(logSizeInBytes) {
r.takeSnapshot(operation.LogIndex, operation.LogTerm)
}
}
Expand Down
2 changes: 1 addition & 1 deletion state_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ type StateMachine interface {

// NeedSnapshot returns true if a snapshot should be taken of the state machine and false
// otherwise.
NeedSnapshot(stateSizeInBytes int64) bool
NeedSnapshot(logSizeInBytes int64) bool
}
2 changes: 1 addition & 1 deletion testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (s *stateMachineMock) Restore(snapshot *Snapshot) error {
return nil
}

func (s *stateMachineMock) NeedSnapshot(stateSizeInBytes int64) bool {
func (s *stateMachineMock) NeedSnapshot(logSizeInBytes int64) bool {
s.mu.Lock()
defer s.mu.Unlock()
return s.snapshotting && len(s.operations)%s.snapshotSize == 0
Expand Down

0 comments on commit 8140a05

Please sign in to comment.