forked from lni/dragonboat
/
adapter.go
372 lines (319 loc) · 10.1 KB
/
adapter.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright 2017-2021 Lei Ni (nilei81@gmail.com) and other contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package rsm
import (
"io"
"github.com/ik-tech/dragonboat/v3/config"
pb "github.com/ik-tech/dragonboat/v3/raftpb"
sm "github.com/ik-tech/dragonboat/v3/statemachine"
)
// IStateMachine is an adapter interface for underlying sm.IStateMachine,
// sm.IConcurrentStateMachine and sm.IOnDIskStateMachine instances.
type IStateMachine interface {
Open(<-chan struct{}) (uint64, error)
Update(entries []sm.Entry) ([]sm.Entry, error)
Lookup(query interface{}) (interface{}, error)
NALookup(query []byte) ([]byte, error)
Sync() error
Prepare() (interface{}, error)
Save(interface{},
io.Writer, sm.ISnapshotFileCollection, <-chan struct{}) error
Recover(io.Reader, []sm.SnapshotFile, <-chan struct{}) error
Close() error
GetHash() (uint64, error)
Concurrent() bool
OnDisk() bool
Type() pb.StateMachineType
}
// InMemStateMachine is a regular state machine not capable of concurrent
// access from multiple goroutines.
type InMemStateMachine struct {
sm sm.IStateMachine
h sm.IHash
na sm.IExtended
}
var _ IStateMachine = (*InMemStateMachine)(nil)
// NewInMemStateMachine creates a new InMemStateMachine instance.
func NewInMemStateMachine(s sm.IStateMachine) *InMemStateMachine {
i := &InMemStateMachine{sm: s}
if h, ok := s.(sm.IHash); ok {
i.h = h
}
if na, ok := s.(sm.IExtended); ok {
i.na = na
}
return i
}
// Open opens the state machine.
func (i *InMemStateMachine) Open(stopc <-chan struct{}) (uint64, error) {
panic("Open() called on InMemStateMachine")
}
// Update updates the state machine.
func (i *InMemStateMachine) Update(entries []sm.Entry) ([]sm.Entry, error) {
if len(entries) != 1 {
panic("len(entries) != 1")
}
var err error
entries[0].Result, err = i.sm.Update(entries[0].Cmd)
return entries, err
}
// Lookup queries the state machine.
func (i *InMemStateMachine) Lookup(query interface{}) (interface{}, error) {
return i.sm.Lookup(query)
}
// NALookup queries the state machine.
func (i *InMemStateMachine) NALookup(query []byte) ([]byte, error) {
if i.na == nil {
return nil, sm.ErrNotImplemented
}
return i.na.NALookup(query)
}
// Sync synchronizes all in-core state with that on disk.
func (i *InMemStateMachine) Sync() error {
panic("Sync not implemented in InMemStateMachine")
}
// Prepare makes preparations for taking concurrent snapshot.
func (i *InMemStateMachine) Prepare() (interface{}, error) {
panic("Prepare not implemented in InMemStateMachine")
}
// Save saves the snapshot.
func (i *InMemStateMachine) Save(ctx interface{},
w io.Writer, fc sm.ISnapshotFileCollection, stopc <-chan struct{}) error {
if ctx != nil {
panic("snapshot ctx is not nil")
}
return i.sm.SaveSnapshot(w, fc, stopc)
}
// Recover recovers the state machine from a snapshot.
func (i *InMemStateMachine) Recover(r io.Reader,
fs []sm.SnapshotFile, stopc <-chan struct{}) error {
return i.sm.RecoverFromSnapshot(r, fs, stopc)
}
// Close closes the state machine.
func (i *InMemStateMachine) Close() error {
return i.sm.Close()
}
// GetHash returns the uint64 hash value representing the state of a state
// machine.
func (i *InMemStateMachine) GetHash() (uint64, error) {
if i.h == nil {
return 0, sm.ErrNotImplemented
}
return i.h.GetHash()
}
// Concurrent returns a boolean flag indicating whether the state machine is
// capable of taking concurrent snapshot.
func (i *InMemStateMachine) Concurrent() bool {
return false
}
// OnDisk returns a boolean flag indicating whether this is an on disk state
// machine.
func (i *InMemStateMachine) OnDisk() bool {
return false
}
// Type returns the type of the state machine.
func (i *InMemStateMachine) Type() pb.StateMachineType {
return pb.RegularStateMachine
}
// ConcurrentStateMachine is an IStateMachine type capable of taking concurrent
// snapshots.
type ConcurrentStateMachine struct {
sm sm.IConcurrentStateMachine
h sm.IHash
na sm.IExtended
}
// NewConcurrentStateMachine creates a new ConcurrentStateMachine instance.
func NewConcurrentStateMachine(s sm.IConcurrentStateMachine) *ConcurrentStateMachine {
v := &ConcurrentStateMachine{sm: s}
if h, ok := s.(sm.IHash); ok {
v.h = h
}
if na, ok := s.(sm.IExtended); ok {
v.na = na
}
return v
}
// Open opens the state machine.
func (s *ConcurrentStateMachine) Open(stopc <-chan struct{}) (uint64, error) {
panic("Open() not implemented ConcurrentStateMachine")
}
// Update updates the state machine.
func (s *ConcurrentStateMachine) Update(entries []sm.Entry) ([]sm.Entry, error) {
return s.sm.Update(entries)
}
// Lookup queries the state machine.
func (s *ConcurrentStateMachine) Lookup(query interface{}) (interface{}, error) {
return s.sm.Lookup(query)
}
// NALookup queries the state machine.
func (s *ConcurrentStateMachine) NALookup(query []byte) ([]byte, error) {
if s.na == nil {
return nil, sm.ErrNotImplemented
}
return s.na.NALookup(query)
}
// Sync synchronizes all in-core state with that on disk.
func (s *ConcurrentStateMachine) Sync() error {
panic("Sync not implemented in ConcurrentStateMachine")
}
// Prepare makes preparations for taking concurrent snapshot.
func (s *ConcurrentStateMachine) Prepare() (interface{}, error) {
return s.sm.PrepareSnapshot()
}
// Save saves the snapshot.
func (s *ConcurrentStateMachine) Save(ctx interface{},
w io.Writer, fc sm.ISnapshotFileCollection, stopc <-chan struct{}) error {
return s.sm.SaveSnapshot(ctx, w, fc, stopc)
}
// Recover recovers the state machine from a snapshot.
func (s *ConcurrentStateMachine) Recover(r io.Reader,
fs []sm.SnapshotFile, stopc <-chan struct{}) error {
return s.sm.RecoverFromSnapshot(r, fs, stopc)
}
// Close closes the state machine.
func (s *ConcurrentStateMachine) Close() error {
return s.sm.Close()
}
// GetHash returns the uint64 hash value representing the state of a state
// machine.
func (s *ConcurrentStateMachine) GetHash() (uint64, error) {
if s.h == nil {
return 0, sm.ErrNotImplemented
}
return s.h.GetHash()
}
// Concurrent returns a boolean flag indicating whether the state machine is
// capable of taking concurrent snapshot.
func (s *ConcurrentStateMachine) Concurrent() bool {
return true
}
// OnDisk returns a boolean flag indicating whether this is a on disk state
// machine.
func (s *ConcurrentStateMachine) OnDisk() bool {
return false
}
// Type returns the type of the state machine.
func (s *ConcurrentStateMachine) Type() pb.StateMachineType {
return pb.ConcurrentStateMachine
}
// ITestFS is an interface implemented by test SMs.
type ITestFS interface {
SetTestFS(fs config.IFS)
}
// OnDiskStateMachine is the type to represent an on disk state machine.
type OnDiskStateMachine struct {
sm sm.IOnDiskStateMachine
h sm.IHash
na sm.IExtended
opened bool
}
// NewOnDiskStateMachine creates and returns an on disk state machine.
func NewOnDiskStateMachine(s sm.IOnDiskStateMachine) *OnDiskStateMachine {
r := &OnDiskStateMachine{sm: s}
if h, ok := s.(sm.IHash); ok {
r.h = h
}
if na, ok := s.(sm.IExtended); ok {
r.na = na
}
return r
}
// SetTestFS injects the specified fs to the test SM.
func (s *OnDiskStateMachine) SetTestFS(fs config.IFS) {
if tfs, ok := s.sm.(ITestFS); ok {
plog.Infof("the underlying SM support test fs injection")
tfs.SetTestFS(fs)
}
}
// Open opens the state machine.
func (s *OnDiskStateMachine) Open(stopc <-chan struct{}) (uint64, error) {
if s.opened {
panic("Open invoked again")
}
s.opened = true
applied, err := s.sm.Open(stopc)
if err != nil {
return 0, err
}
return applied, nil
}
// Update updates the state machine.
func (s *OnDiskStateMachine) Update(entries []sm.Entry) ([]sm.Entry, error) {
s.ensureOpened()
return s.sm.Update(entries)
}
// Lookup queries the state machine.
func (s *OnDiskStateMachine) Lookup(query interface{}) (interface{}, error) {
s.ensureOpened()
return s.sm.Lookup(query)
}
// NALookup queries the state machine.
func (s *OnDiskStateMachine) NALookup(query []byte) ([]byte, error) {
s.ensureOpened()
return s.na.NALookup(query)
}
// Sync synchronizes all in-core state with that on disk.
func (s *OnDiskStateMachine) Sync() error {
s.ensureOpened()
return s.sm.Sync()
}
// Prepare makes preparations for taking concurrent snapshot.
func (s *OnDiskStateMachine) Prepare() (interface{}, error) {
s.ensureOpened()
return s.sm.PrepareSnapshot()
}
// Save saves the snapshot.
func (s *OnDiskStateMachine) Save(ctx interface{},
w io.Writer, fc sm.ISnapshotFileCollection, stopc <-chan struct{}) error {
s.ensureOpened()
return s.sm.SaveSnapshot(ctx, w, stopc)
}
// Recover recovers the state machine from a snapshot.
func (s *OnDiskStateMachine) Recover(r io.Reader,
fs []sm.SnapshotFile, stopc <-chan struct{}) error {
s.ensureOpened()
return s.sm.RecoverFromSnapshot(r, stopc)
}
// Close closes the state machine.
func (s *OnDiskStateMachine) Close() error {
return s.sm.Close()
}
// GetHash returns the uint64 hash value representing the state of a state
// machine.
func (s *OnDiskStateMachine) GetHash() (uint64, error) {
s.ensureOpened()
if s.h == nil {
return 0, sm.ErrNotImplemented
}
return s.h.GetHash()
}
// Concurrent returns a boolean flag indicating whether the state machine is
// capable of taking concurrent snapshot.
func (s *OnDiskStateMachine) Concurrent() bool {
return true
}
// OnDisk returns a boolean flag indicating whether this is an on disk state
// machine.
func (s *OnDiskStateMachine) OnDisk() bool {
return true
}
// Type returns the type of the state machine.
func (s *OnDiskStateMachine) Type() pb.StateMachineType {
return pb.OnDiskStateMachine
}
func (s *OnDiskStateMachine) ensureOpened() {
if !s.opened {
panic("not opened")
}
}