-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsm_cmd.go
220 lines (197 loc) · 4.93 KB
/
fsm_cmd.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
package zongzi
import (
"encoding/json"
"strings"
)
const (
command_type_host = "host"
command_type_replica = "replica"
command_type_shard = "shard"
command_type_snapshot = "snapshot"
command_action_del = "del"
command_action_put = "put"
command_action_post = "post"
command_action_status_update = "status-update"
command_action_tags_set = "tags-set"
command_action_tags_setnx = "tags-setnx"
command_action_tags_remove = "tags-remove"
command_action_leader_set = "leader-set"
)
type command struct {
Action string `json:"action"`
Type string `json:"type"`
}
type commandHost struct {
command
Host Host `json:"host"`
}
type commandShard struct {
command
Shard Shard `json:"shard"`
}
type commandReplica struct {
command
Replica Replica `json:"replica"`
}
func newCmdHostPut(nhid, apiAddr, raftAddr string, tagList []string, status HostStatus, shardTypes []string) (b []byte) {
b, _ = json.Marshal(commandHost{command{
Action: command_action_put,
Type: command_type_host,
}, Host{
ApiAddress: apiAddr,
ID: nhid,
Tags: tagMapFromList(tagList),
RaftAddress: raftAddr,
ShardTypes: shardTypes,
Status: status,
}})
return
}
func newCmdHostDel(nhid string) (b []byte) {
b, _ = json.Marshal(commandHost{command{
Action: command_action_del,
Type: command_type_host,
}, Host{
ID: nhid,
}})
return
}
func newCmdShardPost(s Shard) (b []byte) {
b, _ = json.Marshal(commandShard{command{
Action: command_action_post,
Type: command_type_shard,
}, s})
return
}
func newCmdShardPut(s Shard) (b []byte) {
b, _ = json.Marshal(commandShard{command{
Action: command_action_put,
Type: command_type_shard,
}, s})
return
}
func newCmdShardStatusUpdate(id uint64, status ShardStatus) (b []byte) {
b, _ = json.Marshal(commandShard{command{
Action: command_action_status_update,
Type: command_type_shard,
}, Shard{
ID: id,
Status: status,
}})
return
}
func newCmdShardLeaderSet(shardID, replicaID, term uint64) (b []byte) {
b, _ = json.Marshal(commandShard{command{
Action: command_action_leader_set,
Type: command_type_shard,
}, Shard{
ID: shardID,
Leader: replicaID,
Term: term,
}})
return
}
func newCmdShardDel(shardID uint64) (b []byte) {
b, _ = json.Marshal(commandShard{command{
Action: command_action_del,
Type: command_type_shard,
}, Shard{
ID: shardID,
}})
return
}
func newCmdReplicaPost(nhid string, shardID uint64, isNonVoting bool) (b []byte) {
b, _ = json.Marshal(commandReplica{command{
Action: command_action_post,
Type: command_type_replica,
}, Replica{
HostID: nhid,
IsNonVoting: isNonVoting,
ShardID: shardID,
Status: ReplicaStatus_New,
}})
return
}
func newCmdReplicaPut(nhid string, shardID, replicaID uint64, isNonVoting bool) (b []byte) {
b, _ = json.Marshal(commandReplica{command{
Action: command_action_put,
Type: command_type_replica,
}, Replica{
HostID: nhid,
ID: replicaID,
ShardID: shardID,
IsNonVoting: isNonVoting,
}})
return
}
func newCmdReplicaDelete(replicaID uint64) (b []byte) {
b, _ = json.Marshal(commandReplica{command{
Action: command_action_del,
Type: command_type_replica,
}, Replica{
ID: replicaID,
}})
return
}
func newCmdReplicaUpdateStatus(replicaID uint64, status ReplicaStatus) (b []byte) {
b, _ = json.Marshal(commandReplica{command{
Action: command_action_status_update,
Type: command_type_replica,
}, Replica{
ID: replicaID,
Status: status,
}})
return
}
func newCmdTagsSetNX(subject any, tagList ...string) []byte {
return newCmdTags(command_action_tags_setnx, subject, tagList)
}
func newCmdTagsSet(subject any, tagList ...string) []byte {
return newCmdTags(command_action_tags_set, subject, tagList)
}
func newCmdTagsRemove(subject any, tagList ...string) []byte {
return newCmdTags(command_action_tags_remove, subject, tagList)
}
func newCmdTags(action string, subject any, tagList []string) (b []byte) {
switch subject.(type) {
case Host:
b, _ = json.Marshal(commandHost{command{
Action: action,
Type: command_type_host,
}, Host{
ID: subject.(Host).ID,
Tags: tagMapFromList(tagList),
}})
case Shard:
b, _ = json.Marshal(commandShard{command{
Action: action,
Type: command_type_shard,
}, Shard{
ID: subject.(Shard).ID,
Tags: tagMapFromList(tagList),
}})
case Replica:
b, _ = json.Marshal(commandReplica{command{
Action: action,
Type: command_type_replica,
}, Replica{
ID: subject.(Replica).ID,
Tags: tagMapFromList(tagList),
}})
}
return
}
// tagMapFromList converts a list of tags to a map of key (namespace:predicate) and value
//
// ["geo:region=us-west-1"] -> {"geo:region": "us-west-1"}
func tagMapFromList(tagList []string) map[string]string {
var m = map[string]string{}
for _, ts := range tagList {
if i := strings.Index(ts, "="); i >= 0 {
m[ts[:i]] = ts[i+1:]
} else {
m[ts] = ""
}
}
return m
}