-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot.go
274 lines (224 loc) · 6.54 KB
/
snapshot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package raft
import (
"io"
"sync"
"time"
"github.com/pkg/errors"
"github.com/sumimakito/raft/pb"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
)
// Snapshot is a descriptor that holds the snapshot file.
type Snapshot interface {
Meta() (SnapshotMeta, error)
Reader() (io.Reader, error)
// Close is used to close the snapshot's underlying file descriptors or handles.
Close() error
}
type SnapshotPolicy struct {
Applies int
Interval time.Duration
}
type SnapshotMeta interface {
Id() string
Index() uint64
Term() uint64
Configuration() *pb.Configuration
ConfigurationIndex() uint64
Encode() ([]byte, error)
}
type SnapshotSink interface {
io.WriteCloser
Meta() SnapshotMeta
Cancel() error
}
type SnapshatStore interface {
Create(index, term uint64, c *pb.Configuration, cIndex uint64) (SnapshotSink, error)
List() ([]SnapshotMeta, error)
Open(id string) (Snapshot, error)
DecodeMeta(b []byte) (SnapshotMeta, error)
Trim() error
}
type snapshotScheduler struct {
server *Server
service *snapshotService
stopCh chan struct{}
counterTimerMu sync.Mutex
counterTimer *CounterTimer
}
func newSnapshotScheduler(server *Server, service *snapshotService) *snapshotScheduler {
s := &snapshotScheduler{
server: server,
service: service,
stopCh: make(chan struct{}, 1),
counterTimer: NewCounterTimer(
server.opts.snapshotPolicy.Applies,
server.opts.snapshotPolicy.Interval,
),
}
go func() {
s.server.logger.Infow("snapshotScheduler started")
defer s.server.logger.Infow("snapshotScheduler stopped")
for {
select {
case <-s.counterTimer.C():
select {
case s.service.snapshotCh <- struct{}{}:
default:
}
case <-s.stopCh:
s.counterTimer.Stop()
return
}
}
}()
return s
}
// CountApply is called when a command has been applied to the StateMachine.
func (s *snapshotScheduler) CountApply() {
s.counterTimer.Count()
}
func (s *snapshotScheduler) Stop() {
close(s.stopCh)
}
// snapshotService is responsible for triggering snapshot creations under
// the SnapshotPolicy.
type snapshotService struct {
server *Server
startOnce sync.Once
stopOnce sync.Once
schedulerMu sync.RWMutex
scheduler *snapshotScheduler
snapshotCh chan struct{}
stopCh chan struct{}
lastSnapshotConf *pb.Configuration
lastSnapshotMeta SnapshotMeta
}
func newSnapshotService(server *Server) *snapshotService {
s := &snapshotService{
server: server,
snapshotCh: make(chan struct{}, 16),
stopCh: make(chan struct{}, 1),
}
return s
}
func (s *snapshotService) Start() {
s.startOnce.Do(func() {
go func() {
for {
select {
case <-s.snapshotCh:
s.TakeSnapshot()
case <-s.stopCh:
s.server.logger.Infow("snapshotService stopped")
return
}
}
}()
})
}
func (s *snapshotService) Stop() {
s.stopOnce.Do(func() { close(s.stopCh) })
}
func (s *snapshotService) Scheduler() *snapshotScheduler {
return s.scheduler
}
func (s *snapshotService) StartScheduler() {
s.schedulerMu.Lock()
defer s.schedulerMu.Unlock()
if s.scheduler != nil {
s.server.logger.Panic("called StartScheduler() on a running snapshotService")
}
s.scheduler = newSnapshotScheduler(s.server, s)
}
func (s *snapshotService) StopScheduler() {
s.schedulerMu.Lock()
defer s.schedulerMu.Unlock()
if s.scheduler == nil {
s.server.logger.Panic("called StopScheduler() on an idle snapshotService")
}
s.scheduler.Stop()
s.scheduler = nil
}
// TakeSnapshot is used to take a snapshot and trim log entries.
func (s *snapshotService) TakeSnapshot() (SnapshotMeta, error) {
c := s.server.confStore.Committed()
lastApplied := s.server.lastApplied()
if lastApplied.Index == 0 {
// It's unnecessary to take a snapshot since there're no applied logs.
s.server.logger.Debugw("snapshot skipped: no applied logs", logFields(s.server)...)
return nil, nil
}
// Check if our latest snapshot is stale
if m := s.lastSnapshotMeta; m != nil {
// Skip if the snapshot index and configuration are identical to current values.
if m.Index() >= lastApplied.Index && proto.Equal(m.Configuration(), c.Configuration) {
s.server.logger.Debugw("snapshot skipped: snapshot is not stale", logFields(s.server)...)
return nil, nil
}
}
stateMachineSnapshotFuture := newFutureTask[*stateMachineSnapshot, any](nil)
s.server.stateMachineSnapshotCh <- stateMachineSnapshotFuture
s.server.logger.Infow("enqueued state machine snapshot request", logFields(s.server)...)
stmsSnapshot, err := stateMachineSnapshotFuture.Result()
if err != nil {
return nil, err
}
sink, err := s.server.snapshotStore.Create(stmsSnapshot.Index, stmsSnapshot.Term, c.Configuration, c.LogIndex())
if err != nil {
return nil, err
}
snapshotMeta := sink.Meta()
if err := stmsSnapshot.Write(sink); err != nil {
if cancelError := sink.Cancel(); cancelError != nil {
return nil, errors.Wrap(cancelError, err.Error())
}
return nil, err
}
if err := sink.Close(); err != nil {
return nil, err
}
restoreFuture := newFutureTask[any](snapshotMeta)
s.server.logRestoreCh <- restoreFuture
if _, err := restoreFuture.Result(); err != nil {
return nil, err
}
s.lastSnapshotMeta = snapshotMeta
s.server.logger.Infow("snapshot has been taken",
logFields(s.server,
zap.String("snapshot_id", snapshotMeta.Id()),
zap.Uint64("snapshot_index", sink.Meta().Index()),
zap.Uint64("snapshot_term", sink.Meta().Term()))...)
return snapshotMeta, nil
}
// Restore must be called in a channel select branch
func (s *snapshotService) Restore(snapshotId string) (bool, error) {
s.server.logger.Infow("ready to restore snapshot",
logFields(s.server, zap.String("snapshot_id", snapshotId))...)
snapshot, err := s.server.snapshotStore.Open(snapshotId)
if err != nil {
// It's recoverable if errors happen here.
return false, err
}
snapshotMeta, err := snapshot.Meta()
if err != nil {
return false, err
}
// Check if the restoration is necessary.
if snapshotMeta.Index() < s.server.firstLogIndex()-1 {
// Restoration is not necessary.
return false, nil
}
if err := s.server.stateMachine.Restore(snapshot); err != nil {
return false, err
}
if err := s.server.logStore.Restore(snapshotMeta); err != nil {
s.server.logger.Panicw("error occurred while triming logs during restoration",
logFields(s.server, zap.Error(err))...)
}
s.server.setFirstLogIndex(Must2(s.server.logStore.FirstIndex()))
s.server.setLastLogIndex(Must2(s.server.logStore.LastIndex()))
s.server.commitAndApply(snapshotMeta.Index())
s.server.alterConfiguration(newConfiguration(snapshotMeta.Configuration(), snapshotMeta.ConfigurationIndex()))
return true, nil
}