|
| 1 | +// Copyright 2019 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package main |
| 15 | + |
| 16 | +import ( |
| 17 | + "bufio" |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + "regexp" |
| 21 | + "strconv" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/pingcap/dm/pkg/terror" |
| 25 | +) |
| 26 | + |
| 27 | +const ( |
| 28 | + developErrorFile = "errors_develop.txt" |
| 29 | + releaseErrorFile = "errors_release.txt" |
| 30 | + generatedCheckerFile = "{{.CheckerFile}}" |
| 31 | +) |
| 32 | + |
| 33 | +var dumpErrorRe = regexp.MustCompile("^([a-zA-Z].*),\\[code=([0-9]+).*$") |
| 34 | + |
| 35 | +var errors = []struct { |
| 36 | + name string |
| 37 | + err *terror.Error |
| 38 | +}{ |
| 39 | + // sample: |
| 40 | + // {"ErrWorkerExecDDLTimeout", terror.ErrWorkerExecDDLTimeout}, |
| 41 | + // {{.ErrList}} |
| 42 | +} |
| 43 | + |
| 44 | +func genErrors() { |
| 45 | + f, err := os.Create(developErrorFile) |
| 46 | + if err != nil { |
| 47 | + panic(err) |
| 48 | + } |
| 49 | + defer f.Close() |
| 50 | + w := bufio.NewWriter(f) |
| 51 | + for _, item := range errors { |
| 52 | + s := strings.SplitN(item.err.Error(), " ", 2) |
| 53 | + w.WriteString(fmt.Sprintf("%s,%s,\"%s\"\n", item.name, s[0], strings.ReplaceAll(s[1], "\n", "\\n"))) |
| 54 | + } |
| 55 | + w.Flush() |
| 56 | +} |
| 57 | + |
| 58 | +func readErrorFile(filename string) map[string]int64 { |
| 59 | + f, err := os.Open(filename) |
| 60 | + if err != nil { |
| 61 | + panic(err) |
| 62 | + } |
| 63 | + defer f.Close() |
| 64 | + |
| 65 | + result := make(map[string]int64) |
| 66 | + scanner := bufio.NewScanner(f) |
| 67 | + for scanner.Scan() { |
| 68 | + s := scanner.Text() |
| 69 | + match := dumpErrorRe.FindStringSubmatch(s) |
| 70 | + if len(match) != 3 { |
| 71 | + panic(fmt.Sprintf("invalid error: %s", s)) |
| 72 | + } |
| 73 | + code, err := strconv.ParseInt(match[2], 10, 64) |
| 74 | + if err != nil { |
| 75 | + panic(err) |
| 76 | + } |
| 77 | + result[match[1]] = code |
| 78 | + } |
| 79 | + return result |
| 80 | +} |
| 81 | + |
| 82 | +func compareErrors() bool { |
| 83 | + changedErrorCode := make(map[string][]int64) |
| 84 | + duplicateErrorCode := make(map[int64][]string) |
| 85 | + release := readErrorFile(releaseErrorFile) |
| 86 | + dev := readErrorFile(developErrorFile) |
| 87 | + |
| 88 | + for name, code := range dev { |
| 89 | + if releaseCode, ok := release[name]; ok && code != releaseCode { |
| 90 | + changedErrorCode[name] = []int64{releaseCode, code} |
| 91 | + } |
| 92 | + if _, ok := duplicateErrorCode[code]; ok { |
| 93 | + duplicateErrorCode[code] = append(duplicateErrorCode[code], name) |
| 94 | + } else { |
| 95 | + duplicateErrorCode[code] = []string{name} |
| 96 | + } |
| 97 | + } |
| 98 | + for code, names := range duplicateErrorCode { |
| 99 | + if len(names) == 1 { |
| 100 | + delete(duplicateErrorCode, code) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // check each non-new-added error in develop version has the same error code with the release version |
| 105 | + if len(changedErrorCode) > 0 { |
| 106 | + os.Stderr.WriteString("\n************ error code not same with the release version ************\n") |
| 107 | + } |
| 108 | + for name, codes := range changedErrorCode { |
| 109 | + fmt.Fprintf(os.Stderr, "name: %s release code: %d current code: %d\n", name, codes[0], codes[1]) |
| 110 | + } |
| 111 | + |
| 112 | + // check each error in develop version has a unique error code |
| 113 | + if len(duplicateErrorCode) > 0 { |
| 114 | + os.Stderr.WriteString("\n************ error code not unique ************\n") |
| 115 | + } |
| 116 | + for code, names := range duplicateErrorCode { |
| 117 | + fmt.Fprintf(os.Stderr, "code: %d names: %v\n", code, names) |
| 118 | + } |
| 119 | + |
| 120 | + return len(changedErrorCode) == 0 && len(duplicateErrorCode) == 0 |
| 121 | +} |
| 122 | + |
| 123 | +func cleanup(success bool) { |
| 124 | + if success { |
| 125 | + if err := os.Rename(developErrorFile, releaseErrorFile); err != nil { |
| 126 | + panic(err) |
| 127 | + } |
| 128 | + fmt.Println("check pass") |
| 129 | + } else { |
| 130 | + if err := os.Remove(developErrorFile); err != nil { |
| 131 | + panic(err) |
| 132 | + } |
| 133 | + os.Exit(1) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func main() { |
| 138 | + defer func() { |
| 139 | + os.Remove(generatedCheckerFile) |
| 140 | + }() |
| 141 | + genErrors() |
| 142 | + cleanup(compareErrors()) |
| 143 | +} |
0 commit comments