Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: stop mysql query explicitly (#9798)

Danny Xudb3f4dbbcb41
diff --git a/backend/plugin/db/mysql/mysql.go b/backend/plugin/db/mysql/mysql.go
index 46745fabe3..0d2d5a650a 100644
--- a/backend/plugin/db/mysql/mysql.go
+++ b/backend/plugin/db/mysql/mysql.go
@@ -172,6 +172,12 @@ func (driver *Driver) Execute(ctx context.Context, statement string, _ bool, opt
}
defer conn.Close()
+ connectionID, err := getConnectionID(ctx, conn)
+ if err != nil {
+ return 0, err
+ }
+ slog.Debug("connectionID", slog.String("connectionID", connectionID))
+
if opts.BeginFunc != nil {
if err := opts.BeginFunc(ctx, conn); err != nil {
return 0, err
@@ -242,6 +248,13 @@ func (driver *Driver) Execute(ctx context.Context, statement string, _ bool, opt
sqlResult, err := tx.ExecContext(ctx, chunkText)
if err != nil {
+ if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
+ slog.Info("cancel connection", slog.String("connectionID", connectionID))
+ if err := driver.StopConnectionByID(connectionID); err != nil {
+ slog.Error("failed to cancel connection", slog.String("connectionID", connectionID), log.BBError(err))
+ }
+ }
+
return 0, &db.ErrorWithPosition{
Err: errors.Wrapf(err, "failed to execute context in a transaction"),
Start: &storepb.TaskRunResult_Position{
@@ -272,6 +285,11 @@ func (driver *Driver) Execute(ctx context.Context, statement string, _ bool, opt
// QueryConn queries a SQL statement in a given connection.
func (driver *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement string, queryContext *db.QueryContext) ([]*v1pb.QueryResult, error) {
+ connectionID, err := getConnectionID(ctx, conn)
+ if err != nil {
+ return nil, err
+ }
+ slog.Debug("connectionID", slog.String("connectionID", connectionID))
singleSQLs, err := base.SplitMultiSQL(storepb.Engine_MYSQL, statement)
if err != nil {
return nil, err
@@ -288,6 +306,13 @@ func (driver *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement s
results = append(results, &v1pb.QueryResult{
Error: err.Error(),
})
+ if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
+ slog.Info("cancel connection", slog.String("connectionID", connectionID))
+ if err := driver.StopConnectionByID(connectionID); err != nil {
+ slog.Error("failed to cancel connection", slog.String("connectionID", connectionID), log.BBError(err))
+ }
+ break
+ }
} else {
results = append(results, result)
}
@@ -296,6 +321,20 @@ func (driver *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement s
return results, nil
}
+func (driver *Driver) StopConnectionByID(id string) error {
+ // We cannot use placeholder parameter because TiDB doesn't accept it.
+ _, err := driver.db.Exec(fmt.Sprintf("KILL QUERY %s", id))
+ return err
+}
+
+func getConnectionID(ctx context.Context, conn *sql.Conn) (string, error) {
+ var id string
+ if err := conn.QueryRowContext(ctx, `SELECT CONNECTION_ID();`).Scan(&id); err != nil {
+ return "", err
+ }
+ return id, nil
+}
+
func (driver *Driver) querySingleSQL(ctx context.Context, conn *sql.Conn, singleSQL base.SingleSQL, queryContext *db.QueryContext) (*v1pb.QueryResult, error) {
if singleSQL.Empty {
return nil, nil
diff --git a/backend/plugin/db/util/driverutil.go b/backend/plugin/db/util/driverutil.go
index f65897d4dd..f189f86fe7 100644
--- a/backend/plugin/db/util/driverutil.go
+++ b/backend/plugin/db/util/driverutil.go
@@ -8,14 +8,12 @@ import (
"fmt"
"io"
"log/slog"
- "regexp"
"strings"
"time"
"github.com/pkg/errors"
"google.golang.org/protobuf/types/known/durationpb"
- "github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/common/log"
"github.com/bytebase/bytebase/backend/component/masker"
"github.com/bytebase/bytebase/backend/plugin/db"
@@ -27,10 +25,7 @@ import (
// FormatErrorWithQuery will format the error with failed query.
func FormatErrorWithQuery(err error, query string) error {
- if regexp.MustCompile("does not exist").MatchString(err.Error()) {
- return common.Wrapf(err, common.NotFound, "failed to execute query %q", query)
- }
- return common.Wrapf(err, common.DbExecutionError, "failed to execute query %q", query)
+ return errors.Wrapf(err, "failed to execute query %q", query)
}
// ApplyMultiStatements will apply the split statements from scanner.