Skip to content
Merged
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: 1 addition & 13 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ on:
push:
branches:
- 'main'
env:
cache-version: 1
jobs:
validation:
name: Validation
Expand All @@ -17,17 +15,7 @@ jobs:
go-version-file: go.mod
- name: Setup Aqua
uses: ./.github/actions/setup-aqua
- name: Cache Tools
id: cache-tools
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: |
bin
include
key: cache-${{ env.cache-version }}-${{ hashFiles('go.mod') }}-${{ hashFiles('Makefile') }}
- run: make setup
if: steps.cache-tools.outputs.cache-hit != 'true'
- run: make validate
- run: make lint
- run: make check-generate
build:
name: build
Expand Down
46 changes: 46 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
version: "2"
run:
allow-parallel-runners: true
linters:
default: none
enable:
- copyloopvar
- errcheck
- goconst
- gocyclo
- govet
- ineffassign
- modernize
- misspell
- nakedret
- staticcheck
- unconvert
- unparam
- unused
- nilerr
settings:
govet:
disable:
- fieldalignment
- shadow
enable-all: true
modernize:
disable:
- omitzero
exclusions:
rules:
- path: (.+)(_test\.go|test_(.+)\.go)
linters:
- noctx
- errcheck
- goconst
- unparam
# MySQL privilege names in the user definitions naturally repeat
# across users; they are clearer as literals than as constants.
- path: server/initialize\.go
linters:
- goconst
formatters:
enable:
- gofmt
- goimports
38 changes: 4 additions & 34 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
MYSQL_VERSION = 8.4.4

# For Go
GOOS := $(shell go env GOOS)
GOARCH := $(shell go env GOARCH)

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif

GO_FILES := $(shell find . -name '*.go' -print)

PROTOC := protoc
Expand All @@ -22,18 +11,14 @@ all: build/moco-agent
aqua-install:
aqua install

.PHONY: lint
lint: aqua-install
@golangci-lint run --timeout 5m

.PHONY: pinact
pinact:
pinact run -update -min-age 7 -verify

.PHONY: validate
validate: setup aqua-install
test -z "$$(gofmt -s -l . | tee /dev/stderr)"
staticcheck ./...
test -z "$$(custom-checker -restrictpkg.packages=html/template,log $$(go list -tags='$(GOTAGS)' ./... ) 2>&1 | tee /dev/stderr)"
go build ./...
go vet ./...

.PHONY: check-generate
check-generate:
$(MAKE) proto
Expand Down Expand Up @@ -61,21 +46,6 @@ proto/agentrpc_grpc.pb.go: proto/agentrpc.proto
docs/agentrpc.md: proto/agentrpc.proto
$(PROTOC) --doc_out=docs --doc_opt=markdown,$@ $<

.PHONY: setup
setup: custom-checker staticcheck

.PHONY: staticcheck
staticcheck:
if ! which staticcheck >/dev/null; then \
env GOFLAGS= go install honnef.co/go/tools/cmd/staticcheck@latest; \
fi

.PHONY: custom-checker
custom-checker:
if ! which custom-checker >/dev/null; then \
env GOFLAGS= go install github.com/cybozu-go/golang-custom-analyzer/cmd/custom-checker@latest; \
fi

