-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
138 lines (104 loc) · 2.54 KB
/
config.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
package rnas
import (
"sort"
log "github.com/sirupsen/logrus"
)
type size_t int64
type StripeConfig struct {
K int `json:"K"`
M int `json:"M"`
MinDepth int `json:"minDepth"`
StripeDepth int `json:"stripeDepth"`
}
type FileStripe struct {
ID int
Size size_t
// RealSize size_t
ConfigName string
Filepath string
StripeConfig
}
type Shard struct {
fileID int
shardIndex int
serverID string
shardHashname string
dataShard bool
size int
// StripeConfig
}
// Config struct for storing config data
type Config struct {
Name string `json:"name"`
Tolerance int `json:"tolerance"`
Servers []*Server `json:"servers"`
StripeConfig
maps map[string]*Server
slots []string
dryrun bool
}
func(c *Config) Init() {
log.Info("Config initializing...")
if c.K + c.M > len(c.Servers) {
log.Fatalf("need k + m <= servers, but %d + %d > %d", c.K, c.M, len(c.Servers))
}
if c.Tolerance > c.M {
log.Fatalf("need tolerrance <= M, but %d > %d", c.Tolerance, c.M)
}
c.maps = make(map[string]*Server)
c.slots = make([]string, c.K + c.M)
log.Info("- init servers")
for i := range c.Servers {
server := c.Servers[i]
_, ok := c.maps[server.Id]
if ok {
log.Fatalf("Duplicated id: %s", server.Id)
}
if c.dryrun {
server.Type = "dryrun"
}
server.Init(c)
c.maps[server.Id] = server
}
log.Info("- schedule slots")
c.ScheduleSlots()
log.Info("Init Done")
}
func(c *Config) ScheduleSlots() {
log.Info("- start to schedule shard slots")
freeSlots := c.M / c.Tolerance - 1
var servers Servers = c.Servers
sort.Sort(servers)
// n := c.K + c.M - freeSlots
log.Debugf("- free slots: %d", freeSlots)
nextSlotIndex := 0
for i := 0; i < servers.Len() && nextSlotIndex < len(c.slots); i++ {
if !servers[i].reachable {
continue
}
c.slots[nextSlotIndex] = servers[i].Id
nextSlotIndex++
for ;freeSlots > 0; freeSlots-- {
c.slots[nextSlotIndex] = servers[i].Id
nextSlotIndex++
}
}
if nextSlotIndex != len(c.slots) {
log.Fatalf("unenough servers. At least K + M - freeSlots = %d", c.K + c.M - (c.M / c.Tolerance - 1))
}
log.Infof("- slots alloc: %v", c.slots)
}
func(c *Config) TestAll() {
for i := range c.Servers {
server := c.Servers[i]
log.Infof("Start to test speed for %s", server.Id)
err := server.TestSpeed()
if err != nil {
log.Infof("failed to test speed for %s, because: %v", server.Id, err)
continue
}
log.Infof("- Result: ↑ %.2fB/s ↓ %.2fB/s", server.UploadBandwidth, server.DownloadBandwidth)
}
c.ScheduleSlots()
SaveConfigToDB(c)
}