Skip to content

Commit a703f1e

Browse files
LYPWYTcaoxianfei1
authored andcommitted
Impove(enter): enter leader mds directly without id option
Signed-off-by: lyp <[email protected]>
1 parent 1376628 commit a703f1e

File tree

5 files changed

+154
-7
lines changed

5 files changed

+154
-7
lines changed

cli/command/enter.go

+66-5
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,20 @@ package command
2626

2727
import (
2828
"github.com/opencurve/curveadm/cli/cli"
29+
comm "github.com/opencurve/curveadm/internal/common"
2930
"github.com/opencurve/curveadm/internal/configure/topology"
3031
"github.com/opencurve/curveadm/internal/errno"
32+
"github.com/opencurve/curveadm/internal/playbook"
33+
"github.com/opencurve/curveadm/internal/task/task/common"
3134
"github.com/opencurve/curveadm/internal/tools"
3235
"github.com/opencurve/curveadm/internal/utils"
3336
"github.com/spf13/cobra"
3437
)
3538

39+
var (
40+
ATTACH_LEADER_OR_RANDOM_CONTAINER = []int{playbook.ATTACH_LEADER_OR_RANDOM_CONTAINER}
41+
)
42+
3643
type enterOptions struct {
3744
id string
3845
}
@@ -43,8 +50,11 @@ func NewEnterCommand(curveadm *cli.CurveAdm) *cobra.Command {
4350
cmd := &cobra.Command{
4451
Use: "enter ID",
4552
Short: "Enter service container",
46-
Args: utils.ExactArgs(1),
53+
Args: utils.RequiresMaxArgs(1),
4754
PreRunE: func(cmd *cobra.Command, args []string) error {
55+
if len(args) == 0 {
56+
return nil
57+
}
4858
options.id = args[0]
4959
return curveadm.CheckId(options.id)
5060
},
@@ -57,32 +67,83 @@ func NewEnterCommand(curveadm *cli.CurveAdm) *cobra.Command {
5767
return cmd
5868
}
5969

70+
func genLeaderOrRandomPlaybook(curveadm *cli.CurveAdm,
71+
dcs []*topology.DeployConfig) (*playbook.Playbook, error) {
72+
if len(dcs) == 0 {
73+
return nil, errno.ERR_NO_SERVICES_MATCHED
74+
}
75+
76+
steps := ATTACH_LEADER_OR_RANDOM_CONTAINER
77+
pb := playbook.NewPlaybook(curveadm)
78+
for _, step := range steps {
79+
pb.AddStep(&playbook.PlaybookStep{
80+
Type: step,
81+
Configs: dcs,
82+
ExecOptions: playbook.ExecOptions{
83+
SilentSubBar: true,
84+
SilentMainBar: true,
85+
SkipError: true,
86+
},
87+
})
88+
}
89+
return pb, nil
90+
}
91+
92+
func checkOrGetId(curveadm *cli.CurveAdm, dcs []*topology.DeployConfig, options enterOptions) (string, error) {
93+
id := options.id
94+
if id != "" {
95+
return id, nil
96+
}
97+
pb, err := genLeaderOrRandomPlaybook(curveadm, dcs)
98+
if err != nil {
99+
return "", err
100+
}
101+
// run playground
102+
err = pb.Run()
103+
if err != nil {
104+
return "", err
105+
}
106+
// get leader or random container id
107+
value := curveadm.MemStorage().Get(comm.LEADER_OR_RANDOM_ID)
108+
if value == nil {
109+
return "", errno.ERR_NO_LEADER_OR_RANDOM_CONTAINER_FOUND
110+
}
111+
id = value.(common.Leader0rRandom).Id
112+
return id, nil
113+
}
114+
60115
func runEnter(curveadm *cli.CurveAdm, options enterOptions) error {
61116
// 1) parse cluster topology
62117
dcs, err := curveadm.ParseTopology()
63118
if err != nil {
64119
return err
65120
}
66121

67-
// 2) filter service
122+
// 2) check id options
123+
id, err := checkOrGetId(curveadm, dcs, options)
124+
if err != nil {
125+
return err
126+
}
127+
128+
// 3) filter service
68129
dcs = curveadm.FilterDeployConfig(dcs, topology.FilterOption{
69-
Id: options.id,
130+
Id: id,
70131
Role: "*",
71132
Host: "*",
72133
})
73134
if len(dcs) == 0 {
74135
return errno.ERR_NO_SERVICES_MATCHED
75136
}
76137

77-
// 3) get container id
138+
// 4) get container id
78139
dc := dcs[0]
79140
serviceId := curveadm.GetServiceId(dc.GetId())
80141
containerId, err := curveadm.GetContainerId(serviceId)
81142
if err != nil {
82143
return err
83144
}
84145

85-
// 4) attch remote container
146+
// 5) attach remote container
86147
home := dc.GetProjectLayout().ServiceRootDir
87148
return tools.AttachRemoteContainer(curveadm, dc.GetHost(), containerId, home)
88149
}

internal/common/common.go

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const (
4949
POOLSET = "poolset"
5050
POOLSET_DISK_TYPE = "poolset-disktype"
5151
KEY_NUMBER_OF_CHUNKSERVER = "NUMBER_OF_CHUNKSERVER"
52+
LEADER_OR_RANDOM_ID = "LEADER_OR_RANDOM_ID"
5253

5354
// format
5455
KEY_ALL_FORMAT_STATUS = "ALL_FORMAT_STATUS"

internal/errno/errno.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,10 @@ var (
253253
ERR_UNSUPPORT_CLEAN_ITEM = EC(210005, "unsupport clean item")
254254
ERR_NO_SERVICES_MATCHED = EC(210006, "no services matched")
255255
// TODO: please check pool set disk type
256-
ERR_INVALID_DISK_TYPE = EC(210007, "poolset disk type must be lowercase and can only be one of ssd, hdd and nvme")
257-
ERR_UNSUPPORT_DEPLOY_TYPE = EC(210008, "unknown deploy type")
256+
ERR_INVALID_DISK_TYPE = EC(210007, "poolset disk type must be lowercase and can only be one of ssd, hdd and nvme")
257+
ERR_UNSUPPORT_DEPLOY_TYPE = EC(210008, "unknown deploy type")
258+
ERR_NO_LEADER_OR_RANDOM_CONTAINER_FOUND = EC(210009, "no leader or random container found")
259+
258260
// 220: commad options (client common)
259261
ERR_UNSUPPORT_CLIENT_KIND = EC(220000, "unsupport client kind")
260262
// 221: command options (client/bs)

internal/playbook/factory.go

+3
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ const (
8383
GET_CLIENT_STATUS
8484
INSTALL_CLIENT
8585
UNINSTALL_CLIENT
86+
ATTACH_LEADER_OR_RANDOM_CONTAINER
8687

8788
// bs
8889
FORMAT_CHUNKFILE_POOL
@@ -225,6 +226,8 @@ func (p *Playbook) createTasks(step *PlaybookStep) (*tasks.Tasks, error) {
225226
t, err = comm.NewInitServiceStatusTask(curveadm, config.GetDC(i))
226227
case GET_SERVICE_STATUS:
227228
t, err = comm.NewGetServiceStatusTask(curveadm, config.GetDC(i))
229+
case ATTACH_LEADER_OR_RANDOM_CONTAINER:
230+
t, err = comm.NewAttachLeaderOrRandomContainerTask(curveadm, config.GetDC(i))
228231
case CLEAN_SERVICE:
229232
t, err = comm.NewCleanServiceTask(curveadm, config.GetDC(i))
230233
case INIT_SUPPORT:

internal/task/task/common/service_status.go

+80
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ type (
8080
memStorage *utils.SafeMap
8181
}
8282

83+
step2SetLeaderOrRandom struct {
84+
serviceId string
85+
status *string
86+
isLeader *bool
87+
memStorage *utils.SafeMap
88+
}
89+
8390
ServiceStatus struct {
8491
Id string
8592
ParentId string
@@ -94,6 +101,11 @@ type (
94101
DataDir string
95102
Config *topology.DeployConfig
96103
}
104+
105+
Leader0rRandom struct {
106+
IsLeader bool
107+
Id string
108+
}
97109
)
98110

99111
func setServiceStatus(memStorage *utils.SafeMap, id string, status ServiceStatus) {
@@ -218,6 +230,27 @@ func (s *step2FormatServiceStatus) Execute(ctx *context.Context) error {
218230
return nil
219231
}
220232

233+
func (s *step2SetLeaderOrRandom) Execute(ctx *context.Context) error {
234+
id := s.serviceId
235+
IsLeader := *s.isLeader
236+
if !strings.HasPrefix(*s.status, "Up") {
237+
return nil
238+
}
239+
s.memStorage.TX(func(kv *utils.SafeMap) error {
240+
m := Leader0rRandom{false, id}
241+
v := kv.Get(comm.LEADER_OR_RANDOM_ID)
242+
if v != nil && v.(Leader0rRandom).IsLeader {
243+
return nil
244+
}
245+
if IsLeader {
246+
m = Leader0rRandom{true, id}
247+
}
248+
kv.Set(comm.LEADER_OR_RANDOM_ID, m)
249+
return nil
250+
})
251+
return nil
252+
}
253+
221254
func NewInitServiceStatusTask(curveadm *cli.CurveAdm, dc *topology.DeployConfig) (*task.Task, error) {
222255
serviceId := curveadm.GetServiceId(dc.GetId())
223256
containerId, err := curveadm.GetContainerId(serviceId)
@@ -306,3 +339,50 @@ func NewGetServiceStatusTask(curveadm *cli.CurveAdm, dc *topology.DeployConfig)
306339

307340
return t, nil
308341
}
342+
343+
func NewAttachLeaderOrRandomContainerTask(curveadm *cli.CurveAdm, dc *topology.DeployConfig) (*task.Task, error) {
344+
serviceId := curveadm.GetServiceId(dc.GetId())
345+
containerId, err := curveadm.GetContainerId(serviceId)
346+
if curveadm.IsSkip(dc) {
347+
return nil, nil
348+
} else if err != nil {
349+
return nil, err
350+
}
351+
hc, err := curveadm.GetHost(dc.GetHost())
352+
if err != nil {
353+
return nil, err
354+
}
355+
356+
// new task
357+
subname := fmt.Sprintf("host=%s role=%s containerId=%s",
358+
dc.GetHost(), dc.GetRole(), tui.TrimContainerId(containerId))
359+
t := task.NewTask("Enter Leader container", subname, hc.GetSSHConfig())
360+
361+
// add step to task
362+
var status string
363+
var isLeader bool
364+
t.AddStep(&step.ListContainers{
365+
Format: `"{{.Status}}"`,
366+
Filter: fmt.Sprintf("id=%s", containerId),
367+
Out: &status,
368+
ExecOptions: curveadm.ExecOptions(),
369+
})
370+
t.AddStep(&step.Lambda{
371+
Lambda: TrimContainerStatus(&status),
372+
})
373+
t.AddStep(&step2GetLeader{
374+
dc: dc,
375+
containerId: containerId,
376+
status: &status,
377+
isLeader: &isLeader,
378+
execOptions: curveadm.ExecOptions(),
379+
})
380+
t.AddStep(&step2SetLeaderOrRandom{
381+
serviceId: serviceId,
382+
status: &status,
383+
isLeader: &isLeader,
384+
memStorage: curveadm.MemStorage(),
385+
})
386+
387+
return t, nil
388+
}

0 commit comments

Comments
 (0)