-
Notifications
You must be signed in to change notification settings - Fork 28
/
rest_options.go
216 lines (185 loc) · 6.63 KB
/
rest_options.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
// 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"
)
// 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 || uint32(maxFeedsPerDCPAgent) < 0 {
return nil, fmt.Errorf("illegal value for maxFeedsPerDCPAgent: '%v'",
options["maxFeedsPerDCPAgent"])
}
}
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
}
}
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)
}