-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
sessionctx/binloginfo: fix uncomment pre_split_regions ddl-querys in binlog #11762
Changes from 5 commits
1718d9b
a0ca779
41da1a4
eca81e5
bf7746a
dcc7809
4548b56
a4edad8
eecf981
3493380
7fdee22
dca1909
54fabd1
fbb4e64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
package binloginfo | ||
|
||
import ( | ||
"math" | ||
"regexp" | ||
"strings" | ||
"sync" | ||
|
@@ -28,7 +29,7 @@ import ( | |
"github.com/pingcap/tidb/metrics" | ||
"github.com/pingcap/tidb/sessionctx" | ||
"github.com/pingcap/tidb/util/logutil" | ||
binlog "github.com/pingcap/tipb/go-binlog" | ||
"github.com/pingcap/tipb/go-binlog" | ||
"go.uber.org/zap" | ||
"google.golang.org/grpc" | ||
) | ||
|
@@ -41,7 +42,9 @@ func init() { | |
// shared by all sessions. | ||
var pumpsClient *pumpcli.PumpsClient | ||
var pumpsClientLock sync.RWMutex | ||
var shardPat = regexp.MustCompile(`SHARD_ROW_ID_BITS\s*=\s*\d+`) | ||
var shardPat = regexp.MustCompile(`SHARD_ROW_ID_BITS\s*=\s*\d+\s*`) | ||
var preSplitPat = regexp.MustCompile(`PRE_SPLIT_REGIONS\s*=\s*\d+\s*`) | ||
var redundantCommentPat = regexp.MustCompile(` \*\/\s*\/\*!90000`) | ||
|
||
// BinlogInfo contains binlog data and binlog client. | ||
type BinlogInfo struct { | ||
|
@@ -132,11 +135,12 @@ func (info *BinlogInfo) WriteBinlog(clusterID uint64) error { | |
|
||
// SetDDLBinlog sets DDL binlog in the kv.Transaction. | ||
func SetDDLBinlog(client *pumpcli.PumpsClient, txn kv.Transaction, jobID int64, ddlQuery string) { | ||
ddlQuery = AddSpecialComment(ddlQuery) | ||
if client == nil { | ||
return | ||
} | ||
|
||
ddlQuery = addSpecialComment(ddlQuery) | ||
ddlQuery = AddSpecialComment(ddlQuery) | ||
info := &BinlogInfo{ | ||
Data: &binlog.Binlog{ | ||
Tp: binlog.BinlogType_Prewrite, | ||
|
@@ -150,15 +154,45 @@ func SetDDLBinlog(client *pumpcli.PumpsClient, txn kv.Transaction, jobID int64, | |
|
||
const specialPrefix = `/*!90000 ` | ||
|
||
func addSpecialComment(ddlQuery string) string { | ||
// AddSpecialComment uses to add comment for table option in DDL query. | ||
// Export for testing. | ||
func AddSpecialComment(ddlQuery string) string { | ||
if strings.Contains(ddlQuery, specialPrefix) { | ||
return ddlQuery | ||
} | ||
loc := shardPat.FindStringIndex(strings.ToUpper(ddlQuery)) | ||
if loc == nil { | ||
return ddlQuery | ||
return addSpecialCommentByRegexps(ddlQuery, shardPat, preSplitPat) | ||
} | ||
|
||
func addSpecialCommentByRegexps(ddlQuery string, regs ...*regexp.Regexp) string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add some comments for this function. |
||
upperQuery := strings.ToUpper(ddlQuery) | ||
var specialComments []string | ||
minIdx := math.MaxInt64 | ||
for _, reg := range regs { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For SQL : There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. Done. |
||
loc := reg.FindStringIndex(upperQuery) | ||
if len(loc) < 2 { | ||
continue | ||
} | ||
specialComments = append(specialComments, ddlQuery[loc[0]:loc[1]]) | ||
if loc[0] < minIdx { | ||
minIdx = loc[0] | ||
} | ||
ddlQuery = ddlQuery[:loc[0]] + ddlQuery[loc[1]:] | ||
upperQuery = upperQuery[:loc[0]] + upperQuery[loc[1]:] | ||
} | ||
if minIdx != math.MaxInt64 { | ||
query := ddlQuery[:minIdx] + specialPrefix | ||
for _, comment := range specialComments { | ||
if query[len(query)-1] != ' ' { | ||
query += " " | ||
} | ||
query += comment | ||
} | ||
if query[len(query)-1] != ' ' { | ||
query += " " | ||
} | ||
return query + "*/ " + ddlQuery[minIdx:] | ||
} | ||
return ddlQuery[:loc[0]] + specialPrefix + ddlQuery[loc[0]:loc[1]] + ` */` + ddlQuery[loc[1]:] | ||
return ddlQuery | ||
} | ||
|
||
// MockPumpsClient creates a PumpsClient, used for test. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now this variable is redudant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done. thanks