Skip to content

Commit

Permalink
feat: agent support restart proxy
Browse files Browse the repository at this point in the history
Signed-off-by: mlycore <[email protected]>
  • Loading branch information
mlycore committed Dec 27, 2023
1 parent 64c54da commit 01bd32a
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 40 deletions.
64 changes: 33 additions & 31 deletions pitr/agent/internal/cons/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,37 @@ import (
)

var (
Internal = xerror.New(10000, "Internal error.")
InvalidHTTPHeader = xerror.New(10001, "Invalid http header.")
DataNotFound = xerror.New(10002, "Data not found.")
CmdOperateFailed = xerror.New(10003, "Command operate failed.")
BackupPathAlreadyExist = xerror.New(10004, "The backup path already exists.")
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.")
RestoreFailed = xerror.New(10010, "Failed to restore opengauss.")
InvalidDBPort = xerror.New(10011, "Invalid db port or missing db port.")
MissingUsername = xerror.New(10012, "Missing username")
MissingPassword = xerror.New(10013, "Missing password.")
MissingDnBackupPath = xerror.New(10014, "Missing dn backup path.")
InvalidDnThreadsNum = xerror.New(10015, "Invalid dn threads num.")
MissingDnBackupMode = xerror.New(10016, "Missing dn backup mode.")
InvalidDnBackupMode = xerror.New(10017, "Invalid dn backup mode.")
MissingInstance = xerror.New(10018, "Missing instance.")
MissingDnBackupID = xerror.New(10019, "Missing dn backup id.")
BodyParseFailed = xerror.New(10020, "Invalid http request body.")
MissingDBName = xerror.New(10021, "Missing db name.")
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.")
MvPgDataToTempFailed = xerror.New(10026, "Move pgdata dir to temp failed.")
MvTempToPgDataFailed = xerror.New(10027, "Move temp dir to pgdata failed.")
CleanPgDataTempFailed = xerror.New(10028, "Clean pgdata temp dir failed.")
MissingDiskPath = xerror.New(10029, "Missing disk path.")
MissingBackupID = xerror.New(10030, "Missing backup id.")
Internal = xerror.New(10000, "Internal error.")
InvalidHTTPHeader = xerror.New(10001, "Invalid http header.")
DataNotFound = xerror.New(10002, "Data not found.")
CmdOperateFailed = xerror.New(10003, "Command operate failed.")
BackupPathAlreadyExist = xerror.New(10004, "The backup path already exists.")
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.")
RestoreFailed = xerror.New(10010, "Failed to restore opengauss.")
InvalidDBPort = xerror.New(10011, "Invalid db port or missing db port.")
MissingUsername = xerror.New(10012, "Missing username")
MissingPassword = xerror.New(10013, "Missing password.")
MissingDnBackupPath = xerror.New(10014, "Missing dn backup path.")
InvalidDnThreadsNum = xerror.New(10015, "Invalid dn threads num.")
MissingDnBackupMode = xerror.New(10016, "Missing dn backup mode.")
InvalidDnBackupMode = xerror.New(10017, "Invalid dn backup mode.")
MissingInstance = xerror.New(10018, "Missing instance.")
MissingDnBackupID = xerror.New(10019, "Missing dn backup id.")
BodyParseFailed = xerror.New(10020, "Invalid http request body.")
MissingDBName = xerror.New(10021, "Missing db name.")
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.")
MvPgDataToTempFailed = xerror.New(10026, "Move pgdata dir to temp failed.")
MvTempToPgDataFailed = xerror.New(10027, "Move temp dir to pgdata failed.")
CleanPgDataTempFailed = xerror.New(10028, "Clean pgdata temp dir failed.")
MissingDiskPath = xerror.New(10029, "Missing disk path.")
MissingBackupID = xerror.New(10030, "Missing backup id.")
StopShardingSphereFailed = xerror.New(10031, "Stop ShardingSphere failed.")
StartShardingSphereFailed = xerror.New(10031, "Start ShardingSphere failed.")
)
45 changes: 45 additions & 0 deletions pitr/agent/internal/handler/shardingsphere_restart.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package handler

import (
"fmt"

"github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/cons"
"github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/handler/view"
"github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/pkg"
"github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/responder"
"github.com/gofiber/fiber/v2"
)

