-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathrest_options.go
More file actions
272 lines (235 loc) · 8.62 KB
/
rest_options.go
File metadata and controls
272 lines (235 loc) · 8.62 KB
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
// Copyright 2017-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 (
"fmt"
"net/http"
"strconv"
"github.com/couchbase/cbgt"
"github.com/couchbase/cbgt/rest"
log "github.com/couchbase/clog"
bleveSearcher "github.com/blevesearch/bleve/v2/search/searcher"
"github.com/couchbase/cbft/search_history"
)
// List of log levels that maps strings to integers.
// (Works with clog - https://github.com/couchbase/clog)
var LogLevels map[string]uint32
func init() {
LogLevels = make(map[string]uint32)
LogLevels["DEBU"] = 0
LogLevels["INFO"] = 1
LogLevels["CRIT"] = 4
LogLevels["ERRO"] = 3
LogLevels["FATA"] = 4
LogLevels["WARN"] = 2
}
// ManagerOptionsExt is a REST handler that serves as a wrapper for
// ManagerOptions - where it sets the manager options, and updates
// the logLevel upon request.
type ManagerOptionsExt struct {
mgr *cbgt.Manager
mgrOptions *rest.ManagerOptions
}
func NewManagerOptionsExt(mgr *cbgt.Manager) *ManagerOptionsExt {
mgrOptions := rest.NewManagerOptions(mgr)
mgrOptions.Validate = func(options map[string]string) (map[string]string, error) {
// Validate logLevel
logLevelStr := options["logLevel"]
if logLevelStr != "" {
_, exists := LogLevels[logLevelStr]
if !exists {
return nil, fmt.Errorf("invalid setting for"+
" logLevel: %v", logLevelStr)
}
}
// Validate maxReplicasAllowed
maxReplicasAllowed := mgr.GetOption("maxReplicasAllowed")
if options["maxReplicasAllowed"] != maxReplicasAllowed {
return nil, fmt.Errorf("maxReplicasAllowed setting is at '%v',"+
" but request is for '%v'", maxReplicasAllowed,
options["maxReplicasAllowed"])
}
// Validate bucketTypesAllowed
bucketTypesAllowed := mgr.GetOption("bucketTypesAllowed")
if options["bucketTypesAllowed"] != bucketTypesAllowed {
return nil, fmt.Errorf("bucketTypesAllowed setting is at '%v',"+
" but request is for: '%v'", bucketTypesAllowed,
options["bucketTypesAllowed"])
}
// Validate gcMinThreshold
if options["gcMinThreshold"] != "" {
gcMinThreshold, err := strconv.Atoi(options["gcMinThreshold"])
if err != nil || gcMinThreshold < 0 {
return nil, fmt.Errorf("illegal value for gcMinThreshold: '%v'",
options["gcMinThreshold"])
}
}
// Validate gcTriggerPct
if options["gcTriggerPct"] != "" {
gcTriggerPct, err := strconv.Atoi(options["gcTriggerPct"])
if err != nil || gcTriggerPct < 0 {
return nil, fmt.Errorf("illegal value for gcTriggerPct: '%v'",
options["gcTriggerPct"])
}
}
// Validate memStatsLoggingInterval
if options["memStatsLoggingInterval"] != "" {
memStatsLoggingInterval, err := strconv.Atoi(options["memStatsLoggingInterval"])
if err != nil || memStatsLoggingInterval < 0 {
return nil, fmt.Errorf("illegal value for memStatsLoggingInterval: '%v'",
options["memStatsLoggingInterval"])
}
}
if options["bleveMaxClauseCount"] != "" {
bleveMaxClauseCount, err := strconv.Atoi(options["bleveMaxClauseCount"])
if err != nil || bleveMaxClauseCount <= 0 {
return nil, fmt.Errorf("illegal value for bleveMaxClauseCount: '%v'",
options["bleveMaxClauseCount"])
}
}
if options["bleveMaxResultWindow"] != "" {
bleveMaxResultWindow, err := strconv.Atoi(options["bleveMaxResultWindow"])
if err != nil || bleveMaxResultWindow <= 0 {
return nil, fmt.Errorf("illegal value for bleveMaxResultWindow: '%v'",
options["bleveMaxResultWindow"])
}
}
if options["maxFeedsPerDCPAgent"] != "" {
maxFeedsPerDCPAgent, err := strconv.Atoi(options["maxFeedsPerDCPAgent"])
if err != nil || maxFeedsPerDCPAgent < 0 {
return nil, fmt.Errorf("illegal value for maxFeedsPerDCPAgent: '%v'",
options["maxFeedsPerDCPAgent"])
}
}
if options["collectionsLimitPerIndex"] != "" {
collectionsLimitPerIndex, err := strconv.Atoi(options["collectionsLimitPerIndex"])
if err != nil || collectionsLimitPerIndex < 0 {
return nil, fmt.Errorf("illegal value for collectionsLimitPerIndex: '%v'",
options["collectionsLimitPerIndex"])
}
}
// Validate scanPlusFetchBucketWideSeqNos
if options["scanPlusFetchBucketWideSeqNos"] != "" {
_, err := strconv.ParseBool(options["scanPlusFetchBucketWideSeqNos"])
if err != nil {
return nil, fmt.Errorf("illegal value for scanPlusFetchBucketWideSeqNos: '%v'",
options["scanPlusFetchBucketWideSeqNos"])
}
}
// Validate scanPlusNumWorkers
if options["scanPlusNumWorkers"] != "" {
numWorkers, err := strconv.Atoi(options["scanPlusNumWorkers"])
if err != nil {
return nil, fmt.Errorf("illegal value for scanPlusNumWorkers: '%v'",
options["scanPlusNumWorkers"])
}
if numWorkers < 1 {
return nil, fmt.Errorf("scanPlusNumWorkers must be greater than or equal to 1, got: %d", numWorkers)
}
}
// Validate scanPlusNumRetries
if options["scanPlusNumRetries"] != "" {
numRetries, err := strconv.Atoi(options["scanPlusNumRetries"])
if err != nil {
return nil, fmt.Errorf("illegal value for scanPlusNumRetries: '%v'",
options["scanPlusNumRetries"])
}
if numRetries < 1 {
return nil, fmt.Errorf("scanPlusNumRetries must be greater than or equal to 1, got: %d", numRetries)
}
}
if options["customScriptQueriesEnabled"] != "" {
_, err := strconv.ParseBool(options["customScriptQueriesEnabled"])
if err != nil {
return nil, fmt.Errorf("illegal value for customScriptQueriesEnabled: '%v'",
options["customScriptQueriesEnabled"])
}
}
return options, nil
}
return &ManagerOptionsExt{
mgr: mgr,
mgrOptions: mgrOptions,
}
}
func (h *ManagerOptionsExt) ServeHTTP(
w http.ResponseWriter, req *http.Request) {
h.mgrOptions.ServeHTTP(w, req)
// Update log level if requested.
logLevelStr := h.mgr.GetOption("logLevel")
if logLevelStr != "" {
logLevel, _ := LogLevels[logLevelStr]
log.SetLevel(log.LogLevel(logLevel))
}
// Update bleveMaxClauseCount if requested.
bleveMaxClauseCountStr := h.mgr.GetOption("bleveMaxClauseCount")
if bleveMaxClauseCountStr != "" {
bleveMaxClauseCount, _ := strconv.Atoi(bleveMaxClauseCountStr)
bleveSearcher.DisjunctionMaxClauseCount = bleveMaxClauseCount
}
// Update search history settings if requested.
search_history.Service.Refresh(h.mgr.Options())
if err := RefreshCustomScriptQuerySettings(h.mgr.Options()); err != nil {
log.Warnf("rest_options: custom script query settings, err: %v", err)
}
}
type ConciseOptions struct {
mgr *cbgt.Manager
}
func NewConciseOptions(mgr *cbgt.Manager) *ConciseOptions {
return &ConciseOptions{mgr: mgr}
}
func (h *ConciseOptions) ServeHTTP(
w http.ResponseWriter, req *http.Request) {
options := h.mgr.Options()
maxReplicasAllowed, _ := strconv.Atoi(options["maxReplicasAllowed"])
bucketTypesAllowed := options["bucketTypesAllowed"]
bleveMaxResultWindow, _ := strconv.Atoi(options["bleveMaxResultWindow"])
bleveMaxClauseCount, _ := strconv.Atoi(options["bleveMaxClauseCount"])
var deploymentModel string
if val, exists := options["deploymentModel"]; exists {
deploymentModel = val
}
if bleveMaxClauseCount == 0 {
// in case bleveMaxClauseCount wasn't updated by user
bleveMaxClauseCount = DefaultBleveMaxClauseCount
}
var collectionsSupported bool
if isClusterCompatibleFor(FeatureCollectionVersion) {
if nodeDefs, _, err := cbgt.CfgGetNodeDefs(
h.mgr.Cfg(), cbgt.NODE_DEFS_WANTED); err == nil {
if cbgt.IsFeatureSupportedByCluster(FeatureCollections, nodeDefs) {
collectionsSupported = true
}
}
}
var scopedIndexesSupport bool
if isClusterCompatibleFor(FeatureScopedIndexNamesVersion) {
scopedIndexesSupport = true
}
rv := struct {
Status string `json:"status"`
MaxReplicasAllowed int `json:"maxReplicasAllowed"`
BucketTypesAllowed string `json:"bucketTypesAllowed"`
CollectionsSupport bool `json:"collectionsSupport"`
BleveMaxResultWindow int `json:"bleveMaxResultWindow"`
BleveMaxClauseCount int `json:"bleveMaxClauseCount"`
DeploymentModel string `json:"deploymentModel,omitempty"`
ScopedIndexesSupport bool `json:"scopedIndexesSupport"`
}{
Status: "ok",
MaxReplicasAllowed: maxReplicasAllowed,
BucketTypesAllowed: bucketTypesAllowed,
CollectionsSupport: collectionsSupported,
BleveMaxResultWindow: bleveMaxResultWindow,
BleveMaxClauseCount: bleveMaxClauseCount,
DeploymentModel: deploymentModel,
ScopedIndexesSupport: scopedIndexesSupport,
}
rest.MustEncode(w, rv)
}