From 9b80bd1a137a5bc4523541ed5a889d33d0d6f1c4 Mon Sep 17 00:00:00 2001 From: WinfredLIN Date: Fri, 29 Nov 2024 16:59:28 +0800 Subject: [PATCH] modify: according to peer review modify: implementation of backup for grpc server modify: add logs modify: add comments and rename backup to Backup modify: error stack --- sqle/driver/plugin_adapter_v2.go | 30 ++++++++++++++++-- sqle/driver/v2/driver_grpc_server.go | 47 ++++++++++++++++++++++++++-- sqle/driver/v2/driver_interface.go | 7 +++++ sqle/driver/v2/proto/driver_v2.proto | 16 +++++----- sqle/driver/v2/util.go | 2 +- sqle/server/sqled.go | 6 ++-- 6 files changed, 91 insertions(+), 17 deletions(-) diff --git a/sqle/driver/plugin_adapter_v2.go b/sqle/driver/plugin_adapter_v2.go index 791825c1e..0e77c7453 100644 --- a/sqle/driver/plugin_adapter_v2.go +++ b/sqle/driver/plugin_adapter_v2.go @@ -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, @@ -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) { diff --git a/sqle/driver/v2/driver_grpc_server.go b/sqle/driver/v2/driver_grpc_server.go index 7dc35d0a2..eab1fff7b 100644 --- a/sqle/driver/v2/driver_grpc_server.go +++ b/sqle/driver/v2/driver_grpc_server.go @@ -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) { diff --git a/sqle/driver/v2/driver_interface.go b/sqle/driver/v2/driver_interface.go index f8b0f4db0..74464ec57 100644 --- a/sqle/driver/v2/driver_interface.go +++ b/sqle/driver/v2/driver_interface.go @@ -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 diff --git a/sqle/driver/v2/proto/driver_v2.proto b/sqle/driver/v2/proto/driver_v2.proto index e6ad05bbd..02219beaf 100644 --- a/sqle/driver/v2/proto/driver_v2.proto +++ b/sqle/driver/v2/proto/driver_v2.proto @@ -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 { diff --git a/sqle/driver/v2/util.go b/sqle/driver/v2/util.go index ab2bc43a0..53a305c9c 100644 --- a/sqle/driver/v2/util.go +++ b/sqle/driver/v2/util.go @@ -78,7 +78,7 @@ func (m OptionalModule) String() string { case OptionalModuleI18n: return "I18n" case OptionalBackup: - return "backup" + return "Backup" default: return "Unknown" } diff --git a/sqle/server/sqled.go b/sqle/server/sqled.go index 658e3dcd5..0558c77bc 100644 --- a/sqle/server/sqled.go +++ b/sqle/server/sqled.go @@ -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