forked from coreos/torus
/
ring.go
192 lines (179 loc) · 4.64 KB
/
ring.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
package main
import (
"fmt"
"os"
"strconv"
"github.com/coreos/torus"
"github.com/coreos/torus/models"
"github.com/coreos/torus/ring"
"github.com/spf13/cobra"
)
var (
ringType string
peers torus.PeerInfoList
uuids []string
allUUIDs bool
repFactor int
mds torus.MetadataService
)
var ringCommand = &cobra.Command{
Use: "ring",
Short: "modify the ring of the cluster (ADVANCED)",
Run: ringAction,
}
var ringChangeReplicationCommand = &cobra.Command{
Use: "set-replication [AMOUNT]",
Short: "set the replication count for the cluster",
Run: ringChangeReplicationAction,
}
var ringChangeCommand = &cobra.Command{
Use: "manual-change",
Short: "apply a new ring to the cluster",
PreRun: ringChangePreRun,
Run: ringChangeAction,
}
var ringGetCommand = &cobra.Command{
Use: "get",
Short: "get the ring from the cluster",
Run: ringGetAction,
}
func init() {
ringCommand.AddCommand(ringChangeReplicationCommand)
ringCommand.AddCommand(ringChangeCommand)
ringCommand.AddCommand(ringGetCommand)
ringChangeCommand.Flags().StringSliceVar(&uuids, "uuids", []string{}, "uuids to incorporate in the ring")
ringChangeCommand.Flags().BoolVar(&allUUIDs, "all-peers", false, "use all peers in the ring")
ringChangeCommand.Flags().StringVar(&ringType, "type", "single", "type of ring to create")
ringChangeCommand.Flags().IntVarP(&repFactor, "replication", "r", 2, "type of ring to create")
}
func ringAction(cmd *cobra.Command, args []string) {
cmd.Usage()
fmt.Print("\n\tOne ring to rule them all, one ring to find them,\n\tOne ring to bring them all and in the darkness bind them\n\n")
os.Exit(1)
}
func ringGetAction(cmd *cobra.Command, args []string) {
mds := mustConnectToMDS()
ring, err := mds.GetRing()
if err != nil {
die("couldn't get ring: %v", err)
}
fmt.Println(ring.Describe())
}
func ringChangeAction(cmd *cobra.Command, args []string) {
if mds == nil {
mds = mustConnectToMDS()
}
currentRing, err := mds.GetRing()
if err != nil {
die("couldn't get ring: %v", err)
}
var newRing torus.Ring
switch ringType {
case "empty":
newRing, err = ring.CreateRing(&models.Ring{
Type: uint32(ring.Empty),
Version: uint32(currentRing.Version() + 1),
})
case "single":
newRing, err = ring.CreateRing(&models.Ring{
Type: uint32(ring.Single),
Peers: peers,
Version: uint32(currentRing.Version() + 1),
})
case "mod":
newRing, err = ring.CreateRing(&models.Ring{
Type: uint32(ring.Mod),
Peers: peers,
ReplicationFactor: uint32(repFactor),
Version: uint32(currentRing.Version() + 1),
})
case "ketama":
newRing, err = ring.CreateRing(&models.Ring{
Type: uint32(ring.Ketama),
Peers: peers,
ReplicationFactor: uint32(repFactor),
Version: uint32(currentRing.Version() + 1),
})
default:
panic("still unknown ring type")
}
if err != nil {
die("couldn't create new ring: %v", err)
}
cfg := torus.Config{
MetadataAddress: etcdAddress,
}
err = torus.SetRing("etcd", cfg, newRing)
if err != nil {
die("couldn't set new ring: %v", err)
}
}
func ringChangePreRun(cmd *cobra.Command, args []string) {
mds = mustConnectToMDS()
currentPeers, err := mds.GetPeers()
if allUUIDs {
if allUUIDs && len(uuids) != 0 {
die("use only one of --uuids or --all-peers")
}
if err != nil {
die("couldn't get peer list: %v", err)
}
uuids = currentPeers.PeerList()
}
for _, p := range currentPeers {
if p.Address == "" {
continue
}
for _, x := range uuids {
if p.UUID == x {
peers = append(peers, p)
}
}
}
switch ringType {
case "empty":
uuids = nil
return
case "single":
if len(peers) != 1 {
die("require one uuid (use --uuids)\n")
}
return
case "mod":
if len(peers) == 0 {
die("need one of --uuids or --all-peers")
}
case "ketama":
if len(peers) == 0 {
die("need one of --uuids or --all-peers")
}
default:
die(`invalid ring type %s (try "empty", "mod" or "single")`, ringType)
}
}
func ringChangeReplicationAction(cmd *cobra.Command, args []string) {
amount, err := strconv.Atoi(args[0])
if err != nil {
die("not an integer number of replicas: %s", args[0])
}
if mds == nil {
mds = mustConnectToMDS()
}
currentRing, err := mds.GetRing()
if err != nil {
die("couldn't get ring: %v", err)
}
var newRing torus.Ring
if r, ok := currentRing.(torus.ModifyableRing); ok {
newRing, err = r.ChangeReplication(amount)
} else {
die("current ring type cannot support changing the replication amount")
}
if err != nil {
die("couldn't change replication amount: %v", err)
}
err = mds.SetRing(newRing)
if err != nil {
die("couldn't set new ring: %v", err)
}
}