Skip to content

Commit

Permalink
refactor(all): make cluster id flag common (#567)
Browse files Browse the repository at this point in the history
  • Loading branch information
ijsong authored Oct 4, 2023
2 parents ac89e40 + 3953081 commit 42f563b
Show file tree
Hide file tree
Showing 18 changed files with 52 additions and 38 deletions.
9 changes: 3 additions & 6 deletions cmd/benchmark/test_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,...<topic_id:logstream_id>\"",
Expand Down
3 changes: 2 additions & 1 deletion cmd/mrtool/mrtool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -22,7 +23,7 @@ const (
flagAddress = "address"
flagTimeout = "timeout"

defaultClusterID = types.ClusterID(1)
defaultClusterID = flags.DefaultClusterID
defaultTimeout = time.Second
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/varlogadm/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion cmd/varlogadm/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

var (
flagClusterID = flags.ClusterID()
flagClusterID = flags.ClusterID
flagMetadataRepository = flags.MetadataRepositoryAddress()
flagListen = flags.FlagDesc{
Name: "listen",
Expand Down
4 changes: 2 additions & 2 deletions cmd/varlogcli/varlogcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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, ""),
}
Expand Down
7 changes: 1 addition & 6 deletions cmd/varlogmr/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion cmd/varlogmr/metadata_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion cmd/varlogsn/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ""),
Expand Down
2 changes: 1 addition & 1 deletion cmd/varlogsn/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
)

var (
flagClusterID = flags.ClusterID()
flagClusterID = flags.ClusterID
flagStorageNodeID = flags.StorageNodeID()
flagListen = flags.FlagDesc{
Name: "listen",
Expand Down
4 changes: 2 additions & 2 deletions internal/admin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions internal/benchmark/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,7 +29,7 @@ type config struct {

func newConfig(opts []Option) (config, error) {
cfg := config{
cid: DefaultClusterID,
cid: defaultClusterID,
duration: DefaultDuration,
reportInterval: DefaultReportInterval,
}
Expand Down
18 changes: 18 additions & 0 deletions internal/flags/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 0 additions & 7 deletions internal/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions internal/metarepos/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -99,7 +99,7 @@ func newConfig(opts []Option) (config, error) {
raftTick: DefaultRaftTick,
raftDir: DefaultRaftDir,
},
clusterID: DefaultClusterID,
clusterID: defaultClusterID,
rpcAddr: DefaultRPCBindAddress,
raftAddr: DefaultRaftAddress,
debugAddr: DefaultDebugAddress,
Expand Down
4 changes: 2 additions & 2 deletions pkg/mrc/mrconnector/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
1 change: 0 additions & 1 deletion pkg/vflag/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package vflag
import "github.com/kakao/varlog/pkg/types"

const (
DefaultClusterID = types.ClusterID(1)
DefaultStorageNodeID = types.StorageNodeID(1)
DefaultTelemetryCollector = "nop"
)
5 changes: 3 additions & 2 deletions tests/ee/cluster/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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),
Expand Down

0 comments on commit 42f563b

Please sign in to comment.