-
Notifications
You must be signed in to change notification settings - Fork 283
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
sorter(ticdc): use correct cgroup memory limit in some scenarios (#8589) #8623
Merged
ti-chi-bot
merged 6 commits into
pingcap:release-6.1
from
ti-chi-bot:cherry-pick-8589-to-release-6.1
Mar 27, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
435e811
sorter(ticdc): use correct cgroup memory limit in some scenarios (#8589)
amyangfei d011bf5
fork gomemlimit to fit go1.18
amyangfei f7bfa8c
Merge branch 'release-6.1' into cherry-pick-8589-to-release-6.1
amyangfei 32a0601
fix unit test
amyangfei 56f7f37
fix make check
amyangfei d963c5e
fix unit test in unified sorter
amyangfei 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// 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, | ||
// See the License for the specific language governing permissions and | ||
// | ||
// ============================================================ | ||
// Forked from https://github.com/KimMachineGun/automemlimit | ||
// | ||
// Written by Geon Kim <geon0250@gmail.com> | ||
// limitations under the MIT License. | ||
|
||
//go:build linux | ||
// +build linux | ||
|
||
package cgroup | ||
|
||
import ( | ||
"github.com/containerd/cgroups/v3" | ||
"github.com/containerd/cgroups/v3/cgroup1" | ||
"github.com/containerd/cgroups/v3/cgroup2" | ||
) | ||
|
||
// FromCgroup returns the memory limit based on the cgroups version on this system. | ||
func FromCgroup() (uint64, error) { | ||
switch cgroups.Mode() { | ||
case cgroups.Legacy: | ||
return FromCgroupV1() | ||
case cgroups.Hybrid, cgroups.Unified: | ||
return FromCgroupV2() | ||
} | ||
return 0, ErrNoCgroup | ||
} | ||
|
||
// FromCgroupV1 returns the memory limit from the cgroup v1. | ||
func FromCgroupV1() (uint64, error) { | ||
cg, err := cgroup1.Load(cgroup1.RootPath, cgroup1.WithHiearchy( | ||
cgroup1.SingleSubsystem(cgroup1.Default, cgroup1.Memory), | ||
)) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
metrics, err := cg.Stat(cgroup1.IgnoreNotExist) | ||
if err != nil { | ||
return 0, err | ||
} else if metrics.Memory == nil { | ||
return 0, ErrNoLimit | ||
} | ||
|
||
return metrics.Memory.HierarchicalMemoryLimit, nil | ||
} | ||
|
||
// FromCgroupV2 returns the memory limit from the cgroup v2. | ||
func FromCgroupV2() (uint64, error) { | ||
path, err := cgroup2.NestedGroupPath("") | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
m, err := cgroup2.Load(path, cgroup2.WithMountpoint(cgroupMountPoint)) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
stats, err := m.Stat() | ||
if err != nil { | ||
return 0, err | ||
} else if stats.Memory == nil { | ||
return 0, ErrNoLimit | ||
} | ||
|
||
return stats.Memory.UsageLimit, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// 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, | ||
// See the License for the specific language governing permissions and | ||
// | ||
// ============================================================ | ||
// Forked from https://github.com/KimMachineGun/automemlimit | ||
// | ||
// Written by Geon Kim <geon0250@gmail.com> | ||
// limitations under the MIT License. | ||
|
||
//go:build linux | ||
// +build linux | ||
|
||
package cgroup | ||
|
||
import ( | ||
"flag" | ||
"log" | ||
"os" | ||
"testing" | ||
|
||
"github.com/containerd/cgroups/v3" | ||
) | ||
|
||
var ( | ||
cgVersion cgroups.CGMode | ||
expected uint64 | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
flag.Uint64Var(&expected, "expected", 0, "Expected cgroup's memory limit") | ||
flag.Parse() | ||
|
||
cgVersion = cgroups.Mode() | ||
log.Println("Cgroups version:", cgVersion) | ||
|
||
os.Exit(m.Run()) | ||
} | ||
|
||
func TestFromCgroup(t *testing.T) { | ||
_, err := FromCgroup() | ||
if cgVersion == cgroups.Unavailable && err != ErrNoCgroup { | ||
t.Fatalf("FromCgroup() error = %v, wantErr %v", err, ErrNoCgroup) | ||
} | ||
|
||
if err != nil { | ||
t.Fatalf("FromCgroup() error = %v, wantErr %v", err, nil) | ||
} | ||
} | ||
|
||
func TestFromCgroupV1(t *testing.T) { | ||
if cgVersion != cgroups.Legacy { | ||
t.Skip("cgroups v1 is not supported") | ||
} | ||
_, err := FromCgroupV1() | ||
if err != nil { | ||
t.Fatalf("FromCgroupV1() error = %v, wantErr %v", err, nil) | ||
} | ||
} | ||
|
||
func TestFromCgroupV2(t *testing.T) { | ||
if cgVersion != cgroups.Hybrid && cgVersion != cgroups.Unified { | ||
t.Skip("cgroups v2 is not supported") | ||
} | ||
_, err := FromCgroupV2() | ||
if err != nil { | ||
t.Fatalf("FromCgroupV2() error = %v, wantErr %v", err, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// 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, | ||
// See the License for the specific language governing permissions and | ||
// | ||
// ============================================================ | ||
// Forked from https://github.com/KimMachineGun/automemlimit | ||
// | ||
// Written by Geon Kim <geon0250@gmail.com> | ||
// limitations under the MIT License. | ||
|
||
//go:build !linux | ||
// +build !linux | ||
|
||
package cgroup | ||
|
||
func FromCgroup() (uint64, error) { | ||
return 0, ErrCgroupsNotSupported | ||
} | ||
|
||
func FromCgroupV1() (uint64, error) { | ||
return 0, ErrCgroupsNotSupported | ||
} | ||
|
||
func FromCgroupV2() (uint64, error) { | ||
return 0, ErrCgroupsNotSupported | ||
} |
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,56 @@ | ||
// 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, | ||
// See the License for the specific language governing permissions and | ||
// | ||
// ============================================================ | ||
// Forked from https://github.com/KimMachineGun/automemlimit | ||
// | ||
// Written by Geon Kim <geon0250@gmail.com> | ||
// limitations under the MIT License. | ||
|
||
//go:build !linux | ||
// +build !linux | ||
|
||
package cgroup | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestFromCgroup(t *testing.T) { | ||
limit, err := FromCgroup() | ||
if err != ErrCgroupsNotSupported { | ||
t.Fatalf("FromCgroup() error = %v, wantErr %v", err, ErrCgroupsNotSupported) | ||
} | ||
if limit != 0 { | ||
t.Fatalf("FromCgroup() got = %v, want %v", limit, 0) | ||
} | ||
} | ||
|
||
func TestFromCgroupV1(t *testing.T) { | ||
limit, err := FromCgroupV1() | ||
if err != ErrCgroupsNotSupported { | ||
t.Fatalf("FromCgroupV1() error = %v, wantErr %v", err, ErrCgroupsNotSupported) | ||
} | ||
if limit != 0 { | ||
t.Fatalf("FromCgroupV1() got = %v, want %v", limit, 0) | ||
} | ||
} | ||
|
||
func TestFromCgroupV2(t *testing.T) { | ||
limit, err := FromCgroupV2() | ||
if err != ErrCgroupsNotSupported { | ||
t.Fatalf("FromCgroupV2() error = %v, wantErr %v", err, ErrCgroupsNotSupported) | ||
} | ||
if limit != 0 { | ||
t.Fatalf("FromCgroupV2() got = %v, want %v", limit, 0) | ||
} | ||
} |
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.
github.com/KimMachineGun/automemlimit
requires go >= 1.19, so fork the cgroup part to fit go 1.18 in release-6.1