You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by mi...@apache.org on 2023/02/17 09:41:48 UTC

[shardingsphere-on-cloud] branch main updated: feat: gs_ctl start|stop and test it (#211)

This is an automated email from the ASF dual-hosted git repository.

miaoliyao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/shardingsphere-on-cloud.git


The following commit(s) were added to refs/heads/main by this push:
     new c578da7  feat: gs_ctl start|stop and test it (#211)
c578da7 is described below

commit c578da715232cf97408928b2778847e21ed009a0
Author: lltgo <ll...@outlook.com>
AuthorDate: Fri Feb 17 17:41:43 2023 +0800

    feat: gs_ctl start|stop and test it (#211)
    
    * feat: gs_ctl start|stop and test it
    
    * chore:update error code
---
 pitr/agent/internal/cons/error.go         |  2 ++
 pitr/agent/internal/pkg/opengauss.go      | 39 +++++++++++++++++++++++++++----
 pitr/agent/internal/pkg/opengauss_test.go | 18 ++++++++++++++
 3 files changed, 54 insertions(+), 5 deletions(-)

diff --git a/pitr/agent/internal/cons/error.go b/pitr/agent/internal/cons/error.go
index fd5a349..3868f65 100644
--- a/pitr/agent/internal/cons/error.go
+++ b/pitr/agent/internal/cons/error.go
@@ -30,4 +30,6 @@ var (
 	NoPermission           = xerror.New(10005, "No permission to operate.")
 	InstanceAlreadyExist   = xerror.New(10006, "The instance already exist.")
 	InstanceNotExist       = xerror.New(10007, "The instance not exist.")
+	StartOpenGaussFailed   = xerror.New(10008, "Failed to start opengauss.")
+	StopOpenGaussFailed    = xerror.New(10009, "Failed to stop opengauss.")
 )
diff --git a/pitr/agent/internal/pkg/opengauss.go b/pitr/agent/internal/pkg/opengauss.go
index bcbe90d..5437995 100644
--- a/pitr/agent/internal/pkg/opengauss.go
+++ b/pitr/agent/internal/pkg/opengauss.go
@@ -44,6 +44,9 @@ const (
 
 	_addInstanceFmt = "gs_probackup add-instance --backup-path=%s --instance=%s --pgdata=%s 2>&1"
 	_delInstanceFmt = "gs_probackup del-instance --backup-path=%s --instance=%s 2>&1"
+
+	_startOpenGaussFmt = "gs_ctl start --pgdata=%s"
+	_stopOpenGaussFmt  = "gs_ctl stop --pgdata=%s"
 )
 
 func (og *openGauss) AsyncBackup(backupPath, instanceName, backupMode, pgData string) (string, error) {
@@ -117,6 +120,18 @@ func (og *openGauss) Init(backupPath string) error {
 	return nil
 }
 
+func (og *openGauss) deinit(backupPath string) error {
+	if !strings.HasPrefix(backupPath, "/home/omm/") {
+		return cons.NoPermission
+	}
+
+	cmd := fmt.Sprintf(_deinitFmt, backupPath)
+	if _, err := cmds.Exec(og.shell, cmd); err != nil {
+		return fmt.Errorf("cmds.Exec[shell=%s,cmd=%s] return err=%w", og.shell, cmd, err)
+	}
+	return nil
+}
+
 func (og *openGauss) AddInstance(backupPath, instancee, pgData string) error {
 	cmd := fmt.Sprintf(_addInstanceFmt, backupPath, instancee, pgData)
 	_, err := cmds.Exec(og.shell, cmd)
@@ -143,13 +158,27 @@ func (og *openGauss) DelInstance(backupPath, instancee string) error {
 	return nil
 }
 
-func (og *openGauss) deinit(backupPath string) error {
-	if !strings.HasPrefix(backupPath, "/home/omm/") {
-		return cons.NoPermission
+func (og *openGauss) Start(pgData string) error {
+	cmd := fmt.Sprintf(_startOpenGaussFmt, pgData)
+	_, err := cmds.Exec(og.shell, cmd)
+	// already exist and it's not empty
+	if errors.Is(err, cons.CmdOperateFailed) {
+		return cons.StartOpenGaussFailed
+	}
+	if err != nil {
+		return fmt.Errorf("cmds.Exec[shell=%s,cmd=%s] return err=%w", og.shell, cmd, err)
 	}
+	return nil
+}
 
-	cmd := fmt.Sprintf(_deinitFmt, backupPath)
-	if _, err := cmds.Exec(og.shell, cmd); err != nil {
+func (og *openGauss) Stop(pgData string) error {
+	cmd := fmt.Sprintf(_stopOpenGaussFmt, pgData)
+	_, err := cmds.Exec(og.shell, cmd)
+	// already exist and it's not empty
+	if errors.Is(err, cons.CmdOperateFailed) {
+		return cons.StopOpenGaussFailed
+	}
+	if err != nil {
 		return fmt.Errorf("cmds.Exec[shell=%s,cmd=%s] return err=%w", og.shell, cmd, err)
 	}
 	return nil
diff --git a/pitr/agent/internal/pkg/opengauss_test.go b/pitr/agent/internal/pkg/opengauss_test.go
index 73c54d2..fef26e8 100644
--- a/pitr/agent/internal/pkg/opengauss_test.go
+++ b/pitr/agent/internal/pkg/opengauss_test.go
@@ -141,4 +141,22 @@ var _ = Describe("OpenGauss,requires opengauss environment", func() {
 			Expect(errors.Is(err, cons.InstanceNotExist)).To(BeTrue())
 		})
 	})
+
+	Context("Start and Stop", func() {
+		It("start and stop:may fail if no instance exists", func() {
+			og := &openGauss{
+				shell: "/bin/sh",
+			}
+
+			var pgData = "/data/opengauss/3.1.1/data/single_node/"
+			err := og.Stop(pgData)
+			Expect(err).To(BeNil())
+
+			err = og.Stop(pgData)
+            Expect(errors.Is(err, cons.StopOpenGaussFailed)).To(BeTrue())
+
+			err = og.Start(pgData)
+			Expect(err).To(BeNil())
+		})
+	})
 })