Skip to content

Commit

Permalink
Update perror code and regenerate perror.go
Browse files Browse the repository at this point in the history
  • Loading branch information
dveeden committed Jul 4, 2024
1 parent aa83826 commit 0e1b865
Show file tree
Hide file tree
Showing 4 changed files with 727 additions and 312 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
.PHONY: all build test tidy clean
.PHONY: all build test tidy clean generate

GO := go

default: build

build:
build: generate
$(GO) build -o mysql-tester ./src

generate: gen_perror
$(GO) generate ./src

debug:
$(GO) build -gcflags="all=-N -l" -o mysql-tester ./src

Expand Down
28 changes: 18 additions & 10 deletions generate_perror/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"flag"
"fmt"
"log"
"log/slog"
"os"
"os/exec"
"path/filepath"
Expand All @@ -27,7 +28,7 @@ import (
)

const (
fileHeader = `// Copyright 2023 PingCAP, Inc.
fileHeader = `// Copyright 2024 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -41,6 +42,8 @@ const (
// limitations under the License.
// Generated by generate_perror/main.go
// Generated with:
// %s
package main
var MysqlErrNameToNum = map[string]int{
Expand All @@ -52,7 +55,7 @@ var (
)

func init() {
flag.StringVar(&tidbCodePath, "path", "../tidb", "Path to TiDB source code root directory.")
flag.StringVar(&tidbCodePath, "path", "../../tidb", "Path to TiDB source code root directory.")
}

func checkNewErr(errCode string, i int, nameToNum map[string]int) {
Expand Down Expand Up @@ -114,6 +117,11 @@ func main() {
parserCodes := len(NameToNum) - errnoCodes
log.Printf("Got %d New error codes from parser/mysql/errcode.go!", parserCodes)

perrorVersion, err := exec.Command("perror", "-V").Output()
if err != nil {
log.Fatalf("Failed to get version info from perror: %s", err)
}

// similar to:
//seq 1 100000 | xargs perror 2> /dev/null | grep '^MySQL error code MY-[0-9]* ([A-Z_]*).*' | sed 's/^MySQL error code MY-0*\([[:digit:]]*\) (\([^)]*\)).*/"\2": \1,/'
maxError := 20000
Expand All @@ -125,10 +133,10 @@ func main() {
cmd := exec.Command("perror", strconv.Itoa(i))
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
log.Fatalf("Error setting up call to perrror: %s", err)
}
if err = cmd.Start(); err != nil {
log.Fatal(err)
log.Fatalf("Error staring call to perror: %s", err)
}
s := bufio.NewScanner(stdout)
r := regexp.MustCompile(`^MySQL error code MY-0*(\d+) \((\w+)\)`)
Expand All @@ -147,7 +155,7 @@ func main() {
}
err = cmd.Wait()
if err != nil {
log.Fatal(err)
slog.Debug("Warning: Call to 'perror %d' failed: %s", i, err)

Check failure on line 158 in generate_perror/main.go

View workflow job for this annotation

GitHub Actions / test

slog.Debug arg "i" should be a string or a slog.Attr (possible missing key or value)
}
}
if maxError >= 1000 {
Expand All @@ -156,14 +164,14 @@ func main() {
log.Printf("Got %d New error codes from perror!", len(NameToNum)-parserCodes-errnoCodes)
f, err := os.Create("perror.go")
if err != nil {
log.Fatal(err)
log.Fatalf("Failed to create perror.go: %s", err)
}
defer f.Close()
w := bufio.NewWriter(f)
_, err = w.WriteString(fileHeader)
_, err = w.WriteString(fmt.Sprintf(fileHeader, perrorVersion))

if err != nil {
log.Fatal(err)
log.Fatalf("Failed to write fileHeader to file: %s", err)
}

codes := make([]ErrorCodeAndName, 0, len(NameToNum))
Expand All @@ -176,12 +184,12 @@ func main() {
for i := range codes {
_, err = w.WriteString("\t\"" + codes[i].Name + `": ` + strconv.Itoa(codes[i].Code) + ",\n")
if err != nil {
log.Fatal(err)
log.Fatalf("Failed to write error code to file: %s", err)
}
}
_, err = w.WriteString("}\n")
if err != nil {
log.Fatal(err)
log.Fatalf("Failed to write closing line to file: %s", err)
}
w.Flush()
}
3 changes: 3 additions & 0 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ import (
log "github.com/sirupsen/logrus"
)

// Generate perror.go
//go:generate ../gen_perror

var (
host string
port string
Expand Down
Loading

0 comments on commit 0e1b865

Please sign in to comment.