forked from matrixorigin/dragonboat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.go
357 lines (330 loc) · 9.54 KB
/
wrapper.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
// 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 cpp implements a C++11 wrapper to allow IStateMachine to be implemented
in C++11.
This package is internally used by Dragonboat, applications are not expected
to import this package.
*/
package cpp
// initially the wrapper was implemented as a go plugin, this helps to make sure
// that libdragonboatcpp.a is not required for almost every target in Makefile.
// the drawback is also obvious - using a plugin to load another (CPP based)
// plugin is not that cool.
/*
#cgo CFLAGS: -I../../binding/include
#cgo CXXFLAGS: -std=c++11 -O3 -I../../binding/include
#cgo LDFLAGS: -ldl
#include <stdlib.h>
#include "wrapper.h"
*/
import "C"
import (
"errors"
"fmt"
"io"
"os"
"strings"
"sync"
"syscall"
"unsafe"
"github.com/lni/dragonboat/internal/rsm"
"github.com/lni/dragonboat/logger"
"github.com/lni/dragonboat/statemachine"
)
var (
plog = logger.GetLogger("cpp")
)
func getErrnoFromError(err error) int {
if err == nil {
panic("nil err")
}
operr, ok := err.(*os.PathError)
if ok {
errno, ok := operr.Err.(syscall.Errno)
if ok {
return int(errno)
}
}
return int(syscall.EIO)
}
//
// exported go functions to be called by C code.
//
// RemoveManagedGoObject removes the managed object specified by the oid value.
//export RemoveManagedGoObject
func RemoveManagedGoObject(oid uint64) {
RemoveManagedObject(oid)
}
// WriteToManagedWriter is a go helper function exported to C code to allow C
// code to use the Go snapshot writer.
//export WriteToManagedWriter
func WriteToManagedWriter(oid uint64, data []byte) (bool, int) {
wi, ok := GetManagedObject(oid)
if !ok {
panic("failed to get writer")
}
w := wi.(io.Writer)
_, err := w.Write(data)
if err != nil {
plog.Errorf("got err %+v when writing to the snapshot writer", err)
return false, getErrnoFromError(err)
}
return true, 0
}
// ReadFromManagedReader is a go helper function exported to C to allow C code
// to use the Go snapshot reader.
//export ReadFromManagedReader
func ReadFromManagedReader(oid uint64, data []byte) (int, int) {
ri, ok := GetManagedObject(oid)
if !ok {
panic("failed to get reader")
}
r := ri.(io.Reader)
n, err := r.Read(data)
if err != nil {
if err == io.EOF {
return 0, 0
}
plog.Errorf("got err %+v when reading from the snapshot reader", err)
return -1, getErrnoFromError(err)
}
return n, 0
}
// DoneChanClosed is a go helper function exported to C to allow C code to
// check whether the specified done channel has been closed.
//export DoneChanClosed
func DoneChanClosed(oid uint64) bool {
ci, ok := GetManagedObject(oid)
if !ok {
panic("failed to get the done chan")
}
c := ci.(<-chan struct{})
select {
case <-c:
return true
default:
}
return false
}
// AddToSnapshotFileCollection adds the details of an external snapshot file to
// the specified managed file collection instance.
//export AddToSnapshotFileCollection
func AddToSnapshotFileCollection(oid uint64,
fileID uint64, path []byte, metadata []byte) {
fci, ok := GetManagedObject(oid)
if !ok {
panic("failed to get the file collection")
}
fc := fci.(statemachine.ISnapshotFileCollection)
filePath := string(path)
data := make([]byte, len(metadata))
copy(data, metadata)
fc.AddFile(fileID, filePath, data)
}
func getErrorFromErrNo(errno int) error {
if errno == 0 {
return nil
} else if errno == 1 {
return errors.New("failed to access snapshot file")
} else if errno == 2 {
return errors.New("failed to recover from snapshot")
} else if errno == 3 {
return errors.New("failed to save snapshot")
} else if errno == 4 {
return statemachine.ErrSnapshotStopped
} else if errno == 100 {
return errors.New("other snapshot error")
}
return fmt.Errorf("snapshot error with errno %d", errno)
}
// StateMachineWrapper is the IManagedStateMachine managing C++ data store.
type StateMachineWrapper struct {
rsm.OffloadedStatus
// void * points to the actual data store
dataStore *C.CPPStateMachine
done <-chan struct{}
mu sync.RWMutex
rsm.SessionManager
}
func isValidCPPPlugin(soFilepath string) bool {
fn := fmt.Sprintf("./%s", soFilepath)
soName := C.CString(fn)
defer C.free(unsafe.Pointer(soName))
v := C.IsValidDragonboatPlugin(soName)
return C.int(v) == 0
}
// NewStateMachineWrapper creates and returns the new NewStateMachineWrapper
// instance.
func NewStateMachineWrapper(clusterID uint64, nodeID uint64,
dsname string, done <-chan struct{}) rsm.IManagedStateMachine {
cClusterID := C.uint64_t(clusterID)
cNodeID := C.uint64_t(nodeID)
cDSName := C.CString(getCPPSOFileName(dsname))
defer C.free(unsafe.Pointer(cDSName))
return &StateMachineWrapper{
dataStore: C.CreateDBStateMachine(cClusterID, cNodeID, cDSName),
done: done,
SessionManager: rsm.NewSessionManager(),
}
}
func (ds *StateMachineWrapper) destroy() {
C.DestroyDBStateMachine(ds.dataStore)
}
// Offloaded offloads the data store from the specified part of the system.
func (ds *StateMachineWrapper) Offloaded(from rsm.From) {
ds.mu.Lock()
defer ds.mu.Unlock()
ds.SetOffloaded(from)
if ds.ReadyToDestroy() && !ds.Destroyed() {
ds.destroy()
ds.SetDestroyed()
}
}
// Loaded marks the data store as loaded by the specified component.
func (ds *StateMachineWrapper) Loaded(from rsm.From) {
ds.mu.Lock()
defer ds.mu.Unlock()
ds.SetLoaded(from)
}
// Update updates the data store.
func (ds *StateMachineWrapper) Update(session *rsm.Session, seriesID uint64,
data []byte) uint64 {
ds.ensureNotDestroyed()
var dp *C.uchar
dp = nil
if len(data) > 0 {
dp = (*C.uchar)(unsafe.Pointer(&data[0]))
}
if session != nil {
ds.MustHaveClientSeries(session, seriesID)
}
v := C.UpdateDBStateMachine(ds.dataStore, dp, C.size_t(len(data)))
if session != nil {
ds.AddResponse(session, seriesID, uint64(v))
}
return uint64(v)
}
// Lookup queries the data store.
func (ds *StateMachineWrapper) Lookup(data []byte) ([]byte, error) {
ds.mu.RLock()
if ds.Destroyed() {
ds.mu.RUnlock()
return nil, rsm.ErrClusterClosed
}
ds.ensureNotDestroyed()
var dp *C.uchar
dp = nil
if len(data) > 0 {
dp = (*C.uchar)(unsafe.Pointer(&data[0]))
}
r := C.LookupDBStateMachine(ds.dataStore, dp, C.size_t(len(data)))
result := C.GoBytes(unsafe.Pointer(r.result), C.int(r.size))
C.FreeLookupResult(ds.dataStore, r)
ds.mu.RUnlock()
return result, nil
}
// GetHash returns an integer value representing the state of the data store.
func (ds *StateMachineWrapper) GetHash() uint64 {
ds.ensureNotDestroyed()
v := C.GetHashDBStateMachine(ds.dataStore)
return uint64(v)
}
// SaveSnapshot saves the state of the data store to the snapshot file specified
// by the fp input string.
func (ds *StateMachineWrapper) SaveSnapshot(fp string,
collection statemachine.ISnapshotFileCollection) (uint64, error) {
ds.ensureNotDestroyed()
writer, err := rsm.NewSnapshotWriter(fp)
if err != nil {
return 0, err
}
smsz, err := ds.SaveSessions(writer)
if err != nil {
plog.Errorf("save session failed %v", err)
writer.Close()
return 0, err
}
writerOID := AddManagedObject(writer)
collectionOID := AddManagedObject(collection)
doneChOID := AddManagedObject(ds.done)
r := C.SaveSnapshotDBStateMachine(ds.dataStore,
C.uint64_t(writerOID), C.uint64_t(collectionOID), C.uint64_t(doneChOID))
errno := int(r.error)
err = getErrorFromErrNo(errno)
if err != nil {
plog.Errorf("save snapshot failed, %v", err)
writer.Close()
return 0, err
}
sz := uint64(r.size)
if err := writer.SaveHeader(smsz, sz); err != nil {
plog.Errorf("save header failed %v", err)
writer.Close()
return 0, err
}
if err := writer.Close(); err != nil {
return 0, err
}
return uint64(r.size) + smsz + rsm.SnapshotHeaderSize, nil
}
// RecoverFromSnapshot recovers the state of the data store from the snapshot
// file specified by the fp input string.
func (ds *StateMachineWrapper) RecoverFromSnapshot(fp string,
files []statemachine.SnapshotFile) error {
ds.ensureNotDestroyed()
reader, err := rsm.NewSnapshotReader(fp)
if err != nil {
return err
}
header, err := reader.GetHeader()
if err != nil {
reader.Close()
return err
}
reader.ValidateHeader(header)
err = ds.LoadSessions(reader)
if err != nil {
return err
}
cf := C.GetCollectedFile()
defer C.FreeCollectedFile(cf)
for _, file := range files {
fpdata := []byte(file.Filepath)
metadata := file.Metadata
C.AddToCollectedFile(cf, C.uint64_t(file.FileID),
(*C.char)(unsafe.Pointer(&fpdata[0])), C.size_t(len(fpdata)),
(*C.uchar)(unsafe.Pointer(&metadata[0])), C.size_t(len(metadata)))
}
readerOID := AddManagedObject(reader)
doneChOID := AddManagedObject(ds.done)
r := C.RecoverFromSnapshotDBStateMachine(ds.dataStore,
cf, C.uint64_t(readerOID), C.uint64_t(doneChOID))
err = getErrorFromErrNo(int(r))
if err != nil {
reader.Close()
return err
}
reader.ValidatePayload(header)
return reader.Close()
}
func (ds *StateMachineWrapper) ensureNotDestroyed() {
if ds.Destroyed() {
panic("using a destroyed data store instance detected")
}
}
func getCPPSOFileName(dsname string) string {
d := strings.ToLower(dsname)
return fmt.Sprintf("./dragonboat-cpp-plugin-%s.so", d)
}