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

Snapshot connection: revert to explicit table locks when FTWRL is unavailable #14578

Merged
merged 3 commits into from
Nov 23, 2023
Merged
Changes from 2 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
37 changes: 36 additions & 1 deletion go/vt/vttablet/tabletserver/vstreamer/snapshot_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ package vstreamer
import (
"context"
"fmt"
"math"
"strings"
"sync/atomic"
"time"

"github.com/spf13/pflag"

"vitess.io/vitess/go/mysql/replication"
"vitess.io/vitess/go/mysql/sqlerror"

"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/vt/dbconfigs"
Expand Down Expand Up @@ -241,8 +244,40 @@ func (conn *snapshotConn) startSnapshotAllTables(ctx context.Context) (gtid stri

log.Infof("Locking all tables")
if _, err := lockConn.ExecuteFetch("FLUSH TABLES WITH READ LOCK", 1, false); err != nil {
attemptExplicitTablesLocks := false
if sqlErr, ok := err.(*sqlerror.SQLError); ok && sqlErr.Number() == sqlerror.ERAccessDeniedError {
// Access denied. On some systems this is either because the user doesn't have SUPER or RELOAD privileges.
// On some other systems, namely RDS, the command is just unsupported.
// There is an alternative way: run a `LOCK TABLES tbl1 READ, tbl2 READ, ...` for all tables. It not as
// efficient, and make a huge query, but still better than nothing.
attemptExplicitTablesLocks = true
}
log.Infof("Error locking all tables")
return "", err
if !attemptExplicitTablesLocks {
return "", err
}
// get list of all tables
rs, err := conn.ExecuteFetch("show full tables", math.MaxInt32, true)
if err != nil {
return "", err
}

var lockClauses []string
for _, row := range rs.Rows {
tableName := row[0].ToString()
tableType := row[1].ToString()
if tableType != "BASE TABLE" {
continue
}
tableName = sqlparser.String(sqlparser.NewIdentifierCS(tableName))
lockClause := fmt.Sprintf("%s read", tableName)
lockClauses = append(lockClauses, lockClause)
}
query := fmt.Sprintf("lock tables %s", strings.Join(lockClauses, ","))
if _, err := lockConn.ExecuteFetch(query, 1, false); err != nil {
log.Infof("Error explicitly locking all %v tables", len(lockClauses))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this log.Errorf and also print the error as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Also added check for empty table list.

return "", err
}
}
mpos, err := lockConn.PrimaryPosition()
if err != nil {
Expand Down
Loading