-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathpersister.go
298 lines (259 loc) · 8.37 KB
/
persister.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
package collector
// persist oplog on disk
import (
"mongoshake/oplog"
"mongoshake/collector/configure"
"sync"
"mongoshake/common"
"sync/atomic"
"time"
"github.com/vinllen/mgo/bson"
LOG "github.com/vinllen/log4go"
"github.com/vinllen/go-diskqueue"
"github.com/gugemichael/nimo4go"
)
const (
FullSyncReaderOplogStoreDiskReadBatch = 10000
)
type Persister struct {
replset string // name
sync *OplogSyncer // not owned, inner call
// batch data([]byte) together and send to downstream
Buffer [][]byte
nextQueuePosition uint64
// enable disk persist
enableDiskPersist bool
// stage of fetch and store oplog
fetchStage int32
// disk queue used to store oplog temporarily
DiskQueue *diskQueue.DiskQueue
diskQueueMutex sync.Mutex // disk queue mutex
diskQueueLastTs bson.MongoTimestamp // the last oplog timestamp in disk queue
// metric info, used in print
diskWriteCount uint64
diskReadCount uint64
}
func NewPersister(replset string, sync *OplogSyncer) *Persister {
p := &Persister{
replset: replset,
sync: sync,
Buffer: make([][]byte, 0, conf.Options.IncrSyncFetcherBufferCapacity),
nextQueuePosition: 0,
enableDiskPersist: conf.Options.SyncMode == utils.VarSyncModeAll &&
conf.Options.FullSyncReaderOplogStoreDisk,
fetchStage: utils.FetchStageStoreUnknown,
diskQueueLastTs: -1, // initial set 1
}
return p
}
func (p *Persister) Start() {
if p.enableDiskPersist {
go p.retrieve()
}
}
func (p *Persister) SetFetchStage(fetchStage int32) {
LOG.Info("persister replset[%v] update fetch status to: %v", p.replset, utils.LogFetchStage(fetchStage))
atomic.StoreInt32(&p.fetchStage, fetchStage)
}
func (p *Persister) GetFetchStage() int32 {
return atomic.LoadInt32(&p.fetchStage)
}
func (p *Persister) InitDiskQueue(dqName string) {
fetchStage := p.GetFetchStage()
// fetchStage shouldn't change between here
if fetchStage != utils.FetchStageStoreDiskNoApply && fetchStage != utils.FetchStageStoreDiskApply {
LOG.Crashf("persister replset[%v] init disk queue in illegal fetchStage %v",
p.replset, utils.LogFetchStage(fetchStage))
}
if p.DiskQueue != nil {
LOG.Crashf("init disk queue failed: already exist")
}
p.DiskQueue = diskQueue.NewDiskQueue(dqName, conf.Options.LogDirectory,
conf.Options.FullSyncReaderOplogStoreDiskMaxSize, FullSyncReaderOplogStoreDiskReadBatch,
1<<30, 0, 1<<26,
1000, 2*time.Second)
}
func (p *Persister) GetQueryTsFromDiskQueue() bson.MongoTimestamp {
if p.DiskQueue == nil {
LOG.Crashf("persister replset[%v] get query timestamp from nil disk queue", p.replset)
}
logData := p.DiskQueue.GetLastWriteData()
if len(logData) == 0 {
return 0
}
if conf.Options.IncrSyncMongoFetchMethod == utils.VarIncrSyncMongoFetchMethodOplog {
log := new(oplog.PartialLog)
if err := bson.Unmarshal(logData, log); err != nil {
LOG.Crashf("unmarshal oplog[%v] failed[%v]", logData, err)
}
// assert
if log.Namespace == "" {
LOG.Crashf("unmarshal data to oplog failed: %v", log)
}
return log.Timestamp
} else {
// change_stream
log := new(oplog.Event)
if err := bson.Unmarshal(logData, log); err != nil {
LOG.Crashf("unmarshal oplog[%v] failed[%v]", logData, err)
}
// assert
if log.OperationType == "" {
LOG.Crashf("unmarshal data to change stream event failed: %v", log)
}
return log.ClusterTime
}
}
// inject data
func (p *Persister) Inject(input []byte) {
// only used to test the reader, discard anything
switch conf.Options.IncrSyncReaderDebug {
case utils.VarIncrSyncReaderDebugNone:
break
case utils.VarIncrSyncReaderDebugDiscard:
return
case utils.VarIncrSyncReaderDebugPrint:
var test interface{}
bson.Unmarshal(input, &test)
LOG.Info("print debug: %v", test)
default:
break
}
if p.enableDiskPersist {
// current fetch stage
fetchStage := p.GetFetchStage()
if fetchStage == utils.FetchStageStoreMemoryApply {
p.PushToPendingQueue(input)
} else if p.DiskQueue != nil {
if input == nil {
// no need to store
return
}
// store local
p.diskQueueMutex.Lock()
if p.DiskQueue != nil { // double check
// should send to diskQueue
atomic.AddUint64(&p.diskWriteCount, 1)
if err := p.DiskQueue.Put(input); err != nil {
LOG.Crashf("persister inject replset[%v] put oplog to disk queue failed[%v]",
p.replset, err)
}
} else {
// should send to pending queue
p.PushToPendingQueue(input)
}
} else {
LOG.Crashf("persister inject replset[%v] has no diskQueue with fetch stage[%v]",
p.replset, utils.LogFetchStage(fetchStage))
}
} else {
p.PushToPendingQueue(input)
}
}
func (p *Persister) PushToPendingQueue(input []byte) {
flush := false
if input != nil {
p.Buffer = append(p.Buffer, input)
} else {
flush = true
}
if len(p.Buffer) >= conf.Options.IncrSyncFetcherBufferCapacity || (flush && len(p.Buffer) != 0) {
// we could simply ++syncer.resolverIndex. The max uint64 is 9223372036854774807
// and discard the skip situation. we assume nextQueueCursor couldn't be overflow
selected := int(p.nextQueuePosition % uint64(len(p.sync.PendingQueue)))
p.sync.PendingQueue[selected] <- p.Buffer
// clear old Buffer, we shouldn't use "p.Buffer = p.Buffer[:0]" because these addres won't
// be changed in the channel.
// p.Buffer = p.Buffer[:0]
p.Buffer = make([][]byte, 0, conf.Options.IncrSyncFetcherBufferCapacity)
// queue position = (queue position + 1) % n
p.nextQueuePosition++
}
}
func (p *Persister) retrieve() {
for range time.NewTicker(3 * time.Second).C {
stage := atomic.LoadInt32(&p.fetchStage)
switch stage {
case utils.FetchStageStoreDiskApply:
break
case utils.FetchStageStoreUnknown:
// do nothing
case utils.FetchStageStoreDiskNoApply:
// do nothing
default:
LOG.Crashf("invalid fetch stage[%v]", utils.LogFetchStage(stage))
}
}
LOG.Info("persister retrieve for replset[%v] begin to read from disk queue with depth[%v]",
p.replset, p.DiskQueue.Depth())
ticker := time.NewTicker(time.Second)
Loop:
for {
select {
case readData := <-p.DiskQueue.ReadChan():
if len(readData) == 0 {
continue
}
atomic.AddUint64(&p.diskReadCount, uint64(len(readData)))
for _, data := range readData {
p.PushToPendingQueue(data)
}
// move to next read
if err := p.DiskQueue.Next(); err != nil {
LOG.Crashf("persister replset[%v] retrieve get next failed[%v]", p.replset, err)
}
case <-ticker.C:
// check no more data batching?
if p.DiskQueue.Depth() < p.DiskQueue.BatchCount() {
break Loop
}
}
}
LOG.Info("persister retrieve for replset[%v] block fetch with disk queue depth[%v]",
p.replset, p.DiskQueue.Depth())
// wait to finish retrieve and continue fetch to store to memory
p.diskQueueMutex.Lock()
defer p.diskQueueMutex.Unlock() // lock till the end
readData := p.DiskQueue.ReadAll()
if len(readData) > 0 {
atomic.AddUint64(&p.diskReadCount, uint64(len(readData)))
for _, data := range readData {
// or.oplogChan <- &retOplog{&bson.Raw{Kind: 3, Data: data}, nil}
p.PushToPendingQueue(data)
}
// parse the last oplog timestamp
p.diskQueueLastTs = p.GetQueryTsFromDiskQueue()
if err := p.DiskQueue.Next(); err != nil {
LOG.Crash(err)
}
}
if p.DiskQueue.Depth() != 0 {
LOG.Crashf("persister retrieve for replset[%v] finish, but disk queue depth[%v] is not empty",
p.replset, p.DiskQueue.Depth())
}
p.SetFetchStage(utils.FetchStageStoreMemoryApply)
if err := p.DiskQueue.Delete(); err != nil {
LOG.Critical("persister retrieve for replset[%v] close disk queue error. %v", p.replset, err)
}
LOG.Info("persister retriever for replset[%v] exits", p.replset)
}
func (p *Persister) RestAPI() {
type PersistNode struct {
BufferUsed int `json:"buffer_used"`
BufferSize int `json:"buffer_size"`
EnableDiskPersist bool `json:"enable_disk_persist"`
FetchStage string `json:"fetch_stage"`
DiskWriteCount uint64 `json:"disk_write_count"`
DiskReadCount uint64 `json:"disk_read_count"`
}
utils.IncrSyncHttpApi.RegisterAPI("/persist", nimo.HttpGet, func([]byte) interface{} {
return &PersistNode{
BufferSize: conf.Options.IncrSyncFetcherBufferCapacity,
BufferUsed: len(p.Buffer),
EnableDiskPersist: p.enableDiskPersist,
FetchStage: utils.LogFetchStage(p.GetFetchStage()),
DiskWriteCount: atomic.LoadUint64(&p.diskWriteCount),
DiskReadCount: atomic.LoadUint64(&p.diskReadCount),
}
})
}