-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnapshot_controller.go
More file actions
174 lines (129 loc) · 3.19 KB
/
snapshot_controller.go
File metadata and controls
174 lines (129 loc) · 3.19 KB
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
package eventstore
import (
"bytes"
"errors"
"fmt"
"sync/atomic"
"github.com/cfsghost/gosharding"
"github.com/cockroachdb/pebble"
)
func genSnapshotKey(collection []byte, key []byte) []byte {
return bytes.Join([][]byte{
collection,
key,
}, []byte("."))
}
type SnapshotController struct {
options *SnapshotOptions
shard *gosharding.Shard
handler func(*SnapshotRequest) error
}
func NewSnapshotController(options *SnapshotOptions) *SnapshotController {
ss := &SnapshotController{
options: options,
}
ss.initialize()
return ss
}
func (ss *SnapshotController) initialize() error {
// Initializing shard
options := gosharding.NewOptions()
options.PipelineCount = ss.options.WorkerCount
options.BufferSize = ss.options.BufferSize
options.Handler = func(id int32, data interface{}) {
req := data.(*SnapshotRequest)
defer snapshotRequestPool.Put(req)
err := ss.handleRequest(req)
if err != nil {
fmt.Println(err)
return
}
}
// Create shard with options
ss.shard = gosharding.NewShard(options)
return nil
}
func (ss *SnapshotController) handleRequest(req *SnapshotRequest) error {
b := req.Store.db.NewIndexedBatch()
req.Batch = b
err := ss.handle(req)
if err != nil {
fmt.Println(err)
return err
}
// Update snapshot state with the last sequence number
err = ss.updateSnapshotState(req)
if err != nil {
fmt.Println(err)
return err
}
err = b.Commit(pebble.NoSync)
if err != nil {
return err
}
req.Store.requestSync()
return nil
}
func (ss *SnapshotController) handle(req *SnapshotRequest) error {
if ss.handler == nil {
return errors.New("No snapshot handler is set")
}
err := ss.handler(req)
return err
}
func (ss *SnapshotController) updateSnapshotState(req *SnapshotRequest) error {
// Update store property
atomic.StoreUint64((*uint64)(&req.Store.state.snapshotLastSeq), req.Sequence)
return req.Store.state.syncSnapshotLastSeq(req.Store, req.Batch)
}
func (ss *SnapshotController) SetHandler(fn func(*SnapshotRequest) error) {
ss.handler = fn
}
func (ss *SnapshotController) Request(b *pebble.Batch, store *Store, seq uint64, data []byte) error {
// Create a new snapshot request
req := snapshotRequestPool.Get().(*SnapshotRequest)
req.Store = store
req.Sequence = seq
req.Data = data
req.Batch = nil
ss.shard.PushKV(store.name, req)
return nil
}
func (ss *SnapshotController) RecoverSnapshot(store *Store) error {
// Loading last sequence of snapshot
lastSeq := store.state.SnapshotLastSeq()
// No need to recover snapshot
if lastSeq >= store.state.LastSeq() {
return nil
}
key := Uint64ToBytes(lastSeq)
cur, err := store.cfEvent.List([]byte(""), key, &ListOptions{
WithoutRawPrefix: true,
})
if err != nil {
return err
}
for ; !cur.EOF(); cur.Next() {
seq := BytesToUint64(cur.GetKey())
if seq == lastSeq {
continue
}
data := make([]byte, len(cur.GetData()))
copy(data, cur.GetData())
// Create a new snapshot request
req := snapshotRequestPool.Get().(*SnapshotRequest)
req.Store = store
req.Sequence = seq
req.Data = data
req.Batch = nil
// process
err := ss.handleRequest(req)
if err != nil {
return err
}
// Release
snapshotRequestPool.Put(req)
}
cur.Close()
return nil
}