Skip to content

Commit

Permalink
modify: according to peer review
Browse files Browse the repository at this point in the history
modify: implementation of backup for grpc server

modify: add logs

modify: add comments and rename backup to Backup

modify: error stack
  • Loading branch information
winfredLIN committed Nov 29, 2024
1 parent 8e9c03a commit 9b80bd1
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 17 deletions.
30 changes: 27 additions & 3 deletions sqle/driver/plugin_adapter_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,18 @@ type PluginImplV2 struct {
func (s *PluginImplV2) Backup(ctx context.Context, backupStrategy string, sql string) (BackupSql []string, ExecuteInfo string, err error) {
api := "Backup"
s.preLog(api)
var strategy protoV2.BackupStrategy = protoV2.BackupStrategy_ReverseSql
var strategy protoV2.BackupStrategy
switch backupStrategy {
case "reverse_sql":
case driverV2.BackupStrategyReverseSql:
strategy = protoV2.BackupStrategy_ReverseSql
case driverV2.BackupStrategyNone:
strategy = protoV2.BackupStrategy_None
case driverV2.BackupStrategyOriginalRow:
strategy = protoV2.BackupStrategy_OriginalRow
case driverV2.BackupStrategyManually:
strategy = protoV2.BackupStrategy_Manually
default:
return []string{}, "", fmt.Errorf("unsupported strategy %v", backupStrategy)
}
resp, err := s.client.Backup(ctx, &protoV2.BackupReq{
Session: s.Session,
Expand All @@ -195,7 +203,23 @@ func (s *PluginImplV2) Backup(ctx context.Context, backupStrategy string, sql st
}

func (p *PluginImplV2) RecommendBackupStrategy(ctx context.Context, sql string) (*RecommendBackupStrategyRes, error) {
return nil, nil
api := "RecommendBackupStrategy"
p.preLog(api)

resp, err := p.client.RecommendBackupStrategy(ctx, &protoV2.RecommendBackupStrategyReq{
Session: p.Session,
Sql: sql,
})
p.afterLog(api, err)
if err != nil {
return nil, err
}
return &RecommendBackupStrategyRes{
BackupStrategy: resp.BackupStrategy.String(),
BackupStrategyTip: resp.BackupStrategyTip,
TablesRefer: resp.TablesRefer,
SchemasRefer: resp.SchemasRefer,
}, nil
}

func (s *PluginImplV2) preLog(ApiName string) {
Expand Down
47 changes: 45 additions & 2 deletions sqle/driver/v2/driver_grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,55 @@ func (d *DriverGrpcServer) getDriverBySession(session *protoV2.Session) (Driver,
return driver, nil

}

func (d *DriverGrpcServer) Backup(ctx context.Context, req *protoV2.BackupReq) (*protoV2.BackupRes, error) {
return nil, nil
driver, err := d.getDriverBySession(req.Session)
if err != nil {
return &protoV2.BackupRes{}, err
}
res, err := driver.Backup(ctx, &BackupReq{
BackupStrategy: req.BackupStrategy.String(),
Sql: req.Sql,
})
if err != nil {
return nil, errors.Wrap(err, "backup")
}
return &protoV2.BackupRes{
BackupSql: res.BackupSql,
ExecuteInfo: res.ExecuteInfo,
}, nil
}

func (d *DriverGrpcServer) RecommendBackupStrategy(ctx context.Context, req *protoV2.RecommendBackupStrategyReq) (*protoV2.RecommendBackupStrategyRes, error) {
return nil, nil
driver, err := d.getDriverBySession(req.Session)
if err != nil {
return &protoV2.RecommendBackupStrategyRes{}, err
}
res, err := driver.RecommendBackupStrategy(ctx, &RecommendBackupStrategyReq{
Sql: req.Sql,
})
if err != nil {
return &protoV2.RecommendBackupStrategyRes{}, errors.Wrap(err, "backup")
}
var backupStrategyProtoV2 protoV2.BackupStrategy
switch res.BackupStrategy {
case BackupStrategyReverseSql:
backupStrategyProtoV2 = protoV2.BackupStrategy_ReverseSql
case BackupStrategyManually:
backupStrategyProtoV2 = protoV2.BackupStrategy_Manually
case BackupStrategyNone:
backupStrategyProtoV2 = protoV2.BackupStrategy_None
case BackupStrategyOriginalRow:
backupStrategyProtoV2 = protoV2.BackupStrategy_OriginalRow
default:
return nil, fmt.Errorf("unsupported strategy %v", res.BackupStrategy)
}
return &protoV2.RecommendBackupStrategyRes{
BackupStrategy: backupStrategyProtoV2,
BackupStrategyTip: res.BackupStrategyTip,
TablesRefer: res.TablesRefer,
SchemasRefer: res.SchemasRefer,
}, nil
}

func (d *DriverGrpcServer) Metas(ctx context.Context, req *protoV2.Empty) (*protoV2.MetasResponse, error) {
Expand Down
7 changes: 7 additions & 0 deletions sqle/driver/v2/driver_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ type Driver interface {
RecommendBackupStrategy(ctx context.Context, req *RecommendBackupStrategyReq) (*RecommendBackupStrategyRes, error)
}

const (
BackupStrategyNone string = "none" // 不备份(不支持备份、无需备份、选择不备份)
BackupStrategyReverseSql string = "reverse_sql" // 备份为反向SQL
BackupStrategyOriginalRow string = "original_row" // 备份为原始行
BackupStrategyManually string = "manual" // 标记为人工备份
)

type BackupReq struct {
BackupStrategy string
Sql string
Expand Down
16 changes: 8 additions & 8 deletions sqle/driver/v2/proto/driver_v2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ message RecommendBackupStrategyReq {
}

message RecommendBackupStrategyRes {
BackupStrategy backupStrategy = 1;
string backupStrategyTip = 2;
repeated string tablesRefer =3;
repeated string schemasRefer =4;
BackupStrategy backupStrategy = 1; // 备份策略
string backupStrategyTip = 2; // 推荐备份策略的原因
repeated string tablesRefer =3; // sql引用的表,用于备份
repeated string schemasRefer =4; // sql引用的数据库,用于备份
}

enum BackupStrategy {
None = 0;
ReverseSql = 1;
OriginalRow = 2;
Manually = 3;
None = 0; // 备份策略为不备份
ReverseSql = 1; // 备份策略为反向SQL
OriginalRow = 2; // 备份策略为备份原始行
Manually = 3; // 备份策略为人工手动备份
}

enum OptionalModule {
Expand Down
2 changes: 1 addition & 1 deletion sqle/driver/v2/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (m OptionalModule) String() string {
case OptionalModuleI18n:
return "I18n"
case OptionalBackup:
return "backup"
return "Backup"
default:
return "Unknown"
}
Expand Down
6 changes: 3 additions & 3 deletions sqle/server/sqled.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,14 +488,14 @@ func (a *action) backupAndExecSql() error {
if svc.CheckCanTaskBackup(a.task) {
backupTask, err := toBackupTask(a.plugin, executeSQL)
if err != nil {
return err
return fmt.Errorf("in backupAndExecSql when convert toBackupTask, err %w , backup task: %v, task: %v", err, executeSQL.BackupTask.ID, a.task.ID)
}
if err = backupTask.Backup(); err != nil {
return err
return fmt.Errorf("in backupAndExecSql when backupTask Backup, err %w, backup task: %v, task: %v", err, executeSQL.BackupTask.ID, a.task.ID)
}
}
if err := a.execSQL(executeSQL); err != nil {
return err
return fmt.Errorf("in backupAndExecSql when execSQL %v, err %w, backup task: %v, task: %v", executeSQL, err, executeSQL.BackupTask.ID, a.task.ID)
}
}
return nil
Expand Down

0 comments on commit 9b80bd1

Please sign in to comment.