-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into bb7133/fix_31585
- Loading branch information
Showing
62 changed files
with
1,576 additions
and
328 deletions.
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
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,18 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "lightning", | ||
srcs = ["mem_root.go"], | ||
importpath = "github.com/pingcap/tidb/ddl/lightning", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
go_test( | ||
name = "lightning_test", | ||
srcs = ["mem_root_test.go"], | ||
flaky = True, | ||
deps = [ | ||
":lightning", | ||
"@com_github_stretchr_testify//require", | ||
], | ||
) |
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,115 @@ | ||
// Copyright 2022 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 lightning | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// MemRoot is used to track the memory usage for the lightning backfill process. | ||
type MemRoot interface { | ||
Consume(size int64) | ||
Release(size int64) | ||
TestConsume(size int64) bool | ||
ConsumeWithTag(tag string, size int64) | ||
ReleaseWithTag(tag string) | ||
|
||
SetMaxMemoryQuota(quota int64) | ||
MaxMemoryQuota() int64 | ||
CurrentUsage() int64 | ||
CurrentUsageWithTag(tag string) int64 | ||
} | ||
|
||
// memRootImpl is an implementation of MemRoot. | ||
type memRootImpl struct { | ||
maxLimit int64 | ||
currUsage int64 | ||
structSize map[string]int64 | ||
mu sync.RWMutex | ||
} | ||
|
||
// NewMemRootImpl creates a new memRootImpl. | ||
func NewMemRootImpl(maxQuota int64) *memRootImpl { | ||
return &memRootImpl{ | ||
maxLimit: maxQuota, | ||
currUsage: 0, | ||
structSize: make(map[string]int64, 10), | ||
} | ||
} | ||
|
||
// SetMaxMemoryQuota implements MemRoot. | ||
func (m *memRootImpl) SetMaxMemoryQuota(maxQuota int64) { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
m.maxLimit = maxQuota | ||
} | ||
|
||
// MaxMemoryQuota implements MemRoot. | ||
func (m *memRootImpl) MaxMemoryQuota() int64 { | ||
m.mu.RLock() | ||
defer m.mu.RUnlock() | ||
return m.maxLimit | ||
} | ||
|
||
// CurrentUsage implements MemRoot. | ||
func (m *memRootImpl) CurrentUsage() int64 { | ||
m.mu.RLock() | ||
defer m.mu.RUnlock() | ||
return m.currUsage | ||
} | ||
|
||
// CurrentUsageWithTag implements MemRoot. | ||
func (m *memRootImpl) CurrentUsageWithTag(tag string) int64 { | ||
m.mu.RLock() | ||
defer m.mu.RUnlock() | ||
return m.structSize[tag] | ||
} | ||
|
||
// Consume implements MemRoot. | ||
func (m *memRootImpl) Consume(size int64) { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
m.currUsage += size | ||
} | ||
|
||
// Release implements MemRoot. | ||
func (m *memRootImpl) Release(size int64) { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
m.currUsage -= size | ||
} | ||
|
||
// ConsumeWithTag implements MemRoot. | ||
func (m *memRootImpl) ConsumeWithTag(tag string, size int64) { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
m.currUsage += size | ||
m.structSize[tag] = size | ||
} | ||
|
||
// TestConsume implements MemRoot. | ||
func (m *memRootImpl) TestConsume(size int64) bool { | ||
m.mu.RLock() | ||
defer m.mu.RUnlock() | ||
return m.currUsage+size <= m.maxLimit | ||
} | ||
|
||
// ReleaseWithTag implements MemRoot. | ||
func (m *memRootImpl) ReleaseWithTag(tag string) { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
m.currUsage -= m.structSize[tag] | ||
delete(m.structSize, tag) | ||
} |
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,60 @@ | ||
// Copyright 2022 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 lightning_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/pingcap/tidb/ddl/lightning" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMemoryRoot(t *testing.T) { | ||
memRoot := lightning.MemRoot(lightning.NewMemRootImpl(1024)) | ||
require.Equal(t, int64(1024), memRoot.MaxMemoryQuota()) | ||
require.Equal(t, int64(0), memRoot.CurrentUsage()) | ||
|
||
require.True(t, memRoot.TestConsume(1023)) | ||
require.True(t, memRoot.TestConsume(1024)) | ||
require.False(t, memRoot.TestConsume(1025)) | ||
|
||
memRoot.Consume(512) | ||
require.Equal(t, int64(512), memRoot.CurrentUsage()) | ||
require.True(t, memRoot.TestConsume(512)) | ||
require.False(t, memRoot.TestConsume(513)) | ||
require.Equal(t, int64(1024), memRoot.MaxMemoryQuota()) | ||
|
||
memRoot.Release(10) | ||
require.Equal(t, int64(502), memRoot.CurrentUsage()) | ||
require.Equal(t, int64(1024), memRoot.MaxMemoryQuota()) | ||
memRoot.SetMaxMemoryQuota(512) | ||
require.False(t, memRoot.TestConsume(20)) // 502+20 > 512 | ||
memRoot.Release(502) | ||
|
||
require.Equal(t, int64(0), memRoot.CurrentUsage()) | ||
memRoot.SetMaxMemoryQuota(1024) | ||
memRoot.ConsumeWithTag("a", 512) | ||
memRoot.ConsumeWithTag("b", 512) | ||
require.Equal(t, int64(1024), memRoot.CurrentUsage()) | ||
require.False(t, memRoot.TestConsume(1)) | ||
memRoot.ReleaseWithTag("a") | ||
require.Equal(t, int64(512), memRoot.CurrentUsage()) | ||
|
||
memRoot.ReleaseWithTag("a") // Double release. | ||
require.Equal(t, int64(512), memRoot.CurrentUsage()) | ||
require.True(t, memRoot.TestConsume(10)) | ||
memRoot.Consume(10) // Mix usage of tag and non-tag. | ||
require.Equal(t, int64(522), memRoot.CurrentUsage()) | ||
} |
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
Oops, something went wrong.