-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
durability.go
225 lines (186 loc) · 8.14 KB
/
durability.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
/*
Copyright 2021 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 agreed to 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 reparentutil
import (
"fmt"
"vitess.io/vitess/go/vt/log"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
"vitess.io/vitess/go/vt/topo/topoproto"
"vitess.io/vitess/go/vt/vtctl/reparentutil/promotionrule"
)
//=======================================================================
// A NewDurabler is a function that creates a new Durabler based on the
// properties specified in the input map. Every Durabler must
// register a NewDurabler function.
type NewDurabler func() Durabler
var (
// durabilityPolicies is a map that stores the functions needed to create a new Durabler
durabilityPolicies = make(map[string]NewDurabler)
)
func init() {
// register all the durability rules with their functions to create them
RegisterDurability("none", func() Durabler {
return &durabilityNone{}
})
RegisterDurability("semi_sync", func() Durabler {
return &durabilitySemiSync{}
})
RegisterDurability("cross_cell", func() Durabler {
return &durabilityCrossCell{}
})
RegisterDurability("test", func() Durabler {
return &durabilityTest{}
})
}
// Durabler is the interface which is used to get the promotion rules for candidates and the semi sync setup
type Durabler interface {
// promotionRule represents the precedence in which we want to tablets to be promoted.
// The higher the promotion rule of a tablet, the more we want it to be promoted in case of a failover
promotionRule(*topodatapb.Tablet) promotionrule.CandidatePromotionRule
// semiSyncAckers represents the number of semi-sync ackers required for a given tablet if it were to become the PRIMARY instance
semiSyncAckers(*topodatapb.Tablet) int
// isReplicaSemiSync returns whether the "replica" should send semi-sync acks if "primary" were to become the PRIMARY instance
isReplicaSemiSync(primary, replica *topodatapb.Tablet) bool
}
func RegisterDurability(name string, newDurablerFunc NewDurabler) {
if durabilityPolicies[name] != nil {
log.Fatalf("durability policy %v already registered", name)
}
durabilityPolicies[name] = newDurablerFunc
}
//=======================================================================
// GetDurabilityPolicy is used to get a new durability policy from the registered policies
func GetDurabilityPolicy(name string) (Durabler, error) {
newDurabilityCreationFunc, found := durabilityPolicies[name]
if !found {
return nil, fmt.Errorf("durability policy %v not found", name)
}
return newDurabilityCreationFunc(), nil
}
// CheckDurabilityPolicyExists is used to check if the durability policy is part of the registered policies
func CheckDurabilityPolicyExists(name string) bool {
_, found := durabilityPolicies[name]
return found
}
// PromotionRule returns the promotion rule for the instance.
func PromotionRule(durability Durabler, tablet *topodatapb.Tablet) promotionrule.CandidatePromotionRule {
// Prevent panics.
if tablet == nil || tablet.Alias == nil {
return promotionrule.MustNot
}
return durability.promotionRule(tablet)
}
// SemiSyncAckers returns the primary semi-sync setting for the instance.
// 0 means none. Non-zero specifies the number of required ackers.
func SemiSyncAckers(durability Durabler, tablet *topodatapb.Tablet) int {
return durability.semiSyncAckers(tablet)
}
// IsReplicaSemiSync returns the replica semi-sync setting from the tablet record.
// Prefer using this function if tablet record is available.
func IsReplicaSemiSync(durability Durabler, primary, replica *topodatapb.Tablet) bool {
// Prevent panics.
if primary == nil || primary.Alias == nil || replica == nil || replica.Alias == nil {
return false
}
return durability.isReplicaSemiSync(primary, replica)
}
//=======================================================================
// durabilityNone has no semi-sync and returns NeutralPromoteRule for Primary and Replica tablet types, MustNotPromoteRule for everything else
type durabilityNone struct{}
// promotionRule implements the Durabler interface
func (d *durabilityNone) promotionRule(tablet *topodatapb.Tablet) promotionrule.CandidatePromotionRule {
switch tablet.Type {
case topodatapb.TabletType_PRIMARY, topodatapb.TabletType_REPLICA:
return promotionrule.Neutral
}
return promotionrule.MustNot
}
// semiSyncAckers implements the Durabler interface
func (d *durabilityNone) semiSyncAckers(tablet *topodatapb.Tablet) int {
return 0
}
// isReplicaSemiSync implements the Durabler interface
func (d *durabilityNone) isReplicaSemiSync(primary, replica *topodatapb.Tablet) bool {
return false
}
//=======================================================================
// durabilitySemiSync has 1 semi-sync setup. It only allows Primary and Replica type servers to acknowledge semi sync
// It returns NeutralPromoteRule for Primary and Replica tablet types, MustNotPromoteRule for everything else
type durabilitySemiSync struct{}
// promotionRule implements the Durabler interface
func (d *durabilitySemiSync) promotionRule(tablet *topodatapb.Tablet) promotionrule.CandidatePromotionRule {
switch tablet.Type {
case topodatapb.TabletType_PRIMARY, topodatapb.TabletType_REPLICA:
return promotionrule.Neutral
}
return promotionrule.MustNot
}
// semiSyncAckers implements the Durabler interface
func (d *durabilitySemiSync) semiSyncAckers(tablet *topodatapb.Tablet) int {
return 1
}
// isReplicaSemiSync implements the Durabler interface
func (d *durabilitySemiSync) isReplicaSemiSync(primary, replica *topodatapb.Tablet) bool {
switch replica.Type {
case topodatapb.TabletType_PRIMARY, topodatapb.TabletType_REPLICA:
return true
}
return false
}
//=======================================================================
// durabilityCrossCell has 1 semi-sync setup. It only allows Primary and Replica type servers from a different cell to acknowledge semi sync.
// This means that a transaction must be in two cells for it to be acknowledged
// It returns NeutralPromoteRule for Primary and Replica tablet types, MustNotPromoteRule for everything else
type durabilityCrossCell struct{}
// promotionRule implements the Durabler interface
func (d *durabilityCrossCell) promotionRule(tablet *topodatapb.Tablet) promotionrule.CandidatePromotionRule {
switch tablet.Type {
case topodatapb.TabletType_PRIMARY, topodatapb.TabletType_REPLICA:
return promotionrule.Neutral
}
return promotionrule.MustNot
}
// semiSyncAckers implements the Durabler interface
func (d *durabilityCrossCell) semiSyncAckers(tablet *topodatapb.Tablet) int {
return 1
}
// isReplicaSemiSync implements the Durabler interface
func (d *durabilityCrossCell) isReplicaSemiSync(primary, replica *topodatapb.Tablet) bool {
switch replica.Type {
case topodatapb.TabletType_PRIMARY, topodatapb.TabletType_REPLICA:
return primary.Alias.Cell != replica.Alias.Cell
}
return false
}
//=======================================================================
// durabilityTest is like durabilityNone. It overrides the type for a specific tablet to prefer. It is only meant to be used for testing purposes!
type durabilityTest struct{}
// promotionRule implements the Durabler interface
func (d *durabilityTest) promotionRule(tablet *topodatapb.Tablet) promotionrule.CandidatePromotionRule {
if topoproto.TabletAliasString(tablet.Alias) == "zone2-0000000200" {
return promotionrule.Prefer
}
switch tablet.Type {
case topodatapb.TabletType_PRIMARY, topodatapb.TabletType_REPLICA:
return promotionrule.Neutral
}
return promotionrule.MustNot
}
// semiSyncAckers implements the Durabler interface
func (d *durabilityTest) semiSyncAckers(tablet *topodatapb.Tablet) int {
return 0
}
// isReplicaSemiSync implements the Durabler interface
func (d *durabilityTest) isReplicaSemiSync(primary, replica *topodatapb.Tablet) bool {
return false
}