func RestartShardingSphere(ctx *fiber.Ctx) (err error) {
in := &view.RestartShardingSphereIn{}

if err = ctx.BodyParser(in); err != nil {
err = fmt.Errorf("body parse err=%s,wrap=%w", err, cons.BodyParseFailed)
return
}

// restart shardingsphere
if err = pkg.ShardingSphere.Restart(in.Args); err != nil {
err = fmt.Errorf("stop ShardingSphere failure, err=%w", err)
return
}

return responder.Success(ctx, nil)
}
23 changes: 23 additions & 0 deletions pitr/agent/internal/handler/view/shardingsphere.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package view

type RestartShardingSphereIn struct {
Args []string `json:"args,omitempty"`
}
type RestartShardingSphereOut struct{}
9 changes: 7 additions & 2 deletions pitr/agent/internal/pkg/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ package pkg
import "github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/logging"

var (
OG IOpenGauss
OG IOpenGauss
ShardingSphere IShardingSphere
)

func Init(shell, pgData string, log logging.ILog) {
func InitOG(shell, pgData string, log logging.ILog) {
OG = NewOpenGauss(shell, pgData, log)
}

func InitShardingSphere(shell, path string, log logging.ILog) {
ShardingSphere = NewShardingSphere(shell, path, log)
}
87 changes: 87 additions & 0 deletions pitr/agent/internal/pkg/shardingsphere.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package pkg

import (
"errors"
"fmt"
"strings"

"github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/cons"
"github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/cmds"
"github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/logging"
)

type (
shardingsphere struct {
shell string
path string
log logging.ILog
}

IShardingSphere interface {
Restart(args []string) error
}
)

var _ IShardingSphere = (*shardingsphere)(nil)

func NewShardingSphere(shell, path string, log logging.ILog) IShardingSphere {
return &shardingsphere{
shell: shell,
path: path,
log: log,
}
}

const (
_stopFmt = "%s/bin/stop.sh"
_startFmt = "%s/bin/start.sh %s"
)

func (s *shardingsphere) Restart(args []string) error {
var argstr string
if len(args) != 0 {
argstr = strings.Join(args, " ")
}

stopcmd := fmt.Sprintf(_stopFmt, s.path)
output, err := cmds.Exec(s.shell, stopcmd)
s.log.Debug(fmt.Sprintf("stop shardingsphere output[msg=%s,err=%v]", output, err))

if errors.Is(err, cons.CmdOperateFailed) {
return fmt.Errorf("stop shardingsphere failure,err=%s,wrap=%w", err, cons.StopShardingSphereFailed)
}
if err != nil {
return fmt.Errorf("cmds.Exec[shell=%s,cmd=%s] return err=%w", s.shell, stopcmd, err)
}

startcmd := fmt.Sprintf(_startFmt, s.path, argstr)
output, err = cmds.Exec(s.shell, startcmd)
s.log.Debug(fmt.Sprintf("start shardingsphere output[msg=%s,err=%v]", output, err))

if errors.Is(err, cons.CmdOperateFailed) {
return fmt.Errorf("start shardingsphere failure,err=%s,wrap=%w", err, cons.StartShardingSphereFailed)
}
if err != nil {
return fmt.Errorf("cmds.Exec[shell=%s,cmd=%s] return err=%w", s.shell, startcmd, err)
}

return nil

}
18 changes: 11 additions & 7 deletions pitr/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ var (
)

var (
logLevel string
port string
pgData string
tlsCrt string
tlsKey string
envSourceFile string
logLevel string
port string
pgData string
tlsCrt string
tlsKey string
envSourceFile string
shardingspherePath string
)

func init() {
Expand All @@ -64,6 +65,7 @@ func init() {
flag.StringVar(&pgData, "pgdata", "", "Optional:Get the value from cli flags or env")

flag.StringVar(&envSourceFile, "env-source-file", "", "Optional:env source file path")
flag.StringVar(&shardingspherePath, "ss-path", "", "Optional:shardingsphere home")
}

func main() {
Expand Down Expand Up @@ -116,7 +118,8 @@ func main() {
}

log = logging.Init(level)
pkg.Init(shell, pgData, log)
pkg.InitOG(shell, pgData, log)
pkg.InitShardingSphere(shell, shardingspherePath, log)

SetupApp()

Expand Down Expand Up @@ -168,6 +171,7 @@ func SetupApp() {
r.Post("/show", handler.Show)
r.Post("/show/list", handler.ShowList)
r.Post("/diskspace", handler.DiskSpace)
r.Post("/shardingsphere/restart", handler.RestartShardingSphere)
})

// 404
Expand Down

0 comments on commit 01bd32a

Please sign in to comment.