-
Notifications
You must be signed in to change notification settings - Fork 28
/
pindex_alias.go
400 lines (354 loc) · 11.7 KB
/
pindex_alias.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
// 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 cbft
import (
"encoding/binary"
"fmt"
"io"
"strings"
"github.com/blevesearch/bleve/v2"
"github.com/couchbase/cbgt"
"github.com/couchbase/cbgt/rest"
)
var maxAliasTargets = 100
func init() {
// Register alias with empty instantiation functions,
// so that "fulltext-alias" will show up in valid index types.
cbgt.RegisterPIndexImplType("fulltext-alias", &cbgt.PIndexImplType{
Prepare: PrepareAlias,
Validate: ValidateAlias,
Count: CountAlias,
Query: QueryAlias,
Description: "advanced/fulltext-alias" +
" - a full text index alias provides" +
" a naming level of indirection" +
" to one or more actual, target full text indexes",
StartSample: &AliasParams{
Targets: map[string]*AliasParamsTarget{
"yourIndexName": {},
},
},
UI: map[string]string{
"controllerInitName": "blevePIndexInitController",
},
})
}
// AliasParams holds the definition for a user-defined index alias. A
// user-defined index alias can be used as a level of indirection (the
// "LastQuartersSales" alias points currently to the "2014-Q3-Sales"
// index, but the administrator might repoint it in the future without
// changing the application) or to scatter-gather or fan-out a query
// across multiple real indexes (e.g., to query across customer
// records, product catalog, call-center records, etc, in one shot).
type AliasParams struct {
Targets map[string]*AliasParamsTarget `json:"targets"` // Keyed by indexName.
}
type AliasParamsTarget struct {
IndexUUID string `json:"indexUUID,omitempty"` // Optional.
}
// Overrideable for test-ability
var obtainUniqueKeyspaces = obtainIndexBucketScopesForDefinition
func PrepareAlias(mgr *cbgt.Manager, indexDef *cbgt.IndexDef) (*cbgt.IndexDef, error) {
if indexDef == nil {
return nil, cbgt.NewBadRequestError("PrepareAlias, indexDef is nil")
}
// Reset plan params for a full-text alias
indexDef.PlanParams = cbgt.PlanParams{}
// In case of a scoped index alias, decorate any undecorated index targets
if pos := strings.LastIndex(indexDef.Name, "."); pos > 0 {
params, err := parseAliasParams(indexDef.Params)
if err == nil {
for idxName := range params.Targets {
if !strings.Contains(idxName, ".") {
// Remove unscoped index target
delete(params.Targets, idxName)
// Replace with scoped index target
params.Targets[indexDef.Name[:pos]+"."+idxName] = &AliasParamsTarget{}
}
}
}
paramsBytes, err := MarshalJSON(params)
if err != nil {
return nil, cbgt.NewBadRequestError("PrepareAlias, unable to marshal params")
}
indexDef.Params = string(paramsBytes)
}
// map to track unique keyspaces (bucket.scope)
var uniqueKeyspaces map[string]int
var visitedIndexNames map[string]bool
var err error
if uniqueKeyspaces, err = obtainUniqueKeyspaces(
mgr, indexDef, uniqueKeyspaces, visitedIndexNames, 0); err != nil {
return nil, cbgt.NewBadRequestError("PrepareAlias, %v", err)
}
// alias to multiple keyspaces NOT allowed in serverless mode
if len(uniqueKeyspaces) > 1 && ServerlessMode {
return nil, cbgt.NewBadRequestError("PrepareAlias: multiple keyspaces NOT" +
" supported in serverless mode")
}
bucketName, scopeName := getKeyspaceFromScopedIndexName(indexDef.Name)
if len(bucketName) > 0 && len(scopeName) > 0 {
// No name decoration for a partially upgraded cluster
if !isClusterCompatibleFor(FeatureScopedIndexNamesVersion) {
return nil, cbgt.NewBadRequestError("PrepareAlias, scoped index aliases NOT" +
" supported in mixed version cluster")
}
scopedPrefix := bucketName + "." + scopeName
var numErrTargets int
for k, v := range uniqueKeyspaces {
if k != scopedPrefix {
numErrTargets += v
}
}
if numErrTargets > 0 {
return nil, cbgt.NewBadRequestError("PrepareAlias, scoped index alias CANNOT"+
" include %d target(s)", numErrTargets)
}
}
return indexDef, nil
}
func ValidateAlias(indexType, indexName, indexParams string) error {
params := AliasParams{}
if err := UnmarshalJSON([]byte(indexParams), ¶ms); err != nil {
return cbgt.NewBadRequestError("%v", err)
}
if len(params.Targets) == 0 {
return cbgt.NewBadRequestError("ValidateAlias: cannot create index alias" +
" because no index targets were specified")
}
return nil
}
func CountAlias(mgr *cbgt.Manager,
indexName, indexUUID string) (uint64, error) {
alias, _, err := bleveIndexAliasForUserIndexAlias(
mgr,
indexName,
indexUUID,
false,
nil,
nil,
false,
"",
)
if err != nil {
return 0, fmt.Errorf("alias: CountAlias indexAlias error,"+
" indexName: %s, indexUUID: %s, err: %v",
indexName, indexUUID, err)
}
return alias.DocCount()
}
func QueryAlias(mgr *cbgt.Manager, indexName, indexUUID string,
req []byte, res io.Writer) error {
queryCtlParams := cbgt.QueryCtlParams{
Ctl: cbgt.QueryCtl{
Timeout: cbgt.QUERY_CTL_DEFAULT_TIMEOUT_MS,
},
}
err := UnmarshalJSON(req, &queryCtlParams)
if err != nil {
return fmt.Errorf("alias: QueryAlias"+
" parsing queryCtlParams, err: %v", err)
}
var sr *SearchRequest
err = UnmarshalJSON(req, &sr)
if err != nil {
return fmt.Errorf("alias: QueryAlias"+
" parsing searchRequest, err: %v", err)
}
searchRequest, err := sr.ConvertToBleveSearchRequest()
if err != nil {
return fmt.Errorf("alias: QueryAlias"+
" parsing searchRequest, err: %v", err)
}
ctx, cancel, cancelCh := setupContextAndCancelCh(queryCtlParams, nil)
// defer a call to cancel, this ensures that goroutine from
// setupContextAndCancelCh always exits
defer cancel()
alias, multiCollIndex, err := bleveIndexAliasForUserIndexAlias(mgr,
indexName, indexUUID, true,
queryCtlParams.Ctl.Consistency, cancelCh, true,
queryCtlParams.Ctl.PartitionSelection)
if err != nil {
return err
}
searchResponse, err := alias.SearchInContext(ctx, searchRequest)
if err != nil {
return err
}
// additional processing with multi-collection indexes.
if multiCollIndex {
processAliasResponse(searchResponse)
}
rest.MustEncode(res, searchResponse)
return nil
}
func processAliasResponse(searchResponse *bleve.SearchResult) {
if searchResponse != nil {
if len(searchResponse.Hits) > 0 {
for _, hit := range searchResponse.Hits {
// extract indexName for the hit's Index field, which holds the pindex name;
// a pindex name has the format ..
// default_2d9ca20632cb632e_4c1c5584
// where "default" is the index name
var indexName string
if x := strings.LastIndex(hit.Index, "_"); x > 0 && x < len(hit.Index) {
temp := hit.Index[:x]
if x = strings.LastIndex(temp, "_"); x > 0 && x < len(hit.Index) {
indexName = temp[:x]
}
}
// if this is a multi collection index, then strip the collection UID
// from the hit ID and fill the details of source collection
if sdm, multiCollIndex :=
metaFieldValCache.getSourceDetailsMap(indexName); multiCollIndex {
idBytes := []byte(hit.ID)
cuid := binary.LittleEndian.Uint32(idBytes[:4])
if collName, ok := sdm.collUIDNameMap[cuid]; ok {
hit.ID = string(idBytes[4:])
if hit.Fields == nil {
hit.Fields = make(map[string]interface{})
}
hit.Fields["_$c"] = collName
}
}
}
}
}
}
func parseAliasParams(aliasDefParams string) (*AliasParams, error) {
params := AliasParams{}
err := UnmarshalJSON([]byte(aliasDefParams), ¶ms)
if err != nil {
return nil, err
}
return ¶ms, nil
}
func getRemoteClients(mgr *cbgt.Manager) addRemoteClients {
var addRemClients addRemoteClients
if isClusterCompatibleFor(FeatureGrpcVersion) {
addRemClients = addGrpcClients
}
if v := mgr.GetOption("SkipScatterGatherOverGrpc"); len(v) > 0 ||
addRemClients == nil {
addRemClients = addIndexClients
}
return addRemClients
}
// The indexName/indexUUID is for a user-defined index alias.
//
// TODO: One day support user-defined aliases for non-bleve indexes.
func bleveIndexAliasForUserIndexAlias(mgr *cbgt.Manager,
indexName, indexUUID string, ensureCanRead bool,
consistencyParams *cbgt.ConsistencyParams,
cancelCh <-chan bool, groupByNode bool,
partitionSelection string) (
bleve.IndexAlias, bool, error) {
alias := bleve.NewIndexAlias()
indexDefs, _, err := mgr.GetIndexDefs(false)
if err != nil {
return nil, false, fmt.Errorf("alias: could not get indexDefs,"+
" indexName: %s, err: %v", indexName, err)
}
num := 0
seenTargets := make(map[string]bool)
var multiCollIndex bool
var fillAlias func(aliasName, aliasUUID string) error
fillAlias = func(aliasName, aliasUUID string) error {
aliasDef := indexDefs.IndexDefs[aliasName]
if aliasDef == nil {
return fmt.Errorf("alias: could not get aliasDef,"+
" aliasName: %s, indexName: %s",
aliasName, indexName)
}
if aliasDef.Type != "fulltext-alias" {
return fmt.Errorf("alias: not fulltext-alias type: %s,"+
" aliasName: %s, indexName: %s",
aliasDef.Type, aliasName, indexName)
}
if aliasUUID != "" &&
aliasUUID != aliasDef.UUID {
return fmt.Errorf("alias: mismatched aliasUUID: %s,"+
" aliasDef.UUID: %s, aliasName: %s, indexName: %s",
aliasUUID, aliasDef.UUID, aliasName, indexName)
}
var params *AliasParams
params, err = parseAliasParams(aliasDef.Params)
if err != nil {
return fmt.Errorf("alias: could not parse aliasDef.Params: %s,"+
" aliasName: %s, indexName: %s",
aliasDef.Params, aliasName, indexName)
}
for targetName, targetSpec := range params.Targets {
if !multiCollIndex {
_, multiCollIndex = metaFieldValCache.getSourceDetailsMap(targetName)
}
if num > maxAliasTargets {
return fmt.Errorf("alias: too many alias targets,"+
" aliasName: %s, indexName: %s",
aliasName, indexName)
}
targetDef := indexDefs.IndexDefs[targetName]
if targetDef == nil {
return fmt.Errorf("alias: the alias depends upon"+
" a target index that does not exist,"+
" targetName: %q, aliasName: %q",
targetName, aliasName)
}
if targetSpec.IndexUUID != "" &&
targetSpec.IndexUUID != targetDef.UUID {
return fmt.Errorf("alias: mismatched targetSpec.UUID: %s,"+
" targetDef.UUID: %s, targetName: %s,"+
" aliasName: %s, indexName: %s",
targetSpec.IndexUUID, targetDef.UUID, targetName,
aliasName, indexName)
}
// TODO: Convert to registered callbacks instead of if-else-if.
if targetDef.Type == "fulltext-alias" {
inPath, visited := seenTargets[targetName]
if visited && inPath {
return fmt.Errorf("alias: cyclic index alias %s "+
"detected in alias path, indexName: %s", targetName, indexName)
}
if visited {
continue
}
seenTargets[targetName] = true
err = fillAlias(targetName, targetSpec.IndexUUID)
if err != nil {
return err
}
seenTargets[targetName] = false
} else if strings.HasPrefix(targetDef.Type, "fulltext-index") {
if !seenTargets[targetName] {
var subAlias bleve.IndexAlias
subAlias, _, _, err = bleveIndexAlias(mgr,
targetName, targetSpec.IndexUUID, ensureCanRead,
consistencyParams, cancelCh, true, nil,
partitionSelection, getRemoteClients(mgr))
if err != nil {
return err
}
alias.Add(subAlias)
seenTargets[targetName] = true
num += 1
}
} else {
return fmt.Errorf("alias: unsupported target type: %s,"+
" targetName: %s, aliasName: %s, indexName: %s",
targetDef.Type, targetName, aliasName, indexName)
}
}
return nil
}
seenTargets[indexName] = true
err = fillAlias(indexName, indexUUID)
if err != nil {
return nil, false, err
}
return alias, multiCollIndex, nil
}