From 3953081de1c2746746667d0cab68ab1430b2a526 Mon Sep 17 00:00:00 2001 From: Injun Song Date: Mon, 21 Aug 2023 13:25:46 +0900 Subject: [PATCH] refactor(all): make cluster id flag common --- cmd/benchmark/test_command.go | 9 +++------ cmd/mrtool/mrtool.go | 3 ++- cmd/varlogadm/cli.go | 2 +- cmd/varlogadm/flags.go | 2 +- cmd/varlogcli/varlogcli.go | 4 ++-- cmd/varlogmr/flags.go | 7 +------ cmd/varlogmr/metadata_repository.go | 2 +- cmd/varlogsn/cli.go | 2 +- cmd/varlogsn/flags.go | 2 +- internal/admin/config.go | 4 ++-- internal/benchmark/config.go | 5 +++-- internal/flags/cluster.go | 18 ++++++++++++++++++ internal/flags/flags.go | 7 ------- internal/metarepos/config.go | 4 ++-- pkg/mrc/mrconnector/config.go | 4 ++-- pkg/types/types.go | 9 +++++++++ pkg/vflag/defaults.go | 1 - tests/ee/cluster/config.go | 5 +++-- 18 files changed, 52 insertions(+), 38 deletions(-) diff --git a/cmd/benchmark/test_command.go b/cmd/benchmark/test_command.go index da1f2f3f3..41a03db68 100644 --- a/cmd/benchmark/test_command.go +++ b/cmd/benchmark/test_command.go @@ -7,16 +7,13 @@ import ( "github.com/urfave/cli/v2" "github.com/kakao/varlog/internal/benchmark" + "github.com/kakao/varlog/internal/flags" "github.com/kakao/varlog/pkg/types" ) var ( - flagClusterID = &cli.StringFlag{ - Name: "cluster", - Usage: "Cluster ID", - Value: benchmark.DefaultClusterID.String(), - } - flagTarget = &cli.StringSliceFlag{ + flagClusterID = flags.ClusterID + flagTarget = &cli.StringSliceFlag{ Name: "target", Required: true, Usage: "The target of the benchmark load formatted by \"topic1:logstream1,topic2:logstream2,...\"", diff --git a/cmd/mrtool/mrtool.go b/cmd/mrtool/mrtool.go index 764011a4a..2e35f66a4 100644 --- a/cmd/mrtool/mrtool.go +++ b/cmd/mrtool/mrtool.go @@ -10,6 +10,7 @@ import ( "github.com/pkg/errors" "github.com/urfave/cli/v2" + "github.com/kakao/varlog/internal/flags" "github.com/kakao/varlog/pkg/mrc" "github.com/kakao/varlog/pkg/types" ) @@ -22,7 +23,7 @@ const ( flagAddress = "address" flagTimeout = "timeout" - defaultClusterID = types.ClusterID(1) + defaultClusterID = flags.DefaultClusterID defaultTimeout = time.Second ) diff --git a/cmd/varlogadm/cli.go b/cmd/varlogadm/cli.go index c9cfe7b95..ce9f70cba 100644 --- a/cmd/varlogadm/cli.go +++ b/cmd/varlogadm/cli.go @@ -34,7 +34,7 @@ func newStartCommand() *cli.Command { Usage: "start [flags]", Action: start, Flags: []cli.Flag{ - flagClusterID.StringFlag(false, types.ClusterID(1).String()), + flagClusterID, flagListen.StringFlag(false, admin.DefaultListenAddress), flagReplicationFactor, flagLogStreamGCTimeout.DurationFlag(false, admin.DefaultLogStreamGCTimeout), diff --git a/cmd/varlogadm/flags.go b/cmd/varlogadm/flags.go index d583b8552..19203ce34 100644 --- a/cmd/varlogadm/flags.go +++ b/cmd/varlogadm/flags.go @@ -10,7 +10,7 @@ import ( ) var ( - flagClusterID = flags.ClusterID() + flagClusterID = flags.ClusterID flagMetadataRepository = flags.MetadataRepositoryAddress() flagListen = flags.FlagDesc{ Name: "listen", diff --git a/cmd/varlogcli/varlogcli.go b/cmd/varlogcli/varlogcli.go index 50a7848ad..4ae72de8f 100644 --- a/cmd/varlogcli/varlogcli.go +++ b/cmd/varlogcli/varlogcli.go @@ -79,7 +79,7 @@ func commandAction(c *cli.Context) error { logStreamID types.LogStreamID ) mrAddrs = c.StringSlice(flags.MetadataRepositoryAddress().Name) - clusterID, err = types.ParseClusterID(c.String(flags.ClusterID().Name)) + clusterID, err = types.ParseClusterID(c.String(flags.ClusterID.Name)) if err != nil { return err } @@ -116,8 +116,8 @@ func commandAction(c *cli.Context) error { func commonFlags() []cli.Flag { return []cli.Flag{ + flags.ClusterID, flags.MetadataRepositoryAddress().StringSliceFlag(true, nil), - flags.ClusterID().StringFlag(false, types.ClusterID(1).String()), flags.TopicID().StringFlag(true, ""), flags.LogStreamID().StringFlag(false, ""), } diff --git a/cmd/varlogmr/flags.go b/cmd/varlogmr/flags.go index efa2879a2..9b1a6cf93 100644 --- a/cmd/varlogmr/flags.go +++ b/cmd/varlogmr/flags.go @@ -8,12 +8,7 @@ import ( ) var ( - flagClusterID = flags.FlagDesc{ - Name: "cluster-id", - Aliases: []string{"cid"}, - Usage: "cluster id", - Envs: []string{"CLUSTER_ID"}, - } + flagClusterID = flags.ClusterID flagRPCAddr = flags.FlagDesc{ Name: "rpc-address", diff --git a/cmd/varlogmr/metadata_repository.go b/cmd/varlogmr/metadata_repository.go index 1690888c4..10a062391 100644 --- a/cmd/varlogmr/metadata_repository.go +++ b/cmd/varlogmr/metadata_repository.go @@ -121,7 +121,7 @@ func initCLI() *cli.App { Usage: "start [flags]", Action: start, Flags: []cli.Flag{ - flagClusterID.StringFlag(false, metarepos.DefaultClusterID.String()), + flagClusterID, flagRPCAddr.StringFlag(false, metarepos.DefaultRPCBindAddress), flagRaftAddr.StringFlag(false, metarepos.DefaultRaftAddress), flagDebugAddr.StringFlag(false, metarepos.DefaultDebugAddress), diff --git a/cmd/varlogsn/cli.go b/cmd/varlogsn/cli.go index ff651d09b..9ce579441 100644 --- a/cmd/varlogsn/cli.go +++ b/cmd/varlogsn/cli.go @@ -32,7 +32,7 @@ func newStartCommand() *cli.Command { Aliases: []string{"s"}, Action: start, Flags: []cli.Flag{ - flagClusterID.StringFlag(false, types.ClusterID(1).String()), + flagClusterID, flagStorageNodeID.StringFlag(false, types.StorageNodeID(1).String()), flagListen.StringFlag(false, "127.0.0.1:9091"), flagAdvertise.StringFlag(false, ""), diff --git a/cmd/varlogsn/flags.go b/cmd/varlogsn/flags.go index 4cce28677..9bc5de29e 100644 --- a/cmd/varlogsn/flags.go +++ b/cmd/varlogsn/flags.go @@ -16,7 +16,7 @@ const ( ) var ( - flagClusterID = flags.ClusterID() + flagClusterID = flags.ClusterID flagStorageNodeID = flags.StorageNodeID() flagListen = flags.FlagDesc{ Name: "listen", diff --git a/internal/admin/config.go b/internal/admin/config.go index c6bfdcd16..bcf4c2828 100644 --- a/internal/admin/config.go +++ b/internal/admin/config.go @@ -16,7 +16,7 @@ import ( ) const ( - DefaultClusterID = types.ClusterID(1) + defaultClusterID = flags.DefaultClusterID DefaultListenAddress = "127.0.0.1:9090" DefaultReplicationFactor = flags.DefaultReplicationFactor DefaultLogStreamGCTimeout = 24 * time.Hour @@ -39,7 +39,7 @@ type config struct { func newConfig(opts []Option) (config, error) { cfg := config{ - cid: DefaultClusterID, + cid: defaultClusterID, listenAddress: DefaultListenAddress, replicationFactor: DefaultReplicationFactor, logStreamGCTimeout: DefaultLogStreamGCTimeout, diff --git a/internal/benchmark/config.go b/internal/benchmark/config.go index f7711d19e..f1238de13 100644 --- a/internal/benchmark/config.go +++ b/internal/benchmark/config.go @@ -4,11 +4,12 @@ import ( "errors" "time" + "github.com/kakao/varlog/internal/flags" "github.com/kakao/varlog/pkg/types" ) const ( - DefaultClusterID = types.ClusterID(1) + defaultClusterID = flags.DefaultClusterID DefaultMessageSize = 0 DefaultBatchSize = 1 DefaultConcurrency = 0 @@ -28,7 +29,7 @@ type config struct { func newConfig(opts []Option) (config, error) { cfg := config{ - cid: DefaultClusterID, + cid: defaultClusterID, duration: DefaultDuration, reportInterval: DefaultReportInterval, } diff --git a/internal/flags/cluster.go b/internal/flags/cluster.go index 2f0e1ce17..a18409d84 100644 --- a/internal/flags/cluster.go +++ b/internal/flags/cluster.go @@ -4,15 +4,33 @@ import ( "fmt" "github.com/urfave/cli/v2" + + "github.com/kakao/varlog/pkg/types" ) const ( CategoryCluster = "Cluster:" + DefaultClusterID = types.MinClusterID + DefaultReplicationFactor = 1 ) var ( + ClusterID = &cli.IntFlag{ + Name: "cluster-id", + Aliases: []string{"cluster", "cid"}, + Category: CategoryCluster, + EnvVars: []string{"CLUSTER_ID"}, + Value: int(DefaultClusterID), + Action: func(_ *cli.Context, value int) error { + if value < int(types.MinClusterID) || value > int(types.MaxClusterID) { + return fmt.Errorf("invalid value \"%d\" for flag --cluster-id", value) + } + return nil + }, + } + ReplicationFactor = &cli.IntFlag{ Name: "replication-factor", Category: CategoryCluster, diff --git a/internal/flags/flags.go b/internal/flags/flags.go index 984f994e7..7ca98e402 100644 --- a/internal/flags/flags.go +++ b/internal/flags/flags.go @@ -103,13 +103,6 @@ func MetadataRepositoryAddress() *FlagDesc { } } -func ClusterID() *FlagDesc { - return &FlagDesc{ - Name: "cluster-id", - Aliases: []string{"cluster", "cid"}, - } -} - func StorageNodeID() *FlagDesc { return &FlagDesc{ Name: "storage-node-id", diff --git a/internal/metarepos/config.go b/internal/metarepos/config.go index 3c73b58af..88f650353 100644 --- a/internal/metarepos/config.go +++ b/internal/metarepos/config.go @@ -16,7 +16,7 @@ import ( ) const ( - DefaultClusterID = types.ClusterID(1) + defaultClusterID = flags.DefaultClusterID DefaultRPCBindAddress = "0.0.0.0:9092" DefaultDebugAddress = "0.0.0.0:9099" DefaultRaftPort = 10000 @@ -99,7 +99,7 @@ func newConfig(opts []Option) (config, error) { raftTick: DefaultRaftTick, raftDir: DefaultRaftDir, }, - clusterID: DefaultClusterID, + clusterID: defaultClusterID, rpcAddr: DefaultRPCBindAddress, raftAddr: DefaultRaftAddress, debugAddr: DefaultDebugAddress, diff --git a/pkg/mrc/mrconnector/config.go b/pkg/mrc/mrconnector/config.go index ef0c47e2d..5ade3643a 100644 --- a/pkg/mrc/mrconnector/config.go +++ b/pkg/mrc/mrconnector/config.go @@ -6,13 +6,13 @@ import ( "github.com/pkg/errors" "go.uber.org/zap" + "github.com/kakao/varlog/internal/flags" "github.com/kakao/varlog/pkg/types" "github.com/kakao/varlog/pkg/verrors" - "github.com/kakao/varlog/pkg/vflag" ) const ( - defaultClusterID = vflag.DefaultClusterID + defaultClusterID = flags.DefaultClusterID defaultConnTimeout = 1 * time.Second defaultRPCTimeout = 1 * time.Second defaultInitCount = 10 diff --git a/pkg/types/types.go b/pkg/types/types.go index 35c18f467..c8e64e9e0 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -14,6 +14,11 @@ type ClusterID int32 var _ fmt.Stringer = (*ClusterID)(nil) +const ( + MinClusterID = ClusterID(1) + MaxClusterID = ClusterID(math.MaxInt32) +) + func ParseClusterID(s string) (ClusterID, error) { id, err := strconv.ParseInt(s, 10, 32) return ClusterID(id), err @@ -23,6 +28,10 @@ func (cid ClusterID) String() string { return strconv.FormatInt(int64(cid), 10) } +func (cid ClusterID) Invalid() bool { + return cid < MinClusterID +} + type StorageNodeID int32 const MinStorageNodeID = StorageNodeID(1) diff --git a/pkg/vflag/defaults.go b/pkg/vflag/defaults.go index 7db81c9ba..3e44e15df 100644 --- a/pkg/vflag/defaults.go +++ b/pkg/vflag/defaults.go @@ -3,7 +3,6 @@ package vflag import "github.com/kakao/varlog/pkg/types" const ( - DefaultClusterID = types.ClusterID(1) DefaultStorageNodeID = types.StorageNodeID(1) DefaultTelemetryCollector = "nop" ) diff --git a/tests/ee/cluster/config.go b/tests/ee/cluster/config.go index 76c8a8cdf..2c7e32bee 100644 --- a/tests/ee/cluster/config.go +++ b/tests/ee/cluster/config.go @@ -6,11 +6,12 @@ import ( "go.uber.org/zap" "go.uber.org/zap/zaptest" + "github.com/kakao/varlog/internal/flags" "github.com/kakao/varlog/pkg/types" ) const ( - DefaultClusterID = types.ClusterID(1) + defaultClusterID = flags.DefaultClusterID DefaultReplicationFactor = 3 DefaultNumMetaRepos = 3 ) @@ -25,7 +26,7 @@ type Config struct { func NewConfig(t *testing.T, opts ...Option) (Config, error) { cfg := Config{ - cid: DefaultClusterID, + cid: defaultClusterID, replicationFactor: DefaultReplicationFactor, numMetaRepos: DefaultNumMetaRepos, logger: zaptest.NewLogger(t),