Skip to content

Commit aace2bb

Browse files
committed
Fix(migrate): support migrate other service
1 parent b1a3a4e commit aace2bb

File tree

14 files changed

+650
-11
lines changed

14 files changed

+650
-11
lines changed

cli/command/migrate.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,30 @@ import (
3939

4040
var (
4141
MIGRATE_ETCD_STEPS = []int{
42-
playbook.STOP_SERVICE,
43-
playbook.CLEAN_SERVICE, // only container
42+
playbook.ADD_ETCD_MEMBER,
4443
playbook.PULL_IMAGE,
4544
playbook.CREATE_CONTAINER,
4645
playbook.SYNC_CONFIG,
46+
playbook.AMEND_ETCD_CONFIG,
4747
playbook.START_ETCD,
48+
playbook.REMOVE_ETCD_MEMBER,
49+
playbook.AMEND_SERVER_CONFIG, // modify the etcd endpoint in mds.conf
50+
playbook.RESTART_SERVICE, // restart all mds then modify the etcd endpoint
51+
playbook.STOP_SERVICE,
52+
playbook.CLEAN_SERVICE, // only container
4853
playbook.UPDATE_TOPOLOGY,
4954
}
5055

5156
// mds
5257
MIGRATE_MDS_STEPS = []int{
53-
playbook.STOP_SERVICE,
54-
playbook.CLEAN_SERVICE, // only container
5558
playbook.PULL_IMAGE,
5659
playbook.CREATE_CONTAINER,
5760
playbook.SYNC_CONFIG,
5861
playbook.START_MDS,
62+
playbook.AMEND_SERVER_CONFIG, // modify the mds.listen.addr in metaserver.conf
63+
playbook.RESTART_SERVICE, // restart all metaserver then modify the mds.listen.addr
64+
playbook.STOP_SERVICE,
65+
playbook.CLEAN_SERVICE, // only container
5966
playbook.UPDATE_TOPOLOGY,
6067
}
6168

@@ -67,6 +74,8 @@ var (
6774
playbook.CREATE_CONTAINER,
6875
playbook.SYNC_CONFIG,
6976
playbook.START_SNAPSHOTCLONE,
77+
playbook.AMEND_SERVER_CONFIG, // modify the mds.listen.addr in metaserver.conf
78+
playbook.RESTART_SERVICE, // restart all metaserver then modify the mds.listen.addr
7079
playbook.UPDATE_TOPOLOGY,
7180
}
7281

@@ -157,7 +166,7 @@ func checkMigrateTopology(curveadm *cli.CurveAdm, data string) error {
157166
} else if len(dcs2add) < len(dcs2del) {
158167
return errno.ERR_DELETE_SERVICE_WHILE_MIGRATING_IS_DENIED
159168
}
160-
// len(dcs2add) == len(dcs2del)
169+
161170
if len(dcs2add) == 0 {
162171
return errno.ERR_NO_SERVICES_FOR_MIGRATING
163172
}
@@ -199,6 +208,7 @@ func genMigratePlaybook(curveadm *cli.CurveAdm,
199208
migrates := getMigrates(curveadm, data)
200209
role := migrates[0].From.GetRole()
201210
steps := MIGRATE_ROLE_STEPS[role]
211+
etcdDCs := curveadm.FilterDeployConfigByRole(dcs, topology.ROLE_ETCD)
202212

203213
// post clean
204214
if options.clean {
@@ -221,10 +231,25 @@ func genMigratePlaybook(curveadm *cli.CurveAdm,
221231
config := dcs2add
222232
switch step {
223233
case playbook.STOP_SERVICE,
224-
playbook.CLEAN_SERVICE:
234+
playbook.CLEAN_SERVICE,
235+
playbook.ADD_ETCD_MEMBER,
236+
playbook.REMOVE_ETCD_MEMBER:
225237
config = dcs2del
226238
case playbook.BACKUP_ETCD_DATA:
227239
config = curveadm.FilterDeployConfigByRole(dcs, topology.ROLE_ETCD)
240+
// 1. migrate etcd, need to override mds config and restart all mds
241+
// 2. (FS)migrate mds, need to override metaserver config and restart all metaservers
242+
// 3. (BS)migrate mds, need to override chunkserver and snapshot config and restart all chunkservers and snapshotclones
243+
case playbook.AMEND_SERVER_CONFIG,
244+
playbook.RESTART_SERVICE:
245+
if role == topology.ROLE_ETCD {
246+
config = curveadm.FilterDeployConfigByRole(dcs, topology.ROLE_MDS)
247+
} else if role == topology.ROLE_MDS && dcs[0].GetKind() == topology.KIND_CURVEFS {
248+
config = curveadm.FilterDeployConfigByRole(dcs, topology.ROLE_METASERVER)
249+
} else if role == topology.ROLE_MDS && dcs[0].GetKind() == topology.KIND_CURVEBS {
250+
config = curveadm.FilterDeployConfigByRole(dcs, topology.ROLE_CHUNKSERVER)
251+
config = append(config, curveadm.FilterDeployConfigByRole(dcs, topology.ROLE_SNAPSHOTCLONE)...)
252+
}
228253
case
229254
playbook.CREATE_PHYSICAL_POOL,
230255
playbook.CREATE_LOGICAL_POOL,
@@ -251,6 +276,11 @@ func genMigratePlaybook(curveadm *cli.CurveAdm,
251276
optionsKV[comm.KEY_POOLSET] = poolset
252277
case playbook.UPDATE_TOPOLOGY:
253278
optionsKV[comm.KEY_NEW_TOPOLOGY_DATA] = data
279+
case playbook.ADD_ETCD_MEMBER,
280+
playbook.AMEND_ETCD_CONFIG,
281+
playbook.AMEND_SERVER_CONFIG:
282+
optionsKV[comm.KEY_MIGRATE_SERVERS] = migrates
283+
optionsKV[comm.KEY_CLUSTER_DCS] = etcdDCs
254284
}
255285

256286
pb.AddStep(&playbook.PlaybookStep{

internal/common/common.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const (
5757
// migrate
5858
KEY_MIGRATE_STATUS = "MIGRATE_STATUS"
5959
KEY_MIGRATE_COMMON_STATUS = "MIGRATE_COMMON_STATUS"
60+
KEY_CLUSTER_DCS = "CLUSTER_DCS"
6061

6162
// check
6263
KEY_CHECK_WITH_WEAK = "CHECK_WITH_WEAK"

internal/configure/topology/dc_get.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ func (dc *DeployConfig) GetInstances() int { return dc.instanc
121121
func (dc *DeployConfig) GetHostSequence() int { return dc.hostSequence }
122122
func (dc *DeployConfig) GetInstancesSequence() int { return dc.instancesSequence }
123123
func (dc *DeployConfig) GetServiceConfig() map[string]string { return dc.serviceConfig }
124-
func (dc *DeployConfig) GetVariables() *variable.Variables { return dc.variables }
124+
func (dc *DeployConfig) SetServiceConfig(key, value string) {
125+
dc.serviceConfig[key] = value
126+
}
127+
128+
func (dc *DeployConfig) GetVariables() *variable.Variables { return dc.variables }
125129

126130
// (2): config item
127131
func (dc *DeployConfig) GetPrefix() string { return dc.getString(CONFIG_PREFIX) }

internal/configure/topology/variables.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ var (
118118
{name: "cluster_mds_dummy_addr"},
119119
{name: "cluster_mds_dummy_port"},
120120
{name: "cluster_chunkserver_addr", kind: []string{KIND_CURVEBS}},
121-
{name: "cluster_snapshotclone_addr", kind: []string{KIND_CURVEBS}},
121+
{name: "cluster_snapshotclone_addr"},
122122
{name: "cluster_snapshotclone_proxy_addr", kind: []string{KIND_CURVEBS}},
123-
{name: "cluster_snapshotclone_dummy_port", kind: []string{KIND_CURVEBS}},
123+
{name: "cluster_snapshotclone_dummy_port"},
124124
{name: "cluster_snapshotclone_nginx_upstream", kind: []string{KIND_CURVEBS}},
125125
{name: "cluster_snapshot_addr"}, // tools-v2: compatible with some old version image
126126
{name: "cluster_snapshot_dummy_addr"}, // tools-v2

internal/errno/errno.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,9 @@ var (
404404
ERR_GET_CHUNKSERVER_COPYSET = EC(410026, "failed to get chunkserver copyset")
405405
ERR_GET_MIGRATE_COPYSET = EC(410027, "migrate chunkserver copyset info must be 2")
406406
ERR_CONTAINER_NOT_REMOVED = EC(410027, "container not removed")
407+
ERR_GET_CLUSTER_ETCD_ADDR = EC(410028, "failed to get cluster_etcd_addr variable")
408+
ERR_ADD_ETCD_MEMEBER = EC(410029, "failed to add etcd member to existing etcd cluster")
409+
ERR_REMOVE_ETCD_MEMBER = EC(410030, "failed to remove etcd member from existing etcd cluster")
407410
// 420: common (curvebs client)
408411
ERR_VOLUME_ALREADY_MAPPED = EC(420000, "volume already mapped")
409412
ERR_VOLUME_CONTAINER_LOSED = EC(420001, "volume container is losed")

internal/playbook/factory.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ const (
8484
INSTALL_CLIENT
8585
UNINSTALL_CLIENT
8686
ATTACH_LEADER_OR_RANDOM_CONTAINER
87+
ADD_ETCD_MEMBER
88+
AMEND_ETCD_CONFIG
89+
AMEND_SERVER_CONFIG
90+
REMOVE_ETCD_MEMBER
8791

8892
// bs
8993
FORMAT_CHUNKFILE_POOL
@@ -251,6 +255,14 @@ func (p *Playbook) createTasks(step *PlaybookStep) (*tasks.Tasks, error) {
251255
t, err = comm.NewInstallClientTask(curveadm, config.GetCC(i))
252256
case UNINSTALL_CLIENT:
253257
t, err = comm.NewUninstallClientTask(curveadm, nil)
258+
case ADD_ETCD_MEMBER:
259+
t, err = comm.NewAddEtcdMemberTask(curveadm, config.GetDC(i))
260+
case AMEND_ETCD_CONFIG:
261+
t, err = comm.NewAmendEtcdConfigTask(curveadm, config.GetDC(i))
262+
case AMEND_SERVER_CONFIG:
263+
t, err = comm.NewAmendServerConfigTask(curveadm, config.GetDC(i))
264+
case REMOVE_ETCD_MEMBER:
265+
t, err = comm.NewRemoveEtcdMemberTask(curveadm, config.GetDC(i))
254266
// bs
255267
case FORMAT_CHUNKFILE_POOL:
256268
t, err = bs.NewFormatChunkfilePoolTask(curveadm, config.GetFC(i))

internal/task/scripts/enable_etcd_auth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
15-
*/
15+
*/
1616

1717
/*
1818
* Project: Curveadm
1919
* Created Date: 2023-08-02
2020
* Author: wanghai (SeanHai)
21-
*/
21+
*/
2222

2323
package scripts
2424

internal/task/scripts/script.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ var (
3939
WAIT string
4040
//go:embed shell/report.sh
4141
REPORT string
42+
//go:embed shell/add_etcd.sh
43+
ADD_ETCD string
44+
//go:embed shell/remove_etcd.sh
45+
REMOVE_ETCD string
4246

4347
// CurveBS
4448

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env bash
2+
3+
# Usage:
4+
# Example:
5+
# Created Date: 2023-12-15
6+
# Author: Caoxianfei
7+
8+
etcdctl=$1
9+
endpoints=$2
10+
old_name=$3
11+
new_name=$4
12+
new_peer_url=$5
13+
14+
tmplog=/tmp/_curveadm_add_etcd_
15+
16+
output=$(${etcdctl} --endpoints=${endpoints} member list)
17+
if [ $? -ne 0 ]; then
18+
echo "failed to list all etcd members"
19+
exit 1
20+
fi
21+
22+
# if member has added, then skip
23+
id=$(echo "$output" | awk -v name="$new_name" -F ', ' '$3 == name {print $1}')
24+
if [ -z "${id}" ]; then
25+
echo "EXIST"
26+
exit 0
27+
fi
28+
29+
${etcdctl} --endpoints=${endpoints} member add ${new_name} --peer-urls ${new_peer_url} > ${tmplog} 2>&1
30+
if [ $? -ne 0 ]; then
31+
if cat ${tmplog} | grep -q "Peer URLs already exists"; then
32+
exit 0
33+
else
34+
exit 1
35+
fi
36+
fi
37+
38+
39+
# ${etcdctl} --endpoints=${endpoints} member remove ${id}
40+
# if [ $? -ne 0 ]; then
41+
# echo "failed to remove member ${old_name}"
42+
# exit 1
43+
# fi
44+
45+
46+
47+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env bash
2+
3+
# Usage:
4+
# Example:
5+
# Created Date: 2023-12-15
6+
# Author: Caoxianfei
7+
8+
etcdctl=$1
9+
endpoints=$2
10+
old_name=$3
11+
12+
output=$(${etcdctl} --endpoints=${endpoints} member list)
13+
if [ $? -ne 0 ]; then
14+
echo "failed to list all etcd members"
15+
exit 1
16+
fi
17+
18+
id=$(echo "$output" | awk -v name="$old_name" -F ', ' '$3 == name {print $1}')
19+
# if not found the name then exit 0
20+
if [ -z "${id}" ]; then
21+
echo "NOTEXIST"
22+
exit 0
23+
fi
24+
25+
${etcdctl} --endpoints=${endpoints} member remove ${id}
26+
if [ $? -ne 0 ]; then
27+
echo "failed to remove member ${old_name}"
28+
exit 1
29+
fi
30+
31+
32+
33+
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright (c) 2023 NetEase Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* Project: CurveAdm
19+
* Created Date: 2023-12-20
20+
* Author: Caoxianfei
21+
*/
22+
23+
package common
24+
25+
import (
26+
"fmt"
27+
"strconv"
28+
29+
"github.com/opencurve/curveadm/cli/cli"
30+
comm "github.com/opencurve/curveadm/internal/common"
31+
"github.com/opencurve/curveadm/internal/configure"
32+
"github.com/opencurve/curveadm/internal/configure/topology"
33+
"github.com/opencurve/curveadm/internal/errno"
34+
"github.com/opencurve/curveadm/internal/task/context"
35+
"github.com/opencurve/curveadm/internal/task/scripts"
36+
"github.com/opencurve/curveadm/internal/task/step"
37+
"github.com/opencurve/curveadm/internal/task/task"
38+
tui "github.com/opencurve/curveadm/internal/tui/common"
39+
)
40+
41+
func checkAddEtcdMemberStatus(success *bool, out *string) step.LambdaType {
42+
return func(ctx *context.Context) error {
43+
if !*success {
44+
return errno.ERR_ADD_ETCD_MEMEBER.S(*out)
45+
}
46+
if *out == "EXIST" {
47+
return task.ERR_SKIP_TASK
48+
}
49+
return nil
50+
}
51+
}
52+
53+
func NewAddEtcdMemberTask(curveadm *cli.CurveAdm, dc *topology.DeployConfig) (*task.Task, error) {
54+
serviceId := curveadm.GetServiceId(dc.GetId())
55+
containerId, err := curveadm.GetContainerId(serviceId)
56+
if curveadm.IsSkip(dc) {
57+
return nil, nil
58+
} else if err != nil {
59+
return nil, err
60+
}
61+
hc, err := curveadm.GetHost(dc.GetHost())
62+
if err != nil {
63+
return nil, err
64+
}
65+
66+
subname := fmt.Sprintf("host=%s role=%s containerId=%s",
67+
dc.GetHost(), dc.GetRole(), tui.TrimContainerId(containerId))
68+
t := task.NewTask("Add Etcd Member", subname, hc.GetSSHConfig())
69+
70+
host, role := dc.GetHost(), dc.GetRole()
71+
script := scripts.ADD_ETCD
72+
layout := dc.GetProjectLayout()
73+
scriptPath := fmt.Sprintf("%s/add_etcd.sh", layout.ServiceBinDir)
74+
etcdctlPath := layout.ServiceBinDir + "/etcdctl"
75+
endpoints, err := dc.GetVariables().Get("cluster_etcd_addr")
76+
if err != nil {
77+
return nil, errno.ERR_GET_CLUSTER_ETCD_ADDR
78+
}
79+
oldName := fmt.Sprint("etcd", strconv.Itoa(dc.GetHostSequence()), strconv.Itoa(dc.GetInstancesSequence()))
80+
newName := fmt.Sprint("etcd", strconv.Itoa(dc.GetHostSequence()+3), strconv.Itoa(dc.GetInstancesSequence()))
81+
migrates := []*configure.MigrateServer{}
82+
if curveadm.MemStorage().Get(comm.KEY_MIGRATE_SERVERS) != nil {
83+
migrates = curveadm.MemStorage().Get(comm.KEY_MIGRATE_SERVERS).([]*configure.MigrateServer)
84+
}
85+
toService := migrates[0].To
86+
peerUrl := fmt.Sprint("http://", toService.GetListenIp(), ":", strconv.Itoa(toService.GetListenPort()))
87+
addEtcdCmd := fmt.Sprintf("/bin/bash %s %s %s %s %s %s", scriptPath, etcdctlPath, endpoints, oldName, newName, peerUrl)
88+
89+
var success bool
90+
var out string
91+
t.AddStep(&step.ListContainers{
92+
ShowAll: true,
93+
Format: `"{{.ID}}"`,
94+
Filter: fmt.Sprintf("id=%s", containerId),
95+
Out: &out,
96+
ExecOptions: curveadm.ExecOptions(),
97+
})
98+
t.AddStep(&step.Lambda{
99+
Lambda: CheckContainerExist(host, role, containerId, &out),
100+
})
101+
t.AddStep(&step.InstallFile{
102+
ContainerId: &containerId,
103+
ContainerDestPath: scriptPath,
104+
Content: &script,
105+
ExecOptions: curveadm.ExecOptions(),
106+
})
107+
t.AddStep(&step.ContainerExec{
108+
ContainerId: &containerId,
109+
Success: &success,
110+
Out: &out,
111+
Command: addEtcdCmd,
112+
ExecOptions: curveadm.ExecOptions(),
113+
})
114+
t.AddStep(&step.Lambda{
115+
Lambda: checkAddEtcdMemberStatus(&success, &out),
116+
})
117+
118+
return t, nil
119+
}

0 commit comments

Comments
 (0)