forked from dolthub/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
213 lines (186 loc) · 6.33 KB
/
validator.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
// Package topovalidator contains a workflow that validates the
// topology data. It is meant to detect and propose fixes for topology
// problems. Ideally, the topology should always be
// consistent. However, with tasks dying or network problems, there is
// a risk some bad data is left in the topology. Most data problems
// should be self-healing, so it is important to only add new
// validations steps only for corner cases that would be too costly or
// time-consuming to address in the first place.
package topovalidator
import (
"fmt"
"sync"
log "github.com/golang/glog"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/vt/logutil"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/vt/workflow"
workflowpb "github.com/youtube/vitess/go/vt/proto/workflow"
)
const (
topoValidatorFactoryName = "topo_validator"
)
var (
validators = make(map[string]Validator)
)
// Register needs to be called to register the topovalidator factory.
func Register() {
workflow.Register(topoValidatorFactoryName, &WorkflowFactory{})
}
// Validator is an individual process that validates an aspect of the topology.
// Typically, it looks for something wrong, and if it finds anything, it registers a Fixer in the workflow.
type Validator interface {
// Audit is called by the Workflow. It can add Fixer objects to the Workflow.
Audit(ctx context.Context, ts topo.Server, w *Workflow) error
}
// Fixer is the interface to implement to register a job capable of
// fixing something in the topology. It is given the action that the
// user chose.
type Fixer interface {
Action(ctx context.Context, name string) error
}
// RegisterValidator adds a Validator to our list. Typically called at
// init() time.
func RegisterValidator(name string, v Validator) {
if _, ok := validators[name]; ok {
log.Fatalf("Registering duplicate Validator with name %v", name)
}
validators[name] = v
}
// Workflow is the workflow that runs the validation.
// It implements workflow.Workflow.
type Workflow struct {
// fixers is the array of possible fixes.
fixers []*workflowFixer
// runCount is the number of validators we already ran.
runCount int
// 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
// wg is a wait group that will be used to wait for all fixers to be done.
wg sync.WaitGroup
}
// workflowFixer contains all the information about a fixer.
type workflowFixer struct {
name string
message string
fixer Fixer
actions []string
node *workflow.Node
wg *sync.WaitGroup
}
// AddFixer adds a Fixer to the Workflow. It will end up displaying a
// sub-task for the user to act on. When the user presses one action,
// the Fixer.Action() callback will be called.
func (w *Workflow) AddFixer(name, message string, fixer Fixer, actions []string) {
w.fixers = append(w.fixers, &workflowFixer{
name: name,
message: message,
fixer: fixer,
actions: actions,
wg: &w.wg,
})
}
// Run is part of the workflow.Workflow interface.
func (w *Workflow) Run(ctx context.Context, manager *workflow.Manager, wi *topo.WorkflowInfo) error {
w.uiUpdate()
w.rootUINode.Display = workflow.NodeDisplayDeterminate
w.rootUINode.BroadcastChanges(false /* updateChildren */)
// Run all the validators. They may add fixers.
for name, v := range validators {
w.logger.Infof("Running validator: %v", name)
w.uiUpdate()
w.rootUINode.BroadcastChanges(false /* updateChildren */)
err := v.Audit(ctx, manager.TopoServer(), w)
if err != nil {
w.logger.Errorf("Validator %v failed: %v", name, err)
} else {
w.logger.Infof("Validator %v successfully finished", name)
}
w.runCount++
}
// Now for each Fixer, add a sub node.
if len(w.fixers) == 0 {
w.logger.Infof("No problem found")
}
for i, f := range w.fixers {
w.wg.Add(1)
f.node = workflow.NewNode()
w.rootUINode.Children = append(w.rootUINode.Children, f.node)
f.node.PathName = fmt.Sprintf("%v", i)
f.node.Name = f.name
f.node.Message = f.message
f.node.Display = workflow.NodeDisplayIndeterminate
for _, action := range f.actions {
f.node.Actions = append(f.node.Actions, &workflow.Action{
Name: action,
State: workflow.ActionStateEnabled,
Style: workflow.ActionStyleNormal,
})
}
f.node.Listener = f
}
w.uiUpdate()
w.rootUINode.BroadcastChanges(true /* updateChildren */)
// And wait for the workflow to be done.
fixersChan := make(chan struct{})
go func(wg *sync.WaitGroup) {
wg.Wait()
close(fixersChan)
}(&w.wg)
select {
case <-ctx.Done():
return ctx.Err()
case <-fixersChan:
return nil
}
}
// uiUpdate updates the computed parts of the Node, based on the
// current state.
func (w *Workflow) uiUpdate() {
c := len(validators)
w.rootUINode.Progress = 100 * w.runCount / c
w.rootUINode.ProgressMessage = fmt.Sprintf("%v/%v", w.runCount, c)
w.rootUINode.Log = w.logger.String()
}
// Action is part of the workflow.ActionListener interface.
func (f *workflowFixer) Action(ctx context.Context, path, name string) error {
if len(f.node.Actions) == 0 {
// Action was already run.
return nil
}
err := f.fixer.Action(ctx, name)
if err != nil {
f.node.Log = fmt.Sprintf("action %v failed: %v", name, err)
} else {
f.node.Log = fmt.Sprintf("action %v successful", name)
}
f.node.Actions = f.node.Actions[:0]
f.node.State = workflowpb.WorkflowState_Done
f.node.Message = "Addressed(" + name + "): " + f.node.Message
f.node.Display = workflow.NodeDisplayNone
f.node.BroadcastChanges(false /* updateChildren */)
f.wg.Done()
return nil
}
// WorkflowFactory is the factory to register the topo validator
// workflow.
type WorkflowFactory struct{}
// Init is part of the workflow.Factory interface.
func (f *WorkflowFactory) Init(_ *workflow.Manager, w *workflowpb.Workflow, args []string) error {
// No parameters to parse.
if len(args) > 0 {
return fmt.Errorf("%v doesn't take any parameter", topoValidatorFactoryName)
}
w.Name = "Topology Validator"
return nil
}
// Instantiate is part of the workflow.Factory interface.
func (f *WorkflowFactory) Instantiate(_ *workflow.Manager, w *workflowpb.Workflow, rootNode *workflow.Node) (workflow.Workflow, error) {
rootNode.Message = "Validates the Topology and proposes fixes for known issues."
return &Workflow{
logger: logutil.NewMemoryLogger(),
rootUINode: rootNode,
}, nil
}