Skip to content

Commit

Permalink
br: support to stop gc memory limit tuner when restore (#51082)
Browse files Browse the repository at this point in the history
close #51078
  • Loading branch information
Leavrth authored Feb 29, 2024
1 parent 44b1598 commit 8e7658d
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 6 deletions.
1 change: 1 addition & 0 deletions br/cmd/br/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ go_library(
"//pkg/parser/model",
"//pkg/session",
"//pkg/util",
"//pkg/util/gctuner",
"//pkg/util/logutil",
"//pkg/util/memory",
"//pkg/util/metricsutil",
Expand Down
5 changes: 5 additions & 0 deletions br/cmd/br/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/pingcap/tidb/br/pkg/version/build"
"github.com/pingcap/tidb/pkg/config"
"github.com/pingcap/tidb/pkg/session"
"github.com/pingcap/tidb/pkg/util/gctuner"
"github.com/pingcap/tidb/pkg/util/metricsutil"
"github.com/spf13/cobra"
"go.uber.org/zap"
Expand Down Expand Up @@ -48,6 +49,10 @@ func runBackupCommand(command *cobra.Command, cmdName string) error {
// No need to cache the coproceesor result
config.GetGlobalConfig().TiKVClient.CoprCache.CapacityMB = 0

// Disable the memory limit tuner. That's because the server memory is get from TiDB node instead of BR node.
gctuner.GlobalMemoryLimitTuner.DisableAdjustMemoryLimit()
defer gctuner.GlobalMemoryLimitTuner.EnableAdjustMemoryLimit()

if err := task.RunBackup(ctx, tidbGlue, cmdName, &cfg); err != nil {
log.Error("failed to backup", zap.Error(err))
return errors.Trace(err)
Expand Down
5 changes: 5 additions & 0 deletions br/cmd/br/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/pingcap/tidb/br/pkg/version/build"
"github.com/pingcap/tidb/pkg/config"
"github.com/pingcap/tidb/pkg/session"
"github.com/pingcap/tidb/pkg/util/gctuner"
"github.com/pingcap/tidb/pkg/util/metricsutil"
"github.com/spf13/cobra"
"go.uber.org/zap"
Expand Down Expand Up @@ -67,6 +68,10 @@ func runRestoreCommand(command *cobra.Command, cmdName string) error {
// No need to cache the coproceesor result
config.GetGlobalConfig().TiKVClient.CoprCache.CapacityMB = 0

// Disable the memory limit tuner. That's because the server memory is get from TiDB node instead of BR node.
gctuner.GlobalMemoryLimitTuner.DisableAdjustMemoryLimit()
defer gctuner.GlobalMemoryLimitTuner.EnableAdjustMemoryLimit()

if err := task.RunRestore(GetDefaultContext(), tidbGlue, cmdName, &cfg); err != nil {
log.Error("failed to restore", zap.Error(err))
printWorkaroundOnFullRestoreError(command, err)
Expand Down
17 changes: 12 additions & 5 deletions br/pkg/metautil/statsfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ func newStatsWriter(
}
}

// flush temporary and clear []byte to make it garbage collected as soon as possible
func (s *StatsWriter) flushTemporary() ([]byte, error) {
defer s.clearTemporary()
return proto.Marshal(s.statsFile)
}

func (s *StatsWriter) clearTemporary() {
// clear the temporary variables
s.totalSize = 0
Expand All @@ -81,7 +87,7 @@ func (s *StatsWriter) clearTemporary() {

func (s *StatsWriter) writeStatsFileAndClear(ctx context.Context, physicalID int64) error {
fileName := getStatsFileName(physicalID)
content, err := proto.Marshal(s.statsFile)
content, err := s.flushTemporary()
if err != nil {
return errors.Trace(err)
}
Expand All @@ -92,7 +98,7 @@ func (s *StatsWriter) writeStatsFileAndClear(ctx context.Context, physicalID int
}

checksum := sha256.Sum256(content)

sizeOri := uint64(len(content))
encryptedContent, iv, err := Encrypt(content, s.cipher)
if err != nil {
return errors.Trace(err)
Expand All @@ -106,11 +112,9 @@ func (s *StatsWriter) writeStatsFileAndClear(ctx context.Context, physicalID int
Name: fileName,
Sha256: checksum[:],
SizeEnc: uint64(len(encryptedContent)),
SizeOri: uint64(len(content)),
SizeOri: sizeOri,
CipherIv: iv,
})

s.clearTemporary()
return nil
}

Expand Down Expand Up @@ -225,6 +229,9 @@ func downloadStats(
if err := json.Unmarshal(block.JsonTable, jsonTable); err != nil {
return errors.Trace(err)
}
// reset the block.JsonTable to nil to make it garbage collected as soon as possible
block.JsonTable = nil

select {
case <-ectx.Done():
return nil
Expand Down
21 changes: 20 additions & 1 deletion pkg/util/gctuner/memory_limit_tuner.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ type memoryLimitTuner struct {
serverMemLimitBeforeAdjust atomicutil.Uint64
percentageBeforeAdjust atomicutil.Float64
nextGCTriggeredByMemoryLimit atomicutil.Bool

// The flag to disable memory limit adjust. There might be many tasks need to activate it in future,
// so it is integer type.
adjustDisabled atomicutil.Int64
}

// fallbackPercentage indicates the fallback memory limit percentage when turning.
Expand All @@ -55,6 +59,18 @@ func WaitMemoryLimitTunerExitInTest() {
}
}

// DisableAdjustMemoryLimit makes memoryLimitTuner directly return `initGOMemoryLimitValue` when function `calcMemoryLimit` is called.
func (t *memoryLimitTuner) DisableAdjustMemoryLimit() {
t.adjustDisabled.Add(1)
debug.SetMemoryLimit(initGOMemoryLimitValue)
}

// EnableAdjustMemoryLimit makes memoryLimitTuner return an adjusted memory limit when function `calcMemoryLimit` is called.
func (t *memoryLimitTuner) EnableAdjustMemoryLimit() {
t.adjustDisabled.Add(-1)
t.UpdateMemoryLimit()
}

// tuning check the memory nextGC and judge whether this GC is trigger by memory limit.
// Go runtime ensure that it will be called serially.
func (t *memoryLimitTuner) tuning() {
Expand Down Expand Up @@ -155,7 +171,10 @@ func (t *memoryLimitTuner) UpdateMemoryLimit() {
debug.SetMemoryLimit(memoryLimit)
}

func (*memoryLimitTuner) calcMemoryLimit(percentage float64) int64 {
func (t *memoryLimitTuner) calcMemoryLimit(percentage float64) int64 {
if t.adjustDisabled.Load() > 0 {
return initGOMemoryLimitValue
}
memoryLimit := int64(float64(memory.ServerMemoryLimit.Load()) * percentage) // `tidb_server_memory_limit` * `tidb_server_memory_limit_gc_trigger`
if memoryLimit == 0 {
memoryLimit = math.MaxInt64
Expand Down
11 changes: 11 additions & 0 deletions pkg/util/gctuner/memory_limit_tuner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,14 @@ func TestIssue48741(t *testing.T) {
waitingTunningFinishFn()
checkIfMemoryLimitIsModified()
}

func TestSetMemoryLimit(t *testing.T) {
GlobalMemoryLimitTuner.DisableAdjustMemoryLimit()
memory.ServerMemoryLimit.Store(1 << 30) // 1GB
GlobalMemoryLimitTuner.SetPercentage(0.8) // 1GB * 80% = 800MB
GlobalMemoryLimitTuner.UpdateMemoryLimit()
require.Equal(t, initGOMemoryLimitValue, debug.SetMemoryLimit(-1))
GlobalMemoryLimitTuner.EnableAdjustMemoryLimit()
GlobalMemoryLimitTuner.UpdateMemoryLimit()
require.Equal(t, int64(1<<30*80/100), debug.SetMemoryLimit(-1))
}

0 comments on commit 8e7658d

Please sign in to comment.