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 a326b47  feat(pitr):add gs_ctl func for IOpenGauss (#237)
a326b47 is described below

commit a326b4772f02b01b281e8b545844de5c8f338436
Author: lltgo <[email protected]>
AuthorDate: Thu Mar 2 15:52:26 2023 +0800

    feat(pitr):add gs_ctl func for IOpenGauss (#237)
    
    * feat(pitr):add gs_ctl func for IOpenGauss
    
    * chore:update err code
    
    * chore: update comments
---
 pitr/agent/internal/cons/error.go         |  1 +
 pitr/agent/internal/pkg/opengauss.go      | 47 ++++++++++++++++++++++++-------
 pitr/agent/internal/pkg/opengauss_test.go | 15 +++++++++-
 3 files changed, 52 insertions(+), 11 deletions(-)

diff --git a/pitr/agent/internal/cons/error.go 
b/pitr/agent/internal/cons/error.go
index 676fd11..4596a0c 100644
--- a/pitr/agent/internal/cons/error.go
+++ b/pitr/agent/internal/cons/error.go
@@ -47,4 +47,5 @@ var (
        DbConnectionFailed     = xerror.New(10022, "Database connection 
failed.")
        UnmatchBackupID        = xerror.New(10023, "Unmatch any backup id.")
        InvalidPgDataDir       = xerror.New(10024, "Invalid PGDATA dir.")
+       UnknownOgStatus        = xerror.New(10025, "Unknown openGauss status.")
 )
diff --git a/pitr/agent/internal/pkg/opengauss.go 
b/pitr/agent/internal/pkg/opengauss.go
index 5d7b333..3d9ee28 100644
--- a/pitr/agent/internal/pkg/opengauss.go
+++ b/pitr/agent/internal/pkg/opengauss.go
@@ -45,6 +45,7 @@ type (
                DelInstance(backupPath, instance string) error
                Start() error
                Stop() error
+               Status() (string, error)
                Restore(backupPath, instance, backupID string) error
                ShowBackupList(backupPath, instanceName string) 
([]model.Backup, error)
                Auth(user, password, dbName string, dbPort uint16) error
@@ -72,6 +73,7 @@ const (
 
        _startOpenGaussFmt = "gs_ctl start --pgdata=%s"
        _stopOpenGaussFmt  = "gs_ctl stop --pgdata=%s"
+       _statusGaussFmt    = "gs_ctl status --pgdata=%s"
 
        _showListFmt = "gs_probackup show --instance=%s --backup-path=%s 
--format=json 2>&1"
 )
@@ -139,7 +141,7 @@ func (og *openGauss) Init(backupPath string) error {
        _, err := cmds.Exec(og.shell, cmd)
        // already exist and it's not empty
        if errors.Is(err, cons.CmdOperateFailed) {
-               return cons.BackupPathAlreadyExist
+               return fmt.Errorf("init backup path failure,err=%s,wrap=%w", 
err, cons.BackupPathAlreadyExist)
        }
        if err != nil {
                return fmt.Errorf("cmds.Exec[shell=%s,cmd=%s] return err=%w", 
og.shell, cmd, err)
@@ -162,9 +164,8 @@ func (og *openGauss) deinit(backupPath string) error {
 func (og *openGauss) AddInstance(backupPath, instance string) error {
        cmd := fmt.Sprintf(_addInstanceFmt, backupPath, instance, og.pgData)
        _, err := cmds.Exec(og.shell, cmd)
-       // already exist and it's not empty
        if errors.Is(err, cons.CmdOperateFailed) {
-               return cons.InstanceAlreadyExist
+               return fmt.Errorf("add instance failure,err=%s,wrap=%w", err, 
cons.InstanceAlreadyExist)
        }
        if err != nil {
                return fmt.Errorf("cmds.Exec[shell=%s,cmd=%s] return err=%w", 
og.shell, cmd, err)
@@ -177,7 +178,7 @@ func (og *openGauss) DelInstance(backupPath, instancee 
string) error {
        _, err := cmds.Exec(og.shell, cmd)
        // already exist and it's not empty
        if errors.Is(err, cons.CmdOperateFailed) {
-               return cons.InstanceNotExist
+               return fmt.Errorf("delte instance failure,err=%s,wrap=%w", err, 
cons.InstanceNotExist)
        }
        if err != nil {
                return fmt.Errorf("cmds.Exec[shell=%s,cmd=%s] return err=%w", 
og.shell, cmd, err)
@@ -188,9 +189,8 @@ func (og *openGauss) DelInstance(backupPath, instancee 
string) error {
 func (og *openGauss) Start() error {
        cmd := fmt.Sprintf(_startOpenGaussFmt, og.pgData)
        _, err := cmds.Exec(og.shell, cmd)
-       // already exist and it's not empty
        if errors.Is(err, cons.CmdOperateFailed) {
-               return cons.StartOpenGaussFailed
+               return fmt.Errorf("starat openGauss failure,err=%s,wrap=%w", 
err, cons.StartOpenGaussFailed)
        }
        if err != nil {
                return fmt.Errorf("cmds.Exec[shell=%s,cmd=%s] return err=%w", 
og.shell, cmd, err)
@@ -201,9 +201,8 @@ func (og *openGauss) Start() error {
 func (og *openGauss) Stop() error {
        cmd := fmt.Sprintf(_stopOpenGaussFmt, og.pgData)
        _, err := cmds.Exec(og.shell, cmd)
-       // already exist and it's not empty
        if errors.Is(err, cons.CmdOperateFailed) {
-               return cons.StopOpenGaussFailed
+               return fmt.Errorf("stop openGauss failure,err=%s,wrap=%w", err, 
cons.StopOpenGaussFailed)
        }
        if err != nil {
                return fmt.Errorf("cmds.Exec[shell=%s,cmd=%s] return err=%w", 
og.shell, cmd, err)
@@ -211,6 +210,34 @@ func (og *openGauss) Stop() error {
        return nil
 }
 
+/*
+Status return openGauss server status:
+
+               `Running`  return "Runnging",nil
+           `Stopped`   return "Stopped",nil
+
+The others are abnormal states,return "" and error.
+*/
+func (og *openGauss) Status() (string, error) {
+       cmd := fmt.Sprintf(_statusGaussFmt, og.pgData)
+       output, err := cmds.Exec(og.shell, cmd)
+       if errors.Is(err, cons.CmdOperateFailed) {
+               if strings.Contains(err.Error(), "no server running") {
+                       return "Stopped", nil
+               }
+               return "", fmt.Errorf("get openGauss status 
failure,err=[%s],wrap=%w", err, cons.StopOpenGaussFailed)
+       }
+       if err != nil {
+               return "", fmt.Errorf("cmds.Exec[shell=%s,cmd=%s] return 
err=%w", og.shell, cmd, err)
+       }
+
+       if strings.Contains(output, "server is running") {
+               return "Running", nil
+       }
+
+       return "", cons.UnknownOgStatus
+}
+
 // Restore TODO:Dependent environments require integration testing
 func (og *openGauss) Restore(backupPath, instance, backupID string) error {
        if len(og.pgData) < 2 && strings.HasPrefix(og.pgData, "/") {
@@ -218,14 +245,14 @@ func (og *openGauss) Restore(backupPath, instance, 
backupID string) error {
        }
 
        if _, err := cmds.Exec(og.shell, fmt.Sprintf(_rmDirFmt, og.pgData)); 
err != nil {
-               return fmt.Errorf("rm PGDATA dir failure,err=%s,wrap=%s", err, 
cons.RestoreFailed)
+               return fmt.Errorf("rm PGDATA dir failure,err=%s,wrap=%w", err, 
cons.RestoreFailed)
        }
 
        cmd := fmt.Sprintf(_restoreFmt, backupPath, instance, backupID, 
og.pgData)
        outputs, err := cmds.AsyncExec(og.shell, cmd)
 
        for output := range outputs {
-        // TODO just for dev,rm in next commit
+               // TODO just for dev,rm in next commit
                fmt.Println(output.Message)
                if errors.Is(err, cons.CmdOperateFailed) {
                        return fmt.Errorf("outputs get err=%s,wrap=%w", 
output.Error, cons.RestoreFailed)
diff --git a/pitr/agent/internal/pkg/opengauss_test.go 
b/pitr/agent/internal/pkg/opengauss_test.go
index 9656f20..afea562 100644
--- a/pitr/agent/internal/pkg/opengauss_test.go
+++ b/pitr/agent/internal/pkg/opengauss_test.go
@@ -151,14 +151,27 @@ var _ = Describe("OpenGauss,requires opengauss 
environment", func() {
                                pgData: 
"/data/opengauss/3.1.1/data/single_node/",
                        }
 
-                       err := og.Stop()
+                       status, err := og.Status()
                        Expect(err).To(BeNil())
+                       Expect(status).To(Equal("Running"))
+
+                       err = og.Stop()
+                       Expect(err).To(BeNil())
+                       status, err = og.Status()
+                       Expect(err).To(BeNil())
+                       Expect(status).To(Equal("Stopped"))
 
                        err = og.Stop()
                        Expect(errors.Is(err, 
cons.StopOpenGaussFailed)).To(BeTrue())
+                       status, err = og.Status()
+                       Expect(err).To(BeNil())
+                       Expect(status).To(Equal("Stopped"))
 
                        err = og.Start()
                        Expect(err).To(BeNil())
+                       status, err = og.Status()
+                       Expect(err).To(BeNil())
+                       Expect(status).To(Equal("Running"))
                })
        })
 

Reply via email to