-
Notifications
You must be signed in to change notification settings - Fork 12
/
manager_api.go
740 lines (640 loc) · 22.6 KB
/
manager_api.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
// 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"
"regexp"
"strconv"
"sync/atomic"
"time"
log "github.com/couchbase/clog"
)
type BadRequestError struct {
errMsg string
}
func (e *BadRequestError) Error() string {
return e.errMsg
}
func NewBadRequestError(format string, args ...interface{}) error {
return fmt.Errorf("%w", &BadRequestError{
errMsg: fmt.Sprintf(format, args...),
})
}
type InternalServerError struct {
errMsg string
}
func (e *InternalServerError) Error() string {
return e.errMsg
}
func NewInternalServerError(format string, args ...interface{}) error {
return fmt.Errorf("%w", &InternalServerError{
errMsg: fmt.Sprintf(format, args...),
})
}
// cfgRefreshWaitExpiry represents the manager's config
// refresh timeout for metakv api access.
var cfgRefreshWaitExpiry = time.Second * 10
// INDEX_NAME_REGEXP is used to validate index definition names.
const INDEX_NAME_REGEXP = `^[A-Za-z][0-9A-Za-z_\-]*$`
// IndexPrepParams can be used to override any of the
// unset index parameters.
type IndexPrepParams struct {
Params string `json:"params,omitempty"`
SourceName string `json:"sourceName,omitempty"`
IndexName string `json:"indexName,omitempty"`
}
// Creates a logical index definition. A non-"" prevIndexUUID means
// an update to an existing index.
func (mgr *Manager) CreateIndex(sourceType,
sourceName, sourceUUID, sourceParams,
indexType, indexName, indexParams string, planParams PlanParams,
prevIndexUUID string) error {
_, _, err := mgr.CreateIndexEx(&CreateIndexPayload{
SourceType: sourceType,
SourceName: sourceName,
SourceUUID: sourceUUID,
SourceParams: sourceParams,
IndexType: indexType,
IndexName: indexName,
IndexParams: indexParams,
PlanParams: planParams,
PrevIndexUUID: prevIndexUUID,
})
return err
}
type CreateIndexPayload struct {
SourceType string
SourceName string
SourceUUID string
SourceParams string
IndexType string
IndexName string
IndexParams string
PlanParams PlanParams
PrevIndexUUID string
ScopedPrefix string
}
// Enforcing a maximum index name length of 209;
//
// See: MB-59858
// Linux, Windows and OSX enforce a maximum file/dir name length of 255.
// The index partitions directories created for these indexes have a
// hash suffix and here's a sample of how that'd look:
// -> <indexName>_1234567890123456_abcdefgh.pindex
// .. which can be separated into:
// - indexName (variable length)
// - '_' + hash(len=16) + '_' + hash(len=8) + '.pindex' (fixed length=33)
//
// So the max length that we can support for an index name is (255-33) = 222
// Also accommodating additional space (13) for any transient directory
// extensions - like during rebalance when a ".temp" can be suffixed and
// tar/gzip extensions.
const maxDirNameLen = 255
var (
// Additional space reserved for transient extensions (13)
miscExtLen = len(TempPathPrefix) + len(".tar.gz")
MaxIndexNameLength = maxDirNameLen - (26 + len(pindexPathSuffix) + miscExtLen)
)
func (mgr *Manager) CreateIndexEx(payload *CreateIndexPayload) (string, string, error) {
atomic.AddUint64(&mgr.stats.TotCreateIndex, 1)
if payload == nil {
return "", "", NewBadRequestError("manager_api: CreateIndex," +
" payload is nil")
}
adjustedIndexName := payload.ScopedPrefix + payload.IndexName
if payload.PrevIndexUUID == "" {
// index name validations during the fresh index creation.
matched, err := regexp.Match(INDEX_NAME_REGEXP, []byte(payload.IndexName))
if err != nil {
return adjustedIndexName, "", NewBadRequestError("manager_api: CreateIndex,"+
" indexName parsing problem,"+
" indexName: %s, err: %v", payload.IndexName, err)
}
if !matched {
return adjustedIndexName, "", NewBadRequestError("manager_api: CreateIndex,"+
" indexName is invalid, indexName: %q", payload.IndexName)
}
}
if len(adjustedIndexName) > MaxIndexNameLength {
return "", "", NewBadRequestError("manager_api: CreateIndex,"+
" chosen index name is too long, consider one that is less"+
" than %v characters", MaxIndexNameLength)
}
indexDef := &IndexDef{
Type: payload.IndexType,
Name: adjustedIndexName,
Params: payload.IndexParams,
SourceType: payload.SourceType,
SourceName: payload.SourceName,
SourceUUID: payload.SourceUUID,
SourceParams: payload.SourceParams,
PlanParams: payload.PlanParams,
}
pindexImplType, exists := PIndexImplTypes[payload.IndexType]
if !exists {
return adjustedIndexName, "", NewBadRequestError("manager_api: CreateIndex,"+
" unknown indexType: %s", payload.IndexType)
}
var err error
if pindexImplType.Prepare != nil {
indexDef, err = pindexImplType.Prepare(mgr, indexDef)
if err != nil {
return adjustedIndexName, "", fmt.Errorf("manager_api: CreateIndex, Prepare failed,"+
" err: %v", err)
}
}
payload.SourceParams = indexDef.SourceParams
payload.IndexParams = indexDef.Params
payload.IndexName = indexDef.Name
if pindexImplType.Validate != nil {
err = pindexImplType.Validate(
payload.IndexType, payload.IndexName, payload.IndexParams)
if err != nil {
return adjustedIndexName, "", fmt.Errorf("manager_api: CreateIndex, invalid,"+
" err: %v", err)
}
}
// First, check that the source exists.
payload.SourceParams, err = DataSourcePrepParams(
payload.SourceType,
payload.SourceName,
payload.SourceUUID,
payload.SourceParams,
mgr.server,
mgr.Options(),
)
if err != nil {
return adjustedIndexName, "", NewInternalServerError("manager_api: failed to connect to"+
" or retrieve information from source,"+
" sourceType: %s, sourceName: %s, sourceUUID: %s, err: %v",
payload.SourceType, payload.SourceName, payload.SourceUUID, err)
}
indexDef.SourceParams = payload.SourceParams
payload.SourceUUID, err = DataSourceUUID(
payload.SourceType,
payload.SourceName,
payload.SourceParams,
mgr.server, mgr.Options(),
)
if err != nil {
return adjustedIndexName, "", NewInternalServerError("manager_api: failed to fetch sourceUUID"+
" for sourceName: %s, sourceType: %s, err: %v",
payload.SourceName, payload.SourceType, err)
}
if len(payload.SourceUUID) > 0 {
// Feed's SourceUUIDLookUp is optional.
if len(indexDef.SourceUUID) == 0 {
// If sourceUUID is NOT available within the index def, update it.
indexDef.SourceUUID = payload.SourceUUID
} else if indexDef.SourceUUID != payload.SourceUUID {
// The sourceUUID provided within the index definition does NOT match
// the sourceUUID for the sourceName in the system.
return adjustedIndexName, "", NewBadRequestError("manager_api: CreateIndex failed, sourceUUID"+
" mismatched for sourceName: %s", payload.SourceName)
}
}
// Validate maxReplicasAllowed here.
maxReplicasAllowed, _ := strconv.Atoi(mgr.GetOption("maxReplicasAllowed"))
if payload.PlanParams.NumReplicas < 0 ||
payload.PlanParams.NumReplicas > maxReplicasAllowed {
return adjustedIndexName, "", NewBadRequestError("manager_api: CreateIndex failed, maxReplicasAllowed:"+
" '%v', but request for '%v'", maxReplicasAllowed, payload.PlanParams.NumReplicas)
}
nodeDefs, _, err := CfgGetNodeDefs(mgr.cfg, NODE_DEFS_KNOWN)
if err != nil {
return adjustedIndexName, "", NewInternalServerError("manager_api: CreateIndex failed, "+
"CfgGetNodeDefs err: %v", err)
}
if len(nodeDefs.NodeDefs) < payload.PlanParams.NumReplicas+1 {
return adjustedIndexName, "", NewBadRequestError("manager_api: CreateIndex failed, cluster needs %d "+
"search nodes to support the requested replica count of %d",
payload.PlanParams.NumReplicas+1, payload.PlanParams.NumReplicas)
}
version := CfgGetVersion(mgr.cfg)
indexCreateFunc := func() error {
indexDefs, cas, err := CfgGetIndexDefs(mgr.cfg)
if err != nil {
return NewInternalServerError("manager_api: CfgGetIndexDefs err: %v", err)
}
if indexDefs == nil {
indexDefs = NewIndexDefs(version)
}
if VersionGTE(mgr.version, indexDefs.ImplVersion) == false {
return NewInternalServerError("manager_api: could not create index,"+
" indexDefs.ImplVersion: %s > mgr.version: %s",
indexDefs.ImplVersion, mgr.version)
}
prevIndex, exists := indexDefs.IndexDefs[payload.IndexName]
if payload.PrevIndexUUID == "" { // New index creation.
if exists || prevIndex != nil {
return NewBadRequestError("manager_api: cannot create index because"+
" an index with the same name already exists: %s",
payload.IndexName)
}
} else if payload.PrevIndexUUID == "*" {
if exists && prevIndex != nil {
payload.PrevIndexUUID = prevIndex.UUID
}
} else { // Update index definition.
if !exists || prevIndex == nil {
return NewBadRequestError("manager_api: index missing for update,"+
" indexName: %s", payload.IndexName)
}
if prevIndex.UUID != payload.PrevIndexUUID {
return NewBadRequestError("manager_api:"+
" perhaps there was concurrent index definition update,"+
" current index UUID: %s, did not match input UUID: %s",
prevIndex.UUID, payload.PrevIndexUUID)
}
if prevIndex.PlanParams.PlanFrozen {
if (prevIndex.PlanParams.MaxPartitionsPerPIndex !=
indexDef.PlanParams.MaxPartitionsPerPIndex) ||
(prevIndex.PlanParams.NumReplicas !=
indexDef.PlanParams.NumReplicas) {
return NewBadRequestError("manager_api: cannot update"+
" partition or replica count for a planFrozen index,"+
" indexName: %s", payload.IndexName)
}
}
}
indexUUID := NewUUID()
indexDef.UUID = indexUUID
indexDefs.UUID = indexUUID
indexDefs.IndexDefs[payload.IndexName] = indexDef
indexDefs.ImplVersion = version
// NOTE: If our ImplVersion is still too old due to a race, we
// expect a more modern planner to catch it later.
_, err = CfgSetIndexDefs(mgr.cfg, indexDefs, cas)
return err
}
err = RetryOnCASMismatch(indexCreateFunc, 100)
if err != nil {
return adjustedIndexName, "", fmt.Errorf("manager_api: could not save indexDefs,"+
" err: %v", err)
}
mgr.refreshIndexDefsWithTimeout(cfgRefreshWaitExpiry)
mgr.PlannerKick("api/CreateIndex, indexName: " + payload.IndexName)
atomic.AddUint64(&mgr.stats.TotCreateIndexOk, 1)
if payload.PrevIndexUUID == "" {
log.Printf("manager_api: index definition created,"+
" indexType: %s, indexName: %s, indexUUID: %s",
indexDef.Type, indexDef.Name, indexDef.UUID)
} else {
log.Printf("manager_api: index definition updated,"+
" indexType: %s, indexName: %s, indexUUID: %s, prevIndexUUID: %s",
indexDef.Type, indexDef.Name, indexDef.UUID, payload.PrevIndexUUID)
}
if payload.IndexType == "fulltext-index" && len(payload.SourceName) > 0 {
// In case of index updates, reusing the existing dcp stats agent from
// the previous version of the index, since it doesn't increase the number
// of live indexes in the cluster
if payload.PrevIndexUUID == "" {
statsAgentsMap.registerAgents(payload.SourceName, payload.SourceUUID,
payload.SourceParams, mgr.Server(), mgr.Options())
}
}
event := NewSystemEvent(
IndexCreateEventID,
"info",
"Index created",
map[string]interface{}{
"indexName": adjustedIndexName,
"sourceName": indexDef.SourceName,
"indexUUID": indexDef.UUID,
})
if event != nil {
if payload.PrevIndexUUID != "" {
event.EventID = IndexUpdateEventID
event.Description = "Index updated"
}
err = PublishSystemEvent(event)
if err != nil {
log.Errorf("manager_api: unexpected system_event error"+
" err: %v", err)
}
}
return indexDef.Name, indexDef.UUID, nil
}
// DeleteIndex deletes a logical index definition.
func (mgr *Manager) DeleteIndex(indexName string) error {
log.Printf("manager_api: DeleteIndex, indexname: %s", indexName)
_, err := mgr.DeleteIndexEx(indexName, "")
if err != nil {
log.Errorf("manager_api: DeleteIndex, indexname: %s, err: %v",
indexName, err)
}
return err
}
// DeleteIndexEx deletes a logical index definition, with an optional
// indexUUID ("" means don't care).
func (mgr *Manager) DeleteIndexEx(indexName, indexUUID string) (
string, error) {
atomic.AddUint64(&mgr.stats.TotDeleteIndex, 1)
var indexDef *IndexDef
var exists bool
indexDeleteFunc := func() error {
indexDefs, cas, err := CfgGetIndexDefs(mgr.cfg)
if err != nil {
return err
}
if indexDefs == nil {
return NewBadRequestError("manager_api: no indexes on deletion"+
" of indexName: %s", indexName)
}
if VersionGTE(mgr.version, indexDefs.ImplVersion) == false {
return NewInternalServerError("manager_api: could not delete index,"+
" indexDefs.ImplVersion: %s > mgr.version: %s",
indexDefs.ImplVersion, mgr.version)
}
indexDef, exists = indexDefs.IndexDefs[indexName]
if !exists {
return NewBadRequestError("manager_api: index to delete missing,"+
" indexName: %s", indexName)
}
if indexUUID != "" && indexDef.UUID != indexUUID {
return NewBadRequestError("manager_api: index to delete wrong UUID,"+
" indexName: %s", indexName)
}
// Associated couchbase.Bucket instances and gocbcore.Agent/DCPAgent
// instances that are used for stats are closed by the ctl routine.
indexDefs.UUID = NewUUID()
delete(indexDefs.IndexDefs, indexName)
indexDefs.ImplVersion = CfgGetVersion(mgr.cfg)
// NOTE: if our ImplVersion is still too old due to a race, we
// expect a more modern planner to catch it later.
_, err = CfgSetIndexDefs(mgr.cfg, indexDefs, cas)
return err
}
err := RetryOnCASMismatch(indexDeleteFunc, 100)
if err != nil {
return "", fmt.Errorf("manager_api: could not save indexDefs,"+
" err: %v", err)
}
mgr.refreshIndexDefsWithTimeout(cfgRefreshWaitExpiry)
mgr.PlannerKick("api/DeleteIndex, indexName: " + indexName)
atomic.AddUint64(&mgr.stats.TotDeleteIndexOk, 1)
log.Printf("manager_api: index definition deleted,"+
" indexType: %s, indexName: %s, indexUUID: %s",
indexDef.Type, indexDef.Name, indexDef.UUID)
err = PublishSystemEvent(NewSystemEvent(
IndexDeleteEventID,
"info",
"Index deleted",
map[string]interface{}{
"indexName": indexDef.Name,
"sourceName": indexDef.SourceName,
"indexUUID": indexDef.UUID,
}))
if err != nil {
log.Errorf("manager_api: unexpected system_event error"+
" err: %v", err)
}
return indexDef.UUID, nil
}
// Index Control Default Values
//
// Applications interested in updating these defaults must do so
// at init() time.
//
// These defaults primarily serve the purpose of allowing all
// IndexDef consumers to get the value of CanRead and CanWrite
// for an index, even if the NodePlanParams are not present in
// the IndexDef (which is common for indexes that allow all nodes)
var (
DefaultIndexCanRead = true
DefaultIndexCanWrite = true
)
// IndexControl is used to change runtime properties of an index
// definition.
func (mgr *Manager) IndexControl(indexName, indexUUID, readOp, writeOp,
planFreezeOp string) error {
atomic.AddUint64(&mgr.stats.TotIndexControl, 1)
indexControlFunc := func() error {
indexDefs, cas, err := CfgGetIndexDefs(mgr.cfg)
if err != nil {
return err
}
if indexDefs == nil {
return fmt.Errorf("manager_api: no indexes,"+
" index read/write control, indexName: %s", indexName)
}
if VersionGTE(mgr.version, indexDefs.ImplVersion) == false {
return fmt.Errorf("manager_api: index read/write control,"+
" indexName: %s,"+
" indexDefs.ImplVersion: %s > mgr.version: %s",
indexName, indexDefs.ImplVersion, mgr.version)
}
indexDef, exists := indexDefs.IndexDefs[indexName]
if !exists || indexDef == nil {
return fmt.Errorf("manager_api: no index to read/write control,"+
" indexName: %s", indexName)
}
if indexUUID != "" && indexDef.UUID != indexUUID {
return fmt.Errorf("manager_api: index.UUID mismatched")
}
// refresh the UUID as we are updating the indexDef
newIndexUUID := NewUUID()
indexDef.UUID = newIndexUUID
indexDefs.UUID = newIndexUUID
if indexDef.PlanParams.NodePlanParams == nil {
indexDef.PlanParams.NodePlanParams =
map[string]map[string]*NodePlanParam{}
}
if indexDef.PlanParams.NodePlanParams[""] == nil {
indexDef.PlanParams.NodePlanParams[""] =
map[string]*NodePlanParam{}
}
if indexDef.PlanParams.NodePlanParams[""][""] == nil {
indexDef.PlanParams.NodePlanParams[""][""] = &NodePlanParam{
CanRead: true,
CanWrite: true,
}
}
// TODO: Allow for node UUID and planPIndex.Name inputs.
npp := indexDef.PlanParams.NodePlanParams[""][""]
if readOp != "" {
if readOp == "allow" || readOp == "resume" {
npp.CanRead = true
} else {
npp.CanRead = false
}
}
if writeOp != "" {
if writeOp == "allow" || writeOp == "resume" {
npp.CanWrite = true
} else {
npp.CanWrite = false
}
}
// Remove NodePlanParams from indexDef if all nodes are allowed to read/write.
// This is to avoid unnecessary data in the indexDef
//
// Readers of IndexDefs should assume that if NodePlanParams is empty,
// all nodes are allowed to read/write
if npp.CanRead && npp.CanWrite {
delete(indexDef.PlanParams.NodePlanParams[""], "")
delete(indexDef.PlanParams.NodePlanParams, "")
}
if planFreezeOp != "" {
indexDef.PlanParams.PlanFrozen = planFreezeOp == "freeze"
}
_, err = CfgSetIndexDefs(mgr.cfg, indexDefs, cas)
return err
}
err := RetryOnCASMismatch(indexControlFunc, 100)
if err != nil {
return fmt.Errorf("manager_api: could not save indexDefs,"+
" err: %v", err)
}
atomic.AddUint64(&mgr.stats.TotIndexControlOk, 1)
return nil
}
// BumpIndexDefs bumps the uuid of the index defs, to force planners
// and other downstream tasks to re-run.
func (mgr *Manager) BumpIndexDefs(indexDefsUUID string) error {
var indexDefs *IndexDefs
var err error
var cas uint64
bumpIndexDefsFunc := func() error {
indexDefs, cas, err = CfgGetIndexDefs(mgr.cfg)
if err != nil {
return err
}
if indexDefs == nil {
return fmt.Errorf("manager_api: no indexDefs to bump")
}
prevIndexImplVersion := indexDefs.ImplVersion
if VersionGTE(mgr.version, prevIndexImplVersion) == false {
return NewInternalServerError("manager_api: could not bump indexDefs,"+
" indexDefs.ImplVersion: %s > mgr.version: %s",
prevIndexImplVersion, mgr.version)
}
if indexDefsUUID != "" && indexDefs.UUID != indexDefsUUID {
return fmt.Errorf("manager_api: bump indexDefs wrong UUID")
}
indexDefs.UUID = NewUUID()
indexDefs.ImplVersion = prevIndexImplVersion
// NOTE: if our ImplVersion is still too old due to a race, we
// expect a more modern cbgt to do the work instead.
_, err = CfgSetIndexDefs(mgr.cfg, indexDefs, cas)
return err
}
err = RetryOnCASMismatch(bumpIndexDefsFunc, 100)
if err != nil {
return fmt.Errorf("manager_api: could not bump indexDefs,"+
" err: %v", err)
}
log.Printf("manager_api: bumped indexDefs, indexDefsUUID: %s",
indexDefs.UUID)
return nil
}
// DeleteAllIndexFromSource deletes all indexes with a given
// sourceType and sourceName.
func (mgr *Manager) DeleteAllIndexFromSource(
sourceType, sourceName, sourceUUID string) error {
var deletedCount uint64
indexDeleteFromSourceFunc := func() error {
indexDefs, cas, err := CfgGetIndexDefs(mgr.cfg)
if err != nil {
return err
}
if indexDefs == nil {
return fmt.Errorf("manager_api: DeleteAllIndexFromSource, no indexDefs")
}
if VersionGTE(mgr.version, indexDefs.ImplVersion) == false {
return NewInternalServerError("manager_api: DeleteAllIndexFromSource,"+
" indexDefs.ImplVersion: %s > mgr.version: %s",
indexDefs.ImplVersion, mgr.version)
}
// Close associated couchbase.Bucket instances used for stats
statsCBBktMap.closeCouchbaseBucket(sourceName, sourceUUID)
var deletedCount uint64
for indexName, indexDef := range indexDefs.IndexDefs {
if indexDef.SourceType == sourceType &&
indexDef.SourceName == sourceName {
if sourceUUID != "" && indexDef.SourceUUID != "" &&
sourceUUID != indexDef.SourceUUID {
continue
}
atomic.AddUint64(&mgr.stats.TotDeleteIndexBySource, 1)
delete(indexDefs.IndexDefs, indexName)
log.Printf("manager_api: starting index definition deletion,"+
" indexType: %s, indexName: %s, indexUUID: %s",
indexDef.Type, indexDef.Name, indexDef.UUID)
deletedCount++
// Release associated gocbcore.Agent/DCPAgent instances used for stats
statsAgentsMap.releaseAgents(sourceName)
}
}
// exit early if nothing to delete
if deletedCount == 0 {
return nil
}
// update the index definitions
indexDefs.UUID = NewUUID()
indexDefs.ImplVersion = CfgGetVersion(mgr.cfg)
_, err = CfgSetIndexDefs(mgr.cfg, indexDefs, cas)
return err
}
err := RetryOnCASMismatch(indexDeleteFromSourceFunc, 100)
if err != nil {
atomic.AddUint64(&mgr.stats.TotDeleteIndexBySourceErr, deletedCount)
return fmt.Errorf("manager_api: could not save indexDefs,"+
" err: %v", err)
}
atomic.AddUint64(&mgr.stats.TotDeleteIndexBySourceOk, deletedCount)
mgr.refreshIndexDefsWithTimeout(cfgRefreshWaitExpiry)
mgr.PlannerKick("api/DeleteIndexes, for bucket: " + sourceName)
// With MB-19117, we've seen cfg that strangely had empty
// indexDefs, but non-empty planPIndexes. Force bump the
// indexDefs so the planner and other downstream tasks re-run.
err = mgr.BumpIndexDefs("")
if err != nil {
return err
}
log.Printf("manager_api: DeleteAllIndexFromSource," +
" index deletions completed")
return nil
}
// DefaultCfgDebounceOffsetInMs represents the default value for
// the debounce interval for the config events.
var DefaultCfgDebounceOffsetInMs = int(500)
// DefaultNodeOffsetMultiplier represents the default value for
// of an offset multiplier for nodes.
var DefaultNodeOffsetMultiplier = int(4)
func (mgr *Manager) GetCfgDeBounceOffsetAndMultiplier() (int, int) {
offset, found := ParseOptionsInt(mgr.options, "cfgDebounceOffsetInMs")
if !found {
offset = DefaultCfgDebounceOffsetInMs
}
nm, found := ParseOptionsInt(mgr.options, "cfgNodeOffsetMultiplier")
if !found {
nm = DefaultNodeOffsetMultiplier
}
return offset, nm
}
// refreshIndexDefsWithTimeout refresh manager's index defs cache
// to immediately reflect the latest index definitions in the system
// on the node where this request was received (the ctl routine is
// responsible for eventually updating the cache on other nodes).
func (mgr *Manager) refreshIndexDefsWithTimeout(duration time.Duration) {
ch := make(chan error, 1)
refreshIndexDefCache := func() chan error {
go func() {
_, _, err := mgr.GetIndexDefs(true)
ch <- err
}()
return ch
}
select {
case <-refreshIndexDefCache():
case <-time.After(duration):
atomic.AddUint64(&mgr.stats.TotSlowConfigAccess, 1)
log.Printf("manager_api: GetIndexDefs found to be slow")
}
}