/
workflow.go
338 lines (296 loc) · 12.8 KB
/
workflow.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
/*
Copyright 2018 The Vitess Authors
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 agreedto 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 reshardingworkflowgen
// This package contains a workflow to generate horizontal resharding workflows
// that automatically discovers available overlapping shards to split/merge.
import (
"flag"
"fmt"
"path"
"strconv"
"strings"
"github.com/golang/protobuf/proto"
"golang.org/x/net/context"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topotools"
"vitess.io/vitess/go/vt/workflow"
"vitess.io/vitess/go/vt/workflow/resharding"
workflowpb "vitess.io/vitess/go/vt/proto/workflow"
)
const (
codeVersion = 1
keyspaceReshardingFactoryName = "hr_workflow_gen"
phaseName = "create_workflows"
)
// Register registers the KeyspaceResharding as a factory
// in the workflow framework.
func Register() {
workflow.Register(keyspaceReshardingFactoryName, &Factory{})
}
// Factory is the factory to create
// a horizontal resharding workflow.
type Factory struct{}
// Init is part of the workflow.Factory interface.
func (*Factory) Init(m *workflow.Manager, w *workflowpb.Workflow, args []string) error {
subFlags := flag.NewFlagSet(keyspaceReshardingFactoryName, flag.ContinueOnError)
keyspace := subFlags.String("keyspace", "", "Name of keyspace to perform horizontal resharding")
vtworkersStr := subFlags.String("vtworkers", "", "A comma-separated list of vtworker addresses")
minHealthyRdonlyTablets := subFlags.String("min_healthy_rdonly_tablets", "1", "Minimum number of healthy RDONLY tablets required in source shards")
splitCmd := subFlags.String("split_cmd", "SplitClone", "Split command to use to perform horizontal resharding (either SplitClone or LegacySplitClone)")
splitDiffDestTabletType := subFlags.String("split_diff_dest_tablet_type", "RDONLY", "Specifies tablet type to use in destination shards while performing SplitDiff operation")
skipStartWorkflows := subFlags.Bool("skip_start_workflows", true, "If true, newly created workflows will have skip_start set")
phaseEnableApprovalsDesc := fmt.Sprintf("Comma separated phases that require explicit approval in the UI to execute. Phase names are: %v", strings.Join(resharding.WorkflowPhases(), ","))
phaseEnableApprovalsStr := subFlags.String("phase_enable_approvals", strings.Join(resharding.WorkflowPhases(), ","), phaseEnableApprovalsDesc)
if err := subFlags.Parse(args); err != nil {
return err
}
if *keyspace == "" || *vtworkersStr == "" || *minHealthyRdonlyTablets == "" || *splitCmd == "" {
return fmt.Errorf("Keyspace name, min healthy rdonly tablets, split command, and vtworkers information must be provided for horizontal resharding")
}
vtworkers := strings.Split(*vtworkersStr, ",")
w.Name = fmt.Sprintf("Keyspace reshard on %s", *keyspace)
shardsToSplit, err := findSourceAndDestinationShards(m.TopoServer(), *keyspace)
if err != nil {
return err
}
checkpoint, err := initCheckpoint(
*keyspace,
vtworkers,
shardsToSplit,
*minHealthyRdonlyTablets,
*splitCmd,
*splitDiffDestTabletType,
*phaseEnableApprovalsStr,
*skipStartWorkflows,
)
if err != nil {
return err
}
w.Data, err = proto.Marshal(checkpoint)
if err != nil {
return err
}
return nil
}
// Instantiate is part the workflow.Factory interface.
func (*Factory) Instantiate(m *workflow.Manager, w *workflowpb.Workflow, rootNode *workflow.Node) (workflow.Workflow, error) {
rootNode.Message = "This is a workflow to execute a keyspace resharding automatically."
checkpoint := &workflowpb.WorkflowCheckpoint{}
if err := proto.Unmarshal(w.Data, checkpoint); err != nil {
return nil, err
}
workflowsCount, err := strconv.Atoi(checkpoint.Settings["workflows_count"])
if err != nil {
return nil, err
}
hw := &reshardingWorkflowGen{
checkpoint: checkpoint,
rootUINode: rootNode,
logger: logutil.NewMemoryLogger(),
topoServer: m.TopoServer(),
manager: m,
phaseEnableApprovalsParam: checkpoint.Settings["phase_enable_approvals"],
skipStartWorkflowParam: checkpoint.Settings["skip_start_workflows"],
minHealthyRdonlyTabletsParam: checkpoint.Settings["min_healthy_rdonly_tablets"],
keyspaceParam: checkpoint.Settings["keyspace"],
splitDiffDestTabletTypeParam: checkpoint.Settings["split_diff_dest_tablet_type"],
splitCmdParam: checkpoint.Settings["split_cmd"],
workflowsCount: workflowsCount,
}
createWorkflowsUINode := &workflow.Node{
Name: "CreateWorkflows",
PathName: phaseName,
}
hw.rootUINode.Children = []*workflow.Node{
createWorkflowsUINode,
}
phaseNode, err := rootNode.GetChildByPath(phaseName)
if err != nil {
return nil, fmt.Errorf("fails to find phase node for: %v", phaseName)
}
for i := 0; i < workflowsCount; i++ {
taskID := fmt.Sprintf("%s/%v", phaseName, i)
task := hw.checkpoint.Tasks[taskID]
taskUINode := &workflow.Node{
Name: fmt.Sprintf("Split shards %v to %v workflow creation", task.Attributes["source_shards"], task.Attributes["destination_shards"]),
PathName: fmt.Sprintf("%v", i),
}
phaseNode.Children = append(phaseNode.Children, taskUINode)
}
return hw, nil
}
func findSourceAndDestinationShards(ts *topo.Server, keyspace string) ([][][]string, error) {
overlappingShards, err := topotools.FindOverlappingShards(context.Background(), ts, keyspace)
if err != nil {
return nil, err
}
var shardsToSplit [][][]string
for _, os := range overlappingShards {
var sourceShards, destinationShards []string
var sourceShardInfo *topo.ShardInfo
var destinationShardInfos []*topo.ShardInfo
// Judge which side is source shard by checking the number of servedTypes.
if len(os.Left[0].ServedTypes) > 0 {
sourceShardInfo = os.Left[0]
destinationShardInfos = os.Right
} else {
sourceShardInfo = os.Right[0]
destinationShardInfos = os.Left
}
sourceShards = append(sourceShards, sourceShardInfo.ShardName())
for _, d := range destinationShardInfos {
destinationShards = append(destinationShards, d.ShardName())
}
shardsToSplit = append(shardsToSplit, [][]string{sourceShards, destinationShards})
}
return shardsToSplit, nil
}
// initCheckpoint initialize the checkpoint for keyspace reshard
func initCheckpoint(keyspace string, vtworkers []string, shardsToSplit [][][]string, minHealthyRdonlyTablets, splitCmd, splitDiffDestTabletType, phaseEnableApprovals string, skipStartWorkflows bool) (*workflowpb.WorkflowCheckpoint, error) {
sourceShards := 0
destShards := 0
for _, shardToSplit := range shardsToSplit {
sourceShards = sourceShards + len(shardToSplit[0])
destShards = destShards + len(shardToSplit[1])
}
if sourceShards == 0 || destShards == 0 {
return nil, fmt.Errorf("invalid source or destination shards")
}
if len(vtworkers) != destShards {
return nil, fmt.Errorf("there are %v vtworkers, %v destination shards: the number should be same", len(vtworkers), destShards)
}
splitRatio := destShards / sourceShards
if minHealthyRdonlyTabletsVal, err := strconv.Atoi(minHealthyRdonlyTablets); err != nil || minHealthyRdonlyTabletsVal < splitRatio {
return nil, fmt.Errorf("there are not enough rdonly tablets in source shards. You need at least %v, it got: %v", splitRatio, minHealthyRdonlyTablets)
}
tasks := make(map[string]*workflowpb.Task)
usedVtworkersIdx := 0
for i, shardToSplit := range shardsToSplit {
taskID := fmt.Sprintf("%s/%v", phaseName, i)
tasks[taskID] = &workflowpb.Task{
Id: taskID,
State: workflowpb.TaskState_TaskNotStarted,
Attributes: map[string]string{
"source_shards": strings.Join(shardToSplit[0], ","),
"destination_shards": strings.Join(shardToSplit[1], ","),
"vtworkers": strings.Join(vtworkers[usedVtworkersIdx:usedVtworkersIdx+len(shardToSplit[1])], ","),
},
}
usedVtworkersIdx = usedVtworkersIdx + len(shardToSplit[1])
}
return &workflowpb.WorkflowCheckpoint{
CodeVersion: codeVersion,
Tasks: tasks,
Settings: map[string]string{
"vtworkers": strings.Join(vtworkers, ","),
"min_healthy_rdonly_tablets": minHealthyRdonlyTablets,
"split_cmd": splitCmd,
"split_diff_dest_tablet_type": splitDiffDestTabletType,
"phase_enable_approvals": phaseEnableApprovals,
"skip_start_workflows": fmt.Sprintf("%v", skipStartWorkflows),
"workflows_count": fmt.Sprintf("%v", len(shardsToSplit)),
"keyspace": keyspace,
},
}, nil
}
// reshardingWorkflowGen contains meta-information and methods to
// control workflow.
type reshardingWorkflowGen struct {
ctx context.Context
manager *workflow.Manager
topoServer *topo.Server
wi *topo.WorkflowInfo
// logger is the logger we export UI logs from.
logger *logutil.MemoryLogger
// rootUINode is the root node representing the workflow in the UI.
rootUINode *workflow.Node
checkpoint *workflowpb.WorkflowCheckpoint
checkpointWriter *workflow.CheckpointWriter
workflowsCount int
// params to horizontal reshard workflow
phaseEnableApprovalsParam string
minHealthyRdonlyTabletsParam string
keyspaceParam string
splitDiffDestTabletTypeParam string
splitCmdParam string
skipStartWorkflowParam string
}
// Run implements workflow.Workflow interface. It creates one horizontal resharding workflow per shard to split
func (hw *reshardingWorkflowGen) Run(ctx context.Context, manager *workflow.Manager, wi *topo.WorkflowInfo) error {
hw.ctx = ctx
hw.wi = wi
hw.checkpointWriter = workflow.NewCheckpointWriter(hw.topoServer, hw.checkpoint, hw.wi)
hw.rootUINode.Display = workflow.NodeDisplayDeterminate
hw.rootUINode.BroadcastChanges(true /* updateChildren */)
if err := hw.runWorkflow(); err != nil {
hw.setUIMessage(hw.rootUINode, fmt.Sprintf("Keyspace resharding failed to create workflows"))
return err
}
hw.setUIMessage(hw.rootUINode, fmt.Sprintf("Keyspace resharding is finished successfully."))
return nil
}
func (hw *reshardingWorkflowGen) runWorkflow() error {
var tasks []*workflowpb.Task
for i := 0; i < hw.workflowsCount; i++ {
taskID := fmt.Sprintf("%s/%v", phaseName, i)
tasks = append(tasks, hw.checkpoint.Tasks[taskID])
}
workflowsCreator := workflow.NewParallelRunner(hw.ctx, hw.rootUINode, hw.checkpointWriter, tasks, hw.workflowCreator, workflow.Sequential, false /*phaseEnableApprovals we don't require enable approvals in this workflow*/)
return workflowsCreator.Run()
}
func (hw *reshardingWorkflowGen) workflowCreator(ctx context.Context, task *workflowpb.Task) error {
horizontalReshardingParams := []string{
"-keyspace=" + hw.keyspaceParam,
"-vtworkers=" + task.Attributes["vtworkers"],
"-split_cmd=" + hw.splitCmdParam,
"-split_diff_dest_tablet_type=" + hw.splitDiffDestTabletTypeParam,
"-min_healthy_rdonly_tablets=" + hw.minHealthyRdonlyTabletsParam,
"-source_shards=" + task.Attributes["source_shards"],
"-destination_shards=" + task.Attributes["destination_shards"],
"-phase_enable_approvals=" + hw.phaseEnableApprovalsParam,
}
skipStart, err := strconv.ParseBool(hw.skipStartWorkflowParam)
if err != nil {
return err
}
phaseID := path.Dir(task.Id)
phaseUINode, err := hw.rootUINode.GetChildByPath(phaseID)
if err != nil {
return err
}
uuid, err := hw.manager.Create(ctx, "horizontal_resharding", horizontalReshardingParams)
if err != nil {
hw.setUIMessage(phaseUINode, fmt.Sprintf("Couldn't create shard split workflow for source shards: %v. Got error: %v", task.Attributes["source_shards"], err))
return err
}
hw.setUIMessage(phaseUINode, fmt.Sprintf("Created shard split workflow: %v for source shards: %v.", uuid, task.Attributes["source_shards"]))
workflowCmd := "WorkflowCreate horizontal_resharding" + strings.Join(horizontalReshardingParams, " ")
hw.setUIMessage(phaseUINode, fmt.Sprintf("Created workflow with the following params: %v", workflowCmd))
if !skipStart {
err = hw.manager.Start(ctx, uuid)
if err != nil {
hw.setUIMessage(phaseUINode, fmt.Sprintf("Couldn't start shard split workflow: %v for source shards: %v. Got error: %v", uuid, task.Attributes["source_shards"], err))
return err
}
}
return nil
}
func (hw *reshardingWorkflowGen) setUIMessage(node *workflow.Node, message string) {
log.Infof("Keyspace resharding : %v.", message)
hw.logger.Infof(message)
node.Log = hw.logger.String()
node.Message = message
node.BroadcastChanges(false /* updateChildren */)
}