-
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
lightning: refactor to reuse in load data part 5 #42856
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5d9a83f
change
D3Hunter 3e0a09d
change
D3Hunter 885a2f6
rename
D3Hunter 787c493
remove localEngineWriter
D3Hunter 6610d87
mock
D3Hunter 481b41b
remove noop
D3Hunter 0cf112d
bazel
D3Hunter 4f8706e
change
D3Hunter 4f784fd
lint
D3Hunter 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
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
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2023 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package local | ||
|
||
import ( | ||
"github.com/google/uuid" | ||
"github.com/pingcap/tidb/br/pkg/lightning/backend" | ||
"golang.org/x/exp/slices" | ||
) | ||
|
||
// DiskUsage is an interface to obtain the size occupied locally of all engines | ||
type DiskUsage interface { | ||
// EngineFileSizes obtains the size occupied locally of all engines managed | ||
// by this backend. This method is used to compute disk quota. | ||
// It can return nil if the content are all stored remotely. | ||
EngineFileSizes() (res []backend.EngineFileSize) | ||
} | ||
|
||
// CheckDiskQuota verifies if the total engine file size is below the given | ||
// quota. If the quota is exceeded, this method returns an array of engines, | ||
// which after importing can decrease the total size below quota. | ||
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. How can we use 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. didn't look in detail, just copied out |
||
func CheckDiskQuota(mgr DiskUsage, quota int64) ( | ||
largeEngines []uuid.UUID, | ||
inProgressLargeEngines int, | ||
totalDiskSize int64, | ||
totalMemSize int64, | ||
) { | ||
sizes := mgr.EngineFileSizes() | ||
slices.SortFunc(sizes, func(i, j backend.EngineFileSize) bool { | ||
if i.IsImporting != j.IsImporting { | ||
return i.IsImporting | ||
} | ||
return i.DiskSize+i.MemSize < j.DiskSize+j.MemSize | ||
}) | ||
for _, size := range sizes { | ||
totalDiskSize += size.DiskSize | ||
totalMemSize += size.MemSize | ||
if totalDiskSize+totalMemSize > quota { | ||
if size.IsImporting { | ||
inProgressLargeEngines++ | ||
} else { | ||
largeEngines = append(largeEngines, size.UUID) | ||
} | ||
} | ||
} | ||
return | ||
} |
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2023 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package local | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/google/uuid" | ||
"github.com/pingcap/tidb/br/pkg/lightning/backend" | ||
"github.com/pingcap/tidb/br/pkg/mock/mocklocal" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCheckDiskQuota(t *testing.T) { | ||
controller := gomock.NewController(t) | ||
mockDiskUsage := mocklocal.NewMockDiskUsage(controller) | ||
|
||
uuid1 := uuid.MustParse("11111111-1111-1111-1111-111111111111") | ||
uuid3 := uuid.MustParse("33333333-3333-3333-3333-333333333333") | ||
uuid5 := uuid.MustParse("55555555-5555-5555-5555-555555555555") | ||
uuid7 := uuid.MustParse("77777777-7777-7777-7777-777777777777") | ||
uuid9 := uuid.MustParse("99999999-9999-9999-9999-999999999999") | ||
|
||
fileSizes := []backend.EngineFileSize{ | ||
{ | ||
UUID: uuid1, | ||
DiskSize: 1000, | ||
MemSize: 0, | ||
IsImporting: false, | ||
}, | ||
{ | ||
UUID: uuid3, | ||
DiskSize: 2000, | ||
MemSize: 1000, | ||
IsImporting: true, | ||
}, | ||
{ | ||
UUID: uuid5, | ||
DiskSize: 1500, | ||
MemSize: 3500, | ||
IsImporting: false, | ||
}, | ||
{ | ||
UUID: uuid7, | ||
DiskSize: 0, | ||
MemSize: 7000, | ||
IsImporting: true, | ||
}, | ||
{ | ||
UUID: uuid9, | ||
DiskSize: 4500, | ||
MemSize: 4500, | ||
IsImporting: false, | ||
}, | ||
} | ||
|
||
mockDiskUsage.EXPECT().EngineFileSizes().Return(fileSizes).Times(4) | ||
|
||
// No quota exceeded | ||
le, iple, ds, ms := CheckDiskQuota(mockDiskUsage, 30000) | ||
require.Len(t, le, 0) | ||
require.Equal(t, 0, iple) | ||
require.Equal(t, int64(9000), ds) | ||
require.Equal(t, int64(16000), ms) | ||
|
||
// Quota exceeded, the largest one is out | ||
le, iple, ds, ms = CheckDiskQuota(mockDiskUsage, 20000) | ||
require.Equal(t, []uuid.UUID{uuid9}, le) | ||
require.Equal(t, 0, iple) | ||
require.Equal(t, int64(9000), ds) | ||
require.Equal(t, int64(16000), ms) | ||
|
||
// Quota exceeded, the importing one should be ranked least priority | ||
le, iple, ds, ms = CheckDiskQuota(mockDiskUsage, 12000) | ||
require.Equal(t, []uuid.UUID{uuid5, uuid9}, le) | ||
require.Equal(t, 0, iple) | ||
require.Equal(t, int64(9000), ds) | ||
require.Equal(t, int64(16000), ms) | ||
|
||
// Quota exceeded, the importing ones should not be visible | ||
le, iple, ds, ms = CheckDiskQuota(mockDiskUsage, 5000) | ||
require.Equal(t, []uuid.UUID{uuid1, uuid5, uuid9}, le) | ||
require.Equal(t, 1, iple) | ||
require.Equal(t, int64(9000), ds) | ||
require.Equal(t, int64(16000), ms) | ||
} |
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.
moved to below where its methods are defined