forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actionnode.go
342 lines (294 loc) · 10.5 KB
/
actionnode.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
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
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Actions modify the state of a tablet, shard or keyspace.
//
// They are currenty managed through a series of queues stored in topology server.
package actionnode
import (
"encoding/json"
"fmt"
"os"
"os/user"
"strings"
"time"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/jscfg"
"github.com/youtube/vitess/go/vt/hook"
myproto "github.com/youtube/vitess/go/vt/mysqlctl/proto"
"github.com/youtube/vitess/go/vt/topo"
)
const (
// FIXME(msolomon) why is ActionState a type, but Action is not?
//
// Tablet actions. These are triggered by ActionNodes or RPCs,
// and executed by an agent within vttablet.
//
TABLET_ACTION_PING = "Ping"
TABLET_ACTION_SLEEP = "Sleep"
TABLET_ACTION_SET_RDONLY = "SetReadOnly"
TABLET_ACTION_SET_RDWR = "SetReadWrite"
TABLET_ACTION_CHANGE_TYPE = "ChangeType"
TABLET_ACTION_SET_BLACKLISTED_TABLES = "SetBlacklistedTables"
TABLET_ACTION_DEMOTE_MASTER = "DemoteMaster"
TABLET_ACTION_PROMOTE_SLAVE = "PromoteSlave"
// SlaveWasPromoted tells a tablet this previously slave
// tablet is now the master. The tablet will update its
// own topology record.
TABLET_ACTION_SLAVE_WAS_PROMOTED = "SlaveWasPromoted"
TABLET_ACTION_RESTART_SLAVE = "RestartSlave"
// SlaveWasRestarted tells a tablet the mysql master was changed.
// The tablet will check it is indeed the case, and update its own
// topology record.
TABLET_ACTION_SLAVE_WAS_RESTARTED = "SlaveWasRestarted"
// StopSlave will stop MySQL replication.
TABLET_ACTION_STOP_SLAVE = "StopSlave"
// StopSlave will stop MySQL replication after it reaches a
// minimum point.
TABLET_ACTION_STOP_SLAVE_MINIMUM = "StopSlaveMinimum"
// StartSlave will start MySQL replication.
TABLET_ACTION_START_SLAVE = "StartSlave"
TABLET_ACTION_BREAK_SLAVES = "BreakSlaves"
TABLET_ACTION_MASTER_POSITION = "MasterPosition"
TABLET_ACTION_REPARENT_POSITION = "ReparentPosition"
TABLET_ACTION_SLAVE_POSITION = "SlavePosition"
TABLET_ACTION_WAIT_SLAVE_POSITION = "WaitSlavePosition"
TABLET_ACTION_WAIT_BLP_POSITION = "WaitBlpPosition"
TABLET_ACTION_STOP_BLP = "StopBlp"
TABLET_ACTION_START_BLP = "StartBlp"
TABLET_ACTION_RUN_BLP_UNTIL = "RunBlpUntil"
TABLET_ACTION_SCRAP = "Scrap"
TABLET_ACTION_GET_SCHEMA = "GetSchema"
TABLET_ACTION_PREFLIGHT_SCHEMA = "PreflightSchema"
TABLET_ACTION_APPLY_SCHEMA = "ApplySchema"
TABLET_ACTION_RELOAD_SCHEMA = "ReloadSchema"
TABLET_ACTION_EXECUTE_FETCH = "ExecuteFetch"
TABLET_ACTION_GET_PERMISSIONS = "GetPermissions"
TABLET_ACTION_EXECUTE_HOOK = "ExecuteHook"
TABLET_ACTION_GET_SLAVES = "GetSlaves"
TABLET_ACTION_SNAPSHOT = "Snapshot"
TABLET_ACTION_SNAPSHOT_SOURCE_END = "SnapshotSourceEnd"
TABLET_ACTION_RESERVE_FOR_RESTORE = "ReserveForRestore"
TABLET_ACTION_RESTORE = "Restore"
TABLET_ACTION_MULTI_SNAPSHOT = "MultiSnapshot"
TABLET_ACTION_MULTI_RESTORE = "MultiRestore"
//
// Shard actions - involve all tablets in a shard.
// These are just descriptive and used for locking / logging.
//
SHARD_ACTION_REPARENT = "ReparentShard"
SHARD_ACTION_EXTERNALLY_REPARENTED = "ShardExternallyReparented"
// Recompute derived shard-wise data
SHARD_ACTION_REBUILD = "RebuildShard"
// Generic read lock for inexpensive shard-wide actions.
SHARD_ACTION_CHECK = "CheckShard"
// Apply a schema change on an entire shard
SHARD_ACTION_APPLY_SCHEMA = "ApplySchemaShard"
// Changes the ServedTypes inside a shard
SHARD_ACTION_SET_SERVED_TYPES = "SetShardServedTypes"
// Multi-restore on all tablets of a shard in parallel
SHARD_ACTION_MULTI_RESTORE = "ShardMultiRestore"
// Migrate served types from one shard to another
SHARD_ACTION_MIGRATE_SERVED_TYPES = "MigrateServedTypes"
// Update the Shard object (Cells, ...)
SHARD_ACTION_UPDATE_SHARD = "UpdateShard"
//
// Keyspace actions - require very high level locking for consistency.
// These are just descriptive and used for locking / logging.
//
KEYSPACE_ACTION_REBUILD = "RebuildKeyspace"
KEYSPACE_ACTION_APPLY_SCHEMA = "ApplySchemaKeyspace"
KEYSPACE_ACTION_SET_SHARDING_INFO = "SetKeyspaceShardingInfo"
KEYSPACE_ACTION_MIGRATE_SERVED_FROM = "MigrateServedFrom"
//
// SrvShard actions - very local locking, for consistency.
// These are just descriptive and used for locking / logging.
//
SRV_SHARD_ACTION_REBUILD = "RebuildSrvShard"
// all the valid states for an action
ACTION_STATE_QUEUED = ActionState("") // All actions are queued initially
ACTION_STATE_RUNNING = ActionState("Running") // Running inside vtaction process
ACTION_STATE_FAILED = ActionState("Failed") // Ended with a failure
ACTION_STATE_DONE = ActionState("Done") // Ended with no failure
)
// ActionState is the state an ActionNode
type ActionState string
// ActionNode describes a long-running action on a tablet, or an action
// on a shard or keyspace that locks it.
type ActionNode struct {
Action string
ActionGuid string
Error string
State ActionState
Pid int // only != 0 if State == ACTION_STATE_RUNNING
// do not serialize the next fields
// path in topology server representing this action
Path string `json:"-"`
Args interface{} `json:"-"`
Reply interface{} `json:"-"`
}
// ActionNodeFromJson interprets the data from JSON.
func ActionNodeFromJson(data, path string) (*ActionNode, error) {
decoder := json.NewDecoder(strings.NewReader(data))
// decode the ActionNode
node := &ActionNode{}
err := decoder.Decode(node)
if err != nil {
return nil, err
}
node.Path = path
// figure out our args and reply types
switch node.Action {
case TABLET_ACTION_PING:
case TABLET_ACTION_SLEEP:
node.Args = new(time.Duration)
case TABLET_ACTION_SET_RDONLY:
case TABLET_ACTION_SET_RDWR:
case TABLET_ACTION_CHANGE_TYPE:
node.Args = new(topo.TabletType)
case TABLET_ACTION_DEMOTE_MASTER:
case TABLET_ACTION_PROMOTE_SLAVE:
node.Reply = &RestartSlaveData{}
case TABLET_ACTION_SLAVE_WAS_PROMOTED:
case TABLET_ACTION_RESTART_SLAVE:
node.Args = &RestartSlaveData{}
case TABLET_ACTION_SLAVE_WAS_RESTARTED:
node.Args = &SlaveWasRestartedArgs{}
case TABLET_ACTION_BREAK_SLAVES:
case TABLET_ACTION_REPARENT_POSITION:
node.Args = &myproto.ReplicationPosition{}
node.Reply = &RestartSlaveData{}
case TABLET_ACTION_SCRAP:
case TABLET_ACTION_PREFLIGHT_SCHEMA:
node.Args = new(string)
node.Reply = &myproto.SchemaChangeResult{}
case TABLET_ACTION_APPLY_SCHEMA:
node.Args = &myproto.SchemaChange{}
node.Reply = &myproto.SchemaChangeResult{}
case TABLET_ACTION_EXECUTE_HOOK:
node.Args = &hook.Hook{}
node.Reply = &hook.HookResult{}
case TABLET_ACTION_SNAPSHOT:
node.Args = &SnapshotArgs{}
node.Reply = &SnapshotReply{}
case TABLET_ACTION_SNAPSHOT_SOURCE_END:
node.Args = &SnapshotSourceEndArgs{}
case TABLET_ACTION_RESERVE_FOR_RESTORE:
node.Args = &ReserveForRestoreArgs{}
case TABLET_ACTION_RESTORE:
node.Args = &RestoreArgs{}
case TABLET_ACTION_MULTI_SNAPSHOT:
node.Args = &MultiSnapshotArgs{}
node.Reply = &MultiSnapshotReply{}
case TABLET_ACTION_MULTI_RESTORE:
node.Args = &MultiRestoreArgs{}
case SHARD_ACTION_REPARENT:
node.Args = &topo.TabletAlias{}
case SHARD_ACTION_EXTERNALLY_REPARENTED:
node.Args = &topo.TabletAlias{}
case SHARD_ACTION_REBUILD:
case SHARD_ACTION_CHECK:
case SHARD_ACTION_APPLY_SCHEMA:
node.Args = &ApplySchemaShardArgs{}
case SHARD_ACTION_SET_SERVED_TYPES:
node.Args = &SetShardServedTypesArgs{}
case SHARD_ACTION_MULTI_RESTORE:
node.Args = &MultiRestoreArgs{}
case SHARD_ACTION_MIGRATE_SERVED_TYPES:
node.Args = &MigrateServedTypesArgs{}
case SHARD_ACTION_UPDATE_SHARD:
case KEYSPACE_ACTION_REBUILD:
case KEYSPACE_ACTION_APPLY_SCHEMA:
node.Args = &ApplySchemaKeyspaceArgs{}
case KEYSPACE_ACTION_SET_SHARDING_INFO:
case KEYSPACE_ACTION_MIGRATE_SERVED_FROM:
node.Args = &MigrateServedFromArgs{}
case SRV_SHARD_ACTION_REBUILD:
case TABLET_ACTION_SET_BLACKLISTED_TABLES, TABLET_ACTION_GET_SCHEMA,
TABLET_ACTION_RELOAD_SCHEMA, TABLET_ACTION_EXECUTE_FETCH,
TABLET_ACTION_GET_PERMISSIONS,
TABLET_ACTION_SLAVE_POSITION, TABLET_ACTION_WAIT_SLAVE_POSITION,
TABLET_ACTION_MASTER_POSITION, TABLET_ACTION_STOP_SLAVE,
TABLET_ACTION_STOP_SLAVE_MINIMUM, TABLET_ACTION_START_SLAVE,
TABLET_ACTION_GET_SLAVES, TABLET_ACTION_WAIT_BLP_POSITION,
TABLET_ACTION_STOP_BLP, TABLET_ACTION_START_BLP,
TABLET_ACTION_RUN_BLP_UNTIL:
return nil, fmt.Errorf("rpc-only action: %v", node.Action)
default:
return nil, fmt.Errorf("unrecognized action: %v", node.Action)
}
// decode the args
if node.Args != nil {
err = decoder.Decode(node.Args)
} else {
var a interface{}
err = decoder.Decode(&a)
}
if err != nil {
return nil, err
}
// decode the reply
if node.Reply != nil {
err = decoder.Decode(node.Reply)
} else {
var a interface{}
err = decoder.Decode(&a)
}
if err != nil {
return nil, err
}
return node, nil
}
// ToJson returns a JSON representation of the object.
func (n *ActionNode) ToJson() string {
result := jscfg.ToJson(n) + "\n"
if n.Args == nil {
result += "{}\n"
} else {
result += jscfg.ToJson(n.Args) + "\n"
}
if n.Reply == nil {
result += "{}\n"
} else {
result += jscfg.ToJson(n.Reply) + "\n"
}
return result
}
// SetGuid will set the ActionGuid field for the action node
// and return the action node.
func (n *ActionNode) SetGuid() *ActionNode {
now := time.Now().Format(time.RFC3339)
username := "unknown"
if u, err := user.Current(); err == nil {
username = u.Username
}
hostname := "unknown"
if h, err := os.Hostname(); err == nil {
hostname = h
}
n.ActionGuid = fmt.Sprintf("%v-%v-%v", now, username, hostname)
return n
}
// ActionNodeCanBePurged returns true if that ActionNode can be purged
// from the topology server.
func ActionNodeCanBePurged(data string) bool {
actionNode, err := ActionNodeFromJson(data, "")
if err != nil {
log.Warningf("bad action data: %v %#v", err, data)
return true
}
if actionNode.State == ACTION_STATE_RUNNING {
log.Infof("cannot remove running action: %v %v", actionNode.Action, actionNode.ActionGuid)
return false
}
return true
}
// ActionNodeIsStale returns true if that ActionNode is not Running
func ActionNodeIsStale(data string) bool {
actionNode, err := ActionNodeFromJson(data, "")
if err != nil {
log.Warningf("bad action data: %v %#v", err, data)
return false
}
return actionNode.State != ACTION_STATE_RUNNING
}