-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDcpHandler.go
418 lines (369 loc) · 11.2 KB
/
DcpHandler.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// Copyright (c) 2018 Couchbase, Inc.
// 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 dcp
import (
"crypto/sha512"
"encoding/binary"
"fmt"
"github.com/couchbase/gomemcached"
mcc "github.com/couchbase/gomemcached/client"
xdcrParts "github.com/couchbase/goxdcr/base/filter"
xdcrLog "github.com/couchbase/goxdcr/log"
"xdcrDiffer/base"
fdp "xdcrDiffer/fileDescriptorPool"
"xdcrDiffer/utils"
gocbcore "github.com/couchbase/gocbcore/v9"
"os"
"sync"
)
// implements StreamObserver
type DcpHandler struct {
dcpClient *DcpClient
fileDir string
index int
vbList []uint16
numberOfBins int
dataChan chan *Mutation
waitGrp sync.WaitGroup
finChan chan bool
bucketMap map[uint16]map[int]*Bucket
fdPool fdp.FdPoolIface
logger *xdcrLog.CommonLogger
filter xdcrParts.Filter
}
func NewDcpHandler(dcpClient *DcpClient, fileDir string, index int, vbList []uint16, numberOfBins, dataChanSize int, fdPool fdp.FdPoolIface) (*DcpHandler, error) {
if len(vbList) == 0 {
return nil, fmt.Errorf("vbList is empty for handler %v", index)
}
return &DcpHandler{
dcpClient: dcpClient,
fileDir: fileDir,
index: index,
vbList: vbList,
numberOfBins: numberOfBins,
dataChan: make(chan *Mutation, dataChanSize),
finChan: make(chan bool),
bucketMap: make(map[uint16]map[int]*Bucket),
fdPool: fdPool,
logger: dcpClient.logger,
filter: dcpClient.dcpDriver.filter,
}, nil
}
func (dh *DcpHandler) Start() error {
err := dh.initialize()
if err != nil {
return err
}
dh.waitGrp.Add(1)
go dh.processData()
return nil
}
func (dh *DcpHandler) Stop() {
close(dh.finChan)
// this sometimes does not return after a long time
//dh.waitGrp.Wait()
dh.cleanup()
}
func (dh *DcpHandler) initialize() error {
for _, vbno := range dh.vbList {
innerMap := make(map[int]*Bucket)
dh.bucketMap[vbno] = innerMap
for i := 0; i < dh.numberOfBins; i++ {
bucket, err := NewBucket(dh.fileDir, vbno, i, dh.fdPool, dh.logger)
if err != nil {
return err
}
innerMap[i] = bucket
}
}
return nil
}
func (dh *DcpHandler) cleanup() {
for _, vbno := range dh.vbList {
innerMap := dh.bucketMap[vbno]
if innerMap == nil {
dh.logger.Warnf("Cannot find innerMap for vbno %v at cleanup\n", vbno)
continue
}
for i := 0; i < dh.numberOfBins; i++ {
bucket := innerMap[i]
if bucket == nil {
dh.logger.Warnf("Cannot find bucket for vbno %v and index %v at cleanup\n", vbno, i)
continue
}
//fmt.Printf("%v DcpHandler closing bucket %v\n", dh.dcpClient.Name, i)
bucket.close()
}
}
}
func (dh *DcpHandler) processData() {
dh.logger.Debugf("%v DcpHandler %v processData starts..........\n", dh.dcpClient.Name, dh.index)
defer dh.logger.Debugf("%v DcpHandler %v processData exits..........\n", dh.dcpClient.Name, dh.index)
defer dh.waitGrp.Done()
for {
select {
case <-dh.finChan:
goto done
case mut := <-dh.dataChan:
dh.processMutation(mut)
}
}
done:
}
func (dh *DcpHandler) processMutation(mut *Mutation) {
var matched bool
var err error
var errStr string
var filterResult base.FilterResultType
if dh.filter != nil && mut.IsMutation() {
matched, err, errStr, _ = dh.filter.FilterUprEvent(mut.ToUprEvent())
if !matched {
filterResult = base.Filtered
}
if err != nil {
filterResult = base.UnableToFilter
dh.logger.Warnf("Err %v - (%v) when filtering mutation %v", err, errStr, mut)
}
}
valid := dh.dcpClient.dcpDriver.checkpointManager.HandleMutationEvent(mut, filterResult)
if !valid {
// if mutation is out of range, ignore it
return
}
vbno := mut.vbno
index := utils.GetBucketIndexFromKey(mut.key, dh.numberOfBins)
innerMap := dh.bucketMap[vbno]
if innerMap == nil {
panic(fmt.Sprintf("cannot find bucketMap for vbno %v", vbno))
}
bucket := innerMap[index]
if bucket == nil {
panic(fmt.Sprintf("cannot find bucket for index %v", index))
}
bucket.write(mut.Serialize())
}
func (dh *DcpHandler) writeToDataChan(mut *Mutation) {
select {
case dh.dataChan <- mut:
// provides an alternative exit path when dh stops
case <-dh.finChan:
}
}
func (dh *DcpHandler) SnapshotMarker(startSeqno, endSeqno uint64, vbno uint16, streamID uint16, snapshotType gocbcore.SnapshotState) {
dh.dcpClient.dcpDriver.checkpointManager.updateSnapshot(vbno, startSeqno, endSeqno)
}
func (dh *DcpHandler) Mutation(seqno, revId uint64, flags, expiry, lockTime uint32, cas uint64, datatype uint8, vbno uint16, collectionID uint32, streamID uint16, key, value []byte) {
// TODO - collectionID
dh.writeToDataChan(CreateMutation(vbno, key, seqno, revId, cas, flags, expiry, gomemcached.UPR_MUTATION, value, datatype))
}
func (dh *DcpHandler) Deletion(seqno, revId uint64, deleteTime uint32, cas uint64, datatype uint8, vbno uint16, collectionID uint32, streamID uint16, key, value []byte) {
// TODO - collectionID
dh.writeToDataChan(CreateMutation(vbno, key, seqno, revId, cas, 0, 0, gomemcached.UPR_DELETION, value, datatype))
}
func (dh *DcpHandler) Expiration(seqno, revId uint64, deleteTime uint32, cas uint64, vbno uint16, collectionID uint32, streamID uint16, key []byte) {
// TODO - collectionID
dh.writeToDataChan(CreateMutation(vbno, key, seqno, revId, cas, 0, 0, gomemcached.UPR_EXPIRATION, nil, 0 /*dataType*/))
}
func (dh *DcpHandler) End(vbno uint16, streamID uint16, err error) {
dh.dcpClient.dcpDriver.handleVbucketCompletion(vbno, err, "dcp stream ended")
}
func (dh *DcpHandler) CreateCollection(seqNo uint64, version uint8, vbID uint16, manifestUID uint64, scopeID uint32, collectionID uint32, ttl uint32, streamID uint16, key []byte) {
// Don't care
}
func (dh *DcpHandler) DeleteCollection(seqNo uint64, version uint8, vbID uint16, manifestUID uint64, scopeID uint32, collectionID uint32, streamID uint16) {
// Don't care
}
func (dh *DcpHandler) FlushCollection(seqNo uint64, version uint8, vbID uint16, manifestUID uint64, collectionID uint32) {
// Don't care
}
func (dh *DcpHandler) CreateScope(seqNo uint64, version uint8, vbID uint16, manifestUID uint64, scopeID uint32, streamID uint16, key []byte) {
// Don't care
}
func (dh *DcpHandler) DeleteScope(seqNo uint64, version uint8, vbID uint16, manifestUID uint64, scopeID uint32, streamID uint16) {
// Don't care
}
func (dh *DcpHandler) ModifyCollection(seqNo uint64, version uint8, vbID uint16, manifestUID uint64, collectionID uint32, ttl uint32, streamID uint16) {
// Don't care
}
func (dh *DcpHandler) OSOSnapshot(vbID uint16, snapshotType uint32, streamID uint16) {
// Don't care
}
func (dh *DcpHandler) SeqNoAdvanced(vbID uint16, bySeqno uint64, streamID uint16) {
// Don't care
}
type Bucket struct {
data []byte
// current index in data for next write
index int
file *os.File
fileName string
fdPoolCb fdp.FileOp
closeOp func() error
logger *xdcrLog.CommonLogger
}
func NewBucket(fileDir string, vbno uint16, bucketIndex int, fdPool fdp.FdPoolIface, logger *xdcrLog.CommonLogger) (*Bucket, error) {
fileName := utils.GetFileName(fileDir, vbno, bucketIndex)
var cb fdp.FileOp
var closeOp func() error
var err error
var file *os.File
if fdPool == nil {
file, err = os.OpenFile(fileName, os.O_APPEND|os.O_WRONLY|os.O_CREATE, base.FileModeReadWrite)
if err != nil {
return nil, err
}
} else {
_, cb, err = fdPool.RegisterFileHandle(fileName)
if err != nil {
return nil, err
}
closeOp = func() error {
return fdPool.DeRegisterFileHandle(fileName)
}
}
return &Bucket{
data: make([]byte, base.BucketBufferCapacity),
index: 0,
file: file,
fileName: fileName,
fdPoolCb: cb,
closeOp: closeOp,
logger: logger,
}, nil
}
func (b *Bucket) write(item []byte) error {
if b.index+len(item) > base.BucketBufferCapacity {
err := b.flushToFile()
if err != nil {
return err
}
}
copy(b.data[b.index:], item)
b.index += len(item)
return nil
}
func (b *Bucket) flushToFile() error {
var numOfBytes int
var err error
if b.fdPoolCb != nil {
numOfBytes, err = b.fdPoolCb(b.data[:b.index])
} else {
numOfBytes, err = b.file.Write(b.data[:b.index])
}
if err != nil {
return err
}
if numOfBytes != b.index {
return fmt.Errorf("Incomplete write. expected=%v, actual=%v", b.index, numOfBytes)
}
b.index = 0
return nil
}
func (b *Bucket) close() {
err := b.flushToFile()
if err != nil {
b.logger.Errorf("Error flushing to file %v at bucket close err=%v\n", b.fileName, err)
}
if b.fdPoolCb != nil {
err = b.closeOp()
if err != nil {
b.logger.Errorf("Error closing file %v. err=%v\n", b.fileName, err)
}
} else {
err = b.file.Close()
if err != nil {
b.logger.Errorf("Error closing file %v. err=%v\n", b.fileName, err)
}
}
}
type Mutation struct {
vbno uint16
key []byte
seqno uint64
revId uint64
cas uint64
flags uint32
expiry uint32
opCode gomemcached.CommandCode
value []byte
datatype uint8
}
func CreateMutation(vbno uint16, key []byte, seqno, revId, cas uint64, flags, expiry uint32, opCode gomemcached.CommandCode, value []byte, datatype uint8) *Mutation {
return &Mutation{
vbno: vbno,
key: key,
seqno: seqno,
revId: revId,
cas: cas,
flags: flags,
expiry: expiry,
opCode: opCode,
value: value,
}
}
func (m *Mutation) IsExpiration() bool {
return m.opCode == gomemcached.UPR_EXPIRATION
}
func (m *Mutation) IsDeletion() bool {
return m.opCode == gomemcached.UPR_DELETION
}
func (m *Mutation) IsMutation() bool {
return m.opCode == gomemcached.UPR_MUTATION
}
func (m *Mutation) ToUprEvent() *mcc.UprEvent {
return &mcc.UprEvent{
Opcode: m.opCode,
VBucket: m.vbno,
DataType: m.datatype,
Flags: m.flags,
Expiry: m.expiry,
Key: m.key,
Value: m.value,
Cas: m.cas,
Seqno: m.seqno,
}
}
// serialize mutation into []byte
// format:
// keyLen - 2 bytes
// key - length specified by keyLen
// seqno - 8 bytes
// revId - 8 bytes
// cas - 8 bytes
// flags - 4 bytes
// expiry - 4 bytes
// opType - 2 byte
// datatype - 2 byte
// hash - 64 bytes
func (mut *Mutation) Serialize() []byte {
keyLen := len(mut.key)
ret := make([]byte, keyLen+base.BodyLength+2)
bodyHash := sha512.Sum512(mut.value)
pos := 0
binary.BigEndian.PutUint16(ret[pos:pos+2], uint16(keyLen))
pos += 2
copy(ret[pos:pos+keyLen], mut.key)
pos += keyLen
binary.BigEndian.PutUint64(ret[pos:pos+8], mut.seqno)
pos += 8
binary.BigEndian.PutUint64(ret[pos:pos+8], mut.revId)
pos += 8
binary.BigEndian.PutUint64(ret[pos:pos+8], mut.cas)
pos += 8
binary.BigEndian.PutUint32(ret[pos:pos+4], mut.flags)
pos += 4
binary.BigEndian.PutUint32(ret[pos:pos+4], mut.expiry)
pos += 4
binary.BigEndian.PutUint16(ret[pos:pos+2], uint16(mut.opCode))
pos += 2
binary.BigEndian.PutUint16(ret[pos:pos+2], uint16(mut.datatype))
pos += 2
copy(ret[pos:], bodyHash[:])
return ret
}