Skip to content

Commit

Permalink
Impove(enter): enter leader mds directly without id option
Browse files Browse the repository at this point in the history
Signed-off-by: lyp <[email protected]>
  • Loading branch information
LYPWYT committed Dec 4, 2023
1 parent 9d83a9c commit e02f302
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 8 deletions.
72 changes: 65 additions & 7 deletions cli/command/enter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ package command

import (
"github.com/opencurve/curveadm/cli/cli"
comm "github.com/opencurve/curveadm/internal/common"
"github.com/opencurve/curveadm/internal/configure/topology"
"github.com/opencurve/curveadm/internal/errno"
"github.com/opencurve/curveadm/internal/playbook"
"github.com/opencurve/curveadm/internal/tools"
"github.com/opencurve/curveadm/internal/utils"
"github.com/spf13/cobra"
Expand All @@ -43,8 +45,11 @@ func NewEnterCommand(curveadm *cli.CurveAdm) *cobra.Command {
cmd := &cobra.Command{
Use: "enter ID",
Short: "Enter service container",
Args: utils.ExactArgs(1),
Args: utils.RequiresMaxArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return nil
}
options.id = args[0]
return curveadm.CheckId(options.id)
},
Expand All @@ -57,32 +62,85 @@ func NewEnterCommand(curveadm *cli.CurveAdm) *cobra.Command {
return cmd
}

func genStatusForLeaderPlaybook(curveadm *cli.CurveAdm,
dcs []*topology.DeployConfig,
options statusOptions) (*playbook.Playbook, error) {
dcs = curveadm.FilterDeployConfig(dcs, topology.FilterOption{
Id: options.id,
Role: options.role,
Host: options.host,
})
if len(dcs) == 0 {
return nil, errno.ERR_NO_SERVICES_MATCHED
}

steps := []int{playbook.GET_MDS_LEADER}
pb := playbook.NewPlaybook(curveadm)
for _, step := range steps {
pb.AddStep(&playbook.PlaybookStep{
Type: step,
Configs: dcs,
ExecOptions: playbook.ExecOptions{
//Concurrency: 10,
SilentSubBar: true,
SilentMainBar: step == playbook.INIT_SERVIE_STATUS,
SkipError: true,
},
})
}
return pb, nil
}

func runEnter(curveadm *cli.CurveAdm, options enterOptions) error {
// 1) parse cluster topology
dcs, err := curveadm.ParseTopology()
if err != nil {
return err
}
var containerId string
var dc *topology.DeployConfig
Id := options.id

// 2) filter service
// 2) If no id parameter, execute the following
if Id == "" {
// generate get status playbook
statusForLeaderOptions := statusOptions{id: "*", role: ROLE_MDS, host: "*"}
pb, err := genStatusForLeaderPlaybook(curveadm, dcs, statusForLeaderOptions)
if err != nil {
return err
}
// run playground
err = pb.Run()
if err != nil {
return err
}
// get mds leader id
value := curveadm.MemStorage().Get(comm.MDS_LEADER_ID)
Id = value.(string)
if Id == "" {
return errno.ERR_NO_LEADER_CONTAINER_FOUND
}
}

// 3) filter service
dcs = curveadm.FilterDeployConfig(dcs, topology.FilterOption{
Id: options.id,
Id: Id,
Role: "*",
Host: "*",
})
if len(dcs) == 0 {
return errno.ERR_NO_SERVICES_MATCHED
}

// 3) get container id
dc := dcs[0]
// 4) get container id
dc = dcs[0]
serviceId := curveadm.GetServiceId(dc.GetId())
containerId, err := curveadm.GetContainerId(serviceId)
containerId, err = curveadm.GetContainerId(serviceId)
if err != nil {
return err
}

// 4) attch remote container
// 5) attach remote container
home := dc.GetProjectLayout().ServiceRootDir
return tools.AttachRemoteContainer(curveadm, dc.GetHost(), containerId, home)
}
3 changes: 3 additions & 0 deletions internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ const (
SERVICE_STATUS_LOSED = "Losed"
SERVICE_STATUS_UNKNOWN = "Unknown"

// leader
MDS_LEADER_ID = "MDS_LEADER_ID"

// clean
KEY_CLEAN_ITEMS = "CLEAN_ITEMS"
KEY_CLEAN_BY_RECYCLE = "CLEAN_BY_RECYCLE"
Expand Down
3 changes: 2 additions & 1 deletion internal/errno/errno.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ var (
ERR_UNSUPPORT_CLEAN_ITEM = EC(210005, "unsupport clean item")
ERR_NO_SERVICES_MATCHED = EC(210006, "no services matched")
// TODO: please check pool set disk type
ERR_INVALID_DISK_TYPE = EC(210007, "poolset disk type must be lowercase and can only be one of ssd, hdd and nvme")
ERR_INVALID_DISK_TYPE = EC(210007, "poolset disk type must be lowercase and can only be one of ssd, hdd and nvme")
ERR_NO_LEADER_CONTAINER_FOUND = EC(210008, "no leader container found")

// 220: commad options (client common)
ERR_UNSUPPORT_CLIENT_KIND = EC(220000, "unsupport client kind")
Expand Down
3 changes: 3 additions & 0 deletions internal/playbook/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const (
GET_CLIENT_STATUS
INSTALL_CLIENT
UNINSTALL_CLIENT
GET_MDS_LEADER

// bs
FORMAT_CHUNKFILE_POOL
Expand Down Expand Up @@ -225,6 +226,8 @@ func (p *Playbook) createTasks(step *PlaybookStep) (*tasks.Tasks, error) {
t, err = comm.NewInitServiceStatusTask(curveadm, config.GetDC(i))
case GET_SERVICE_STATUS:
t, err = comm.NewGetServiceStatusTask(curveadm, config.GetDC(i))
case GET_MDS_LEADER:
t, err = comm.NewGetMdsLeaderTask(curveadm, config.GetDC(i))
case CLEAN_SERVICE:
t, err = comm.NewCleanServiceTask(curveadm, config.GetDC(i))
case INIT_SUPPORT:
Expand Down
75 changes: 75 additions & 0 deletions internal/task/task/common/service_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ type (
memStorage *utils.SafeMap
}

step2FormatMdsLeaderId struct {
serviceId string
isLeader *bool
memStorage *utils.SafeMap
}

ServiceStatus struct {
Id string
ParentId string
Expand Down Expand Up @@ -109,6 +115,21 @@ func setServiceStatus(memStorage *utils.SafeMap, id string, status ServiceStatus
})
}

func setMdsLeaderId(memStorage *utils.SafeMap, id string, IsLeader bool) {
memStorage.TX(func(kv *utils.SafeMap) error {
m := ""
v := kv.Get(comm.MDS_LEADER_ID)
if v != nil && v.(string) != "" {
return nil
}
if IsLeader {
m = id
}
kv.Set(comm.MDS_LEADER_ID, m)
return nil
})
}

func (s *step2InitStatus) Execute(ctx *context.Context) error {
dc := s.dc
id := s.serviceId
Expand Down Expand Up @@ -218,6 +239,13 @@ func (s *step2FormatServiceStatus) Execute(ctx *context.Context) error {
return nil
}

func (s *step2FormatMdsLeaderId) Execute(ctx *context.Context) error {
id := s.serviceId
IsLeader := *s.isLeader
setMdsLeaderId(s.memStorage, id, IsLeader)
return nil
}

func NewInitServiceStatusTask(curveadm *cli.CurveAdm, dc *topology.DeployConfig) (*task.Task, error) {
serviceId := curveadm.GetServiceId(dc.GetId())
containerId, err := curveadm.GetContainerId(serviceId)
Expand Down Expand Up @@ -306,3 +334,50 @@ func NewGetServiceStatusTask(curveadm *cli.CurveAdm, dc *topology.DeployConfig)

return t, nil
}

func NewGetMdsLeaderTask(curveadm *cli.CurveAdm, dc *topology.DeployConfig) (*task.Task, error) {
serviceId := curveadm.GetServiceId(dc.GetId())
containerId, err := curveadm.GetContainerId(serviceId)
if curveadm.IsSkip(dc) {
return nil, nil
} else if err != nil {
return nil, err
}
hc, err := curveadm.GetHost(dc.GetHost())
if err != nil {
return nil, err
}

// new task
subname := fmt.Sprintf("host=%s role=%s containerId=%s",
dc.GetHost(), dc.GetRole(), tui.TrimContainerId(containerId))
t := task.NewTask("Enter Leader container", subname, hc.GetSSHConfig())

// add step to task
var status string
var isLeader bool
t.AddStep(&step.ListContainers{
ShowAll: true,
Format: `"{{.Status}}"`,
Filter: fmt.Sprintf("id=%s", containerId),
Out: &status,
ExecOptions: curveadm.ExecOptions(),
})
t.AddStep(&step.Lambda{
Lambda: TrimContainerStatus(&status),
})
t.AddStep(&step2GetLeader{
dc: dc,
containerId: containerId,
status: &status,
isLeader: &isLeader,
execOptions: curveadm.ExecOptions(),
})
t.AddStep(&step2FormatMdsLeaderId{
serviceId: serviceId,
isLeader: &isLeader,
memStorage: curveadm.MemStorage(),
})

return t, nil
}

0 comments on commit e02f302

Please sign in to comment.