forked from vitessio/vitess
/
cleaner.go
196 lines (172 loc) · 6.41 KB
/
cleaner.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
// 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.
package wrangler
import (
"fmt"
"sync"
"time"
"github.com/youtube/vitess/go/vt/concurrency"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/vt/topo/topoproto"
"golang.org/x/net/context"
topodatapb "github.com/youtube/vitess/go/vt/proto/topodata"
)
// Cleaner remembers a list of cleanup steps to perform. Just record
// action cleanup steps, and execute them at the end in reverse
// order, with various guarantees.
type Cleaner struct {
// mu protects the following members
mu sync.Mutex
actions []cleanerActionReference
}
// cleanerActionReference is the node used by Cleaner
type cleanerActionReference struct {
name string
target string
action CleanerFunction
}
// CleanerFunction is the interface that clean-up actions need to implement
type CleanerFunction func(context.Context, *Wrangler) error
// Record will add a cleaning action to the list
func (cleaner *Cleaner) Record(name, target string, action CleanerFunction) {
cleaner.mu.Lock()
cleaner.actions = append(cleaner.actions, cleanerActionReference{
name: name,
target: target,
action: action,
})
cleaner.mu.Unlock()
}
type cleanUpHelper struct {
err error
}
// CleanUp will run the recorded actions.
// If an action on a target fails, it will not run the next action on
// the same target.
// We return the aggregate errors for all cleanups.
// CleanUp uses its own context, with a timeout of 5 minutes, so that clean up action will run even if the original context times out.
// TODO(alainjobart) Actions should run concurrently on a per target
// basis. They are then serialized on each target.
func (cleaner *Cleaner) CleanUp(wr *Wrangler) error {
// we use a background context so we're not dependent on the original context timeout
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
actionMap := make(map[string]*cleanUpHelper)
rec := concurrency.AllErrorRecorder{}
cleaner.mu.Lock()
for i := len(cleaner.actions) - 1; i >= 0; i-- {
actionReference := cleaner.actions[i]
helper, ok := actionMap[actionReference.target]
if !ok {
helper = &cleanUpHelper{
err: nil,
}
actionMap[actionReference.target] = helper
}
if helper.err != nil {
wr.Logger().Warningf("previous action failed on target %v, not running %v", actionReference.target, actionReference.name)
continue
}
err := actionReference.action(ctx, wr)
if err != nil {
helper.err = err
rec.RecordError(err)
wr.Logger().Errorf("action %v failed on %v: %v", actionReference.name, actionReference.target, err)
} else {
wr.Logger().Infof("action %v successful on %v", actionReference.name, actionReference.target)
}
}
cleaner.mu.Unlock()
cancel()
return rec.Error()
}
// RemoveActionByName removes an action from the cleaner list
func (cleaner *Cleaner) RemoveActionByName(name, target string) error {
cleaner.mu.Lock()
defer cleaner.mu.Unlock()
for i, action := range cleaner.actions {
if action.name == name && action.target == target {
newActions := make([]cleanerActionReference, 0, len(cleaner.actions)-1)
if i > 0 {
newActions = append(newActions, cleaner.actions[0:i]...)
}
if i < len(cleaner.actions)-1 {
newActions = append(newActions, cleaner.actions[i+1:len(cleaner.actions)]...)
}
cleaner.actions = newActions
return nil
}
}
return topo.ErrNoNode
}
//
// ChangeSlaveTypeAction CleanerFunction
//
// ChangeSlaveTypeActionName is the name of the action to change a slave type
// (can be used to find such an action by name)
const ChangeSlaveTypeActionName = "ChangeSlaveTypeAction"
// RecordChangeSlaveTypeAction records a new ChangeSlaveTypeAction
// into the specified Cleaner
func RecordChangeSlaveTypeAction(cleaner *Cleaner, tabletAlias *topodatapb.TabletAlias, from topodatapb.TabletType, to topodatapb.TabletType) {
cleaner.Record(ChangeSlaveTypeActionName, topoproto.TabletAliasString(tabletAlias), func(ctx context.Context, wr *Wrangler) error {
ti, err := wr.ts.GetTablet(ctx, tabletAlias)
if err != nil {
return err
}
if ti.Type != from {
return fmt.Errorf("tablet %v is not of the right type (got %v expected %v), not changing it to %v", topoproto.TabletAliasString(tabletAlias), ti.Type, from, to)
}
if !topo.IsTrivialTypeChange(ti.Type, to) {
return fmt.Errorf("tablet %v type change %v -> %v is not an allowed transition for ChangeSlaveType", topoproto.TabletAliasString(tabletAlias), ti.Type, to)
}
// ask the tablet to make the change
return wr.tmc.ChangeType(ctx, ti.Tablet, to)
})
}
//
// TabletTagAction CleanerFunction
//
// TabletTagActionName is the name of the Tag action
const TabletTagActionName = "TabletTagAction"
// RecordTabletTagAction records a new action to set / remove a tag
// into the specified Cleaner
func RecordTabletTagAction(cleaner *Cleaner, tabletAlias *topodatapb.TabletAlias, name, value string) {
cleaner.Record(TabletTagActionName, topoproto.TabletAliasString(tabletAlias), func(ctx context.Context, wr *Wrangler) error {
_, err := wr.TopoServer().UpdateTabletFields(ctx, tabletAlias, func(tablet *topodatapb.Tablet) error {
if tablet.Tags == nil {
tablet.Tags = make(map[string]string)
}
if value != "" {
tablet.Tags[name] = value
} else {
delete(tablet.Tags, name)
}
return nil
})
return err
})
}
//
// StartSlaveAction CleanerAction
//
// StartSlaveActionName is the name of the slave start action
const StartSlaveActionName = "StartSlaveAction"
// RecordStartSlaveAction records a new action to restart binlog replication on a server
// into the specified Cleaner
func RecordStartSlaveAction(cleaner *Cleaner, tablet *topodatapb.Tablet) {
cleaner.Record(StartSlaveActionName, topoproto.TabletAliasString(tablet.Alias), func(ctx context.Context, wr *Wrangler) error {
return wr.TabletManagerClient().StartSlave(ctx, tablet)
})
}
//
// StartBlpAction CleanerAction
//
// StartBlpActionName is the name of the action to start binlog player
const StartBlpActionName = "StartBlpAction"
// RecordStartBlpAction records an action to restart binlog replication on a server
// into the specified Cleaner
func RecordStartBlpAction(cleaner *Cleaner, tablet *topodatapb.Tablet) {
cleaner.Record(StartBlpActionName, topoproto.TabletAliasString(tablet.Alias), func(ctx context.Context, wr *Wrangler) error {
return wr.TabletManagerClient().StartBlp(ctx, tablet)
})
}