-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
upgrade.go
317 lines (274 loc) · 9.96 KB
/
upgrade.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
/*
* Copyright 2020 Dgraph Labs, Inc. and Contributors
*
* 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 upgrade
import (
"fmt"
"net"
"os"
"sort"
"strconv"
"strings"
"github.com/dgraph-io/dgraph/x"
"github.com/spf13/cobra"
)
var (
// Upgrade is the sub-command used to upgrade dgraph cluster.
Upgrade x.SubCommand
// zeroVersion represents &version{major: 0, minor: 0, patch: 0}
zeroVersion = &version{}
// allChanges contains all the changes ever introduced since the beginning of dgraph in the form
// of a list of change sets.
allChanges changeList
)
type versionComparisonResult uint8
const (
acl = "acl"
alpha = "alpha"
user = "user"
password = "password"
deleteOld = "deleteOld"
from = "from"
to = "to"
versionFmtBeforeCalVer = "v%d.%d.%d"
versionFmtAfterCalVer = "v%d.%02d.%d"
less versionComparisonResult = iota
equal
greater
)
// version represents a mainstream release version
// for eg: for v20.03.1, major = 20, minor = 3, patch = 1
type version struct {
major int
minor int
patch int
}
func (v *version) String() string {
if v == nil {
v = zeroVersion
}
versionFmt := versionFmtAfterCalVer
if v.major < 20 {
versionFmt = versionFmtBeforeCalVer
}
return fmt.Sprintf(versionFmt, v.major, v.minor, v.patch)
}
// Compare compares v with other version, and tells if v is equal, less, or greater than other.
// It interprets nil as &version{major: 0, minor: 0, patch: 0}
func (v *version) Compare(other *version) versionComparisonResult {
if v == nil {
v = zeroVersion
}
if other == nil {
other = zeroVersion
}
switch {
case v.major > other.major:
return greater
case v.major < other.major:
return less
case v.minor > other.minor:
return greater
case v.minor < other.minor:
return less
case v.patch > other.patch:
return greater
case v.patch < other.patch:
return less
}
return equal
}
// change represents the action that needs to be taken during upgrade as a result of some breaking
// change
type change struct {
name string // a short name to identify the change
description string // longer description
// The minimum from version, upgrading from which this change can be applied.
// For a version less than this value, this change is not applicable
minFromVersion *version
applyFunc func() error // function which applies the change
}
// changeSet represents a set of changes that were introduced in some version
type changeSet struct {
introducedIn *version // the version in which the changes were introduced
// the list of changes that were introduced in the above version
// it should contain changes in sorted order, based on their order of introduction as they would
// be applied in the order they are present in this list
changes []*change
}
// changeList represents a list of changeSet, i.e., a list of all the changes that were ever
// introduced since the beginning of Dgraph.
//The changeSets in this list are supposed to be in sorted order based on when they were introduced.
type changeList []*changeSet
type commandInput struct {
fromVersion *version
toVersion *version
}
func init() {
Upgrade.Cmd = &cobra.Command{
Use: "upgrade",
Short: "Run the Dgraph upgrade tool",
Long: "This tool is supported only for the mainstream release versions of Dgraph, " +
"not for the beta releases.",
Run: func(cmd *cobra.Command, args []string) {
run()
},
}
flag := Upgrade.Cmd.Flags()
flag.Bool(acl, false, "upgrade ACL from v1.2.2 to >=v20.03.0")
flag.StringP(alpha, "a", "127.0.0.1:9080", "Dgraph Alpha gRPC server address")
flag.StringP(user, "u", "", "Username of ACL user")
flag.StringP(password, "p", "", "Password of ACL user")
flag.BoolP(deleteOld, "d", true, "Delete the older ACL predicates")
flag.StringP(from, "f", "", "The version string from which to upgrade, e.g.: v1.2.2")
flag.StringP(to, "t", "", "The version string till which to upgrade, e.g.: v20.03.0")
}
func run() {
cmdInput, err := validateAndParseInput()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
applyChangeList(cmdInput, allChanges)
}
func validateAndParseInput() (*commandInput, error) {
if !Upgrade.Conf.GetBool(acl) {
return nil, formatAsFlagParsingError(acl,
fmt.Errorf("we only support acl upgrade as of now"))
}
_, _, err := net.SplitHostPort(strings.TrimSpace(Upgrade.Conf.GetString(alpha)))
if err != nil {
return nil, formatAsFlagParsingError(alpha, err)
}
if strings.TrimSpace(Upgrade.Conf.GetString(user)) == "" {
return nil, formatAsFlagRequiredError(user)
}
if strings.TrimSpace(Upgrade.Conf.GetString(password)) == "" {
return nil, formatAsFlagRequiredError(password)
}
fromVersionParsed, err := parseVersionFromString(Upgrade.Conf.GetString(from))
if err != nil {
return nil, formatAsFlagParsingError(from, err)
}
toVersionParsed, err := parseVersionFromString(Upgrade.Conf.GetString(to))
if err != nil {
return nil, formatAsFlagParsingError(to, err)
}
if fromVersionParsed.Compare(toVersionParsed) != less {
return nil, fmt.Errorf("error: `%s` must be less than `%s`", from, to)
}
return &commandInput{fromVersion: fromVersionParsed, toVersion: toVersionParsed}, nil
}
func formatAsFlagParsingError(flag string, err error) error {
return fmt.Errorf("error parsing flag `%s`: %w", flag, err)
}
func formatAsFlagRequiredError(flag string) error {
return formatAsFlagParsingError(flag, fmt.Errorf("`%s` is required", flag))
}
// parseVersionFromString parses a version given as string to internal representation.
// Some examples for input and output:
// 1. input : v1.2.2
// output: &version{major: 1, minor: 2, patch: 2}, nil
// 2. input : v20.03.0-beta.20200320
// output: &version{major: 20, minor: 3, patch: 0}, nil
// 3. input : 1.2.2
// output: nil, error
// 4. input : v1.2.2s
// output: nil, error
func parseVersionFromString(v string) (*version, error) {
v = strings.TrimSpace(v)
if v == "" || v[:1] != "v" {
return nil, fmt.Errorf("version can't be empty and must start with `v`. E.g.: v1.2.2")
}
versionSplit := strings.Split(v[1:], ".")
result := &version{}
var err error
result.major, err = strconv.Atoi(versionSplit[0])
if err != nil {
return nil, fmt.Errorf("error parsing major version: %w", err)
}
result.minor, err = strconv.Atoi(versionSplit[1])
if err != nil {
return nil, fmt.Errorf("error parsing minor version: %w", err)
}
// the third part of split might contain some extra things like `beta` appended with -,
// so split again and take the first part as patch
patchSplit := strings.Split(versionSplit[2], "-")
result.patch, err = strconv.Atoi(patchSplit[0])
if err != nil {
return nil, fmt.Errorf("error parsing patch version: %w", err)
}
return result, nil
}
func applyChangeList(cmdInput *commandInput, list changeList) {
fmt.Println("**********************************************")
fmt.Println("Version to upgrade from:", cmdInput.fromVersion)
fmt.Println("Version to upgrade to :", cmdInput.toVersion)
fmt.Println("**********************************************")
// sort the changeList based on the version the changes were introduced in
sort.SliceStable(list, func(i, j int) bool {
iVersion := list[i].introducedIn
jVersion := list[j].introducedIn
return iVersion.Compare(jVersion) == less
})
isInitialFromVersion := true
for _, changeSet := range list {
// apply the change set only if the version in which it was introduced is <= the version
// to which we want to upgrade and also >= the version from which we want to upgrade.
// If cmdInput.fromVersion hasn't been changed by us yet, then make sure that it is always
// less than the introduced version, ignore equality. So, if upgrading from version x to
// some other version, then the changes introduced in version x don't get applied.
fromConditionSatisfied := false
if isInitialFromVersion {
fromConditionSatisfied = cmdInput.fromVersion.Compare(changeSet.introducedIn) == less
} else {
fromConditionSatisfied = cmdInput.fromVersion.Compare(changeSet.introducedIn) != greater
}
if cmdInput.toVersion.Compare(changeSet.introducedIn) != less && fromConditionSatisfied {
fmt.Println("Applying the change set introduced in version:", changeSet.introducedIn)
// Go over every change in the change set and check if it should be applied. If yes,
// apply it, otherwise skip it.
for i, change := range changeSet.changes {
fmt.Println(fmt.Sprintf(""+
"\tApplying change %d:\n"+
"\t\tName : %s\n"+
"\t\tDescription: %s", i+1, change.name, change.description))
// apply the change only if the min version from which it should be applied
// is <= the version from which we want to upgrade
if cmdInput.fromVersion.Compare(change.minFromVersion) != less {
if err := change.applyFunc(); err != nil {
fmt.Println("\t\tStatus : Error")
fmt.Println("\t\tError :", err)
fmt.Println("\t\tCan't continue. Exiting!!!")
os.Exit(1)
} else {
fmt.Println("\t\tStatus : Successful")
}
} else {
fmt.Println("\t\tStatus : Skipped (min version mismatch)")
}
}
// update the fromVersion to reflect the version till which DB has been upgraded,
// so that minFromVersion check is satisfied for the changes in next changeSets
cmdInput.fromVersion = changeSet.introducedIn
isInitialFromVersion = false
fmt.Println("\tDB state now upgraded to:", cmdInput.fromVersion)
} else {
fmt.Println("Skipping the change set introduced in version:", changeSet.introducedIn)
}
}
fmt.Println()
fmt.Println("Upgrade finished!!!")
}