Skip to content
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

cdc/sorter: make unified sorter cgroup aware #3436

Merged
merged 11 commits into from
Nov 12, 2021
21 changes: 11 additions & 10 deletions cdc/sorter/unified/backend_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"time"
"unsafe"

"github.com/mackerelio/go-osstat/memory"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/log"
Expand All @@ -34,6 +33,7 @@ import (
cerrors "github.com/pingcap/ticdc/pkg/errors"
"github.com/pingcap/ticdc/pkg/filelock"
"github.com/pingcap/ticdc/pkg/util"
"github.com/pingcap/tidb/util/memory"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -99,6 +99,13 @@ func newBackEndPool(dir string, captureAddr string) (*backEndPool, error) {
metricSorterOnDiskDataSizeGauge := sorter.OnDiskDataSizeGauge.WithLabelValues(captureAddr, id)
metricSorterOpenFileCountGauge := sorter.OpenFileCountGauge.WithLabelValues(captureAddr, id)

// TODO: The underlaying implementation only recognizes cgroups set by
// containers, we need to support cgroups set by systemd or manually.
overvenus marked this conversation as resolved.
Show resolved Hide resolved
// See https://github.com/pingcap/tidb/issues/22132
totalMemory, err := memory.MemTotal()
if err != nil {
log.Panic("read memory stat failed", zap.Error(err))
}
for {
select {
case <-ret.cancelCh:
Expand All @@ -112,14 +119,8 @@ func newBackEndPool(dir string, captureAddr string) (*backEndPool, error) {
metricSorterOpenFileCountGauge.Set(float64(atomic.LoadInt64(&openFDCount)))

// update memPressure
m, err := memory.Get()

failpoint.Inject("getMemoryPressureFails", func() {
m = nil
err = errors.New("injected get memory pressure failure")
})

if err != nil {
usedMemory, err := memory.MemUsed()
if err != nil || totalMemory == 0 {
failpoint.Inject("sorterDebug", func() {
log.Panic("unified sorter: getting system memory usage failed", zap.Error(err))
})
Expand All @@ -130,7 +131,7 @@ func newBackEndPool(dir string, captureAddr string) (*backEndPool, error) {
// encountered, we can fail gracefully.
atomic.StoreInt32(&ret.memPressure, 100)
} else {
memPressure := m.Used * 100 / m.Total
memPressure := usedMemory * 100 / totalMemory
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure the totalMemory not equal zero?

atomic.StoreInt32(&ret.memPressure, int32(memPressure))
}

Expand Down
11 changes: 7 additions & 4 deletions cdc/sorter/unified/backend_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/pingcap/ticdc/pkg/config"
"github.com/pingcap/ticdc/pkg/filelock"
"github.com/pingcap/ticdc/pkg/util/testleak"
"github.com/pingcap/tidb/util/memory"
)

type backendPoolSuite struct{}
Expand Down Expand Up @@ -325,9 +326,11 @@ func (s *backendPoolSuite) TestCleanUpStaleLockNoPermission(c *check.C) {
func (s *backendPoolSuite) TestGetMemoryPressureFailure(c *check.C) {
defer testleak.AfterTest(c)()

err := failpoint.Enable("github.com/pingcap/ticdc/cdc/sorter/unified/getMemoryPressureFails", "return(true)")
c.Assert(err, check.IsNil)
defer failpoint.Disable("github.com/pingcap/ticdc/cdc/sorter/unified/getMemoryPressureFails") //nolint:errcheck
origin := memory.MemTotal
defer func() {
memory.MemTotal = origin
}()
memory.MemTotal = func() (uint64, error) { return 0, nil }

dir := c.MkDir()
backEndPool, err := newBackEndPool(dir, "")
Expand All @@ -336,7 +339,7 @@ func (s *backendPoolSuite) TestGetMemoryPressureFailure(c *check.C) {
defer backEndPool.terminate()

after := time.After(time.Second * 20)
tick := time.Tick(time.Second * 1)
tick := time.Tick(time.Millisecond * 100)
for {
select {
case <-after:
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ require (
github.com/labstack/echo/v4 v4.6.1
github.com/lib/pq v1.3.0 // indirect
github.com/linkedin/goavro/v2 v2.9.8
github.com/mackerelio/go-osstat v0.1.0
github.com/mattn/go-colorable v0.1.11 // indirect
github.com/mattn/go-shellwords v1.0.3
github.com/mattn/go-sqlite3 v2.0.2+incompatible // indirect
Expand Down
5 changes: 1 addition & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,6 @@ github.com/lib/pq v1.3.0 h1:/qkRGz8zljWiDcFvgpwUpwIAPu3r07TDvs3Rws+o/pU=
github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/linkedin/goavro/v2 v2.9.8 h1:jN50elxBsGBDGVDEKqUlDuU1cFwJ11K/yrJCBMe/7Wg=
github.com/linkedin/goavro/v2 v2.9.8/go.mod h1:UgQUb2N/pmueQYH9bfqFioWxzYCZXSfF8Jw03O5sjqA=
github.com/mackerelio/go-osstat v0.1.0 h1:e57QHeHob8kKJ5FhcXGdzx5O6Ktuc5RHMDIkeqhgkFA=
github.com/mackerelio/go-osstat v0.1.0/go.mod h1:1K3NeYLhMHPvzUu+ePYXtoB58wkaRpxZsGClZBJyIFw=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
Expand Down Expand Up @@ -1227,7 +1225,6 @@ golang.org/x/sys v0.0.0-20181228144115-9a3f9b0469bb/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190410235845-0ad05ae3009d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down Expand Up @@ -1267,8 +1264,8 @@ golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200905004654-be1d3432aa8f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down