-
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
external: fix decoding the last key in split keys #59613
Merged
+156
−30
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
06fd418
lightning: add log for debug
CbcWestwolf 51537dd
logutil.Key
CbcWestwolf aee14c5
more logs
CbcWestwolf 6002802
0224
CbcWestwolf 8ff59d8
try fix
CbcWestwolf af13a6a
remove all debug log
CbcWestwolf bd8a1a2
update name
CbcWestwolf baa5e7e
Add tests
CbcWestwolf 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 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 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 |
---|---|---|
|
@@ -31,6 +31,7 @@ import ( | |
"github.com/pingcap/tidb/pkg/lightning/log" | ||
"github.com/pingcap/tidb/pkg/lightning/membuf" | ||
"github.com/pingcap/tidb/pkg/metrics" | ||
"github.com/pingcap/tidb/pkg/util/intest" | ||
"github.com/pingcap/tidb/pkg/util/logutil" | ||
"go.uber.org/atomic" | ||
"go.uber.org/zap" | ||
|
@@ -98,7 +99,6 @@ type Engine struct { | |
endKey []byte | ||
jobKeys [][]byte | ||
splitKeys [][]byte | ||
regionSplitSize int64 | ||
smallBlockBufPool *membuf.Pool | ||
largeBlockBufPool *membuf.Pool | ||
|
||
|
@@ -110,8 +110,7 @@ type Engine struct { | |
// this flag also affects the strategy of loading data, either: | ||
// less load routine + check and read hotspot file concurrently (add-index uses this one) | ||
// more load routine + read each file using 1 reader (import-into uses this one) | ||
checkHotspot bool | ||
mergerIterConcurrency int | ||
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. DITTO |
||
checkHotspot bool | ||
|
||
keyAdapter common.KeyAdapter | ||
duplicateDetection bool | ||
|
@@ -348,19 +347,11 @@ func (e *Engine) loadBatchRegionData(ctx context.Context, jobKeys [][]byte, outC | |
prev = cur | ||
} | ||
// last range key may be a nextKey so we should try to remove the trailing 0 if decoding failed | ||
nextKey := false | ||
lastKey := jobKeys[len(jobKeys)-1] | ||
cur, err4 := e.keyAdapter.Decode(nil, lastKey) | ||
if err4 != nil && lastKey[len(lastKey)-1] == 0 { | ||
nextKey = true | ||
cur, err4 = e.keyAdapter.Decode(nil, lastKey[:len(lastKey)-1]) | ||
} | ||
cur, err4 := e.tryDecodeEndKey(lastKey) | ||
if err4 != nil { | ||
return err4 | ||
} | ||
if nextKey { | ||
cur = kv.Key(cur).Next() | ||
} | ||
ranges = append(ranges, common.Range{ | ||
Start: prev, | ||
End: cur, | ||
|
@@ -438,43 +429,64 @@ func (e *Engine) GetKeyRange() (startKey []byte, endKey []byte, err error) { | |
return e.startKey, e.endKey, nil | ||
} | ||
|
||
// when duplicate detection feature is enabled, the end key comes from | ||
// DupDetectKeyAdapter.Encode or Key.Next(). We try to decode it and check the | ||
// error. | ||
|
||
start, err := e.keyAdapter.Decode(nil, e.startKey) | ||
startKey, err = e.keyAdapter.Decode(nil, e.startKey) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
end, err := e.keyAdapter.Decode(nil, e.endKey) | ||
if err == nil { | ||
return start, end, nil | ||
} | ||
// handle the case that end key is from Key.Next() | ||
if e.endKey[len(e.endKey)-1] != 0 { | ||
return nil, nil, err | ||
} | ||
endEncoded := e.endKey[:len(e.endKey)-1] | ||
end, err = e.keyAdapter.Decode(nil, endEncoded) | ||
endKey, err = e.tryDecodeEndKey(e.endKey) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
return start, kv.Key(end).Next(), nil | ||
return startKey, endKey, nil | ||
} | ||
|
||
// GetRegionSplitKeys implements common.Engine. | ||
func (e *Engine) GetRegionSplitKeys() ([][]byte, error) { | ||
splitKeys := make([][]byte, len(e.splitKeys)) | ||
var ( | ||
err error | ||
splitKey []byte | ||
) | ||
for i, k := range e.splitKeys { | ||
var err error | ||
splitKeys[i], err = e.keyAdapter.Decode(nil, k) | ||
if i < len(e.splitKeys)-1 { | ||
splitKey, err = e.keyAdapter.Decode(nil, k) | ||
} else { | ||
splitKey, err = e.tryDecodeEndKey(k) | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
splitKeys[i] = splitKey | ||
} | ||
return splitKeys, nil | ||
} | ||
|
||
// tryDecodeEndKey tries to decode the key from two sources. | ||
// When duplicate detection feature is enabled, the **end key** comes from | ||
// DupDetectKeyAdapter.Encode or Key.Next(). We try to decode it and check the | ||
// error. | ||
func (e Engine) tryDecodeEndKey(key []byte) (decoded []byte, err error) { | ||
decoded, err = e.keyAdapter.Decode(nil, key) | ||
if err == nil { | ||
return | ||
} | ||
if _, ok := e.keyAdapter.(common.NoopKeyAdapter); ok { | ||
// NoopKeyAdapter.Decode always return nil error | ||
intest.Assert(false, "Unreachable code path") | ||
return nil, err | ||
} | ||
// handle the case that end key is from Key.Next() | ||
if key[len(key)-1] != 0 { | ||
return nil, err | ||
} | ||
key = key[:len(key)-1] | ||
decoded, err = e.keyAdapter.Decode(nil, key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return kv.Key(decoded).Next(), nil | ||
} | ||
|
||
// Close implements common.Engine. | ||
func (e *Engine) Close() error { | ||
if e.smallBlockBufPool != nil { | ||
|
This file contains 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
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.
just remove useless field