This repository has been archived by the owner on Nov 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 188
/
strategy_space.go
90 lines (73 loc) · 2.58 KB
/
strategy_space.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2019 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
// limitations under the License.
package purger
import (
"fmt"
"strings"
"github.com/pingcap/errors"
"github.com/siddontang/go/sync2"
"github.com/pingcap/dm/pkg/streamer"
"github.com/pingcap/dm/pkg/utils"
)
// spaceArgs represents args needed by spaceStrategy
type spaceArgs struct {
relayBaseDir string
remainSpace int64 // if remain space (GB) in @RelayBaseDir less than this, then it can be purged
uuids []string
activeRelayLog *streamer.RelayLogInfo // earliest active relay log info
}
func (sa *spaceArgs) SetActiveRelayLog(active *streamer.RelayLogInfo) {
sa.activeRelayLog = active
}
func (sa *spaceArgs) String() string {
return fmt.Sprintf("(RelayBaseDir: %s, AllowMinRemainSpace: %dGB, UUIDs: %s, ActiveRelayLog: %s)",
sa.relayBaseDir, sa.remainSpace, strings.Join(sa.uuids, ";"), sa.activeRelayLog)
}
// spaceStrategy represents a relay purge strategy by remain space in dm-worker node
type spaceStrategy struct {
purging sync2.AtomicInt32
}
func newSpaceStrategy() PurgeStrategy {
return &spaceStrategy{}
}
func (s *spaceStrategy) Check(args interface{}) (bool, error) {
sa, ok := args.(*spaceArgs)
if !ok {
return false, errors.NotValidf("args (%T) %+v", args, args)
}
storageSize, err := utils.GetStorageSize(sa.relayBaseDir)
if err != nil {
return false, errors.Annotatef(err, "get storage size for directory %s", sa.relayBaseDir)
}
requiredBytes := uint64(sa.remainSpace) * 1024 * 1024 * 1024
return storageSize.Available < requiredBytes, nil
}
func (s *spaceStrategy) Do(args interface{}) error {
if !s.purging.CompareAndSwap(0, 1) {
return ErrSelfPurging
}
defer s.purging.Set(0)
sa, ok := args.(*spaceArgs)
if !ok {
return errors.NotValidf("args (%T) %+v", args, args)
}
// NOTE: we purge all inactive relay log files when available space less than @remainSpace
// maybe we can refine this to purge only part of this files every time
return errors.Trace(purgeRelayFilesBeforeFile(sa.relayBaseDir, sa.uuids, sa.activeRelayLog))
}
func (s *spaceStrategy) Purging() bool {
return s.purging.Get() > 0
}
func (s *spaceStrategy) Type() strategyType {
return strategySpace
}