-
Notifications
You must be signed in to change notification settings - Fork 12
/
gocouchbase_utils.go
460 lines (383 loc) · 12.7 KB
/
gocouchbase_utils.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// Copyright 2018-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"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/couchbase/cbauth"
"github.com/couchbase/go-couchbase"
"github.com/couchbase/gomemcached"
)
const SOURCE_TYPE_COUCHBASE = "couchbase"
const SOURCE_TYPE_DCP = "dcp"
func init() {
if gomemcached.MaxBodyLen < int(3e7) { // 30,000,000.
gomemcached.MaxBodyLen = int(3e7)
}
}
// ----------------------------------------------------------------
type cbBucketInfo struct {
cbBkt *couchbase.Bucket
lastChecked time.Time
}
type cbBucketMap struct {
// Mutex to serialize access to entries
m sync.Mutex
// Map of couchbase.Bucket instances by bucket <name>:<uuid>
entries map[string]*cbBucketInfo
}
var statsCBBktMap *cbBucketMap
// Fetches a couchbase bucket instance with the requested uuid,
// if not found creates a new instance and stashes it in the map.
func (cb *cbBucketMap) fetchCouchbaseBucket(name, uuid, params, server string,
options map[string]string) (*couchbase.Bucket, error) {
cb.m.Lock()
defer cb.m.Unlock()
key := name + ":" + uuid
createNewInstance := false
_, exists := cb.entries[key]
if !exists {
// If not found, create new bucket instance
createNewInstance = true
} else {
timeSinceLastCheck := time.Since(cb.entries[key].lastChecked)
if timeSinceLastCheck >= CouchbaseNodesRecheckInterval {
// If time elapsed since last check is greater than the set
// CouchbaseNodesRecheckInterval, re-check to see the state
// of the couchbase cluster.
if cb.entries[key].cbBkt.NodeListChanged() {
// If a change has been detected, reset the bucket instance.
cb.entries[key].cbBkt.Close()
delete(cb.entries, key)
createNewInstance = true
} else {
// Update the last checked time
cb.entries[key].lastChecked = time.Now()
}
}
}
if createNewInstance {
bucket, err := CouchbaseBucket(name, uuid, params, server, options)
if err != nil {
return nil, err
}
vbm := bucket.VBServerMap()
if vbm == nil || len(vbm.VBucketMap) == 0 {
bucket.Close()
return nil, fmt.Errorf("gocouchbase_utils: CouchbaseBucket"+
" vbucket map not available yet,"+
" server: %s, bucketName: %s",
server, name)
}
cb.entries[key] = &cbBucketInfo{cbBkt: bucket, lastChecked: time.Now()}
}
return cb.entries[key].cbBkt, nil
}
// Closes and removes the couchbase bucket instance with the uuid.
func (cb *cbBucketMap) closeCouchbaseBucket(name, uuid string) {
cb.m.Lock()
defer cb.m.Unlock()
key := name + ":" + uuid
bktInfo, exists := cb.entries[key]
if exists {
bktInfo.cbBkt.Close()
delete(cb.entries, key)
}
}
func (cb *cbBucketMap) closeAllCouchbaseBuckets() {
cb.m.Lock()
for _, entry := range cb.entries {
entry.cbBkt.Close()
}
if len(cb.entries) > 0 {
cb.entries = make(map[string]*cbBucketInfo)
}
cb.m.Unlock()
}
func init() {
// Initialize statsCBBktMap
statsCBBktMap = &cbBucketMap{
entries: make(map[string]*cbBucketInfo),
}
}
// ----------------------------------------------------------------
// CouchbaseBucket is a helper function to connect to a couchbase bucket.
func CouchbaseBucket(sourceName, sourceUUID, sourceParams, serverIn string,
options map[string]string) (*couchbase.Bucket, error) {
server, poolName, bucketName :=
CouchbaseParseSourceName(serverIn, "default", sourceName)
auth, err := cbAuth(sourceName, sourceParams, options)
if err != nil {
return nil, fmt.Errorf("gocouchbase_utils: CouchbaseBucket, cbAuth,"+
" bucketName: %s, err: %v", bucketName, err)
}
// If sourceName were a couchbase REST/HTTP URL, a single server URL
// is what is built and returned from CouchbaseParseSourceName, in
// which case the svrs array below will contain just the one server.
// If the sourceName weren't a URL, the serverIn passed into the
// API is just returned as is. Note that as the user is permitted
// to add multiple servers concatenated - delimited by the ';', the
// following Split operation is necessary to break meaningful URLs
// apart.
//
// Following this, we iterate over all the meaningful URLs and
// attempt connection with the couchbase cluster until a successful
// connection is made.
svrs := strings.Split(server, ";")
var client couchbase.Client
for _, svr := range svrs {
client, err = couchbase.ConnectWithAuth(svr, auth)
if err == nil {
break
}
}
if err != nil {
return nil, fmt.Errorf("gocouchbase_utils: CouchbaseBucket"+
" connection failed, server: %s, poolName: %s,"+
" bucketName: %s, sourceParams: %q, err: %v,"+
" please check that your authUser and authPassword are correct"+
" and that your couchbase cluster (%q) is available",
server, poolName, bucketName, sourceParams, err, server)
}
ss := GetSecuritySetting()
if ss.EncryptionEnabled {
if err = client.InitTLS(ClientCertFile); err != nil {
return nil, fmt.Errorf("gocouchbase_helper: CouchbaseBucket"+
" failed to initialize TLS for client, server: %s, err: %v",
server, err)
}
}
pool, err := client.GetPool(poolName)
if err != nil {
return nil, fmt.Errorf("gocouchbase_utils: CouchbaseBucket"+
" failed GetPool, server: %s, poolName: %s,"+
" bucketName: %s, sourceParams: %q, err: %v",
server, poolName, bucketName, sourceParams, err)
}
bucket, err := pool.GetBucket(bucketName)
if err != nil {
return nil, err
}
if sourceUUID != "" && sourceUUID != bucket.UUID {
bucket.Close()
return nil, ErrCouchbaseMismatchedBucketUUID
}
return bucket, nil
}
// ----------------------------------------------------------------
// CouchbasePartitions parses a sourceParams for a couchbase
// data-source/feed.
func CouchbasePartitions(sourceType, sourceName, sourceUUID, sourceParams,
serverIn string, options map[string]string) (
partitions []string, err error) {
bucket, err := statsCBBktMap.fetchCouchbaseBucket(sourceName, sourceUUID,
sourceParams, serverIn, options)
if err != nil {
return nil, err
}
// Validate bucketTypesAllowed here.
bucketTypesAllowed := "membase"
if options["bucketTypesAllowed"] != "" {
bucketTypesAllowed = options["bucketTypesAllowed"]
}
if !strings.Contains(bucketTypesAllowed, bucket.Type) {
return nil, fmt.Errorf("bucketTypesAllowed: '%v', but request for '%v'",
bucketTypesAllowed, bucket.Type)
}
vbm := bucket.VBServerMap()
if vbm == nil || len(vbm.VBucketMap) == 0 {
return nil, fmt.Errorf("gocouchbase_utils: CouchbasePartitions"+
" no VBServerMap, server: %s, sourceName: %s, err: %v",
serverIn, sourceName, err)
}
// NOTE: We assume that vbucket numbers are continuous
// integers starting from 0.
numVBuckets := len(vbm.VBucketMap)
rv := make([]string, numVBuckets)
for i := 0; i < numVBuckets; i++ {
rv[i] = strconv.Itoa(i)
}
return rv, nil
}
// ----------------------------------------------------------------
// CouchbasePartitionSeqs returns a map keyed by partition/vbucket ID
// with values of each vbucket's UUID / high_seqno. It implements the
// FeedPartitionsFunc func signature.
func CouchbasePartitionSeqs(sourceType, sourceName, sourceUUID,
sourceParams, serverIn string,
options map[string]string) (
map[string]UUIDSeq, error) {
if options["disableCollectionsSupport"] == "true" {
return nil,
fmt.Errorf("CouchbasePartitionSeqs supported only with collections")
}
bucket, err := statsCBBktMap.fetchCouchbaseBucket(sourceName, sourceUUID,
sourceParams, serverIn, options)
if err != nil {
return nil, err
}
rv := map[string]UUIDSeq{}
stats := bucket.GatherStats("vbucket")
collectionsStats := bucket.GatherStats("collections-details")
for node, gatheredStats := range stats {
if gatheredStats.Err != nil {
return nil, gatheredStats.Err
}
nodeStats := gatheredStats.Stats
if len(nodeStats) <= 0 {
continue
}
for _, vbid := range vbucketIdStrings {
stateVal, ok := nodeStats["vb_"+vbid]
if !ok || stateVal != "active" {
continue
}
// obtains seq no. for _default collection of _default scope only
seqStr, ok := collectionsStats[node].Stats["vb_"+vbid+":0x0:high_seqno"]
if !ok {
continue
}
seq, err := strconv.ParseUint(seqStr, 10, 64)
if err == nil {
rv[vbid+":_default:_default"] = UUIDSeq{
UUID: "0",
Seq: seq,
}
}
}
}
return rv, nil
}
// ----------------------------------------------------------------
// CouchbaseStats returns a map of aggregated ("aggStats") and
// per-node stats ("nodesStats"). It implements the FeedStatsFunc
// func signature.
func CouchbaseStats(sourceType, sourceName, sourceUUID,
sourceParams, serverIn string,
options map[string]string, statsKind string) (
map[string]interface{}, error) {
bucket, err := statsCBBktMap.fetchCouchbaseBucket(sourceName, sourceUUID,
sourceParams, serverIn, options)
if err != nil {
return nil, err
}
if statsKind == "collections" {
// collections not supported via go-couchbase
statsKind = ""
}
nodesStats := bucket.GetStats(statsKind)
aggStats := map[string]int64{} // Calculate aggregates.
for _, nodeStats := range nodesStats {
for k, v := range nodeStats {
iv, err := strconv.ParseInt(v, 10, 64)
if err == nil {
aggStats[k] += iv
}
}
}
rv := map[string]interface{}{
"aggStats": aggStats,
"nodesStats": nodesStats,
}
if statsKind == "" {
rv["docCount"] = aggStats["curr_items"]
}
return rv, nil
}
// ----------------------------------------------------------------
// CouchbaseSourceVBucketLookUp looks up the source vBucketID for a given
// document ID and index.
func CouchbaseSourceVBucketLookUp(docID, serverIn string,
sourceDetails *IndexDef, req *http.Request) (string, error) {
server, uname, pwd, err := parseParams(serverIn, req)
if err != nil {
return "", err
}
authParams := `{"authUser": "` + uname + `",` + `"authPassword":"` + pwd + `"}`
if sourceDetails.SourceType != SOURCE_TYPE_COUCHBASE &&
sourceDetails.SourceType != SOURCE_TYPE_DCP {
return "", fmt.Errorf("operation not supported on " +
sourceDetails.SourceType + " type bucket " +
sourceDetails.SourceName)
}
bucket, err := CouchbaseBucket(sourceDetails.SourceName, "",
authParams, server, nil)
if err != nil {
return "", err
}
defer bucket.Close()
vbm := bucket.VBServerMap()
if vbm == nil || len(vbm.VBucketMap) == 0 {
return "", fmt.Errorf("gocouchbase_utils: CouchbaseSourceVBucketLookUp"+
" no VBServerMap, server: %s, sourceName: %s, err: %v",
server, sourceDetails.SourceName, err)
}
return strconv.Itoa(int(bucket.VBHash(docID))), nil
}
// ----------------------------------------------------------------
// CouchbaseSourceUUIDLookUp fetches the sourceUUID for the provided sourceName.
func CouchbaseSourceUUIDLookUp(sourceName, sourceParams, serverIn string,
options map[string]string) (string, error) {
bucket, err := CouchbaseBucket(sourceName, "", sourceParams, serverIn, options)
if err != nil {
return "", fmt.Errorf("gocouchbase_utils: CouchbaseSourceUUIDLookUp,"+
" bucketName: %v, err: %v", sourceName, err)
}
uuid := bucket.GetUUID()
bucket.Close()
return uuid, nil
}
// ----------------------------------------------------------------
// CBAuthParams are common couchbase data-source/feed specific
// connection parameters that may be part of a sourceParams JSON.
type CBAuthParams struct {
AuthUser string `json:"authUser"` // May be "" for no auth.
AuthPassword string `json:"authPassword"`
AuthSaslUser string `json:"authSaslUser"` // May be "" for no auth.
AuthSaslPassword string `json:"authSaslPassword"`
}
func (d *CBAuthParams) GetCredentials() (string, string, string) {
// TODO: bucketName not necessarily userName.
return d.AuthUser, d.AuthPassword, d.AuthUser
}
// ----------------------------------------------------------------
// CBAuthParamsSasl implements the cbdatasource.ServerCredProvider
// interface.
type CBAuthParamsSasl struct {
CBAuthParams
}
func (d *CBAuthParamsSasl) GetSaslCredentials() (string, string) {
return d.AuthSaslUser, d.AuthSaslPassword
}
func cbAuth(sourceName, sourceParams string, options map[string]string) (
auth couchbase.AuthHandler, err error) {
params := &CBAuthParams{}
if sourceParams != "" {
err := UnmarshalJSON([]byte(sourceParams), params)
if err != nil {
return nil, fmt.Errorf("gocouchbase_utils: cbAuth" +
" failed to parse sourceParams JSON to CBAuthParams")
}
}
auth = params
if params.AuthSaslUser != "" {
auth = &CBAuthParamsSasl{*params}
}
authType := ""
if options != nil {
authType = options["authType"]
}
if authType == "cbauth" {
auth = cbauth.NewAuthHandler(nil).ForBucket(sourceName)
}
return auth, nil
}