Skip to content

Commit

Permalink
refactor: aggregate some commonly used dirs
Browse files Browse the repository at this point in the history
Signed-off-by: sh2 <[email protected]>
  • Loading branch information
shawnh2 committed Aug 3, 2023
1 parent 06e6ab4 commit 39d0180
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 60 deletions.
18 changes: 12 additions & 6 deletions pkg/deployer/baremetal/component/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
}
}

Expand Down
25 changes: 10 additions & 15 deletions pkg/deployer/baremetal/component/datanode.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,28 @@ 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
dataNodePidDirs []string
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
}
Expand All @@ -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
Expand Down
25 changes: 10 additions & 15 deletions pkg/deployer/baremetal/component/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 7 additions & 10 deletions pkg/deployer/baremetal/component/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,27 @@ 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,
}
}

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 {
Expand Down
23 changes: 10 additions & 13 deletions pkg/deployer/baremetal/component/metasrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion pkg/deployer/baremetal/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 39d0180

Please sign in to comment.