From 39d0180a1e0b8d77be722c3e9b191209d6b45a29 Mon Sep 17 00:00:00 2001 From: sh2 Date: Thu, 3 Aug 2023 15:57:16 +0800 Subject: [PATCH] refactor: aggregate some commonly used dirs Signed-off-by: sh2 --- pkg/deployer/baremetal/component/cluster.go | 18 +++++++++----- pkg/deployer/baremetal/component/datanode.go | 25 ++++++++------------ pkg/deployer/baremetal/component/etcd.go | 25 ++++++++------------ pkg/deployer/baremetal/component/frontend.go | 17 ++++++------- pkg/deployer/baremetal/component/metasrv.go | 23 ++++++++---------- pkg/deployer/baremetal/deployer.go | 7 +++++- 6 files changed, 55 insertions(+), 60 deletions(-) diff --git a/pkg/deployer/baremetal/component/cluster.go b/pkg/deployer/baremetal/component/cluster.go index 5029bbd3..01c388dc 100644 --- a/pkg/deployer/baremetal/component/cluster.go +++ b/pkg/deployer/baremetal/component/cluster.go @@ -28,6 +28,13 @@ import ( "github.com/GreptimeTeam/gtctl/pkg/logger" ) +// WorkDirs include all the dirs used in bare-metal mode. +type WorkDirs struct { + DataDir string + LogsDir string + PidsDir string +} + // BareMetalCluster describes all the components need to be deployed under bare-metal mode. type BareMetalCluster struct { MetaSrv BareMetalClusterComponent @@ -51,13 +58,12 @@ type BareMetalClusterComponent interface { Delete(ctx context.Context) error } -func NewGreptimeDBCluster(config *config.Cluster, dataDir, logsDir, pidsDir string, - wg *sync.WaitGroup, logger logger.Logger) *BareMetalCluster { +func NewGreptimeDBCluster(config *config.Cluster, workDirs WorkDirs, wg *sync.WaitGroup, logger logger.Logger) *BareMetalCluster { return &BareMetalCluster{ - MetaSrv: newMetaSrv(config.MetaSrv, logsDir, pidsDir, wg, logger), - Datanode: newDataNodes(config.Datanode, config.MetaSrv.ServerAddr, dataDir, logsDir, pidsDir, wg, logger), - Frontend: newFrontend(config.Frontend, config.MetaSrv.ServerAddr, logsDir, pidsDir, wg, logger), - Etcd: newEtcd(dataDir, logsDir, pidsDir, wg, logger), + MetaSrv: newMetaSrv(config.MetaSrv, workDirs, wg, logger), + Datanode: newDataNodes(config.Datanode, config.MetaSrv.ServerAddr, workDirs, wg, logger), + Frontend: newFrontend(config.Frontend, config.MetaSrv.ServerAddr, workDirs, wg, logger), + Etcd: newEtcd(workDirs, wg, logger), } } diff --git a/pkg/deployer/baremetal/component/datanode.go b/pkg/deployer/baremetal/component/datanode.go index 0440c92d..a8c01282 100644 --- a/pkg/deployer/baremetal/component/datanode.go +++ b/pkg/deployer/baremetal/component/datanode.go @@ -33,11 +33,9 @@ type datanode struct { config *config.Datanode metaSrvAddr string - dataDir string - logsDir string - pidsDir string - wg *sync.WaitGroup - logger logger.Logger + workDirs WorkDirs + wg *sync.WaitGroup + logger logger.Logger dataHome string dataNodeLogDirs []string @@ -45,21 +43,18 @@ type datanode struct { dataNodeDataDirs []string } -func newDataNodes(config *config.Datanode, metaSrvAddr, dataDir, logsDir, pidsDir string, - wg *sync.WaitGroup, logger logger.Logger) BareMetalClusterComponent { +func newDataNodes(config *config.Datanode, metaSrvAddr string, workDirs WorkDirs, wg *sync.WaitGroup, logger logger.Logger) BareMetalClusterComponent { return &datanode{ config: config, metaSrvAddr: metaSrvAddr, - dataDir: dataDir, - logsDir: logsDir, - pidsDir: pidsDir, + workDirs: workDirs, wg: wg, logger: logger, } } func (d *datanode) Start(ctx context.Context, binary string) error { - dataHome := path.Join(d.dataDir, "home") + dataHome := path.Join(d.workDirs.DataDir, "home") if err := utils.CreateDirIfNotExists(dataHome); err != nil { return err } @@ -68,23 +63,23 @@ func (d *datanode) Start(ctx context.Context, binary string) error { for i := 0; i < d.config.Replicas; i++ { dirName := fmt.Sprintf("datanode.%d", i) - datanodeLogDir := path.Join(d.logsDir, dirName) + datanodeLogDir := path.Join(d.workDirs.LogsDir, dirName) if err := utils.CreateDirIfNotExists(datanodeLogDir); err != nil { return err } d.dataNodeLogDirs = append(d.dataNodeLogDirs, datanodeLogDir) - datanodePidDir := path.Join(d.pidsDir, dirName) + datanodePidDir := path.Join(d.workDirs.PidsDir, dirName) if err := utils.CreateDirIfNotExists(datanodePidDir); err != nil { return err } d.dataNodePidDirs = append(d.dataNodePidDirs, datanodePidDir) - walDir := path.Join(d.dataDir, dirName, "wal") + walDir := path.Join(d.workDirs.DataDir, dirName, "wal") if err := utils.CreateDirIfNotExists(walDir); err != nil { return err } - d.dataNodeDataDirs = append(d.dataNodeDataDirs, path.Join(d.dataDir, dirName)) + d.dataNodeDataDirs = append(d.dataNodeDataDirs, path.Join(d.workDirs.DataDir, dirName)) if err := runBinary(ctx, binary, d.BuildArgs(ctx, i, walDir), datanodeLogDir, datanodePidDir, d.wg, d.logger); err != nil { return err diff --git a/pkg/deployer/baremetal/component/etcd.go b/pkg/deployer/baremetal/component/etcd.go index 5cf559c8..8df856eb 100644 --- a/pkg/deployer/baremetal/component/etcd.go +++ b/pkg/deployer/baremetal/component/etcd.go @@ -24,31 +24,26 @@ import ( ) type etcd struct { - dataDir string - logsDir string - pidsDir string - wg *sync.WaitGroup - logger logger.Logger + workDirs WorkDirs + wg *sync.WaitGroup + logger logger.Logger etcdDirs []string } -func newEtcd(dataDir, logsDir, pidsDir string, - wg *sync.WaitGroup, logger logger.Logger) BareMetalClusterComponent { +func newEtcd(workDirs WorkDirs, wg *sync.WaitGroup, logger logger.Logger) BareMetalClusterComponent { return &etcd{ - dataDir: dataDir, - logsDir: logsDir, - pidsDir: pidsDir, - wg: wg, - logger: logger, + workDirs: workDirs, + wg: wg, + logger: logger, } } func (e *etcd) Start(ctx context.Context, binary string) error { var ( - etcdDataDir = path.Join(e.dataDir, "etcd") - etcdLogDir = path.Join(e.logsDir, "etcd") - etcdPidDir = path.Join(e.pidsDir, "etcd") + etcdDataDir = path.Join(e.workDirs.DataDir, "etcd") + etcdLogDir = path.Join(e.workDirs.LogsDir, "etcd") + etcdPidDir = path.Join(e.workDirs.PidsDir, "etcd") etcdDirs = []string{etcdDataDir, etcdLogDir, etcdPidDir} ) for _, dir := range etcdDirs { diff --git a/pkg/deployer/baremetal/component/frontend.go b/pkg/deployer/baremetal/component/frontend.go index c9a0d1a7..a4c27d05 100644 --- a/pkg/deployer/baremetal/component/frontend.go +++ b/pkg/deployer/baremetal/component/frontend.go @@ -29,21 +29,18 @@ type frontend struct { config *config.Frontend metaSrvAddr string - logsDir string - pidsDir string - wg *sync.WaitGroup - logger logger.Logger + workDirs WorkDirs + wg *sync.WaitGroup + logger logger.Logger frontendDirs []string } -func newFrontend(config *config.Frontend, metaSrvAddr, logsDir, pidsDir string, - wg *sync.WaitGroup, logger logger.Logger) BareMetalClusterComponent { +func newFrontend(config *config.Frontend, metaSrvAddr string, workDirs WorkDirs, wg *sync.WaitGroup, logger logger.Logger) BareMetalClusterComponent { return &frontend{ config: config, metaSrvAddr: metaSrvAddr, - logsDir: logsDir, - pidsDir: pidsDir, + workDirs: workDirs, wg: wg, logger: logger, } @@ -51,8 +48,8 @@ func newFrontend(config *config.Frontend, metaSrvAddr, logsDir, pidsDir string, func (f *frontend) Start(ctx context.Context, binary string) error { var ( - frontendLogDir = path.Join(f.logsDir, "frontend") - frontendPidDir = path.Join(f.pidsDir, "frontend") + frontendLogDir = path.Join(f.workDirs.LogsDir, "frontend") + frontendPidDir = path.Join(f.workDirs.PidsDir, "frontend") frontendDirs = []string{frontendLogDir, frontendPidDir} ) for _, dir := range frontendDirs { diff --git a/pkg/deployer/baremetal/component/metasrv.go b/pkg/deployer/baremetal/component/metasrv.go index 6dc697b6..6ccdfc3c 100644 --- a/pkg/deployer/baremetal/component/metasrv.go +++ b/pkg/deployer/baremetal/component/metasrv.go @@ -31,29 +31,26 @@ import ( type metaSrv struct { config *config.MetaSrv - logsDir string - pidsDir string - wg *sync.WaitGroup - logger logger.Logger + workDirs WorkDirs + wg *sync.WaitGroup + logger logger.Logger metaSrvDirs []string } -func newMetaSrv(config *config.MetaSrv, logsDir, pidsDir string, - wg *sync.WaitGroup, logger logger.Logger) BareMetalClusterComponent { +func newMetaSrv(config *config.MetaSrv, workDirs WorkDirs, wg *sync.WaitGroup, logger logger.Logger) BareMetalClusterComponent { return &metaSrv{ - config: config, - logsDir: logsDir, - pidsDir: pidsDir, - wg: wg, - logger: logger, + config: config, + workDirs: workDirs, + wg: wg, + logger: logger, } } func (m *metaSrv) Start(ctx context.Context, binary string) error { var ( - metaSrvLogDir = path.Join(m.logsDir, "metasrv") - metaSrvPidDir = path.Join(m.pidsDir, "metasrv") + metaSrvLogDir = path.Join(m.workDirs.LogsDir, "metasrv") + metaSrvPidDir = path.Join(m.workDirs.PidsDir, "metasrv") metaSrvDirs = []string{metaSrvLogDir, metaSrvPidDir} ) for _, dir := range metaSrvDirs { diff --git a/pkg/deployer/baremetal/deployer.go b/pkg/deployer/baremetal/deployer.go index 5a75da03..2835243b 100644 --- a/pkg/deployer/baremetal/deployer.go +++ b/pkg/deployer/baremetal/deployer.go @@ -87,7 +87,12 @@ func NewDeployer(l logger.Logger, clusterName string, opts ...Option) (Interface return nil, err } - d.bm = component.NewGreptimeDBCluster(d.config.Cluster, d.dataDir, d.logsDir, d.pidsDir, &d.wg, d.logger) + workDirs := component.WorkDirs{ + DataDir: d.dataDir, + LogsDir: d.logsDir, + PidsDir: d.pidsDir, + } + d.bm = component.NewGreptimeDBCluster(d.config.Cluster, workDirs, &d.wg, d.logger) } return d, nil