Skip to content

Commit

Permalink
feat: cli restore 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 01bd32a commit 4f1bca9
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 14 deletions.
11 changes: 9 additions & 2 deletions pitr/cli/internal/cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strings"
"time"

"github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/cmd/view"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/model"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/xerr"
Expand Down Expand Up @@ -94,6 +95,8 @@ func restore() error {
return xerr.NewCliErr(fmt.Sprintf("new ss-proxy failed, err:%s", err.Error()))
}

as := pkg.NewAgentServer(fmt.Sprintf("%s:%d", Host, AgentPort))

// get backup record
var bak *model.LsBackup
if CSN != "" {
Expand Down Expand Up @@ -133,7 +136,7 @@ func restore() error {

logging.Info("Restore backup data to openGauss success!")
// restore metadata to ss-proxy
if err := restoreDataToSSProxy(proxy, bak); err != nil {
if err := restoreDataToSSProxy(proxy, as, bak); err != nil {
return xerr.NewCliErr(fmt.Sprintf("restore metadata to ss-proxy failed:%s", err.Error()))
}
logging.Info("Restore success!")
Expand Down Expand Up @@ -164,7 +167,7 @@ func checkDatabaseExist(proxy pkg.IShardingSphereProxy, bak *model.LsBackup) err
return getUserApproveInTerminal(prompt)
}

func restoreDataToSSProxy(proxy pkg.IShardingSphereProxy, lsBackup *model.LsBackup) error {
func restoreDataToSSProxy(proxy pkg.IShardingSphereProxy, as pkg.IAgentServer, lsBackup *model.LsBackup) error {
// drop database if exists
for _, shardingDBName := range databaseNamesExist {
logging.Info(fmt.Sprintf("Dropping database: [%s] ...", shardingDBName))
Expand All @@ -173,6 +176,10 @@ func restoreDataToSSProxy(proxy pkg.IShardingSphereProxy, lsBackup *model.LsBack
}
}

if err := as.RestartShardingSphere(&view.RestartShardingSphereIn{Args: []string{"3308"}}); err != nil {
return xerr.NewCliErr(fmt.Sprintf("restart ss-proxy failed:%s", err.Error()))
}

// import metadata
if err := proxy.ImportMetaData(lsBackup.SsBackup.ClusterInfo); err != nil {
return xerr.NewCliErr(fmt.Sprintf("Import metadata to ss-proxy failed:%s", err.Error()))
Expand Down
28 changes: 28 additions & 0 deletions pitr/cli/internal/cmd/view/shardingsphere.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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"`
}

type RestartShardingSphereOut struct {
Code int `json:"code" validate:"required"`
Msg string `json:"msg" validate:"required"`
Data string `json:"data"`
}
47 changes: 35 additions & 12 deletions pitr/cli/internal/pkg/agent-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"net/http"

"github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/cmd/view"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/model"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/xerr"
"github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/httputils"
Expand All @@ -30,12 +31,13 @@ import (
type agentServer struct {
addr string

_apiBackup string
_apiRestore string
_apiShowDetail string
_apiShowList string
_apiDiskspace string
_apiHealthCheck string
_apiBackup string
_apiRestore string
_apiShowDetail string
_apiShowList string
_apiDiskspace string
_apiHealthCheck string
_apiRestartShardingSphere string
}

type IAgentServer interface {
Expand All @@ -46,6 +48,7 @@ type IAgentServer interface {
ShowDetail(in *model.ShowDetailIn) (*model.BackupInfo, error)
ShowList(in *model.ShowListIn) ([]model.BackupInfo, error)
ShowDiskSpace(in *model.DiskSpaceIn) (*model.DiskSpaceInfo, error)
RestartShardingSphere(in *view.RestartShardingSphereIn) error
}

var _ IAgentServer = (*agentServer)(nil)
Expand All @@ -54,12 +57,13 @@ func NewAgentServer(addr string) IAgentServer {
return &agentServer{
addr: addr,

_apiBackup: "/api/backup",
_apiRestore: "/api/restore",
_apiShowDetail: "/api/show",
_apiShowList: "/api/show/list",
_apiDiskspace: "/api/diskspace",
_apiHealthCheck: "/api/healthz",
_apiBackup: "/api/backup",
_apiRestore: "/api/restore",
_apiShowDetail: "/api/show",
_apiShowList: "/api/show/list",
_apiDiskspace: "/api/diskspace",
_apiHealthCheck: "/api/healthz",
_apiRestartShardingSphere: "/api/shardingsphere/restart",
}
}

Expand Down Expand Up @@ -185,3 +189,22 @@ func (as *agentServer) DeleteBackup(in *model.DeleteBackupIn) error {

return nil
}

// nolint:dupl
func (as *agentServer) RestartShardingSphere(in *view.RestartShardingSphereIn) error {
url := fmt.Sprintf("%s%s", as.addr, as._apiBackup)

out := &view.RestartShardingSphereOut{}
r := httputils.NewRequest(context.Background(), http.MethodPost, url)
r.Body(in)

if err := r.Send(out); err != nil {
return xerr.NewUnknownErr(url, in, out, err)
}

if out.Code != 0 {
return xerr.NewAgentServerErr(out.Code, out.Msg)
}

return nil
}
7 changes: 7 additions & 0 deletions pitr/cli/internal/pkg/shardingsphere-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type (
Unlock() error
ImportMetaData(in *model.ClusterInfo) error
DropDatabase(shardingDBName string) error
Restart() error
}
)

Expand Down Expand Up @@ -224,3 +225,9 @@ func (ss *shardingSphereProxy) DropDatabase(databaseName string) error {
}
return nil
}

// Restart 重启 ShardingSphere Proxy
func (ss *shardingSphereProxy) Restart() error {

return nil
}

0 comments on commit 4f1bca9

Please sign in to comment.