From e24edec81fd50962bc1915abdabe5618b65b68e0 Mon Sep 17 00:00:00 2001 From: David Norton Date: Thu, 30 Oct 2014 12:06:30 -0400 Subject: [PATCH] Fix #943: don't try to take 2 snapshots at a time --- _vendor/raft/server.go | 2 ++ _vendor/raft/snapshot_test.go | 13 +++++++++++++ coordinator/raft_server.go | 2 ++ 3 files changed, 17 insertions(+) diff --git a/_vendor/raft/server.go b/_vendor/raft/server.go index 922a0466963..b012af1a5f5 100644 --- a/_vendor/raft/server.go +++ b/_vendor/raft/server.go @@ -1203,6 +1203,7 @@ func (s *server) TakeSnapshot() error { state, err := s.stateMachine.Save() if err != nil { + s.pendingSnapshot = nil return err } @@ -1237,6 +1238,7 @@ func (s *server) saveSnapshot() error { // Write snapshot to disk. if err := s.pendingSnapshot.save(); err != nil { + s.pendingSnapshot = nil return err } diff --git a/_vendor/raft/snapshot_test.go b/_vendor/raft/snapshot_test.go index ac840e9559d..3cbde39b9bd 100644 --- a/_vendor/raft/snapshot_test.go +++ b/_vendor/raft/snapshot_test.go @@ -1,6 +1,7 @@ package raft import ( + "errors" "fmt" "io/ioutil" "os" @@ -121,6 +122,18 @@ func TestSnapshotRequest(t *testing.T) { }) } +func TestTakeSnapshotWhenStateMachineSaveFails(t *testing.T) { + runServerWithMockStateMachine(Leader, func(s Server, m *mock.Mock) { + m.On("Save").Return([]byte("foo"), errors.New("test error message")) + + s.Do(&testCommand1{}) + err := s.TakeSnapshot() + assert.Equal(t, err.Error(), "test error message") + assert.Nil(t, s.(*server).pendingSnapshot) + s.Stop() + }) +} + func runServerWithMockStateMachine(state string, fn func(s Server, m *mock.Mock)) { var m mockStateMachine s := newTestServer("1", &testTransporter{}) diff --git a/coordinator/raft_server.go b/coordinator/raft_server.go index ee3ed19c9d3..0ffd1ac66d8 100644 --- a/coordinator/raft_server.go +++ b/coordinator/raft_server.go @@ -331,6 +331,8 @@ const ( ) func (s *RaftServer) ForceLogCompaction() error { + s.mutex.Lock() + defer s.mutex.Unlock() err := s.raftServer.TakeSnapshot() if err != nil { log.Error("Cannot take snapshot: %s", err)