-
Notifications
You must be signed in to change notification settings - Fork 28
/
server_groups.go
391 lines (330 loc) · 9.72 KB
/
server_groups.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
// Copyright 2021-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 (
"bufio"
"encoding/json"
"fmt"
"io"
"net/http"
"sort"
"strings"
"time"
"github.com/couchbase/cbgt"
log "github.com/couchbase/clog"
)
// serverGroupTracker tracks the server group changes within a
// cluster and reflects those cross rack node movemements to
// the node definitions.
type serverGroupTracker struct {
mgr *cbgt.Manager
url string
prevRev string
notifCh chan *streamingPoolResponse
}
// streamingPoolResponse represents the server group uri
// details from a poolStreaming endpoint.
type streamingPoolResponse struct {
ServerGroupsUri string `json:"serverGroupsUri"`
}
func StartServerGroupTracker(mgr *cbgt.Manager) {
sg := serverGroupTracker{
url: mgr.Server() + "/poolsStreaming/default",
mgr: mgr,
notifCh: make(chan *streamingPoolResponse, 1),
}
go sg.listen()
}
func (st *serverGroupTracker) listen() {
decodeAndNotifyResponse := func(resp []byte) error {
sg := &streamingPoolResponse{}
err := json.Unmarshal(resp, &sg)
if err != nil {
return err
}
st.notifCh <- sg
return nil
}
st.handleServerGroupUpdates()
err := ListenStreamingEndpoint("server_groups", st.url, decodeAndNotifyResponse, nil)
if err != nil {
log.Errorf("server_groups: listenStreamingEndpoint, err: %v", err)
}
}
// handleServerGroupUpdates listens to the server group updates
// and debounces the updates to minimise the config writes.
// It reuses the cfg debounce configuration values.
func (st *serverGroupTracker) handleServerGroupUpdates() {
if st.mgr.Cfg() == nil {
return
}
// get the config debounce offset and node offset multiplier.
offset, nm := st.mgr.GetCfgDeBounceOffsetAndMultiplier()
// compute the node specific offset from the node index.
pos := 1
nodeDefs, _ := st.mgr.GetNodeDefs(cbgt.NODE_DEFS_KNOWN, false)
// Sorting the node UUIDs to have a consistent ordering across the nodes.
var sortedNodeUUID []string
if nodeDefs != nil {
for _, nodeDef := range nodeDefs.NodeDefs {
sortedNodeUUID = append(sortedNodeUUID, nodeDef.UUID)
}
sort.Strings(sortedNodeUUID)
for _, nodeDefUUID := range sortedNodeUUID {
if nodeDefUUID == st.mgr.UUID() {
break
}
pos++
}
}
dbinterval := nm * pos * offset
log.Printf("server_groups: debounce duration: %d MS", dbinterval)
var resp *streamingPoolResponse
go func() {
for {
resp = <-st.notifCh
debounceTimeCh := time.After(time.Millisecond * time.Duration(dbinterval))
DEBOUNCE_LOOP:
for {
select {
case resp = <-st.notifCh:
// NOOP upon more updates.
case <-debounceTimeCh:
break DEBOUNCE_LOOP
}
}
if !strings.Contains(resp.ServerGroupsUri, "v=") {
log.Warnf("server_groups: no rev found %s",
resp.ServerGroupsUri)
continue
}
// parse the server group revision number.
var msgs []string
rev := resp.ServerGroupsUri[strings.Index(
resp.ServerGroupsUri, "v=")+2:]
// skip if there is no change in the server group rev numbers.
if rev == st.prevRev {
continue
}
// get the node UUIDs for all the fts nodes.
nodeUUIDs := getNodeUUIDs(st.mgr)
// get the latest server group for all the nodes.
nodeHierarchy, err := fetchServerGroupDetails(st.mgr, nodeUUIDs)
if err != nil {
continue
}
// update the server groups for all the nodes if required.
for _, kind := range []string{cbgt.NODE_DEFS_KNOWN,
cbgt.NODE_DEFS_WANTED} {
nodeDefs, _, err := cbgt.CfgGetNodeDefs(st.mgr.Cfg(), kind)
if err != nil {
continue
}
var updated bool
nodeDefs.UUID = cbgt.NewUUID()
for uuid, nodeDef := range nodeDefs.NodeDefs {
if container, ok := nodeHierarchy[uuid]; ok {
if nodeDef.Container != container {
msgs = append(msgs, fmt.Sprintf("server_groups:"+
" node: %s has moved from server group: %s to"+
" group: %s", uuid, nodeDef.Container,
container))
nodeDef.Container = container
updated = true
}
}
}
if !updated {
log.Debugf("server_groups: no server group membership"+
" updates found for nodeDefs kind: %s", kind)
continue
}
nodeDefsSetFunc := func() error {
_, err = cbgt.CfgSetNodeDefs(st.mgr.Cfg(), kind,
nodeDefs, cbgt.CFG_CAS_FORCE)
return err
}
// Keep retrying till the node defs are successfully set.
err = cbgt.RetryOnCASMismatch(nodeDefsSetFunc, -1)
if err != nil {
log.Warnf("server_groups: CfgSetNodeDefs failed, for"+
" kind: %s, err: %v", kind, err)
break
}
st.prevRev = rev
log.Printf("server_groups: nodeDefs updated for kind: %s", kind)
break
}
if len(msgs) > 0 {
msgs = cbgt.StringsRemoveDuplicates(msgs)
for _, msg := range msgs {
log.Printf(msg)
}
}
}
}()
}
// getNodeUUIDs collects all the unique search node UUIDs
// from the node definitions.
func getNodeUUIDs(mgr *cbgt.Manager) []string {
var nodeUUIDs []string
for _, kind := range []string{cbgt.NODE_DEFS_KNOWN,
cbgt.NODE_DEFS_WANTED} {
nodeDefs, err := mgr.GetNodeDefs(kind, true)
if err != nil {
continue
}
for _, nodeDef := range nodeDefs.NodeDefs {
nodeUUIDs = append(nodeUUIDs, nodeDef.UUID)
}
}
return cbgt.StringsRemoveDuplicates(nodeUUIDs)
}
type serverGroups struct {
ServerGroups []serverGroup `json:"groups"`
}
type serverGroup struct {
Name string `json:"name"`
NodeDetails []nodeDetail `json:"nodes"`
}
type nodeDetail struct {
NodeUUID string `json:"nodeUUID"`
}
// fetchServerGroupDetails retrives the server group node hierarchy
// for all the given node UUIDs.
func fetchServerGroupDetails(mgr *cbgt.Manager, uuids []string) (
map[string]string, error) {
if len(uuids) == 0 {
return nil, fmt.Errorf("empty UUID list")
}
url := mgr.Server() + "/pools/default/serverGroups"
u, err := cbgt.CBAuthURL(url)
if err != nil {
return nil, fmt.Errorf("server_groups: auth for ns_server,"+
" url: %s, authType: %s, err: %v",
url, "cbauth", err)
}
req, err := http.NewRequest("GET", u, nil)
if err != nil {
return nil, err
}
resp, err := cbgt.HttpClient().Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respBuf, err := io.ReadAll(resp.Body)
if err != nil || len(respBuf) == 0 {
return nil, fmt.Errorf("server_groups: error reading resp.Body,"+
" url: %s, resp: %#v, err: %v", url, resp, err)
}
var sg serverGroups
err = json.Unmarshal(respBuf, &sg)
if err != nil {
return nil, fmt.Errorf("server_groups: error parsing respBuf: %s,"+
" url: %s, err: %v", respBuf, url, err)
}
uuidMap := cbgt.StringsToMap(uuids)
nodeHierarchy := make(map[string]string, len(uuids))
for _, g := range sg.ServerGroups {
for _, nd := range g.NodeDetails {
if _, found := uuidMap[nd.NodeUUID]; found {
nodeHierarchy[nd.NodeUUID] = "datacenter/" + g.Name
}
}
}
return nodeHierarchy, nil
}
// listenStreamingEndpoint is a generic stream endpoint listen
// utility function which accepts callback methods for decoding the
// stream response, notify methods and a stop signalling channel.
func ListenStreamingEndpoint(msg, url string,
decodeAndNotifyResponse func([]byte) error, stopCh chan struct{}) error {
backoffStartSleepMS := 500
backoffFactor := float32(1.5)
backoffMaxSleepMS := 60000
RECONNECT:
for {
stopCh2 := make(chan struct{})
var resp *http.Response
// keep retrying with backoff logic upon connection errors.
cbgt.ExponentialBackoffLoop(msg+" listening",
func() int {
u, err := cbgt.CBAuthURL(url)
if err != nil {
log.Warnf(msg+": auth for ns_server,"+
" server: %s, authType: %s, err: %v",
url, "cbauth", err)
return 0
}
req, err := http.NewRequest("GET", u, nil)
if err != nil {
log.Warnf(msg+": req, err: %v", err)
return 0
}
req.Header.Add("Content-Type", "application/json")
resp, err = cbgt.HttpClient().Do(req)
if err != nil || (resp.StatusCode != 200 && resp.StatusCode != 404) {
log.Warnf(msg+": http client, response: %+v, err: %v", resp, err)
return 0
} else if resp.StatusCode == 404 {
return -1
}
log.Printf(msg + ": pool streaming started")
return -1 // success
},
backoffStartSleepMS, backoffFactor, backoffMaxSleepMS,
)
terminator(stopCh, stopCh2, resp.Body)
reader := bufio.NewReader(resp.Body)
for {
if stopCh != nil {
select {
case <-stopCh:
resp.Body.Close()
log.Warnf(msg + ": terminating pool streaming")
return nil
default:
}
}
resBytes, err := reader.ReadBytes('\n')
if err != nil {
log.Warnf(msg+": reconnecting upon reader, bytes read: %d, err: %v",
len(resBytes), err)
close(stopCh2)
resp.Body.Close()
continue RECONNECT
}
if len(resBytes) == 1 && resBytes[0] == '\n' {
continue
}
err = decodeAndNotifyResponse(resBytes)
if err != nil {
log.Warnf(msg+": notify, err: %v", err)
continue
}
}
}
}
// terminator is a routine that would listen to stop channel parallely to the
// streaming endpoint listener. essentially, this aids in immediate cleanup of the
// routine, especially when the streaming endpoint doesn't send anything (no change
// was detected in that endpoint).
func terminator(stopCh, stopCh2 chan struct{}, respBody io.ReadCloser) {
go func(stopCh, stopCh2 chan struct{}, respBody io.ReadCloser) {
if stopCh != nil {
select {
case <-stopCh:
respBody.Close()
return
case <-stopCh2:
return
}
}
}(stopCh, stopCh2, respBody)
}