-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathcluster.go
More file actions
352 lines (319 loc) · 9.82 KB
/
cluster.go
File metadata and controls
352 lines (319 loc) · 9.82 KB
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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 store
import (
"context"
"encoding/json"
"errors"
"fmt"
"sort"
"strconv"
"strings"
"sync/atomic"
"github.com/apache/kvrocks-controller/consts"
)
type Cluster struct {
Name string `json:"name"`
Version atomic.Int64 `json:"-"`
Shards []*Shard `json:"shards"`
}
func NewCluster(name string, nodes []string, replicas int) (*Cluster, error) {
if len(nodes) == 0 {
return nil, errors.New("cluster nodes should NOT be empty")
}
if replicas < 0 {
return nil, errors.New("replicas should NOT be less than 0")
}
if replicas == 0 {
replicas = 1
}
if len(nodes)%replicas != 0 {
return nil, errors.New("cluster nodes should be divisible by replicas")
}
shardCount := len(nodes) / replicas
shards := make([]*Shard, 0)
slotRanges := CalculateSlotRanges(shardCount)
for i := 0; i < shardCount; i++ {
shard := NewShard()
shard.Nodes = make([]Node, 0)
for j := 0; j < replicas; j++ {
addr := nodes[i*replicas+j]
role := RoleMaster
if j != 0 {
role = RoleSlave
}
node := NewClusterNode(addr, "")
node.SetRole(role)
shard.Nodes = append(shard.Nodes, node)
}
shard.SlotRanges = append(shard.SlotRanges, slotRanges[i])
shards = append(shards, shard)
}
cluster := &Cluster{Name: name, Shards: shards}
cluster.Version.Store(1)
return cluster, nil
}
func (cluster *Cluster) Clone() *Cluster {
clone := &Cluster{
Name: cluster.Name,
Shards: make([]*Shard, 0),
}
clone.Version.Store(cluster.Version.Load())
for _, shard := range cluster.Shards {
clone.Shards = append(clone.Shards, shard.Clone())
}
return clone
}
// SetPassword will set the password for all nodes in the cluster.
func (cluster *Cluster) SetPassword(password string) {
for i := 0; i < len(cluster.Shards); i++ {
for j := 0; j < len(cluster.Shards[i].Nodes); j++ {
cluster.Shards[i].Nodes[j].SetPassword(password)
}
}
}
func (cluster *Cluster) ToSlotString() (string, error) {
var builder strings.Builder
for i, shard := range cluster.Shards {
shardSlotsString, err := shard.ToSlotsString()
if err != nil {
return "", fmt.Errorf("found err at shard[%d]: %w", i, err)
}
builder.WriteString(shardSlotsString)
}
return builder.String(), nil
}
func (cluster *Cluster) GetShard(shardIndex int) (*Shard, error) {
if shardIndex < 0 || shardIndex >= len(cluster.Shards) {
return nil, consts.ErrIndexOutOfRange
}
return cluster.Shards[shardIndex], nil
}
func (cluster *Cluster) AddNode(shardIndex int, addr, role, password string) (*ClusterNode, error) {
if shardIndex < 0 || shardIndex >= len(cluster.Shards) {
return nil, consts.ErrIndexOutOfRange
}
return cluster.Shards[shardIndex].addNode(addr, role, password)
}
func (cluster *Cluster) RemoveNode(shardIndex int, nodeID string) error {
if shardIndex < 0 || shardIndex >= len(cluster.Shards) {
return consts.ErrIndexOutOfRange
}
return cluster.Shards[shardIndex].removeNode(nodeID)
}
func (cluster *Cluster) PromoteNewMaster(ctx context.Context,
shardIdx int, masterNodeID, preferredNodeID string,
) (string, error) {
shard, err := cluster.GetShard(shardIdx)
if err != nil {
return "", err
}
newMasterNodeID, err := shard.PromoteNewMaster(ctx, masterNodeID, preferredNodeID)
if err != nil {
return "", err
}
cluster.Shards[shardIdx] = shard
return newMasterNodeID, nil
}
func (cluster *Cluster) SyncToNodes(ctx context.Context) error {
for i := 0; i < len(cluster.Shards); i++ {
for _, node := range cluster.Shards[i].Nodes {
if err := node.SyncClusterInfo(ctx, cluster); err != nil {
return err
}
}
}
return nil
}
func (cluster *Cluster) GetNodes() []Node {
nodes := make([]Node, 0)
for i := 0; i < len(cluster.Shards); i++ {
nodes = append(nodes, cluster.Shards[i].Nodes...)
}
return nodes
}
func (cluster *Cluster) Reset(ctx context.Context) error {
for i := 0; i < len(cluster.Shards); i++ {
for _, node := range cluster.Shards[i].Nodes {
if err := node.Reset(ctx); err != nil {
return err
}
}
}
return nil
}
func (cluster *Cluster) findShardIndexBySlot(slot SlotRange) (int, error) {
sourceShardIdx := -1
for i := 0; i < len(cluster.Shards); i++ {
slotRanges := cluster.Shards[i].SlotRanges
for _, slotRange := range slotRanges {
if slotRange.HasOverlap(slot) {
if sourceShardIdx != -1 {
return sourceShardIdx, consts.ErrSlotRangeBelongsToMultipleShards
}
sourceShardIdx = i
}
}
}
if sourceShardIdx == -1 {
return -1, consts.ErrSlotNotBelongToAnyShard
}
return sourceShardIdx, nil
}
func (cluster *Cluster) MigrateSlot(ctx context.Context, slot SlotRange, targetShardIdx int, slotOnly bool) error {
if targetShardIdx < 0 || targetShardIdx >= len(cluster.Shards) {
return consts.ErrIndexOutOfRange
}
sourceShardIdx, err := cluster.findShardIndexBySlot(slot)
if err != nil {
return err
}
if sourceShardIdx == targetShardIdx {
return consts.ErrShardIsSame
}
if slotOnly {
// clear source migrating info to avoid mismatch migrating slot error
cluster.Shards[sourceShardIdx].ClearMigrateState()
cluster.Shards[sourceShardIdx].SlotRanges = RemoveSlotFromSlotRanges(cluster.Shards[sourceShardIdx].SlotRanges, slot)
cluster.Shards[targetShardIdx].SlotRanges = AddSlotToSlotRanges(cluster.Shards[targetShardIdx].SlotRanges, slot)
return nil
}
if cluster.Shards[sourceShardIdx].IsMigrating() || cluster.Shards[targetShardIdx].IsMigrating() {
return consts.ErrShardSlotIsMigrating
}
// Send the migration command to the source node
sourceMasterNode := cluster.Shards[sourceShardIdx].GetMasterNode()
if sourceMasterNode == nil {
return consts.ErrNotFound
}
targetNodeID := cluster.Shards[targetShardIdx].GetMasterNode().ID()
if err := sourceMasterNode.MigrateSlot(ctx, slot, targetNodeID); err != nil {
return err
}
// Will start the data migration in the background
cluster.Shards[sourceShardIdx].MigratingSlot = FromSlotRange(slot)
cluster.Shards[sourceShardIdx].TargetShardIndex = targetShardIdx
return nil
}
func (cluster *Cluster) SetSlot(ctx context.Context, slot int, targetNodeID string) error {
version := cluster.Version.Add(1)
for i := 0; i < len(cluster.Shards); i++ {
for _, node := range cluster.Shards[i].Nodes {
clusterNode, ok := node.(*ClusterNode)
if !ok {
continue
}
err := clusterNode.GetClient().Do(ctx, "CLUSTERX", "SETSLOT", slot, "NODE", targetNodeID, version).Err()
if err != nil {
return err
}
}
}
return nil
}
// ParseCluster will parse the cluster string into cluster topology.
func ParseCluster(clusterStr string) (*Cluster, error) {
if len(clusterStr) == 0 {
return nil, errors.New("cluster nodes string error")
}
nodeStrings := strings.Split(clusterStr, "\n")
if len(nodeStrings) == 0 {
return nil, errors.New("cluster nodes string parser error")
}
var clusterVer int64 = -1
var shards Shards
slaveNodes := make(map[string][]Node)
for _, nodeString := range nodeStrings {
fields := strings.Split(nodeString, " ")
if len(fields) < 7 {
return nil, fmt.Errorf("require at least 7 fields, node info[%s]", nodeString)
}
node := &ClusterNode{
id: fields[0],
addr: strings.Split(fields[1], "@")[0],
}
if strings.Contains(fields[2], ",") {
node.role = strings.Split(fields[2], ",")[1]
} else {
node.role = fields[2]
}
var err error
clusterVer, err = strconv.ParseInt(fields[6], 10, 64)
if err != nil {
return nil, fmt.Errorf("node version error, node info[%q]", nodeString)
}
if node.role == RoleMaster {
shard := NewShard()
shard.Nodes = append(shard.Nodes, node)
// remain fields are slot ranges
for i := 8; i < len(fields); i++ {
slotRange, err := ParseSlotRange(fields[i])
if err != nil {
return nil, fmt.Errorf("parse slots error for node[%s]: %w", nodeString, err)
}
shard.SlotRanges = append(shard.SlotRanges, *slotRange)
}
shards = append(shards, shard)
} else if node.role == RoleSlave {
slaveNodes[fields[3]] = append(slaveNodes[fields[3]], node)
} else {
return nil, fmt.Errorf("node role error, node info[%q]", nodeString)
}
}
if clusterVer == -1 {
return nil, fmt.Errorf("no cluster version, cluster info[%q]", clusterStr)
}
sort.Sort(shards)
for i := 0; i < len(shards); i++ {
masterNode := shards[i].Nodes[0]
shards[i].Nodes = append(shards[i].Nodes, slaveNodes[masterNode.ID()]...)
}
clusterInfo := &Cluster{
Shards: shards,
}
clusterInfo.Version.Store(clusterVer)
return clusterInfo, nil
}
// MarshalJSON is a custom function since the atomic.Int64 type does not directly implement JSON marshaling.
func (cluster *Cluster) MarshalJSON() ([]byte, error) {
type Alias Cluster // to avoid recursion
return json.Marshal(&struct {
Version int64 `json:"version"`
*Alias
}{
Version: cluster.Version.Load(),
Alias: (*Alias)(cluster),
})
}
// UnmarshalJSON is a custom function since the atomic.Int64 type does not directly implement JSON unmarshaling.
func (cluster *Cluster) UnmarshalJSON(data []byte) error {
type Alias Cluster
aux := &struct {
Version int64 `json:"version"`
*Alias
}{
Alias: (*Alias)(cluster),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
cluster.Version.Store(aux.Version)
return nil
}