Skip to content

Commit

Permalink
util: add test for old version cgroup (#41976)
Browse files Browse the repository at this point in the history
close #41808
  • Loading branch information
hawkingrei authored Mar 9, 2023
1 parent cae1f70 commit 0212c50
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 10 deletions.
2 changes: 1 addition & 1 deletion resourcemanager/util/mock_gpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (*MockGPool) Tune(_ int) {

// LastTunerTs is only for test
func (*MockGPool) LastTunerTs() time.Time {
panic("implement me")
return time.Now().Add(-1 * 10 * time.Second)
}

// MaxInFlight is only for test
Expand Down
46 changes: 45 additions & 1 deletion util/cgroup/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,51 @@ go_library(
"@com_github_pingcap_errors//:errors",
"@com_github_pingcap_log//:log",
"@org_uber_go_zap//:zap",
],
] + select({
"@io_bazel_rules_go//go/platform:aix": [
"@com_github_pingcap_failpoint//:failpoint",
],
"@io_bazel_rules_go//go/platform:android": [
"@com_github_pingcap_failpoint//:failpoint",
],
"@io_bazel_rules_go//go/platform:darwin": [
"@com_github_pingcap_failpoint//:failpoint",
],
"@io_bazel_rules_go//go/platform:dragonfly": [
"@com_github_pingcap_failpoint//:failpoint",
],
"@io_bazel_rules_go//go/platform:freebsd": [
"@com_github_pingcap_failpoint//:failpoint",
],
"@io_bazel_rules_go//go/platform:illumos": [
"@com_github_pingcap_failpoint//:failpoint",
],
"@io_bazel_rules_go//go/platform:ios": [
"@com_github_pingcap_failpoint//:failpoint",
],
"@io_bazel_rules_go//go/platform:js": [
"@com_github_pingcap_failpoint//:failpoint",
],
"@io_bazel_rules_go//go/platform:linux": [
"@com_github_pingcap_failpoint//:failpoint",
],
"@io_bazel_rules_go//go/platform:netbsd": [
"@com_github_pingcap_failpoint//:failpoint",
],
"@io_bazel_rules_go//go/platform:openbsd": [
"@com_github_pingcap_failpoint//:failpoint",
],
"@io_bazel_rules_go//go/platform:plan9": [
"@com_github_pingcap_failpoint//:failpoint",
],
"@io_bazel_rules_go//go/platform:solaris": [
"@com_github_pingcap_failpoint//:failpoint",
],
"@io_bazel_rules_go//go/platform:windows": [
"@com_github_pingcap_failpoint//:failpoint",
],
"//conditions:default": [],
}),
)

go_test(
Expand Down
11 changes: 11 additions & 0 deletions util/cgroup/cgroup_cpu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,22 @@ import (
"os"
"runtime"
"strings"

"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
)

// GetCgroupCPU returns the CPU usage and quota for the current cgroup.
func GetCgroupCPU() (CPUUsage, error) {
failpoint.Inject("GetCgroupCPUErr", func(val failpoint.Value) {
//nolint:forcetypeassert
if val.(bool) {
var cpuUsage CPUUsage
failpoint.Return(cpuUsage, errors.Errorf("mockAddBatchDDLJobsErr"))
}
})
cpuusage, err := getCgroupCPU("/")

cpuusage.NumCPU = runtime.NumCPU()
return cpuusage, err
}
Expand Down
9 changes: 9 additions & 0 deletions util/cgroup/cgroup_cpu_unsupport.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,20 @@ package cgroup

import (
"runtime"

"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
)

// GetCgroupCPU returns the CPU usage and quota for the current cgroup.
func GetCgroupCPU() (CPUUsage, error) {
var cpuUsage CPUUsage
failpoint.Inject("GetCgroupCPUErr", func(val failpoint.Value) {
//nolint:forcetypeassert
if val.(bool) {
failpoint.Return(cpuUsage, errors.Errorf("mockAddBatchDDLJobsErr"))
}
})
cpuUsage.NumCPU = runtime.NumCPU()
return cpuUsage, nil
}
Expand Down
17 changes: 14 additions & 3 deletions util/cpu/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,20 @@ go_library(
go_test(
name = "cpu_test",
timeout = "short",
srcs = ["cpu_test.go"],
embed = [":cpu"],
srcs = [
"cpu_test.go",
"main_test.go",
],
flaky = True,
race = "on",
deps = ["@com_github_stretchr_testify//require"],
shard_count = 2,
deps = [
":cpu",
"//resourcemanager/scheduler",
"//resourcemanager/util",
"//testkit/testsetup",
"@com_github_pingcap_failpoint//:failpoint",
"@com_github_stretchr_testify//require",
"@org_uber_go_goleak//:goleak",
],
)
50 changes: 45 additions & 5 deletions util/cpu/cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cpu
package cpu_test

import (
"runtime"
"sync"
"testing"
"time"

"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/resourcemanager/scheduler"
"github.com/pingcap/tidb/resourcemanager/util"
"github.com/pingcap/tidb/util/cpu"
"github.com/stretchr/testify/require"
)

func TestCPUValue(t *testing.T) {
observer := NewCPUObserver()
observer := cpu.NewCPUObserver()
exit := make(chan struct{})
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
Expand All @@ -44,13 +48,49 @@ func TestCPUValue(t *testing.T) {
observer.Start()
for n := 0; n < 10; n++ {
time.Sleep(1 * time.Second)
value, unsupported := GetCPUUsage()
value, unsupported := cpu.GetCPUUsage()
require.False(t, unsupported)
require.GreaterOrEqual(t, value, 0.0)
require.Greater(t, value, 0.0)
require.Less(t, value, 1.0)
}

observer.Stop()
close(exit)
wg.Wait()
}

func TestFailpointCPUValue(t *testing.T) {
failpoint.Enable("github.com/pingcap/tidb/util/cgroup/GetCgroupCPUErr", "return(true)")
defer func() {
failpoint.Disable("github.com/pingcap/tidb/util/cgroup/GetCgroupCPUErr")
}()
observer := cpu.NewCPUObserver()
exit := make(chan struct{})
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-exit:
return
default:
runtime.Gosched()
}
}
}()
}
observer.Start()
for n := 0; n < 10; n++ {
time.Sleep(1 * time.Second)
value, unsupported := cpu.GetCPUUsage()
require.True(t, unsupported)
require.Equal(t, value, 0.0)
}
sch := scheduler.CPUScheduler{}
require.Equal(t, scheduler.Hold, sch.Tune(util.UNKNOWN, util.NewMockGPool("test")))
// we do not stop the observer, because we inject the fail-point and the observer will not start.
// if this test case happen goleak, it must have a bug.
close(exit)
wg.Wait()
}
33 changes: 33 additions & 0 deletions util/cpu/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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 cpu_test

import (
"testing"

"github.com/pingcap/tidb/testkit/testsetup"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
opts := []goleak.Option{
goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"),
goleak.IgnoreTopFunction("github.com/lestrrat-go/httprc.runFetchWorker"),
goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"),
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
}
testsetup.SetupForCommonTest()
goleak.VerifyTestMain(m, opts...)
}

0 comments on commit 0212c50

Please sign in to comment.