-
Notifications
You must be signed in to change notification settings - Fork 307
sync-diff: replace hardcoded use of MD5 with configurable option to support FIPS-compliant environments. #12622
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
Open
maxz-db
wants to merge
1
commit into
pingcap:master
Choose a base branch
from
maxz-db:sync-diff-inspector-fips
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+125
−37
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,7 +68,8 @@ func (a *MySQLTableAnalyzer) AnalyzeSplitter(ctx context.Context, table *common. | |
| type MySQLSources struct { | ||
| tableDiffs []*common.TableDiff | ||
|
|
||
| sourceTablesMap map[string][]*common.TableShardSource | ||
| sourceTablesMap map[string][]*common.TableShardSource | ||
| checksumAlgorithm config.ChecksumAlgorithm | ||
| } | ||
|
|
||
| func getMatchedSourcesForTable(sourceTablesMap map[string][]*common.TableShardSource, table *common.TableDiff) []*common.TableShardSource { | ||
|
|
@@ -114,7 +115,7 @@ func (s *MySQLSources) GetCountAndMD5(ctx context.Context, tableRange *splitter. | |
|
|
||
| for _, ms := range matchSources { | ||
| go func(ms *common.TableShardSource) { | ||
| count, checksum, err := utils.GetCountAndMD5Checksum(ctx, ms.DBConn, ms.OriginSchema, ms.OriginTable, table.Info, chunk.Where, "", chunk.Args) | ||
| count, checksum, err := utils.GetCountAndChecksum(ctx, ms.DBConn, ms.OriginSchema, ms.OriginTable, table.Info, chunk.Where, "", chunk.Args, string(s.checksumAlgorithm)) | ||
| infoCh <- &ChecksumInfo{ | ||
| Checksum: checksum, | ||
| Count: count, | ||
|
|
@@ -408,9 +409,14 @@ func NewMySQLSources(ctx context.Context, tableDiffs []*common.TableDiff, ds []* | |
| return nil, errors.Annotatef(err, "please make sure the filter is correct.") | ||
| } | ||
|
|
||
| checksumAlgorithm := config.MD5 | ||
| if len(ds) > 0 { | ||
| checksumAlgorithm = ds[0].ChecksumAlgorithm | ||
| } | ||
|
Comment on lines
+413
to
+415
Contributor
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.
|
||
| mss := &MySQLSources{ | ||
| tableDiffs: tableDiffs, | ||
| sourceTablesMap: sourceTablesMap, | ||
| tableDiffs: tableDiffs, | ||
| sourceTablesMap: sourceTablesMap, | ||
| checksumAlgorithm: checksumAlgorithm, | ||
| } | ||
| return mss, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -857,21 +857,25 @@ func GetTableSize(ctx context.Context, db *sql.DB, schemaName, tableName string) | |
| return dataSize.Int64, nil | ||
| } | ||
|
|
||
| // GetCountAndMD5Checksum returns checksum code and count of some data by given condition | ||
| func GetCountAndMD5Checksum( | ||
| // GetCountAndChecksum returns checksum code and count of some data by given condition | ||
| func GetCountAndChecksum( | ||
| ctx context.Context, | ||
| db *sql.DB, schemaName, tableName string, | ||
| tbInfo *model.TableInfo, limitRange string, indexHint string, args []any, | ||
| tbInfo *model.TableInfo, limitRange string, indexHint string, | ||
| args []any, checksumAlgorithm string, | ||
|
Contributor
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. can we pass the param as ChecksumAlgorithm instead of string |
||
| ) (int64, uint64, error) { | ||
| /* | ||
| calculate MD5 checksum and count example: | ||
| calculate checksum and count example (MD5): | ||
| mysql> SELECT COUNT(*) as CNT, BIT_XOR(CAST(CONV(SUBSTRING(MD5(CONCAT_WS(',', `id`, `name`, CONCAT(ISNULL(`id`), ISNULL(`name`)))), 1, 16), 16, 10) AS UNSIGNED) ^ CAST(CONV(SUBSTRING(MD5(CONCAT_WS(',', `id`, `name`, CONCAT(ISNULL(`id`), ISNULL(`name`)))), 17, 16), 16, 10) AS UNSIGNED)) as CHECKSUM FROM `a`.`t`; | ||
| +--------+---------------------- | ||
| | CNT | CHECKSUM | | ||
| +--------+---------------------- | ||
| | 100000 | 3462532621352132810 | | ||
| +--------+---------------------- | ||
| 1 row in set (0.46 sec) | ||
|
|
||
| calculate checksum and count example (SHA256): | ||
| mysql> SELECT COUNT(*) as CNT, BIT_XOR(CAST(CONV(SUBSTRING(SHA2(CONCAT_WS(',', `id`, `name`, CONCAT(ISNULL(`id`), ISNULL(`name`))), 256), 1, 16), 16, 10) AS UNSIGNED) ^ CAST(CONV(SUBSTRING(SHA2(CONCAT_WS(',', `id`, `name`, CONCAT(ISNULL(`id`), ISNULL(`name`))), 256), 17, 16), 16, 10) AS UNSIGNED)) as CHECKSUM FROM `a`.`t`; | ||
| */ | ||
| columnNames := make([]string, 0, len(tbInfo.Columns)) | ||
| columnIsNull := make([]string, 0, len(tbInfo.Columns)) | ||
|
|
@@ -892,16 +896,27 @@ func GetCountAndMD5Checksum( | |
| columnIsNull = append(columnIsNull, fmt.Sprintf("ISNULL(%s)", name)) | ||
| } | ||
|
|
||
| query := fmt.Sprintf("SELECT %s COUNT(*) as CNT, BIT_XOR(CAST(CONV(SUBSTRING(MD5(CONCAT_WS(',', %s, CONCAT(%s))), 1, 16), 16, 10) AS UNSIGNED) ^ CAST(CONV(SUBSTRING(MD5(CONCAT_WS(',', %s, CONCAT(%s))), 17, 16), 16, 10) AS UNSIGNED)) as CHECKSUM FROM %s WHERE %s;", | ||
| indexHint, | ||
| strings.Join(columnNames, ", "), | ||
| strings.Join(columnIsNull, ", "), | ||
| var checksumFuncTemplate string | ||
| if checksumAlgorithm == "sha256" { | ||
| checksumFuncTemplate = "SHA2(%s, 256)" | ||
| } else { | ||
| checksumFuncTemplate = "MD5(%s)" | ||
| } | ||
|
|
||
| concatExpr := fmt.Sprintf("CONCAT_WS(',', %s, CONCAT(%s))", | ||
| strings.Join(columnNames, ", "), | ||
| strings.Join(columnIsNull, ", "), | ||
| strings.Join(columnIsNull, ", ")) | ||
|
|
||
| checksumExpr := fmt.Sprintf(checksumFuncTemplate, concatExpr) | ||
|
|
||
| query := fmt.Sprintf("SELECT %s COUNT(*) as CNT, BIT_XOR(CAST(CONV(SUBSTRING(%s, 1, 16), 16, 10) AS UNSIGNED) ^ CAST(CONV(SUBSTRING(%s, 17, 16), 16, 10) AS UNSIGNED)) as CHECKSUM FROM %s WHERE %s;", | ||
| indexHint, | ||
| checksumExpr, | ||
| checksumExpr, | ||
| dbutil.TableName(schemaName, tableName), | ||
| limitRange, | ||
| ) | ||
| log.Debug("count and checksum", zap.String("sql", query), zap.Reflect("args", args)) | ||
| log.Debug("count and checksum", zap.String("sql", query), zap.Reflect("args", args), zap.String("checksum-algorithm", checksumAlgorithm)) | ||
|
|
||
| var count sql.NullInt64 | ||
| var checksum uint64 | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
How about passing
ConfigtobuildSourceFromCfg, so we don't need storeChecksumAlgorithmin theDataSource.