forked from troubling/hummingbird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
priorityrep.go
439 lines (419 loc) · 12.8 KB
/
priorityrep.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
// Copyright (c) 2015 Rackspace
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package objectserver
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/troubling/hummingbird/common"
"github.com/troubling/hummingbird/common/ring"
"github.com/troubling/hummingbird/common/srv"
"golang.org/x/net/http2"
)
type devLimiter struct {
inUse map[int]int
m sync.Mutex
max int
somethingFinished chan struct{}
}
func (d *devLimiter) start(j *PriorityRepJob) bool {
d.m.Lock()
doable := d.inUse[j.FromDevice.Id] < d.max
doable = doable && d.inUse[j.ToDevice.Id] < d.max
if doable {
d.inUse[j.FromDevice.Id] += 1
d.inUse[j.ToDevice.Id] += 1
}
d.m.Unlock()
return doable
}
func (d *devLimiter) finished(j *PriorityRepJob) {
d.m.Lock()
d.inUse[j.FromDevice.Id] -= 1
d.inUse[j.ToDevice.Id] -= 1
d.m.Unlock()
select {
case d.somethingFinished <- struct{}{}:
default:
}
}
func (d *devLimiter) waitForSomethingToFinish() {
<-d.somethingFinished
}
func SendPriRepJob(job *PriorityRepJob, client *http.Client) (string, bool) {
url := fmt.Sprintf("%s://%s:%d/priorityrep", job.FromDevice.Scheme, job.FromDevice.ReplicationIp, job.FromDevice.ReplicationPort)
jsonned, err := json.Marshal(job)
if err != nil {
return fmt.Sprintf("Failed to serialize job for some reason: %s", err), false
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonned))
if err != nil {
return fmt.Sprintf("Failed to create request for some reason: %s", err), false
}
req.ContentLength = int64(len(jsonned))
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return fmt.Sprintf("Error moving partition %d: %v",
job.Partition, err), false
}
defer resp.Body.Close()
if resp.StatusCode/100 == 2 {
if data, err := ioutil.ReadAll(resp.Body); err == nil {
prp := PriorityReplicationResult{}
if err = json.Unmarshal(data, &prp); err == nil {
return fmt.Sprintf("Replicating partition %d from %s/%s to %s/%s replicated %d objects with %d errors",
job.Partition, job.FromDevice.Ip, job.FromDevice.Device, job.ToDevice.Ip, job.ToDevice.Device,
prp.ObjectsReplicated, prp.ObjectsErrored), prp.Success
} else {
return fmt.Sprintf("could not get valid response for partition %d: %v",
job.Partition, err), false
}
} else {
return fmt.Sprintf("could not read body forpartition %d: %v",
job.Partition, err), false
}
} else if resp.StatusCode == 404 {
return fmt.Sprintf("partition %d: not found on %s/%s",
job.Partition, job.FromDevice.Ip, job.FromDevice.Device), true
}
return fmt.Sprintf("Bad status code moving partition %d: %d",
job.Partition, resp.StatusCode), false
}
// doPriRepJobs executes a list of PriorityRepJobs, limiting concurrent jobs per device to deviceMax.
func doPriRepJobs(jobs []*PriorityRepJob, deviceMax int, client *http.Client) []uint64 {
limiter := &devLimiter{inUse: make(map[int]int), max: deviceMax, somethingFinished: make(chan struct{}, 1)}
wg := sync.WaitGroup{}
badParts := []uint64{}
var badPartsLock sync.Mutex
for len(jobs) > 0 {
foundDoable := false
for i := range jobs {
if !limiter.start(jobs[i]) {
continue
}
foundDoable = true
wg.Add(1)
go func(job *PriorityRepJob) {
defer wg.Done()
defer limiter.finished(job)
res, ok := SendPriRepJob(job, client)
fmt.Println(res)
if !ok {
badPartsLock.Lock()
badParts = append(badParts, job.Partition)
badPartsLock.Unlock()
}
}(jobs[i])
jobs = append(jobs[:i], jobs[i+1:]...)
break
}
if !foundDoable {
limiter.waitForSomethingToFinish()
}
}
wg.Wait()
return badParts
}
// getPartMoveJobs takes two rings and creates a list of jobs for any partition moves between them.
func getPartMoveJobs(oldRing, newRing ring.Ring, overrideParts []uint64, policy int) []*PriorityRepJob {
allNewDevices := map[string]bool{}
for _, dev := range newRing.AllDevices() {
if dev == nil {
continue
}
allNewDevices[fmt.Sprintf("%s:%d/%s", dev.Ip, dev.Port, dev.Device)] = true
}
jobs := make([]*PriorityRepJob, 0)
for i := uint64(0); true; i++ {
partition := i
if len(overrideParts) > 0 {
if int(partition) < len(overrideParts) {
partition = overrideParts[partition]
} else {
break
}
}
olddevs := oldRing.GetNodes(partition)
newdevs := newRing.GetNodes(partition)
if olddevs == nil || newdevs == nil {
break
}
for i := range olddevs {
if olddevs[i].Id != newdevs[i].Id {
// TODO: handle if a node just changes positions, which doesn't happen, but isn't against the contract.
fromDev := olddevs[i]
if _, ok := allNewDevices[fmt.Sprintf("%s:%d/%s", fromDev.Ip, fromDev.Port, fromDev.Device)]; !ok {
fromDev = olddevs[(i+1)%len(olddevs)]
}
jobs = append(jobs, &PriorityRepJob{
Partition: partition,
FromDevice: fromDev,
ToDevice: newdevs[i],
Policy: policy,
})
}
}
}
return jobs
}
func objectRingPolicyIndex(s string) (int, error) {
if !strings.Contains(s, "object") {
return 0, fmt.Errorf("object not in string: %v", s)
}
re := regexp.MustCompile(`object-(\d*)`)
match := re.FindStringSubmatch(s)
if match == nil {
return 0, nil
} else {
policyIdx, err := strconv.Atoi(match[1])
if err != nil {
return 0, fmt.Errorf("invalid policy index: %v\n", match[1])
}
return policyIdx, nil
}
}
func doMoveParts(args []string, cnf srv.ConfigLoader) int {
flags := flag.NewFlagSet("moveparts", flag.ExitOnError)
policy := flags.Int("p", 0, "policy index to use")
certFile := flags.String("certfile", "", "Cert file to use for setting up https client")
keyFile := flags.String("keyfile", "", "Key file to use for setting up https client")
flags.Usage = func() {
fmt.Fprintf(os.Stderr, "USAGE: hummingbird moveparts [old ringfile]")
flags.PrintDefaults()
}
flags.Parse(args)
if len(flags.Args()) != 1 {
flags.Usage()
return 1
}
oldPolicy, policyErr := objectRingPolicyIndex(flags.Arg(0))
if policyErr != nil {
fmt.Println("Invalid ring:", policyErr)
return 1
}
if oldPolicy != *policy {
fmt.Printf("Old policy: %v doesn't match specified policy: %v\n", oldPolicy, *policy)
return 1
}
hashPathPrefix, hashPathSuffix, err := cnf.GetHashPrefixAndSuffix()
if err != nil {
fmt.Println("Unable to load hash path prefix and suffix:", err)
return 1
}
oldRing, err := ring.LoadRing(flags.Arg(0), hashPathPrefix, hashPathSuffix)
if err != nil {
fmt.Println("Unable to load old ring:", err)
return 1
}
curRing, err := ring.GetRing("object", hashPathPrefix, hashPathSuffix, *policy)
if err != nil {
fmt.Println("Unable to load current ring:", err)
return 1
}
transport := &http.Transport{
MaxIdleConnsPerHost: 100,
MaxIdleConns: 0,
}
if *certFile != "" && *keyFile != "" {
tlsConf, err := common.NewClientTLSConfig(*certFile, *keyFile)
if err != nil {
fmt.Println("Error getting TLS config:", err)
return 1
}
transport.TLSClientConfig = tlsConf
if err = http2.ConfigureTransport(transport); err != nil {
fmt.Println("Error setting up http2:", err)
return 1
}
}
client := &http.Client{Timeout: time.Hour,
Transport: transport,
}
badParts := []uint64{}
for {
jobs := getPartMoveJobs(oldRing, curRing, badParts, *policy)
lastRun := len(jobs)
for i := len(jobs) - 1; i > 0; i-- { // shuffle jobs list
j := rand.Intn(i + 1)
jobs[j], jobs[i] = jobs[i], jobs[j]
}
fmt.Println("Job count:", len(jobs))
badParts = doPriRepJobs(jobs, 2, client)
if len(badParts) == 0 {
break
} else {
fmt.Printf("Finished run of partitions. retrying %d.\n", len(badParts))
fmt.Println("NOTE: This will loop on any partitions not found on any primary")
if lastRun == len(badParts) {
time.Sleep(time.Minute * 5)
} else {
time.Sleep(time.Second * 5)
}
}
}
fmt.Println("Done sending jobs.")
return 0
}
// MoveParts takes two object .ring.gz files as []string{oldRing, newRing} and dispatches priority replication jobs to rebalance data in line with any ring changes.
func MoveParts(args []string, cnf srv.ConfigLoader) {
ret := doMoveParts(args, cnf)
os.Exit(ret)
}
// getRestoreDeviceJobs takes an ip address and device name, and creates a list of jobs to restore that device's data from peers.
func getRestoreDeviceJobs(theRing ring.Ring, ip string, devName string, srcRegionOnly int, allPeers bool, overrideParts []uint64, policy int) []*PriorityRepJob {
jobs := make([]*PriorityRepJob, 0)
for i := uint64(0); true; i++ {
partition := i
if len(overrideParts) > 0 {
if int(partition) < len(overrideParts) {
partition = overrideParts[partition]
} else {
break
}
}
devs := theRing.GetNodes(partition)
if devs == nil {
break
}
var toDev *ring.Device
for _, dev := range devs {
if dev.Device == devName && (dev.Ip == ip || dev.ReplicationIp == ip) {
toDev = dev
break
}
}
if toDev != nil {
foundJob := false
for len(devs) > 0 {
rd := rand.Intn(len(devs))
src := devs[rd]
devs = append(devs[:rd], devs[rd+1:]...)
if src.Device == toDev.Device && (src.Ip == toDev.Ip || src.ReplicationIp == toDev.ReplicationIp) {
continue
}
if srcRegionOnly >= 0 && src.Region != srcRegionOnly {
continue
}
jobs = append(jobs, &PriorityRepJob{
Partition: partition,
FromDevice: src,
ToDevice: toDev,
Policy: policy,
})
foundJob = true
if !allPeers {
break
}
}
if !foundJob {
fmt.Printf("Could not find job for partition: %d\n", partition)
}
}
}
return jobs
}
// RestoreDevice takes an IP address and device name such as []string{"172.24.0.1", "sda1"} and attempts to restores its data from peers.
func RestoreDevice(args []string, cnf srv.ConfigLoader) {
flags := flag.NewFlagSet("restoredevice", flag.ExitOnError)
policy := flags.Int("p", 0, "policy index to use")
region := flags.Int("region", -1, "restore device only from peers in specified region")
ringLoc := flags.String("r", "", "Specify which ring file to use")
conc := flags.Int("c", 2, "limit of per device concurrency priority repl calls")
full := flags.Bool("f", false, "send priority replicate calls to every qualifying peer primary (slow)")
certFile := flags.String("certfile", "", "Cert file to use for setting up https client")
keyFile := flags.String("keyfile", "", "Key file to use for setting up https client")
flags.Usage = func() {
fmt.Fprintf(os.Stderr, "USAGE: hummingbird restoredevice [ip] [device]\n")
flags.PrintDefaults()
}
flags.Parse(args)
if len(flags.Args()) != 2 {
flags.Usage()
return
}
hashPathPrefix, hashPathSuffix, err := cnf.GetHashPrefixAndSuffix()
if err != nil {
fmt.Println("Unable to load hash path prefix and suffix:", err)
return
}
var objRing ring.Ring
if *ringLoc == "" {
objRing, err = ring.GetRing("object", hashPathPrefix, hashPathSuffix, *policy)
if err != nil {
fmt.Println("Unable to load ring:", err)
return
}
} else {
objRing, err = ring.LoadRing(*ringLoc, hashPathPrefix, hashPathSuffix)
if err != nil {
fmt.Println("Unable to load ring:", err)
return
}
}
transport := &http.Transport{
MaxIdleConnsPerHost: 100,
MaxIdleConns: 0,
}
if *certFile != "" && *keyFile != "" {
tlsConf, err := common.NewClientTLSConfig(*certFile, *keyFile)
if err != nil {
fmt.Println("Error getting TLS config:", err)
return
}
transport.TLSClientConfig = tlsConf
if err = http2.ConfigureTransport(transport); err != nil {
fmt.Println("Error setting up http2:", err)
return
}
}
client := &http.Client{
Timeout: time.Hour * 4,
Transport: transport,
}
badParts := []uint64{}
for {
jobs := getRestoreDeviceJobs(objRing, flags.Arg(0), flags.Arg(1), *region, *full, badParts, *policy)
lastRun := len(jobs)
fmt.Println("Job count:", len(jobs))
for i := len(jobs) - 1; i > 0; i-- { // shuffle jobs list
j := rand.Intn(i + 1)
jobs[j], jobs[i] = jobs[i], jobs[j]
}
badParts = doPriRepJobs(jobs, *conc, client)
if len(badParts) == 0 {
break
} else {
fmt.Printf("Finished run of partitions. retrying %d.\n", len(badParts))
fmt.Println("NOTE: This will loop on any partitions not found on any primary")
if lastRun == len(badParts) {
time.Sleep(time.Minute * 5)
} else {
time.Sleep(time.Second * 5)
}
}
}
fmt.Println("Done sending jobs.")
}