forked from lni/dragonboat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
managedstatemachine.go
403 lines (366 loc) · 11.7 KB
/
managedstatemachine.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright 2017-2019 Lei Ni (nilei81@gmail.com)
//
// 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 (
"errors"
"io"
"sync"
"github.com/lni/dragonboat/internal/settings"
"github.com/lni/dragonboat/statemachine"
)
var (
// ErrClusterClosed indicates that the cluster has been closed
ErrClusterClosed = errors.New("raft cluster already closed")
)
// From identifies a component in the system.
type From uint64
var (
// LRUMaxSessionCount is the largest number of client sessions that can be
// concurrently managed by a LRUSession instance.
LRUMaxSessionCount = settings.Hard.LRUMaxSessionCount
)
const (
// FromNodeHost indicates the data store has been loaded by or offloaded from
// nodehost.
FromNodeHost From = iota
// FromStepWorker indicates that the data store has been loaded by or
// offloaded from the step worker.
FromStepWorker
// FromCommitWorker indicates that the data store has been loaded by or
// offloaded from the commit worker.
FromCommitWorker
// FromSnapshotWorker indicates that the data store has been loaded by or
// offloaded from the snapshot worker.
FromSnapshotWorker
)
// OffloadedStatus is used for tracking whether the managed data store has been
// offloaded from various system components.
type OffloadedStatus struct {
readyToDestroy bool
destroyed bool
offloadedFromNodeHost bool
offloadedFromStepWorker bool
offloadedFromCommitWorker bool
offloadedFromSnapshotWorker bool
loadedByStepWorker bool
loadedByCommitWorker bool
loadedBySnapshotWorker bool
}
// ReadyToDestroy returns a boolean value indicating whether the the managed data
// store is ready to be destroyed.
func (o *OffloadedStatus) ReadyToDestroy() bool {
return o.readyToDestroy
}
// Destroyed returns a boolean value indicating whether the belonging object
// has been destroyed.
func (o *OffloadedStatus) Destroyed() bool {
return o.destroyed
}
// SetDestroyed set the destroyed flag to be true
func (o *OffloadedStatus) SetDestroyed() {
o.destroyed = true
}
// SetLoaded marks the managed data store as loaded from the specified
// component.
func (o *OffloadedStatus) SetLoaded(from From) {
if o.offloadedFromNodeHost {
if from == FromStepWorker ||
from == FromCommitWorker ||
from == FromSnapshotWorker {
plog.Panicf("loaded from %v after offloaded from nodehost", from)
}
}
if from == FromNodeHost {
panic("not suppose to get loaded notification from nodehost")
} else if from == FromStepWorker {
o.loadedByStepWorker = true
} else if from == FromCommitWorker {
o.loadedByCommitWorker = true
} else if from == FromSnapshotWorker {
o.loadedBySnapshotWorker = true
} else {
panic("unknown offloadFrom value")
}
}
// SetOffloaded marks the managed data store as offloaded from the specified
// component.
func (o *OffloadedStatus) SetOffloaded(from From) {
if from == FromNodeHost {
o.offloadedFromNodeHost = true
} else if from == FromStepWorker {
o.offloadedFromStepWorker = true
} else if from == FromCommitWorker {
o.offloadedFromCommitWorker = true
} else if from == FromSnapshotWorker {
o.offloadedFromSnapshotWorker = true
} else {
panic("unknown offloadFrom value")
}
if from == FromNodeHost {
if !o.loadedByStepWorker {
o.offloadedFromStepWorker = true
}
if !o.loadedByCommitWorker {
o.offloadedFromCommitWorker = true
}
if !o.loadedBySnapshotWorker {
o.offloadedFromSnapshotWorker = true
}
}
if o.offloadedFromNodeHost &&
o.offloadedFromCommitWorker &&
o.offloadedFromSnapshotWorker &&
o.offloadedFromStepWorker {
o.readyToDestroy = true
}
}
// IManagedStateMachine is the interface used to manage data store.
type IManagedStateMachine interface {
GetSessionHash() uint64
UpdateRespondedTo(*Session, uint64)
UnregisterClientID(clientID uint64) uint64
RegisterClientID(clientID uint64) uint64
ClientRegistered(clientID uint64) (*Session, bool)
UpdateRequired(*Session, uint64) (uint64, bool, bool)
Update(*Session, uint64, []byte) uint64
Lookup([]byte) ([]byte, error)
GetHash() uint64
SaveSnapshot(string, statemachine.ISnapshotFileCollection) (uint64, error)
RecoverFromSnapshot(string, []statemachine.SnapshotFile) error
Offloaded(From)
Loaded(From)
}
// ManagedStateMachineFactory is the factory function type for creating an
// IManagedStateMachine instance.
type ManagedStateMachineFactory func(clusterID uint64,
nodeID uint64, stopc <-chan struct{}) IManagedStateMachine
// SessionManager is the wrapper struct that implements client session related
// functionalites used in the IManagedStateMachine interface.
type SessionManager struct {
sessions *lrusession
}
// NewSessionManager returns a new SessionManager instance.
func NewSessionManager() SessionManager {
return SessionManager{
sessions: newLRUSession(LRUMaxSessionCount),
}
}
// GetSessionHash returns an uint64 integer representing the state of the
// session manager.
func (ds *SessionManager) GetSessionHash() uint64 {
return ds.sessions.getHash()
}
// UpdateRespondedTo updates the responded to value of the specified
// client session.
func (ds *SessionManager) UpdateRespondedTo(session *Session,
respondedTo uint64) {
session.clearTo(RaftSeriesID(respondedTo))
}
// RegisterClientID registers a new client, it returns the input client id
// if it is previously unknown, or 0 when the client has already been
// registered.
func (ds *SessionManager) RegisterClientID(clientID uint64) uint64 {
es, ok := ds.sessions.getSession(RaftClientID(clientID))
if ok {
if es.ClientID != RaftClientID(clientID) {
plog.Panicf("returned an expected session, got id %d, want %d",
es.ClientID, clientID)
}
plog.Warningf("client ID %d already exist", clientID)
return 0
}
s := newSession(RaftClientID(clientID))
ds.sessions.addSession(RaftClientID(clientID), *s)
return clientID
}
// UnregisterClientID removes the specified client session from the system.
// It returns the client id if the client is successfully removed, or 0
// if the client session does not exist.
func (ds *SessionManager) UnregisterClientID(clientID uint64) uint64 {
es, ok := ds.sessions.getSession(RaftClientID(clientID))
if !ok {
return 0
}
if es.ClientID != RaftClientID(clientID) {
plog.Panicf("returned an expected session, got id %d, want %d",
es.ClientID, clientID)
}
ds.sessions.delSession(RaftClientID(clientID))
return clientID
}
// ClientRegistered returns whether the specified client exists in the system.
func (ds *SessionManager) ClientRegistered(clientID uint64) (*Session, bool) {
es, ok := ds.sessions.getSession(RaftClientID(clientID))
if ok {
if es.ClientID != RaftClientID(clientID) {
plog.Panicf("returned an expected session, got id %d, want %d",
es.ClientID, clientID)
}
}
return es, ok
}
// UpdateRequired return a tuple of request result, responded before,
// update required.
func (ds *SessionManager) UpdateRequired(session *Session,
seriesID uint64) (uint64, bool, bool) {
if session.hasResponded(RaftSeriesID(seriesID)) {
return 0, true, false
}
v, ok := session.getResponse(RaftSeriesID(seriesID))
if ok {
return v, false, false
}
return 0, false, true
}
// MustHaveClientSeries checks whether the session manager contains a client
// session identified as clientID and whether it has seriesID responded.
func (ds *SessionManager) MustHaveClientSeries(session *Session,
seriesID uint64) {
_, ok := session.getResponse(RaftSeriesID(seriesID))
if ok {
panic("already has response in session")
}
}
// AddResponse adds the specified result to the session.
func (ds *SessionManager) AddResponse(session *Session,
seriesID uint64, result uint64) {
session.addResponse(RaftSeriesID(seriesID), result)
}
// SaveSessions saves the sessions to the provided io.writer.
func (ds *SessionManager) SaveSessions(writer io.Writer) (uint64, error) {
return ds.sessions.save(writer)
}
// LoadSessions loads and restores sessions from io.Reader.
func (ds *SessionManager) LoadSessions(reader io.Reader) error {
return ds.sessions.load(reader)
}
// NativeStateMachine is the IManagedStateMachine object used to manage native
// data store in Golang.
type NativeStateMachine struct {
dataStore statemachine.IStateMachine
done <-chan struct{}
// see dragonboat-doc on how this RWMutex can be entirely avoided
mu sync.RWMutex
OffloadedStatus
SessionManager
}
// NewNativeStateMachine creates and returns a new NativeStateMachine object.
func NewNativeStateMachine(ds statemachine.IStateMachine,
done <-chan struct{}) IManagedStateMachine {
s := &NativeStateMachine{
dataStore: ds,
done: done,
SessionManager: NewSessionManager(),
}
return s
}
func (ds *NativeStateMachine) closeStateMachine() {
ds.dataStore.Close()
}
// Offloaded offloads the data store from the specified part of the system.
func (ds *NativeStateMachine) Offloaded(from From) {
ds.mu.Lock()
defer ds.mu.Unlock()
ds.SetOffloaded(from)
if ds.ReadyToDestroy() && !ds.Destroyed() {
ds.closeStateMachine()
ds.SetDestroyed()
}
}
// Loaded marks the statemachine as loaded by the specified component.
func (ds *NativeStateMachine) Loaded(from From) {
ds.mu.Lock()
defer ds.mu.Unlock()
ds.SetLoaded(from)
}
// Update updates the data store.
func (ds *NativeStateMachine) Update(session *Session,
seriesID uint64, data []byte) uint64 {
if session != nil {
_, ok := session.getResponse(RaftSeriesID(seriesID))
if ok {
panic("already has response in session")
}
}
v := ds.dataStore.Update(data)
if session != nil {
session.addResponse(RaftSeriesID(seriesID), v)
}
return v
}
// Lookup queries the data store.
func (ds *NativeStateMachine) Lookup(data []byte) ([]byte, error) {
ds.mu.RLock()
if ds.Destroyed() {
ds.mu.RUnlock()
return nil, ErrClusterClosed
}
v := ds.dataStore.Lookup(data)
ds.mu.RUnlock()
return v, nil
}
// GetHash returns an integer value representing the state of the data store.
func (ds *NativeStateMachine) GetHash() uint64 {
return ds.dataStore.GetHash()
}
// SaveSnapshot saves the state of the data store to the snapshot file specified
// by the fp input string.
func (ds *NativeStateMachine) SaveSnapshot(fp string,
collection statemachine.ISnapshotFileCollection) (uint64, error) {
writer, err := NewSnapshotWriter(fp)
if err != nil {
return 0, err
}
defer func() {
err = writer.Close()
}()
smsz, err := ds.sessions.save(writer)
if err != nil {
return 0, err
}
sz, err := ds.dataStore.SaveSnapshot(writer, collection, ds.done)
if err != nil {
return 0, err
}
if err = writer.SaveHeader(smsz, sz); err != nil {
return 0, err
}
return sz + smsz + SnapshotHeaderSize, nil
}
// RecoverFromSnapshot recovers the state of the data store from the snapshot
// file specified by the fp input string.
func (ds *NativeStateMachine) RecoverFromSnapshot(fp string,
files []statemachine.SnapshotFile) (err error) {
reader, err := NewSnapshotReader(fp)
if err != nil {
return err
}
defer func() {
err = reader.Close()
}()
header, err := reader.GetHeader()
if err != nil {
return err
}
reader.ValidateHeader(header)
if err = ds.sessions.load(reader); err != nil {
return err
}
if err = ds.dataStore.RecoverFromSnapshot(reader, files, ds.done); err != nil {
plog.Errorf("statemachine.RecoverFromSnapshot returned %v", err)
return err
}
reader.ValidatePayload(header)
return err
}