Skip to content

Commit 3dd5320

Browse files
Add a command to kill couchbase on nodes present in a cluster (#145)
* first commit * SDKQE-3683 Add a command to kill coubhase server on nodes present in cluster
1 parent 8e3aa6d commit 3dd5320

6 files changed

Lines changed: 118 additions & 0 deletions

File tree

cmd/chaos-killcouchbase.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"go.uber.org/zap"
6+
)
7+
8+
var chaosKillCouchbaseCmd = &cobra.Command{
9+
Use: "kill-couchbase <cluster-id> [<node-id-or-ip> ...]",
10+
Short: "Kills couchbase service on node/s present in the cluster.",
11+
Args: cobra.MinimumNArgs(1),
12+
Run: func(cmd *cobra.Command, args []string) {
13+
helper := CmdHelper{}
14+
logger := helper.GetLogger()
15+
ctx := helper.GetContext()
16+
17+
_, deployer, cluster := helper.IdentifyCluster(ctx, args[0])
18+
nodeIdents := args[1:]
19+
20+
var nodeIds []string
21+
for _, nodeIdent := range nodeIdents {
22+
node := helper.IdentifyNode(ctx, cluster, nodeIdent)
23+
nodeIds = append(nodeIds, node.GetID())
24+
}
25+
26+
err := deployer.KillCouchbase(ctx, cluster.GetID(), nodeIds)
27+
if err != nil {
28+
logger.Fatal("failed to kill couchbase", zap.Error(err))
29+
}
30+
},
31+
}
32+
33+
func init() {
34+
chaosCmd.AddCommand(chaosKillCouchbaseCmd)
35+
}

deployment/caodeploy/deployer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,3 +780,7 @@ func (d *Deployer) SetNodeRecovery(ctx context.Context, clusterID string, nodeID
780780
func (d *Deployer) RebalanceCluster(ctx context.Context, clusterID string, nodesToEject []string) error {
781781
return errors.New("caodeploy does not support rebalance cluster")
782782
}
783+
784+
func (d *Deployer) KillCouchbase(ctx context.Context, clusterID string, nodes []string) error {
785+
return errors.New("caodeploy does not support killing couchbase process")
786+
}

deployment/clouddeploy/deployer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2479,3 +2479,7 @@ func (d *Deployer) SetNodeRecovery(ctx context.Context, clusterID string, nodeID
24792479
func (d *Deployer) RebalanceCluster(ctx context.Context, clusterID string, nodesToEject []string) error {
24802480
return errors.New("clouddeploy does not support rebalance cluster")
24812481
}
2482+
2483+
func (d *Deployer) KillCouchbase(ctx context.Context, clusterID string, nodeIDs []string) error {
2484+
return errors.New("clouddeploy does not support killing couchbase process")
2485+
}

deployment/deployer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,5 @@ type Deployer interface {
150150
CreateS3Link(ctx context.Context, columnarID, linkName, region, endpoint, accessKey, secretKey string) error
151151
DropLink(ctx context.Context, columnarID, linkName string) error
152152
EnableDataApi(ctx context.Context, clusterID string) error
153+
KillCouchbase(ctx context.Context, clusterID string, nodes []string) error
153154
}

deployment/dockerdeploy/deployer.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8+
"github.com/docker/docker/api/types/container"
9+
"github.com/docker/docker/pkg/stdcopy"
810
"io"
911
"os"
1012
"path"
@@ -1301,6 +1303,74 @@ func (d *Deployer) getNodeOTP(ctx context.Context, clusterID string, nodeId stri
13011303
return "", nil
13021304
}
13031305

1306+
func (d *Deployer) execInContainer(ctx context.Context, containerID string, cmd []string) error {
1307+
execOpts := container.ExecOptions{
1308+
Cmd: cmd,
1309+
AttachStdout: true,
1310+
AttachStderr: true,
1311+
Tty: false,
1312+
Privileged: false,
1313+
}
1314+
1315+
execResp, err := d.dockerCli.ContainerExecCreate(ctx, containerID, execOpts)
1316+
if err != nil {
1317+
return fmt.Errorf("ContainerExecCreate failed: %w", err)
1318+
}
1319+
1320+
attachResp, err := d.dockerCli.ContainerExecAttach(ctx, execResp.ID, container.ExecAttachOptions{})
1321+
if err != nil {
1322+
return fmt.Errorf("ContainerExecAttach failed: %w", err)
1323+
}
1324+
defer attachResp.Close()
1325+
1326+
_, err = stdcopy.StdCopy(os.Stdout, os.Stderr, attachResp.Reader)
1327+
if err != nil {
1328+
return fmt.Errorf("copying output failed: %w", err)
1329+
}
1330+
1331+
return nil
1332+
}
1333+
1334+
/*
1335+
KillCouchbase stops the couchbase-server process on the specified nodes.
1336+
The runSv supervisor will automatically restart couchbase-server within 10 seconds.
1337+
This method is used to simulate a stop and restart of the couchbase server.
1338+
*/
1339+
func (d *Deployer) KillCouchbase(ctx context.Context, clusterID string, nodeIDs []string) error {
1340+
var nodeContainerIDs []string
1341+
1342+
for _, nodeId := range nodeIDs {
1343+
node, err := d.getNode(ctx, clusterID, nodeId)
1344+
if err != nil {
1345+
return errors.Wrap(err, "failed to get node")
1346+
}
1347+
1348+
nodeContainerIDs = append(nodeContainerIDs, node.ContainerID)
1349+
}
1350+
if len(nodeIDs) == 0 {
1351+
clusterInfo, err := d.getCluster(ctx, clusterID)
1352+
if err != nil {
1353+
return errors.Wrap(err, "failed to get cluster info")
1354+
}
1355+
1356+
for _, node := range clusterInfo.Nodes {
1357+
nodeContainerIDs = append(nodeContainerIDs, node.ContainerID)
1358+
}
1359+
}
1360+
1361+
for _, nodeContainerID := range nodeContainerIDs {
1362+
d.logger.Info("killing couchbase process on node",
1363+
zap.String("containerID", nodeContainerID))
1364+
1365+
err := d.execInContainer(ctx, nodeContainerID, []string{"pkill", "-f", "couchbase-server"})
1366+
if err != nil {
1367+
return errors.Wrapf(err, "failed to kill couchbase process on node %s", nodeContainerID)
1368+
}
1369+
}
1370+
1371+
return nil
1372+
}
1373+
13041374
func (d *Deployer) RedeployCluster(ctx context.Context, clusterID string) error {
13051375
return errors.New("docker deploy does not support redeploy cluster")
13061376
}

deployment/localdeploy/deployer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,7 @@ func (d *Deployer) SetNodeRecovery(ctx context.Context, clusterID string, nodeID
271271
func (d *Deployer) RebalanceCluster(ctx context.Context, clusterID string, nodesToEject []string) error {
272272
return errors.New("localdeploy does not support rebalance cluster")
273273
}
274+
275+
func (d *Deployer) KillCouchbase(ctx context.Context, clusterID string, nodes []string) error {
276+
return errors.New("localdeploy does not support killing couchbase process")
277+
}

0 commit comments

Comments
 (0)