github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/raft/synclogstore.go (about) 1 // Copyright 2018 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package raft 5 6 import ( 7 "io" 8 "sync" 9 10 "github.com/hashicorp/raft" 11 ) 12 13 type closableStore interface { 14 raft.LogStore 15 io.Closer 16 } 17 18 // syncLogStore is a raft.LogStore that ensures calls to the various 19 // interface methods (as well as Close) are goroutine safe. The log 20 // store is shared between the raft worker and the raft-backstop 21 // because a boltdb file can't be opened for writing from two places, 22 // and the backstop worker needs to be able to append an updated 23 // configuration to recover the raft cluster when there's no way to 24 // reach quorum. 25 type syncLogStore struct { 26 mu sync.Mutex 27 store closableStore 28 } 29 30 // FirstIndex is part of raft.LogStore. 31 func (s *syncLogStore) FirstIndex() (uint64, error) { 32 s.mu.Lock() 33 defer s.mu.Unlock() 34 return s.store.FirstIndex() 35 } 36 37 // LastIndex is part of raft.LogStore. 38 func (s *syncLogStore) LastIndex() (uint64, error) { 39 s.mu.Lock() 40 defer s.mu.Unlock() 41 return s.store.LastIndex() 42 } 43 44 // GetLog is part of raft.LogStore. 45 func (s *syncLogStore) GetLog(index uint64, log *raft.Log) error { 46 s.mu.Lock() 47 defer s.mu.Unlock() 48 return s.store.GetLog(index, log) 49 } 50 51 // StoreLog is part of raft.LogStore. 52 func (s *syncLogStore) StoreLog(log *raft.Log) error { 53 s.mu.Lock() 54 defer s.mu.Unlock() 55 return s.store.StoreLog(log) 56 } 57 58 // StoreLogs is part of raft.LogStore. 59 func (s *syncLogStore) StoreLogs(logs []*raft.Log) error { 60 s.mu.Lock() 61 defer s.mu.Unlock() 62 return s.store.StoreLogs(logs) 63 } 64 65 // DeleteRange is part of raft.LogStore. 66 func (s *syncLogStore) DeleteRange(min, max uint64) error { 67 s.mu.Lock() 68 defer s.mu.Unlock() 69 return s.store.DeleteRange(min, max) 70 } 71 72 // Close closes the underlying logstore. 73 func (s *syncLogStore) Close() error { 74 s.mu.Lock() 75 defer s.mu.Unlock() 76 return s.store.Close() 77 }