-
Notifications
You must be signed in to change notification settings - Fork 28
/
pindex_copy_request.go
440 lines (389 loc) · 11.9 KB
/
pindex_copy_request.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
// 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 (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"runtime"
"sort"
"sync"
"sync/atomic"
"time"
"github.com/couchbase/cbgt"
log "github.com/couchbase/clog"
)
var FeatureFileTransferRebalance = "fileTransferRebalance"
// CopyPartitionRequest represents the partition copy request.
type CopyPartitionRequest struct {
SourceNodes []*cbgt.NodeDef // ordered by preference
SourcePIndexName string
SourceUUID string
CancelCh chan struct{}
Stats *CopyPartitionStats
}
type CopyPartitionStats struct {
TotCopyPartitionStart int32
TotCopyPartitionSkipped int32
TotCopyPartitionServerErrors int32
TotCopyPartitionChecksumErrors int32
TotCopyPartitionErrors int32
TotCopyPartitionRetries int32
TotCopyPartitionFailed int32
TotCopyPartitionFinished int32
TotCopyPartitionCancelled int32
TotCopyPartitionTimeInMs int32
TotCopyPartitionOnHttp2 int32
CopyPartitionNumBytesExpected int32
CopyPartitionNumBytesReceived int32
}
func (s *CopyPartitionStats) WriteJSON(w io.Writer) {
t := atomic.LoadInt32(&s.TotCopyPartitionStart)
stats := fmt.Sprintf(`{"TotCopyPartitionStart":%d`, t)
t = atomic.LoadInt32(&s.TotCopyPartitionFinished)
stats += fmt.Sprintf(`,"TotCopyPartitionFinished":%d`, t)
t = atomic.LoadInt32(&s.TotCopyPartitionTimeInMs)
stats += fmt.Sprintf(`,"TotCopyPartitionTimeInMs":%d`, t)
t = atomic.LoadInt32(&s.TotCopyPartitionFailed)
stats += fmt.Sprintf(`,"TotCopyPartitionFailed":%d`, t)
t = atomic.LoadInt32(&s.TotCopyPartitionRetries)
stats += fmt.Sprintf(`,"TotCopyPartitionRetries":%d`, t)
t = atomic.LoadInt32(&s.TotCopyPartitionErrors)
stats += fmt.Sprintf(`,"TotCopyPartitionErrors":%d`, t)
t = atomic.LoadInt32(&s.TotCopyPartitionSkipped)
stats += fmt.Sprintf(`,"TotCopyPartitionSkipped":%d`, t)
t = atomic.LoadInt32(&s.TotCopyPartitionCancelled)
stats += fmt.Sprintf(`,"TotCopyPartitionCancelled":%d`, t)
t = atomic.LoadInt32(&s.TotCopyPartitionOnHttp2)
stats += fmt.Sprintf(`,"TotCopyPartitionOnHttp2":%d`, t)
finished := atomic.LoadInt32(&s.TotCopyPartitionFinished)
started := atomic.LoadInt32(&s.TotCopyPartitionStart)
if started != 0 && started == finished {
stats += `,"TransferProgress": 1`
} else if atomic.LoadInt32(&s.CopyPartitionNumBytesExpected) > 0 {
prog := float64(atomic.LoadInt32(&s.CopyPartitionNumBytesReceived)) /
float64(atomic.LoadInt32(&s.CopyPartitionNumBytesExpected))
stats += `,"TransferProgress":` + fmt.Sprintf("%.6f", prog)
}
w.Write([]byte(stats))
w.Write(cbgt.JsonCloseBrace)
}
// buildCopyPartitionRequest returns the partition copy request.
// Currently this is a naive implementation which just checks the existence
// of pindexes among the known node lists and then sort them based on the
// partition state, rack and number of total partitions/load on those nodes.
func buildCopyPartitionRequest(pindexName string, stats *CopyPartitionStats,
mgr *cbgt.Manager, stopCh chan struct{}) (*CopyPartitionRequest, error) {
planPIndexes, _, err := cbgt.CfgGetPlanPIndexes(mgr.Cfg())
if err != nil {
return nil, fmt.Errorf("pindex_copy_request:"+
" CfgGetPlanPIndexes err: %v", err)
}
if planPIndexes == nil {
return nil, fmt.Errorf("pindex_copy_request:" +
" skipped on nil planPIndexes")
}
var planPIndex *cbgt.PlanPIndex
var exists bool
if planPIndex, exists = planPIndexes.PlanPIndexes[pindexName]; !exists ||
len(planPIndex.Nodes) == 0 {
return nil, fmt.Errorf("pindex_copy_request:"+
" missing pindex: %s in planPIndexes", pindexName)
}
nodeDefs, _, err := cbgt.CfgGetNodeDefs(mgr.Cfg(), cbgt.NODE_DEFS_KNOWN)
if err != nil {
return nil, err
}
formerPrimary := ""
// get all the potential source nodes except self.
uuids := make([]string, len(planPIndex.Nodes))
for uuid, node := range planPIndex.Nodes {
if node.Priority == 0 && node.CanRead && node.CanWrite {
formerPrimary = uuid
}
// if the current node is the primary
// then no file copying is needed.
if mgr.UUID() == formerPrimary {
atomic.StoreInt32(&stats.TotCopyPartitionSkipped, int32(1))
return nil, nil
}
// skip self.
if mgr.UUID() == uuid {
continue
}
uuids = append(uuids, uuid)
}
// check explicitly whether the requested pindex resides in those
// nodes, and if so gives back a set of potential source node uuids.
sourceUUIDs := getNodesHostingPIndex(uuids, pindexName,
mgr.GetOption("authType"), nodeDefs)
if len(sourceUUIDs) == 0 {
atomic.StoreInt32(&stats.TotCopyPartitionSkipped, int32(1))
return nil, nil
}
return buildCopyPartitionRequestUtil(planPIndexes, pindexName, stats,
nodeDefs, sourceUUIDs, formerPrimary, mgr, stopCh)
}
// buildCopyPartitionRequestUtil creates the partition copy request
// with a sorted list of source nodeDefs for the given pindex.
func buildCopyPartitionRequestUtil(planPIndexes *cbgt.PlanPIndexes,
pindexName string, stats *CopyPartitionStats, nodeDefs *cbgt.NodeDefs,
sourceUUIDs []string, formerPrimary string, mgr *cbgt.Manager,
stopCh chan struct{}) (*CopyPartitionRequest, error) {
// compute the given partition load on the cluster nodes.
nodeParitionCount := make(map[string]int)
for _, pp := range planPIndexes.PlanPIndexes {
for node := range pp.Nodes {
if _, ok := nodeParitionCount[node]; !ok {
nodeParitionCount[node] = 1
} else {
nodeParitionCount[node]++
}
}
}
// form the source partition candidates.
planPIndex := planPIndexes.PlanPIndexes[pindexName]
var sps []*srcPartition
for _, uuid := range sourceUUIDs {
if nodeDef, ok := nodeDefs.NodeDefs[uuid]; ok {
node := planPIndex.Nodes[uuid]
sp := &srcPartition{nodeDef: nodeDef,
partitionCountOnNode: nodeParitionCount[uuid]}
if node.Priority == 0 {
sp.state = "primary"
} else if node.Priority > 0 {
sp.state = "replica"
}
sps = append(sps, sp)
}
}
// sort them in the order of preference.
sort.Sort(&srcPartitionSorter{
sps: sps,
state: "primary",
rack: mgr.Container()})
if len(sps) > 0 {
var sourceNodes []*cbgt.NodeDef
for _, sp := range sps {
sourceNodes = append(sourceNodes, sp.nodeDef)
}
return &CopyPartitionRequest{
SourceNodes: sourceNodes,
SourcePIndexName: planPIndex.Name,
SourceUUID: planPIndex.SourceUUID,
Stats: stats,
CancelCh: stopCh,
}, nil
}
return nil, nil
}
// getNodesHostingPIndex confirms the list of node UUIDs
// hosting the given pindex.
func getNodesHostingPIndex(uuids []string,
pindex, authType string, nodeDefs *cbgt.NodeDefs) []string {
if len(uuids) == 0 {
return nil
}
urlMap := getStatsUrls(uuids, authType, nodeDefs)
var wg sync.WaitGroup
size := len(urlMap)
type statsReq struct {
uuid string
url string
}
type statsResp struct {
uuid string
status bool
err error
}
requestCh := make(chan *statsReq, size)
responseCh := make(chan *statsResp, size)
nWorkers := getWorkerCount(size)
// spawn the stats get workers
for i := 0; i < nWorkers; i++ {
wg.Add(1)
go func() {
for reqs := range requestCh {
ctx, cancel := context.WithTimeout(context.Background(),
60*time.Second)
defer cancel()
req, _ := http.NewRequestWithContext(ctx, "GET", reqs.url, nil)
httpClient := cbgt.HttpClient()
res, err := httpClient.Do(req)
if err != nil {
responseCh <- &statsResp{
uuid: reqs.uuid,
status: false,
err: fmt.Errorf("pindex_copy_request:"+
" getNodesHostingPIndex, get stats for pindex: %s,"+
" err: %v", reqs.url, err),
}
continue
}
// parse the response to get the pindex status
pindexesData := struct {
PIndexes map[string]struct {
Partitions map[string]struct {
UUID string `json:"uuid"`
Seq uint64 `json:"seq"`
} `json:"partitions"`
Basic struct {
DocCount uint64 `json:"DocCount"`
} `json:"basic"`
} `json:"pindexes"`
}{}
data, derr := io.ReadAll(res.Body)
if err == nil && derr != nil {
responseCh <- &statsResp{
uuid: reqs.uuid,
status: false,
err: derr,
}
log.Printf("pindex_copy_request: getNodesHostingPIndex,"+
" grab stats for pindex: %s, err: %v", reqs.url, err)
continue
}
err = json.Unmarshal(data, &pindexesData)
if err != nil {
log.Printf("pindex_copy_request: getNodesHostingPIndex,"+
" get stats for pindex: %s, err: %v", reqs.url, err)
continue
}
if _, exists := pindexesData.PIndexes[pindex]; exists {
log.Printf("pindex_copy_request: pindex: %s found on node:"+
" %s", pindex, reqs.uuid)
responseCh <- &statsResp{
uuid: reqs.uuid,
status: true,
err: nil,
}
continue
}
// no pindex found on host
responseCh <- &statsResp{
uuid: reqs.uuid,
status: false,
err: fmt.Errorf("pindex not found"),
}
continue
}
wg.Done()
}()
}
for node, url := range urlMap {
requestCh <- &statsReq{uuid: node, url: url}
}
close(requestCh)
wg.Wait()
close(responseCh)
var UUIDs []string
for resp := range responseCh {
if resp.status {
UUIDs = append(UUIDs, resp.uuid)
} else {
log.Printf("pindex_copy_request: missing pindex on node %s,"+
" err: %v", resp.uuid, resp.err)
}
}
return UUIDs
}
// getStatsUrls forms the stats url map.
func getStatsUrls(uuids []string, authType string,
nodeDefs *cbgt.NodeDefs) map[string]string {
nodeURL := make(map[string]string)
if nodeDefs == nil {
return nodeURL
}
urlWithAuth := func(authType, urlStr string) (string, error) {
if authType == "cbauth" {
return cbgt.CBAuthURL(urlStr)
}
return urlStr, nil
}
ss := cbgt.GetSecuritySetting()
for _, uuid := range uuids {
if node, ok := nodeDefs.NodeDefs[uuid]; ok {
hostPortUrl := "http://" + node.HostPort
if ss.EncryptionEnabled {
if u, err := node.HttpsURL(); err == nil {
hostPortUrl = u
}
}
urlStr := hostPortUrl + "/api/stats?partitions=true"
urlStr, err := urlWithAuth(authType, urlStr)
if err != nil {
continue
}
nodeURL[uuid] = urlStr
}
}
return nodeURL
}
// srcPartition wraps the potential source partition
// with additional info to help prioritising it.
type srcPartition struct {
nodeDef *cbgt.NodeDef
partitionCountOnNode int
state string
}
// srcPartitionSorter helps to prioritise the
// order of candidate source nodes.
// Preference order of nodes is dervied from,
// - same rack presence,
// - filtering state like primary/replica and
// - current number of partitions/load on the node.
type srcPartitionSorter struct {
sps []*srcPartition
rack string
state string
}
func (ns *srcPartitionSorter) Len() int {
return len(ns.sps)
}
func (ns *srcPartitionSorter) Less(i, j int) bool {
si := ns.Score(i)
sj := ns.Score(j)
if si > sj {
return true
}
return false
}
func (ns srcPartitionSorter) Swap(i, j int) {
ns.sps[i], ns.sps[j] = ns.sps[j], ns.sps[i]
}
func (ns *srcPartitionSorter) Score(i int) float64 {
sp := ns.sps[i]
// weightage is given to the preferred target state.
stateSkipFactor := 1.0
if ns.state != "" && sp.state != ns.state {
stateSkipFactor = 0
}
// same rack nodes are higher scored.
localRackBoostFactor := 0.2
if sp.nodeDef.Container == ns.rack {
localRackBoostFactor = 1
}
// nodes with high number existing partition load gets
// lower score and vice versa.
nodeLoadBalanceFactor := .3
if sp.partitionCountOnNode != 0 {
nodeLoadBalanceFactor *= float64(1) / float64(sp.partitionCountOnNode)
}
score := localRackBoostFactor *
stateSkipFactor * nodeLoadBalanceFactor
return score
}
func getWorkerCount(itemCount int) int {
ncpu := runtime.NumCPU()
if itemCount < ncpu {
return itemCount
}
return ncpu
}