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

support go time package format gc time #996

Merged
merged 11 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"fmt"
"net"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -301,3 +302,16 @@ func WaitUntilTimeout(name string, fn func(), timeout time.Duration) {
case <-exited:
}
}

// ParseGCDuration parses gc durations. The default unit is day.
func ParseGCDuration(gc string) (time.Duration, error) {
d, err := strconv.ParseUint(gc, 10, 64)
if err == nil {
return time.Duration(d) * 24 * time.Hour, nil
}
gcDuration, err := time.ParseDuration(gc)
if err != nil {
return 0, errors.Errorf("unsupported gc time %s, please use 7 or 8h(max unit is hour) format gc time. err: %s", gc, err)
july2993 marked this conversation as resolved.
Show resolved Hide resolved
}
return gcDuration, nil
}
18 changes: 18 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,24 @@ func (s *utilSuite) TestStdLogger(c *C) {
c.Assert(entrys[2].Message, Matches, ".*hola:Goodbye!.*")
}

func (s *utilSuite) TestParseGCDuration(c *C) {
gc := "7"
expectDuration := 7 * 24 * time.Hour
duration, err := ParseGCDuration(gc)
c.Assert(err, IsNil)
c.Assert(duration, Equals, expectDuration)

gc = "30m"
expectDuration = 30 * time.Minute
duration, err = ParseGCDuration(gc)
c.Assert(err, IsNil)
c.Assert(duration, Equals, expectDuration)

gc = "7d"
_, err = ParseGCDuration(gc)
c.Assert(err, ErrorMatches, `unsupported gc time 7d, please use 7 or 8h\(max unit is hour\) format gc time.*`)
july2993 marked this conversation as resolved.
Show resolved Hide resolved
}

type getAddrIPSuite struct{}

var _ = Suite(&getAddrIPSuite{})
Expand Down
17 changes: 11 additions & 6 deletions pump/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const (
defaultListenAddr = "127.0.0.1:8250"
defautMaxMsgSize = math.MaxInt32 // max grpc message size
defaultHeartbeatInterval = 2
defaultGC = 7
defaultGC = "7"
defaultDataDir = "data.pump"

// default interval time to generate fake binlog, the unit is second
Expand All @@ -65,8 +65,8 @@ type Config struct {
EtcdDialTimeout time.Duration
DataDir string `toml:"data-dir" json:"data-dir"`
HeartbeatInterval int `toml:"heartbeat-interval" json:"heartbeat-interval"`
// pump only stores binlog events whose ts >= current time - GC(day)
GC int `toml:"gc" json:"gc"`
july2993 marked this conversation as resolved.
Show resolved Hide resolved
// pump only stores binlog events whose ts >= current time - GC Time. The default unit is day
GC string `toml:"gc" json:"gc"`
LogFile string `toml:"log-file" json:"log-file"`
Security security.Config `toml:"security" json:"security"`

Expand All @@ -76,6 +76,7 @@ type Config struct {
MetricsInterval int
configFile string
printVersion bool
GCDuration time.Duration `tome:"-" json:"-"`
tls *tls.Config
Storage storage.Config `toml:"storage" json:"storage"`
}
Expand All @@ -100,7 +101,7 @@ func NewConfig() *Config {
fs.StringVar(&cfg.EtcdURLs, "pd-urls", defaultEtcdURLs, "a comma separated list of the PD endpoints")
fs.StringVar(&cfg.DataDir, "data-dir", "", "the path to store binlog data")
fs.IntVar(&cfg.HeartbeatInterval, "heartbeat-interval", defaultHeartbeatInterval, "number of seconds between heartbeat ticks")
fs.IntVar(&cfg.GC, "gc", defaultGC, "recycle binlog files older than gc days")
fs.StringVar(&cfg.GC, "gc", defaultGC, "recycle binlog files older than gc time. default unit is day. also accept 8h format time(max unit is hour)")
fs.StringVar(&cfg.LogLevel, "L", "info", "log level: debug, info, warn, error, fatal")
fs.StringVar(&cfg.MetricsAddr, "metrics-addr", "", "prometheus pushgateway address, leaves it empty will disable prometheus push")
fs.IntVar(&cfg.MetricsInterval, "metrics-interval", 15, "prometheus client push interval in second, set \"0\" to disable prometheus push")
Expand Down Expand Up @@ -176,6 +177,10 @@ func (cfg *Config) Parse(arguments []string) error {
util.AdjustString(&cfg.DataDir, defaultDataDir)
util.AdjustInt(&cfg.HeartbeatInterval, defaultHeartbeatInterval)

cfg.GCDuration, err = util.ParseGCDuration(cfg.GC)
if err != nil {
return err
}
return cfg.validate()
}

Expand All @@ -186,8 +191,8 @@ func (cfg *Config) configFromFile(path string) error {
// validate checks whether the configuration is valid
func (cfg *Config) validate() error {
// check GC
if cfg.GC <= 0 {
return errors.Errorf("GC is %d, must bigger than 0", cfg.GC)
if cfg.GCDuration <= 0 {
return errors.Errorf("GC is %s, must bigger than 0", cfg.GC)
}

// check ListenAddr
Expand Down
3 changes: 2 additions & 1 deletion pump/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"io/ioutil"
"os"
"path"
"time"

"github.com/BurntSushi/toml"
. "github.com/pingcap/check"
Expand All @@ -29,7 +30,7 @@ type testConfigSuite struct{}

func (s *testConfigSuite) TestValidate(c *C) {
cfg := Config{}
cfg.GC = 1
cfg.GCDuration = 24 * time.Hour
cfg.ListenAddr = "http://:8250"
cfg.EtcdURLs = "http://192.168.10.23:7777"

Expand Down
2 changes: 1 addition & 1 deletion pump/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func NewServer(cfg *Config) (*Server, error) {
cancel: cancel,
metrics: metrics,
tiStore: tiStore,
gcDuration: time.Duration(cfg.GC) * 24 * time.Hour,
gcDuration: cfg.GCDuration,
pdCli: pdCli,
cfg: cfg,
triggerGC: make(chan time.Time),
Expand Down
2 changes: 1 addition & 1 deletion pump/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ func (s *startServerSuite) TestStartPumpServer(c *C) {
ctx: ctx,
cancel: cancel,
tiStore: nil,
gcDuration: time.Duration(cfg.GC) * 24 * time.Hour,
gcDuration: 24 * time.Hour,
pdCli: nil,
cfg: cfg,
triggerGC: make(chan time.Time),
Expand Down