-
Notifications
You must be signed in to change notification settings - Fork 12
/
feed_tap.go
276 lines (239 loc) · 6.54 KB
/
feed_tap.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
// Copyright 2014-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package cbgt
import (
"fmt"
"io"
log "github.com/couchbase/clog"
"github.com/couchbase/go-couchbase"
"github.com/couchbase/gomemcached/client"
)
const SOURCE_GOCOUCHBASE_TAP = "couchbase-tap"
// DEST_EXTRAS_TYPE_TAP represents the extras that comes from TAP
// protocol.
const DEST_EXTRAS_TYPE_TAP = DestExtrasType(0x0001)
func init() {
RegisterFeedType(SOURCE_GOCOUCHBASE_TAP,
&FeedType{
Start: StartTAPFeed,
Partitions: CouchbasePartitions,
Public: false,
Description: "general/" + SOURCE_GOCOUCHBASE_TAP +
" - Couchbase Server data source, via TAP protocol",
StartSample: &TAPFeedParams{},
})
}
// StartDCPFeed starts a TAP related feed and is registered at
// init/startup time with the system via RegisterFeedType().
func StartTAPFeed(mgr *Manager, feedName, indexName, indexUUID,
sourceType, sourceName, bucketUUID, params string,
dests map[string]Dest) error {
server, poolName, bucketName :=
CouchbaseParseSourceName(mgr.server, "default", sourceName)
feed, err := NewTAPFeed(feedName, indexName, server, poolName,
bucketName, bucketUUID, params, BasicPartitionFunc, dests,
mgr.tagsMap != nil && !mgr.tagsMap["feed"])
if err != nil {
return fmt.Errorf("feed_tap: could not prepare TAP to server: %s,"+
" bucketName: %s, indexName: %s, err: %v",
mgr.server, bucketName, indexName, err)
}
err = feed.Start()
if err != nil {
return fmt.Errorf("feed_tap: could not start, server: %s, err: %v",
mgr.server, err)
}
err = mgr.registerFeed(feed)
if err != nil {
feed.Close()
return err
}
return nil
}
// A TAPFeed implements the Feed interface and handles the TAP
// protocol to receive data from a couchbase data source.
type TAPFeed struct {
name string
indexName string
url string
poolName string
bucketName string
bucketUUID string
params *TAPFeedParams
pf DestPartitionFunc
dests map[string]Dest
disable bool
closeCh chan bool
doneCh chan bool
doneErr error
doneMsg string
}
// TAPFeedParams represents the JSON of the sourceParams for a TAP
// feed.
type TAPFeedParams struct {
BackoffFactor float32 `json:"backoffFactor"`
SleepInitMS int `json:"sleepInitMS"`
SleepMaxMS int `json:"sleepMaxMS"`
}
// NewTAPFeed creates a new, ready-to-be-started TAPFeed.
func NewTAPFeed(name, indexName, url, poolName, bucketName, bucketUUID,
paramsStr string, pf DestPartitionFunc, dests map[string]Dest,
disable bool) (*TAPFeed, error) {
params := &TAPFeedParams{}
if paramsStr != "" {
err := UnmarshalJSON([]byte(paramsStr), params)
if err != nil {
return nil, err
}
}
return &TAPFeed{
name: name,
indexName: indexName,
url: url,
poolName: poolName,
bucketName: bucketName,
bucketUUID: bucketUUID,
params: params,
pf: pf,
dests: dests,
disable: disable,
closeCh: make(chan bool),
doneCh: make(chan bool),
doneErr: nil,
doneMsg: "",
}, nil
}
func (t *TAPFeed) Name() string {
return t.name
}
func (t *TAPFeed) IndexName() string {
return t.indexName
}
func (t *TAPFeed) Start() error {
if t.disable {
log.Printf("feed_tap: disable, name: %s", t.Name())
return nil
}
log.Printf("feed_tap: start, name: %s", t.Name())
backoffFactor := t.params.BackoffFactor
if backoffFactor <= 0.0 {
backoffFactor = FEED_BACKOFF_FACTOR
}
sleepInitMS := t.params.SleepInitMS
if sleepInitMS <= 0 {
sleepInitMS = FEED_SLEEP_INIT_MS
}
sleepMaxMS := t.params.SleepMaxMS
if sleepMaxMS <= 0 {
sleepMaxMS = FEED_SLEEP_MAX_MS
}
go ExponentialBackoffLoop(t.Name(),
func() int {
progress, err := t.feed()
if err != nil {
log.Warnf("feed_tap: name: %s, progress: %d, err: %v",
t.Name(), progress, err)
}
return progress
},
sleepInitMS, backoffFactor, sleepMaxMS)
return nil
}
func (t *TAPFeed) feed() (int, error) {
select {
case <-t.closeCh:
t.doneErr = nil
t.doneMsg = "closeCh closed"
close(t.doneCh)
return -1, nil
default:
}
bucket, err := couchbase.GetBucket(t.url, t.poolName, t.bucketName)
if err != nil {
return 0, err
}
defer bucket.Close()
if t.bucketUUID != "" && t.bucketUUID != bucket.UUID {
bucket.Close()
return -1, fmt.Errorf("feed_tap: mismatched bucket uuid,"+
"bucketName: %s, bucketUUID: %s, bucket.UUID: %s",
t.bucketName, t.bucketUUID, bucket.UUID)
}
args := memcached.TapArguments{}
vbuckets, err := ParsePartitionsToVBucketIds(t.dests)
if err != nil {
return -1, err
}
if len(vbuckets) > 0 {
args.VBuckets = vbuckets
}
feed, err := bucket.StartTapFeed(&args)
if err != nil {
return 0, err
}
defer feed.Close()
// TODO: Maybe TAPFeed should do a rollback to zero if it finds it
// needs to do a full backfill.
// TODO: This TAPFeed implementation currently only works against
// a couchbase cluster that has just a single node.
log.Printf("feed_tap: running, url: %s,"+
" poolName: %s, bucketName: %s, vbuckets: %#v",
t.url, t.poolName, t.bucketName, vbuckets)
loop:
for {
select {
case <-t.closeCh:
t.doneErr = nil
t.doneMsg = "closeCh closed"
close(t.doneCh)
return -1, nil
case req, alive := <-feed.C:
if !alive {
break loop
}
log.Printf("feed_tap: received from url: %s,"+
" poolName: %s, bucketName: %s, opcode: %s, req: %#v",
t.url, t.poolName, t.bucketName, req.Opcode, req)
partition, dest, err :=
VBucketIdToPartitionDest(t.pf, t.dests, req.VBucket, req.Key)
if err != nil {
return 1, err
}
if req.Opcode == memcached.TapMutation {
// TODO: TAP feed, what about flags, expiration, etc?
err = dest.DataUpdate(partition, req.Key, 0, req.Value,
req.Cas, DEST_EXTRAS_TYPE_NIL, nil)
} else if req.Opcode == memcached.TapDeletion {
// TODO: TAP feed, what about flags, expiration, etc?
err = dest.DataDelete(partition, req.Key, 0,
req.Cas, DEST_EXTRAS_TYPE_NIL, nil)
}
if err != nil {
return 1, err
}
}
}
return 1, nil
}
func (t *TAPFeed) Close() error {
select {
case <-t.doneCh:
return t.doneErr
default:
}
close(t.closeCh)
<-t.doneCh
return t.doneErr
}
func (t *TAPFeed) Dests() map[string]Dest {
return t.dests
}
func (t *TAPFeed) Stats(w io.Writer) error {
_, err := w.Write([]byte("{}"))
return err
}