From db2630aa025baf841346aa293e0b84cb85e83dc1 Mon Sep 17 00:00:00 2001 From: Sen Han <00hnes@gmail.com> Date: Wed, 15 Feb 2023 12:25:47 +0800 Subject: [PATCH 1/8] try fix getCgroupCPU Signed-off-by: Sen Han <00hnes@gmail.com> --- pkg/cgroup/cgroup_cpu.go | 4 +++- pkg/cgroup/cgroup_cpu_test.go | 10 +++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/pkg/cgroup/cgroup_cpu.go b/pkg/cgroup/cgroup_cpu.go index 9d63a1b9093..df1c104f77f 100644 --- a/pkg/cgroup/cgroup_cpu.go +++ b/pkg/cgroup/cgroup_cpu.go @@ -21,6 +21,8 @@ import ( "github.com/pingcap/errors" ) +var errNoCPUControllerDetected = errors.New("no cpu controller detected") + // Helper function for getCgroupCPU. Root is always "/", except in tests. func getCgroupCPUHelper(root string) (CPUUsage, error) { path, err := detectControlPath(filepath.Join(root, procPathCGroup), "cpu,cpuacct") @@ -30,7 +32,7 @@ func getCgroupCPUHelper(root string) (CPUUsage, error) { // No CPU controller detected if path == "" { - return CPUUsage{}, errors.New("no cpu controller detected") + return CPUUsage{}, errNoCPUControllerDetected } mount, ver, err := getCgroupDetails(filepath.Join(root, procPathMountInfo), path, "cpu,cpuacct") diff --git a/pkg/cgroup/cgroup_cpu_test.go b/pkg/cgroup/cgroup_cpu_test.go index fad947632de..7d6e0a98f92 100644 --- a/pkg/cgroup/cgroup_cpu_test.go +++ b/pkg/cgroup/cgroup_cpu_test.go @@ -42,9 +42,13 @@ func TestGetCgroupCPU(t *testing.T) { }() } cpu, err := GetCgroupCPU() - require.NoError(t, err) - require.NotZero(t, cpu.Period) - require.Less(t, int64(1), cpu.Period) + if err == errNoCPUControllerDetected { + require.False(t, InContainer(), "Please check linux version > v4.7.x. This is related to cgroup compatibility.") + } else { + require.NoError(t, err) + require.NotZero(t, cpu.Period) + require.Less(t, int64(1), cpu.Period) + } close(exit) wg.Wait() } From e4b99b507af303fbf22b78acfb522a3c64277118 Mon Sep 17 00:00:00 2001 From: Sen Han <00hnes@gmail.com> Date: Wed, 15 Feb 2023 12:26:33 +0800 Subject: [PATCH 2/8] add some log Signed-off-by: Sen Han <00hnes@gmail.com> --- pkg/cgroup/cgroup_cpu_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/cgroup/cgroup_cpu_test.go b/pkg/cgroup/cgroup_cpu_test.go index 7d6e0a98f92..4853e0d445d 100644 --- a/pkg/cgroup/cgroup_cpu_test.go +++ b/pkg/cgroup/cgroup_cpu_test.go @@ -21,7 +21,9 @@ import ( "sync" "testing" + "github.com/pingcap/log" "github.com/stretchr/testify/require" + "go.uber.org/zap" ) func TestGetCgroupCPU(t *testing.T) { @@ -42,9 +44,12 @@ func TestGetCgroupCPU(t *testing.T) { }() } cpu, err := GetCgroupCPU() + log.Info("InContainer()", zap.Bool("InContainer", InContainer())) if err == errNoCPUControllerDetected { + log.Info("errNoCPUControllerDetected", zap.Bool("errNoCPUControllerDetected", true)) require.False(t, InContainer(), "Please check linux version > v4.7.x. This is related to cgroup compatibility.") } else { + log.Info("errNoCPUControllerDetected", zap.Bool("errNoCPUControllerDetected", false)) require.NoError(t, err) require.NotZero(t, cpu.Period) require.Less(t, int64(1), cpu.Period) From 6078724922e5abb370956888e4a87db1a74f5421 Mon Sep 17 00:00:00 2001 From: Sen Han <00hnes@gmail.com> Date: Wed, 15 Feb 2023 16:25:17 +0800 Subject: [PATCH 3/8] ignore error if kernel version is too old Signed-off-by: Sen Han <00hnes@gmail.com> --- pkg/cgroup/cgroup_cpu_test.go | 50 ++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/pkg/cgroup/cgroup_cpu_test.go b/pkg/cgroup/cgroup_cpu_test.go index 4853e0d445d..22bc15530a3 100644 --- a/pkg/cgroup/cgroup_cpu_test.go +++ b/pkg/cgroup/cgroup_cpu_test.go @@ -17,8 +17,12 @@ package cgroup import ( + "fmt" + "regexp" "runtime" + "strconv" "sync" + "syscall" "testing" "github.com/pingcap/log" @@ -26,6 +30,42 @@ import ( "go.uber.org/zap" ) +func checkKernelVersionNewerThan(t *testing.T, major, minor int) bool { + u := syscall.Utsname{} + err := syscall.Uname(&u) + require.NoError(t, err) + releaseBs := make([]byte, 0, len(u.Release)) + for _, v := range u.Release { + if v == 0 { + break + } + releaseBs = append(releaseBs, byte(v)) + } + releaseStr := string(releaseBs) + log.Info("kernel release string", zap.String("release-str", releaseStr)) + versionInfoRE := regexp.MustCompile(`[0-9]+\.[0-9]+\.[0-9]+`) + kernelVerion := versionInfoRE.FindAllString(releaseStr, 1) + require.Equal(t, 1, len(kernelVerion), fmt.Sprintf("release str is %s", releaseStr)) + kernelVersionPartRE := regexp.MustCompile(`[0-9]+`) + kernelVersionParts := kernelVersionPartRE.FindAllString(kernelVerion[0], -1) + require.Equal(t, 3, len(kernelVersionParts), fmt.Sprintf("kernel verion str is %s", kernelVerion[0])) + log.Info("parsed kernel version parts", zap.String("major", kernelVersionParts[0]), zap.String("minor", kernelVersionParts[1]), zap.String("patch", kernelVersionParts[2])) + mustConvInt := func(s string) int { + i, err := strconv.Atoi(s) + require.NoError(t, err, s) + return i + } + versionNewerThanFlag := false + if mustConvInt(kernelVersionParts[0]) > major { + versionNewerThanFlag = true + } else { + if mustConvInt(kernelVersionParts[0]) == major && mustConvInt(kernelVersionParts[1]) > minor { + versionNewerThanFlag = true + } + } + return versionNewerThanFlag +} + func TestGetCgroupCPU(t *testing.T) { exit := make(chan struct{}) var wg sync.WaitGroup @@ -44,12 +84,14 @@ func TestGetCgroupCPU(t *testing.T) { }() } cpu, err := GetCgroupCPU() - log.Info("InContainer()", zap.Bool("InContainer", InContainer())) if err == errNoCPUControllerDetected { - log.Info("errNoCPUControllerDetected", zap.Bool("errNoCPUControllerDetected", true)) - require.False(t, InContainer(), "Please check linux version > v4.7.x. This is related to cgroup compatibility.") + // for more information, please refer https://github.com/pingcap/tidb/pull/41347 + if checkKernelVersionNewerThan(t, 4, 7) { + require.NoError(t, err, "linux version > v4.7 and err still happens") + } else { + log.Info("the error is ignored because the kernel is too old") + } } else { - log.Info("errNoCPUControllerDetected", zap.Bool("errNoCPUControllerDetected", false)) require.NoError(t, err) require.NotZero(t, cpu.Period) require.Less(t, int64(1), cpu.Period) From a6de27adc735fcce36d818ae93f6c2518978d1bb Mon Sep 17 00:00:00 2001 From: Sen Han <00hnes@gmail.com> Date: Wed, 15 Feb 2023 16:28:23 +0800 Subject: [PATCH 4/8] remove TestGetCgroupCPU from flaky tests list Signed-off-by: Sen Han <00hnes@gmail.com> --- pkg/cgroup/cgroup_cpu_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/cgroup/cgroup_cpu_test.go b/pkg/cgroup/cgroup_cpu_test.go index 22bc15530a3..06e4c5bb0b2 100644 --- a/pkg/cgroup/cgroup_cpu_test.go +++ b/pkg/cgroup/cgroup_cpu_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -//go:build linux && enable_flaky_tests +//go:build linux package cgroup @@ -89,7 +89,7 @@ func TestGetCgroupCPU(t *testing.T) { if checkKernelVersionNewerThan(t, 4, 7) { require.NoError(t, err, "linux version > v4.7 and err still happens") } else { - log.Info("the error is ignored because the kernel is too old") + log.Info("the 'no cpu controller detected' error is ignored because the kernel is too old") } } else { require.NoError(t, err) From f461fc9f3731aa7cf16618a74ad093e2e95f742e Mon Sep 17 00:00:00 2001 From: Sen Han <00hnes@gmail.com> Date: Wed, 15 Feb 2023 16:30:42 +0800 Subject: [PATCH 5/8] change log level from info into warn Signed-off-by: Sen Han <00hnes@gmail.com> --- pkg/cgroup/cgroup_cpu_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cgroup/cgroup_cpu_test.go b/pkg/cgroup/cgroup_cpu_test.go index 06e4c5bb0b2..977d6b35a40 100644 --- a/pkg/cgroup/cgroup_cpu_test.go +++ b/pkg/cgroup/cgroup_cpu_test.go @@ -89,7 +89,7 @@ func TestGetCgroupCPU(t *testing.T) { if checkKernelVersionNewerThan(t, 4, 7) { require.NoError(t, err, "linux version > v4.7 and err still happens") } else { - log.Info("the 'no cpu controller detected' error is ignored because the kernel is too old") + log.Warn("the 'no cpu controller detected' error is ignored because the kernel is too old") } } else { require.NoError(t, err) From dfdb98ad394112959b4b2be2e71ab0e2aa6ceb9c Mon Sep 17 00:00:00 2001 From: Sen Han <00hnes@gmail.com> Date: Wed, 15 Feb 2023 16:39:09 +0800 Subject: [PATCH 6/8] use log provided by testing pkg Signed-off-by: Sen Han <00hnes@gmail.com> --- pkg/cgroup/cgroup_cpu_test.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pkg/cgroup/cgroup_cpu_test.go b/pkg/cgroup/cgroup_cpu_test.go index 977d6b35a40..5eb0fd2e659 100644 --- a/pkg/cgroup/cgroup_cpu_test.go +++ b/pkg/cgroup/cgroup_cpu_test.go @@ -25,9 +25,7 @@ import ( "syscall" "testing" - "github.com/pingcap/log" "github.com/stretchr/testify/require" - "go.uber.org/zap" ) func checkKernelVersionNewerThan(t *testing.T, major, minor int) bool { @@ -42,14 +40,15 @@ func checkKernelVersionNewerThan(t *testing.T, major, minor int) bool { releaseBs = append(releaseBs, byte(v)) } releaseStr := string(releaseBs) - log.Info("kernel release string", zap.String("release-str", releaseStr)) + t.Log("kernel release string:", releaseStr) versionInfoRE := regexp.MustCompile(`[0-9]+\.[0-9]+\.[0-9]+`) kernelVerion := versionInfoRE.FindAllString(releaseStr, 1) require.Equal(t, 1, len(kernelVerion), fmt.Sprintf("release str is %s", releaseStr)) kernelVersionPartRE := regexp.MustCompile(`[0-9]+`) kernelVersionParts := kernelVersionPartRE.FindAllString(kernelVerion[0], -1) require.Equal(t, 3, len(kernelVersionParts), fmt.Sprintf("kernel verion str is %s", kernelVerion[0])) - log.Info("parsed kernel version parts", zap.String("major", kernelVersionParts[0]), zap.String("minor", kernelVersionParts[1]), zap.String("patch", kernelVersionParts[2])) + t.Logf("parsed kernel version parts: major %s, minor %s, patch %s", + kernelVersionParts[0], kernelVersionParts[1], kernelVersionParts[2]) mustConvInt := func(s string) int { i, err := strconv.Atoi(s) require.NoError(t, err, s) @@ -89,7 +88,7 @@ func TestGetCgroupCPU(t *testing.T) { if checkKernelVersionNewerThan(t, 4, 7) { require.NoError(t, err, "linux version > v4.7 and err still happens") } else { - log.Warn("the 'no cpu controller detected' error is ignored because the kernel is too old") + t.Log("the 'no cpu controller detected' error is ignored because the kernel is too old") } } else { require.NoError(t, err) From f1860703aa7ced331c8143ce244717cacb0e59a7 Mon Sep 17 00:00:00 2001 From: Sen Han <00hnes@gmail.com> Date: Thu, 16 Feb 2023 10:55:23 +0800 Subject: [PATCH 7/8] use err var instead of constant string Signed-off-by: Sen Han <00hnes@gmail.com> --- pkg/cgroup/cgroup_cpu_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cgroup/cgroup_cpu_test.go b/pkg/cgroup/cgroup_cpu_test.go index 5eb0fd2e659..f9d1cebfe78 100644 --- a/pkg/cgroup/cgroup_cpu_test.go +++ b/pkg/cgroup/cgroup_cpu_test.go @@ -88,7 +88,7 @@ func TestGetCgroupCPU(t *testing.T) { if checkKernelVersionNewerThan(t, 4, 7) { require.NoError(t, err, "linux version > v4.7 and err still happens") } else { - t.Log("the 'no cpu controller detected' error is ignored because the kernel is too old") + t.Logf("the error '%s' is ignored because the kernel is too old", err) } } else { require.NoError(t, err) From a746178d3788aa36e748686430d1339bede05053 Mon Sep 17 00:00:00 2001 From: Sen Han <00hnes@gmail.com> Date: Thu, 16 Feb 2023 12:05:40 +0800 Subject: [PATCH 8/8] fix Signed-off-by: Sen Han <00hnes@gmail.com> --- pkg/cgroup/cgroup_cpu_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pkg/cgroup/cgroup_cpu_test.go b/pkg/cgroup/cgroup_cpu_test.go index f9d1cebfe78..c0dbdfcd2b9 100644 --- a/pkg/cgroup/cgroup_cpu_test.go +++ b/pkg/cgroup/cgroup_cpu_test.go @@ -57,10 +57,8 @@ func checkKernelVersionNewerThan(t *testing.T, major, minor int) bool { versionNewerThanFlag := false if mustConvInt(kernelVersionParts[0]) > major { versionNewerThanFlag = true - } else { - if mustConvInt(kernelVersionParts[0]) == major && mustConvInt(kernelVersionParts[1]) > minor { - versionNewerThanFlag = true - } + } else if mustConvInt(kernelVersionParts[0]) == major && mustConvInt(kernelVersionParts[1]) > minor { + versionNewerThanFlag = true } return versionNewerThanFlag }