-
Notifications
You must be signed in to change notification settings - Fork 104
/
cluster_searchindexes.go
706 lines (593 loc) · 19.2 KB
/
cluster_searchindexes.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
package gocb
import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"time"
"github.com/pkg/errors"
)
type jsonSearchIndexResp struct {
Status string `json:"status"`
IndexDef *jsonSearchIndex `json:"indexDef"`
}
type jsonSearchIndexDefs struct {
IndexDefs map[string]jsonSearchIndex `json:"indexDefs"`
ImplVersion string `json:"implVersion"`
}
type jsonSearchIndexesResp struct {
Status string `json:"status"`
IndexDefs jsonSearchIndexDefs `json:"indexDefs"`
}
type jsonSearchIndex struct {
UUID string `json:"uuid"`
Name string `json:"name"`
SourceName string `json:"sourceName"`
Type string `json:"type"`
Params map[string]interface{} `json:"params"`
SourceUUID string `json:"sourceUUID"`
SourceParams map[string]interface{} `json:"sourceParams"`
SourceType string `json:"sourceType"`
PlanParams map[string]interface{} `json:"planParams"`
}
// SearchIndex is used to define a search index.
type SearchIndex struct {
// UUID is required for updates. It provides a means of ensuring consistency, the UUID must match the UUID value
// for the index on the server.
UUID string
// Name represents the name of this index.
Name string
// SourceName is the name of the source of the data for the index e.g. bucket name.
SourceName string
// Type is the type of index, e.g. fulltext-index or fulltext-alias.
Type string
// IndexParams are index properties such as store type and mappings.
Params map[string]interface{}
// SourceUUID is the UUID of the data source, this can be used to more tightly tie the index to a source.
SourceUUID string
// SourceParams are extra parameters to be defined. These are usually things like advanced connection and tuning
// parameters.
SourceParams map[string]interface{}
// SourceType is the type of the data source, e.g. couchbase or nil depending on the Type field.
SourceType string
// PlanParams are plan properties such as number of replicas and number of partitions.
PlanParams map[string]interface{}
}
func (si *SearchIndex) fromData(data jsonSearchIndex) error {
si.UUID = data.UUID
si.Name = data.Name
si.SourceName = data.SourceName
si.Type = data.Type
si.Params = data.Params
si.SourceUUID = data.SourceUUID
si.SourceParams = data.SourceParams
si.SourceType = data.SourceType
si.PlanParams = data.PlanParams
return nil
}
func (si *SearchIndex) toData() (jsonSearchIndex, error) {
var data jsonSearchIndex
data.UUID = si.UUID
data.Name = si.Name
data.SourceName = si.SourceName
data.Type = si.Type
data.Params = si.Params
data.SourceUUID = si.SourceUUID
data.SourceParams = si.SourceParams
data.SourceType = si.SourceType
data.PlanParams = si.PlanParams
return data, nil
}
// SearchIndexManager provides methods for performing Couchbase search index management.
type SearchIndexManager struct {
mgmtProvider mgmtProvider
tracer RequestTracer
meter Meter
}
func (sm *SearchIndexManager) tryParseErrorMessage(req *mgmtRequest, resp *mgmtResponse) error {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
logDebugf("Failed to read search index response body: %s", err)
return nil
}
var bodyErr error
if strings.Contains(strings.ToLower(string(b)), "index not found") {
bodyErr = ErrIndexNotFound
} else if strings.Contains(strings.ToLower(string(b)), "index with the same name already exists") {
bodyErr = ErrIndexExists
} else {
bodyErr = errors.New(string(b))
}
return makeGenericMgmtError(bodyErr, req, resp)
}
func (sm *SearchIndexManager) doMgmtRequest(req mgmtRequest) (*mgmtResponse, error) {
resp, err := sm.mgmtProvider.executeMgmtRequest(req)
if err != nil {
return nil, err
}
return resp, nil
}
// GetAllSearchIndexOptions is the set of options available to the search indexes GetAllIndexes operation.
type GetAllSearchIndexOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
}
// GetAllIndexes retrieves all of the search indexes for the cluster.
func (sm *SearchIndexManager) GetAllIndexes(opts *GetAllSearchIndexOptions) ([]SearchIndex, error) {
if opts == nil {
opts = &GetAllSearchIndexOptions{}
}
start := time.Now()
defer valueRecord(sm.meter, meterValueServiceManagement, "manager_search_get_all_indexes", start)
span := createSpan(sm.tracer, opts.ParentSpan, "manager_search_get_all_indexes", "management")
span.SetAttribute("db.operation", "GET /api/index")
defer span.End()
req := mgmtRequest{
Service: ServiceTypeSearch,
Method: "GET",
Path: "/api/index",
IsIdempotent: true,
RetryStrategy: opts.RetryStrategy,
Timeout: opts.Timeout,
parentSpanCtx: span.Context(),
}
resp, err := sm.doMgmtRequest(req)
if err != nil {
return nil, err
}
defer ensureBodyClosed(resp.Body)
if resp.StatusCode != 200 {
idxErr := sm.tryParseErrorMessage(&req, resp)
if idxErr != nil {
return nil, idxErr
}
return nil, makeMgmtBadStatusError("failed to get index", &req, resp)
}
var indexesResp jsonSearchIndexesResp
jsonDec := json.NewDecoder(resp.Body)
err = jsonDec.Decode(&indexesResp)
if err != nil {
return nil, err
}
indexDefs := indexesResp.IndexDefs.IndexDefs
var indexes []SearchIndex
for _, indexData := range indexDefs {
var index SearchIndex
err := index.fromData(indexData)
if err != nil {
return nil, err
}
indexes = append(indexes, index)
}
return indexes, nil
}
// GetSearchIndexOptions is the set of options available to the search indexes GetIndex operation.
type GetSearchIndexOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
}
// GetIndex retrieves a specific search index by name.
func (sm *SearchIndexManager) GetIndex(indexName string, opts *GetSearchIndexOptions) (*SearchIndex, error) {
if opts == nil {
opts = &GetSearchIndexOptions{}
}
start := time.Now()
defer valueRecord(sm.meter, meterValueServiceManagement, "manager_search_get_index", start)
path := fmt.Sprintf("/api/index/%s", indexName)
span := createSpan(sm.tracer, opts.ParentSpan, "manager_search_get_index", "management")
span.SetAttribute("db.operation", "GET "+path)
defer span.End()
req := mgmtRequest{
Service: ServiceTypeSearch,
Method: "GET",
Path: path,
IsIdempotent: true,
RetryStrategy: opts.RetryStrategy,
Timeout: opts.Timeout,
parentSpanCtx: span.Context(),
}
resp, err := sm.doMgmtRequest(req)
if err != nil {
return nil, err
}
defer ensureBodyClosed(resp.Body)
if resp.StatusCode != 200 {
idxErr := sm.tryParseErrorMessage(&req, resp)
if idxErr != nil {
return nil, idxErr
}
return nil, makeMgmtBadStatusError("failed to get index", &req, resp)
}
var indexResp jsonSearchIndexResp
jsonDec := json.NewDecoder(resp.Body)
err = jsonDec.Decode(&indexResp)
if err != nil {
return nil, err
}
var indexDef SearchIndex
err = indexDef.fromData(*indexResp.IndexDef)
if err != nil {
return nil, err
}
return &indexDef, nil
}
// UpsertSearchIndexOptions is the set of options available to the search index manager UpsertIndex operation.
type UpsertSearchIndexOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
}
// UpsertIndex creates or updates a search index.
func (sm *SearchIndexManager) UpsertIndex(indexDefinition SearchIndex, opts *UpsertSearchIndexOptions) error {
if opts == nil {
opts = &UpsertSearchIndexOptions{}
}
if indexDefinition.Name == "" {
return invalidArgumentsError{"index name cannot be empty"}
}
if indexDefinition.Type == "" {
return invalidArgumentsError{"index type cannot be empty"}
}
start := time.Now()
defer valueRecord(sm.meter, meterValueServiceManagement, "manager_search_upsert_index", start)
path := fmt.Sprintf("/api/index/%s", indexDefinition.Name)
span := createSpan(sm.tracer, opts.ParentSpan, "manager_search_upsert_index", "management")
span.SetAttribute("db.operation", "PUT "+path)
defer span.End()
indexData, err := indexDefinition.toData()
if err != nil {
return err
}
b, err := json.Marshal(indexData)
if err != nil {
return err
}
req := mgmtRequest{
Service: ServiceTypeSearch,
Method: "PUT",
Path: path,
Headers: map[string]string{
"cache-control": "no-cache",
},
Body: b,
RetryStrategy: opts.RetryStrategy,
Timeout: opts.Timeout,
parentSpanCtx: span.Context(),
}
resp, err := sm.doMgmtRequest(req)
if err != nil {
return err
}
defer ensureBodyClosed(resp.Body)
if resp.StatusCode != 200 {
idxErr := sm.tryParseErrorMessage(&req, resp)
if idxErr != nil {
return idxErr
}
return makeMgmtBadStatusError("failed to create index", &req, resp)
}
return nil
}
// DropSearchIndexOptions is the set of options available to the search index DropIndex operation.
type DropSearchIndexOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
}
// DropIndex removes the search index with the specific name.
func (sm *SearchIndexManager) DropIndex(indexName string, opts *DropSearchIndexOptions) error {
if opts == nil {
opts = &DropSearchIndexOptions{}
}
if indexName == "" {
return invalidArgumentsError{"indexName cannot be empty"}
}
start := time.Now()
defer valueRecord(sm.meter, meterValueServiceManagement, "manager_search_drop_index", start)
path := fmt.Sprintf("/api/index/%s", indexName)
span := createSpan(sm.tracer, opts.ParentSpan, "manager_search_drop_index", "management")
span.SetAttribute("db.operation", "DELETE "+path)
defer span.End()
req := mgmtRequest{
Service: ServiceTypeSearch,
Method: "DELETE",
Path: path,
RetryStrategy: opts.RetryStrategy,
Timeout: opts.Timeout,
parentSpanCtx: span.Context(),
}
resp, err := sm.doMgmtRequest(req)
if err != nil {
return err
}
defer ensureBodyClosed(resp.Body)
if resp.StatusCode != 200 {
return makeMgmtBadStatusError("failed to drop the index", &req, resp)
}
return nil
}
// AnalyzeDocumentOptions is the set of options available to the search index AnalyzeDocument operation.
type AnalyzeDocumentOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
}
// AnalyzeDocument returns how a doc is analyzed against a specific index.
func (sm *SearchIndexManager) AnalyzeDocument(indexName string, doc interface{}, opts *AnalyzeDocumentOptions) ([]interface{}, error) {
if opts == nil {
opts = &AnalyzeDocumentOptions{}
}
if indexName == "" {
return nil, invalidArgumentsError{"indexName cannot be empty"}
}
path := fmt.Sprintf("/api/index/%s/analyzeDoc", indexName)
span := createSpan(sm.tracer, opts.ParentSpan, "manager_search_analyze_document", "management")
span.SetAttribute("db.operation", "POST "+path)
defer span.End()
b, err := json.Marshal(doc)
if err != nil {
return nil, err
}
req := mgmtRequest{
Service: ServiceTypeSearch,
Method: "POST",
Path: path,
Body: b,
IsIdempotent: true,
RetryStrategy: opts.RetryStrategy,
Timeout: opts.Timeout,
parentSpanCtx: span.Context(),
}
resp, err := sm.doMgmtRequest(req)
if err != nil {
return nil, err
}
defer ensureBodyClosed(resp.Body)
if resp.StatusCode != 200 {
idxErr := sm.tryParseErrorMessage(&req, resp)
if idxErr != nil {
return nil, idxErr
}
return nil, makeMgmtBadStatusError("failed to analyze document", &req, resp)
}
var analysis struct {
Status string `json:"status"`
Analyzed []interface{} `json:"analyzed"`
}
jsonDec := json.NewDecoder(resp.Body)
err = jsonDec.Decode(&analysis)
if err != nil {
return nil, err
}
return analysis.Analyzed, nil
}
// GetIndexedDocumentsCountOptions is the set of options available to the search index GetIndexedDocumentsCount operation.
type GetIndexedDocumentsCountOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
}
// GetIndexedDocumentsCount retrieves the document count for a search index.
func (sm *SearchIndexManager) GetIndexedDocumentsCount(indexName string, opts *GetIndexedDocumentsCountOptions) (uint64, error) {
if opts == nil {
opts = &GetIndexedDocumentsCountOptions{}
}
if indexName == "" {
return 0, invalidArgumentsError{"indexName cannot be empty"}
}
path := fmt.Sprintf("/api/index/%s/count", indexName)
span := createSpan(sm.tracer, opts.ParentSpan, "manager_search_get_indexed_documents_count", "management")
span.SetAttribute("db.operation", "GET "+path)
defer span.End()
req := mgmtRequest{
Service: ServiceTypeSearch,
Method: "GET",
Path: path,
IsIdempotent: true,
RetryStrategy: opts.RetryStrategy,
Timeout: opts.Timeout,
parentSpanCtx: span.Context(),
}
resp, err := sm.doMgmtRequest(req)
if err != nil {
return 0, err
}
defer ensureBodyClosed(resp.Body)
if resp.StatusCode != 200 {
idxErr := sm.tryParseErrorMessage(&req, resp)
if idxErr != nil {
return 0, idxErr
}
return 0, makeMgmtBadStatusError("failed to get the indexed documents count", &req, resp)
}
var count struct {
Count uint64 `json:"count"`
}
jsonDec := json.NewDecoder(resp.Body)
err = jsonDec.Decode(&count)
if err != nil {
return 0, err
}
return count.Count, nil
}
func (sm *SearchIndexManager) performControlRequest(
tracectx RequestSpanContext,
method, uri string,
timeout time.Duration,
retryStrategy RetryStrategy,
) error {
req := mgmtRequest{
Service: ServiceTypeSearch,
Method: method,
Path: uri,
IsIdempotent: true,
Timeout: timeout,
RetryStrategy: retryStrategy,
parentSpanCtx: tracectx,
}
resp, err := sm.doMgmtRequest(req)
if err != nil {
return err
}
defer ensureBodyClosed(resp.Body)
if resp.StatusCode != 200 {
idxErr := sm.tryParseErrorMessage(&req, resp)
if idxErr != nil {
return idxErr
}
return makeMgmtBadStatusError("failed to perform the control request", &req, resp)
}
return nil
}
// PauseIngestSearchIndexOptions is the set of options available to the search index PauseIngest operation.
type PauseIngestSearchIndexOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
}
// PauseIngest pauses updates and maintenance for an index.
func (sm *SearchIndexManager) PauseIngest(indexName string, opts *PauseIngestSearchIndexOptions) error {
if opts == nil {
opts = &PauseIngestSearchIndexOptions{}
}
if indexName == "" {
return invalidArgumentsError{"indexName cannot be empty"}
}
path := fmt.Sprintf("/api/index/%s/ingestControl/pause", indexName)
span := createSpan(sm.tracer, opts.ParentSpan, "manager_search_pause_ingest", "management")
span.SetAttribute("db.operation", "POST "+path)
defer span.End()
return sm.performControlRequest(
span.Context(),
"POST",
path,
opts.Timeout,
opts.RetryStrategy)
}
// ResumeIngestSearchIndexOptions is the set of options available to the search index ResumeIngest operation.
type ResumeIngestSearchIndexOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
}
// ResumeIngest resumes updates and maintenance for an index.
func (sm *SearchIndexManager) ResumeIngest(indexName string, opts *ResumeIngestSearchIndexOptions) error {
if opts == nil {
opts = &ResumeIngestSearchIndexOptions{}
}
if indexName == "" {
return invalidArgumentsError{"indexName cannot be empty"}
}
path := fmt.Sprintf("/api/index/%s/ingestControl/resume", indexName)
span := createSpan(sm.tracer, opts.ParentSpan, "manager_search_resume_ingest", "management")
span.SetAttribute("db.operation", "POST "+path)
defer span.End()
return sm.performControlRequest(
span.Context(),
"POST",
path,
opts.Timeout,
opts.RetryStrategy)
}
// AllowQueryingSearchIndexOptions is the set of options available to the search index AllowQuerying operation.
type AllowQueryingSearchIndexOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
}
// AllowQuerying allows querying against an index.
func (sm *SearchIndexManager) AllowQuerying(indexName string, opts *AllowQueryingSearchIndexOptions) error {
if opts == nil {
opts = &AllowQueryingSearchIndexOptions{}
}
if indexName == "" {
return invalidArgumentsError{"indexName cannot be empty"}
}
path := fmt.Sprintf("/api/index/%s/queryControl/allow", indexName)
span := createSpan(sm.tracer, opts.ParentSpan, "manager_search_allow_querying", "management")
span.SetAttribute("db.operation", "POST "+path)
defer span.End()
return sm.performControlRequest(
span.Context(),
"POST",
path,
opts.Timeout,
opts.RetryStrategy)
}
// DisallowQueryingSearchIndexOptions is the set of options available to the search index DisallowQuerying operation.
type DisallowQueryingSearchIndexOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
}
// DisallowQuerying disallows querying against an index.
func (sm *SearchIndexManager) DisallowQuerying(indexName string, opts *AllowQueryingSearchIndexOptions) error {
if opts == nil {
opts = &AllowQueryingSearchIndexOptions{}
}
if indexName == "" {
return invalidArgumentsError{"indexName cannot be empty"}
}
path := fmt.Sprintf("/api/index/%s/queryControl/disallow", indexName)
span := createSpan(sm.tracer, opts.ParentSpan, "manager_search_disallow_querying", "management")
span.SetAttribute("db.operation", "POST "+path)
defer span.End()
return sm.performControlRequest(
span.Context(),
"POST",
path,
opts.Timeout,
opts.RetryStrategy)
}
// FreezePlanSearchIndexOptions is the set of options available to the search index FreezePlan operation.
type FreezePlanSearchIndexOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
}
// FreezePlan freezes the assignment of index partitions to nodes.
func (sm *SearchIndexManager) FreezePlan(indexName string, opts *AllowQueryingSearchIndexOptions) error {
if opts == nil {
opts = &AllowQueryingSearchIndexOptions{}
}
if indexName == "" {
return invalidArgumentsError{"indexName cannot be empty"}
}
path := fmt.Sprintf("/api/index/%s/planFreezeControl/freeze", indexName)
span := createSpan(sm.tracer, opts.ParentSpan, "manager_search_freeze_plan", "management")
span.SetAttribute("db.operation", "POST "+path)
defer span.End()
return sm.performControlRequest(
span.Context(),
"POST",
path,
opts.Timeout,
opts.RetryStrategy)
}
// UnfreezePlanSearchIndexOptions is the set of options available to the search index UnfreezePlan operation.
type UnfreezePlanSearchIndexOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
}
// UnfreezePlan unfreezes the assignment of index partitions to nodes.
func (sm *SearchIndexManager) UnfreezePlan(indexName string, opts *AllowQueryingSearchIndexOptions) error {
if opts == nil {
opts = &AllowQueryingSearchIndexOptions{}
}
if indexName == "" {
return invalidArgumentsError{"indexName cannot be empty"}
}
path := fmt.Sprintf("/api/index/%s/planFreezeControl/unfreeze", indexName)
span := createSpan(sm.tracer, opts.ParentSpan, "manager_search_unfreeze_plan", "management")
span.SetAttribute("db.operation", "POST "+path)
defer span.End()
return sm.performControlRequest(
span.Context(),
"POST",
path,
opts.Timeout,
opts.RetryStrategy)
}