.PHONY: clean
clean:
rm -rf build
30 changes: 30 additions & 0 deletions aqua-checksums.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,36 @@
"checksum": "434D2FC10049A39FE069C6370CD45C1330871B01A4500FAC970FCD6BFB1D1FE9",
"algorithm": "sha256"
},
{
"id": "github_release/github.com/golangci/golangci-lint/v2.12.2/golangci-lint-2.12.2-darwin-amd64.tar.gz",
"checksum": "F6F06D94B6241521C53D15450C5209B028270BF966F842AFB11C030C79F5BC16",
"algorithm": "sha256"
},
{
"id": "github_release/github.com/golangci/golangci-lint/v2.12.2/golangci-lint-2.12.2-darwin-arm64.tar.gz",
"checksum": "A9C54498731B3128F79E090BE6110F3E5FFFCCC617B08142ED244D4126C73F29",
"algorithm": "sha256"
},
{
"id": "github_release/github.com/golangci/golangci-lint/v2.12.2/golangci-lint-2.12.2-linux-amd64.tar.gz",
"checksum": "8DF580D2670FED8FA984AAC0507099AF8DF275E665215F5C7A2AE3943893A553",
"algorithm": "sha256"
},
{
"id": "github_release/github.com/golangci/golangci-lint/v2.12.2/golangci-lint-2.12.2-linux-arm64.tar.gz",
"checksum": "44CD40A8C76C86755375ADFEEA52CFD3533CB43D7BD647771E0AE065E166DF3A",
"algorithm": "sha256"
},
{
"id": "github_release/github.com/golangci/golangci-lint/v2.12.2/golangci-lint-2.12.2-windows-amd64.zip",
"checksum": "BD42E3EBC8CB4ECECB86941983BAAF1DC221BBB04D838E94CE63B49CC91E02BB",
"algorithm": "sha256"
},
{
"id": "github_release/github.com/golangci/golangci-lint/v2.12.2/golangci-lint-2.12.2-windows-arm64.zip",
"checksum": "947B9A5BF762D465710B376C156F0184ABB2168378B0826AF1899E0EE7183742",
"algorithm": "sha256"
},
{
"id": "github_release/github.com/grpc/grpc-go/cmd/protoc-gen-go-grpc/v1.5.1/protoc-gen-go-grpc.v1.5.1.darwin.amd64.tar.gz",
"checksum": "C12C259D59C8B451F24B6ABDAD6D055FD0884C5268B7247FD7E5EC9D2432D1F1",
Expand Down
1 change: 1 addition & 0 deletions aqua.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ packages:
- name: protocolbuffers/protobuf-go/protoc-gen-go@v1.36.11
- name: grpc/grpc-go/protoc-gen-go-grpc@cmd/protoc-gen-go-grpc/v1.6.1
- name: pseudomuto/protoc-gen-doc@v1.5.1
- name: golangci/golangci-lint@v2.12.2
- name: suzuki-shunsuke/pinact@v4.1.1
4 changes: 2 additions & 2 deletions cmd/cp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func run() error {
return fmt.Errorf("unable to open source file %q: %w", os.Args[1], err)
}

defer sf.Close()
defer func() { _ = sf.Close() }()

fi, err := sf.Stat()
if err != nil {
Expand All @@ -52,7 +52,7 @@ func run() error {
return fmt.Errorf("unable to create destination file %q: %w", os.Args[1], err)
}

defer df.Close()
defer func() { _ = df.Close() }()

if _, err = io.Copy(df, sf); err != nil {
return fmt.Errorf("unable to copy %q to %q: %w", os.Args[1], os.Args[2], err)
Expand Down
15 changes: 8 additions & 7 deletions cmd/moco-agent/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var config struct {

type mysqlLogger struct{}

func (l mysqlLogger) Print(v ...interface{}) {}
func (l mysqlLogger) Print(v ...any) {}

var rootCmd = &cobra.Command{
Use: "moco-agent",
Expand All @@ -72,17 +72,16 @@ var rootCmd = &cobra.Command{
if err != nil {
return err
}
defer zapLogger.Sync()
defer func() { _ = zapLogger.Sync() }()
rLogger := zapr.NewLogger(zapLogger)

// Read required values for agent from ENV
podName := os.Getenv(mocoagent.PodNameEnvKey)
if podName == "" {
return fmt.Errorf("%s is empty", mocoagent.PodNameEnvKey)
}
index := -1
fields := strings.Split(podName, "-")
index, _ = strconv.Atoi(fields[len(fields)-1])
index, _ := strconv.Atoi(fields[len(fields)-1])
Comment thread
yamatcha marked this conversation as resolved.
agentPassword := os.Getenv(mocoagent.AgentPasswordEnvKey)
if agentPassword == "" {
return fmt.Errorf("%s is empty", mocoagent.AgentPasswordEnvKey)
Expand Down Expand Up @@ -116,9 +115,11 @@ var rootCmd = &cobra.Command{
if err != nil {
return err
}
defer agent.CloseDB()
defer func() { _ = agent.CloseDB() }()

mysql.SetLogger(mysqlLogger{})
if err := mysql.SetLogger(mysqlLogger{}); err != nil {
return err
}

registry := prometheus.DefaultRegisterer
metrics.Init(registry, clusterName, index)
Expand Down Expand Up @@ -290,7 +291,7 @@ func initializeMySQLForMOCO(ctx context.Context, socketPath string, logger logr.
}
}

defer db.Close()
defer func() { _ = db.Close() }()

return server.Init(ctx, db, socketPath)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/moco-init/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func initMySQL(mysqld string) error {
if err != nil {
return err
}
defer f.Close()
defer func() { _ = f.Close() }()

if err := unix.Syncfs(int(f.Fd())); err != nil {
return fmt.Errorf("failed to sync fs: %w", err)
Expand All @@ -172,7 +172,7 @@ func initMySQL(mysqld string) error {
if err != nil {
return err
}
defer g.Close()
defer func() { _ = g.Close() }()
return g.Sync()
}

Expand Down Expand Up @@ -270,7 +270,7 @@ func createConf() error {
if err != nil {
return fmt.Errorf("failed to create my.cnf file: %w", err)
}
defer f.Close()
defer func() { _ = f.Close() }()

if err := tmpl.Execute(f, v); err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions server/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (a *Agent) Clone(ctx context.Context, req *proto.CloneRequest) error {
if err != nil {
return fmt.Errorf("failed to connect to mysqld through %s: %w", a.mysqlSocketPath, err)
}
defer cloneDB.Close()
defer func() { _ = cloneDB.Close() }()

logger.Info("start cloning instance", "donor", donorAddr)
_, err = cloneDB.Exec(`CLONE INSTANCE FROM ?@?:? IDENTIFIED BY ?`, req.User, req.Host, req.Port, req.Password)
Expand Down Expand Up @@ -100,7 +100,7 @@ func (a *Agent) Clone(ctx context.Context, req *proto.CloneRequest) error {
logger.Error(err, "failed to connect to mysqld after bootstrap")
return err
}
defer initDB.Close()
defer func() { _ = initDB.Close() }()

if err := InitExternal(context.Background(), initDB); err != nil {
logger.Error(err, "failed to initialize after clone")
Expand All @@ -123,7 +123,7 @@ func waitBootstrap(user, password, socket string, timeout time.Duration, logger

db, err := GetMySQLConnLocalSocket(user, password, socket)
if err == nil {
db.Close()
_ = db.Close()
return nil
}
logger.Error(err, "connection failed")
Expand Down
2 changes: 1 addition & 1 deletion server/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func Init(ctx context.Context, db *sqlx.DB, socket string) error {
}
time.Sleep(1 * time.Second)
}
defer db.Close()
defer func() { _ = db.Close() }()

if err := dropLocalRootUser(ctx, db); err != nil {
return err
Expand Down
5 changes: 2 additions & 3 deletions server/mysqld_health.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ import (

// Health returns the health check result of own MySQL
func (a *Agent) MySQLDHealth(w http.ResponseWriter, r *http.Request) {
rows, err := a.db.QueryxContext(r.Context(), `SELECT VERSION()`)
if err != nil {
var version string
if err := a.db.GetContext(r.Context(), &version, `SELECT VERSION()`); err != nil {
a.logger.Info("health check failed")
http.Error(w, "failed to execute a query", http.StatusServiceUnavailable)
return
}
rows.Close()
}

func (a *Agent) MySQLDReady(w http.ResponseWriter, r *http.Request) {
Expand Down
12 changes: 6 additions & 6 deletions server/mysqld_health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ var _ = Describe("health", func() {
Expect(err).NotTo(HaveOccurred())
_, err = donorDB.Exec("CREATE TABLE foo.bar (i INT PRIMARY KEY) ENGINE=InnoDB")
Expect(err).NotTo(HaveOccurred())
items := []interface{}{100, 299, 993, 9292}
items := []any{100, 299, 993, 9292}
_, err = donorDB.Exec("INSERT INTO foo.bar (i) VALUES (?), (?), (?), (?)", items...)
Expect(err).NotTo(HaveOccurred())

Expand All @@ -119,7 +119,7 @@ var _ = Describe("health", func() {
Expect(err).NotTo(HaveOccurred())

By("checking readiness")
Eventually(func() interface{} {
Eventually(func() any {
return getReady(agent)
}).Should(HaveHTTPStatus(http.StatusOK))

Expand All @@ -131,14 +131,14 @@ var _ = Describe("health", func() {
time.Sleep(200 * time.Millisecond)
_, err = donorDB.Exec("ALTER TABLE foo.bar ADD COLUMN hoge TEXT")
Expect(err).NotTo(HaveOccurred())
Eventually(func() interface{} {
Eventually(func() any {
return getReady(agent)
}).Should(HaveHTTPStatus(http.StatusServiceUnavailable))

By("unlocking replica")
_, err = replicaDB.Exec(`UNLOCK INSTANCE`)
Expect(err).NotTo(HaveOccurred())
Eventually(func() interface{} {
Eventually(func() any {
return getReady(agent)
}).Should(HaveHTTPStatus(http.StatusOK))
})
Expand Down Expand Up @@ -196,10 +196,10 @@ var _ = Describe("health", func() {
// The uptime observed by the agent is about 15s smaller than the process uptime reported by kernel
// The test flow takes about 35s from the process start to this point.
// We set Consistently timeout to 60(transactionQueueingWait) - (35 - 15) - 5(margin) = 35
Consistently(func() interface{} {
Consistently(func() any {
return getReady(agent)
}).WithPolling(time.Second).WithTimeout(time.Second * 35).ShouldNot(HaveHTTPStatus(http.StatusOK))
Eventually(func() interface{} {
Eventually(func() any {
return getReady(agent)
}).WithPolling(time.Second).WithTimeout(time.Second * 10).Should(HaveHTTPStatus(http.StatusOK))
})
Expand Down
2 changes: 1 addition & 1 deletion server/suite_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package server

import (
"log" // restrictpkg:ignore to suppress mysql client logs.
"log"
"os"
"testing"
"time"
Expand Down