forked from lni/dragonboat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
115 lines (102 loc) · 3.4 KB
/
client.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
// Copyright 2017-2019 Lei Ni ([email protected])
//
// 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 drummer
import (
"context"
"errors"
pb "github.com/lni/dragonboat/drummer/drummerpb"
)
var (
// ErrInvalidRequest indicates the request can not be fulfilled as it is
// regarded as invalid.
ErrInvalidRequest = errors.New("invalid drummer request")
)
// AddDrummerServer adds a new drummer node with specified nodeID and address
// to the Drummer cluster.
func AddDrummerServer(ctx context.Context, client pb.DrummerClient,
nodeID uint64, address string) (*pb.Empty, error) {
req := &pb.DrummerConfigRequest{
NodeId: nodeID,
Address: address,
}
return client.AddDrummerServer(ctx, req)
}
// RemoveDrummerServer removes the specified node from the Drummer cluster.
func RemoveDrummerServer(ctx context.Context, client pb.DrummerClient,
nodeID uint64) (*pb.Empty, error) {
req := &pb.DrummerConfigRequest{
NodeId: nodeID,
}
return client.RemoveDrummerServer(ctx, req)
}
// SubmitCreateDrummerChange submits Drummer change used for defining clusters.
func SubmitCreateDrummerChange(ctx context.Context, client pb.DrummerClient,
clusterID uint64, members []uint64, appName string) error {
checkClusterIDValue(clusterID)
if len(appName) == 0 {
panic("empty app name")
}
if len(members) == 0 {
panic("empty members")
}
change := pb.Change{
Type: pb.Change_CREATE,
ClusterId: clusterID,
Members: members,
AppName: appName,
}
req, err := client.SubmitChange(ctx, &change)
if err != nil {
return err
}
if req.Code == pb.ChangeResponse_BOOTSTRAPPED {
return ErrInvalidRequest
}
return nil
}
// GetClusterCollection returns known clusters from the Drummer server.
func GetClusterCollection(ctx context.Context,
client pb.DrummerClient) (*pb.ClusterCollection, error) {
return client.GetClusters(ctx, &pb.Empty{})
}
// GetClusterStates returns cluster states known to the Drummer server.
func GetClusterStates(ctx context.Context,
client pb.DrummerClient, clusters []uint64) (*pb.ClusterStates, error) {
req := &pb.ClusterStateRequest{
ClusterIdList: clusters,
}
return client.GetClusterStates(ctx, req)
}
// SubmitRegions submits regions info to the Drummer server.
func SubmitRegions(ctx context.Context,
client pb.DrummerClient, region pb.Regions) error {
_, err := client.SetRegions(ctx, ®ion)
return err
}
// SubmitBootstrappped sets the bootstrapped flag on Drummer server.
func SubmitBootstrappped(ctx context.Context,
client pb.DrummerClient) error {
_, err := client.SetBootstrapped(ctx, &pb.Empty{})
return err
}
// GetNodeHostCollection returns nodehosts known to the Drummer.
func GetNodeHostCollection(ctx context.Context,
client pb.DrummerClient) (*pb.NodeHostCollection, error) {
return client.GetNodeHostCollection(ctx, &pb.Empty{})
}
func checkClusterIDValue(clusterID uint64) {
if clusterID == 0 {
panic("cluster id must be specified and it can not be 0")
}
}