Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update perror code and regenerate perror.go #127

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,25 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- name: checkout
- name: checkout mysql-tester
uses: actions/checkout@v4
with:
path: 'mysql-tester'
- name: checkout TiDB
uses: actions/checkout@v4
with:
repository: pingcap/tidb
path: 'tidb'
- name: setup go
uses: actions/setup-go@v4
with:
go-version: 1.21
go-version-file: 'mysql-tester/go.mod'
- name: vet
working-directory: 'mysql-tester'
run: go vet ./...
- name: build
working-directory: 'mysql-tester'
run: make build
- name: test
working-directory: 'mysql-tester'
run: make test
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 starting 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 failed", "errorcode", i, "error", err)
}
}
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
Loading