Skip to content

Commit 85ccb64

Browse files
authored
feat(cluster): add cluster backup and restore functionality (#14539)
1 parent add9fed commit 85ccb64

48 files changed

Lines changed: 3356 additions & 28 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎agent/app/service/app.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ type IAppService interface {
6161
type AppInstallHooks struct {
6262
AfterCreate func(appInstall *model.AppInstall) error
6363
AfterCopyData func(appInstall *model.AppInstall) error
64+
AfterStart func(*model.AppInstall, *task.Task) error
65+
AfterFinish func()
6466
PrepareSiteDir func(model.App, model.AppDetail, request.AppInstallCreate, *task.Task) error
6567
}
6668

@@ -644,6 +646,11 @@ func (a AppService) installWithHooks(req request.AppInstallCreate, executeScript
644646
if err = upApp(t, appInstall, req.PullImage, req.UseLifecycleScripts); err != nil {
645647
return err
646648
}
649+
if hooks != nil && hooks.AfterStart != nil {
650+
if err := hooks.AfterStart(appInstall, t); err != nil {
651+
return err
652+
}
653+
}
647654
updateToolApp(appInstall)
648655
return nil
649656
}
@@ -661,6 +668,9 @@ func (a AppService) installWithHooks(req request.AppInstallCreate, executeScript
661668
installTask.AddSubTaskWithOps(task.GetTaskName(appInstall.Name, task.TaskInstall, task.TaskScopeApp), installApp, handleAppStatus, 0, installTimeout)
662669

663670
go func() {
671+
if hooks != nil && hooks.AfterFinish != nil {
672+
defer hooks.AfterFinish()
673+
}
664674
if taskErr := installTask.Execute(); taskErr != nil {
665675
appInstall.Status = constant.StatusInstallErr
666676
appInstall.Message = taskErr.Error()

‎agent/app/service/app_install.go‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,19 @@ func (a *AppInstallService) Operate(req request.AppInstalledOperate) error {
286286
if req.Operate == constant.Upgrade && constant.IsClusterApp(install.App.Key) {
287287
return buserr.New("ErrClusterAppOnly")
288288
}
289+
if constant.IsClusterApp(install.App.Key) {
290+
switch req.Operate {
291+
case constant.Start, constant.Stop, constant.Restart, constant.Rebuild, constant.Delete:
292+
unlock, err := lockClusterDataOperation(install)
293+
if err != nil {
294+
return err
295+
}
296+
defer unlock()
297+
if err := guardClusterRestore(install); err != nil {
298+
return err
299+
}
300+
}
301+
}
289302
if install.App.Key == "ollama" || install.App.Key == "vllm" {
290303
if req.Operate != constant.Upgrade {
291304
if _, loaded := appUpgradeLocks.LoadOrStore(install.ID, struct{}{}); loaded {
@@ -495,6 +508,20 @@ func (a *AppInstallService) UpdateWithBeforeRestart(req request.AppInstalledUpda
495508
if err != nil {
496509
return err
497510
}
511+
if constant.IsClusterApp(installed.App.Key) {
512+
unlock, err := lockClusterDataOperation(installed)
513+
if err != nil {
514+
return err
515+
}
516+
defer unlock()
517+
installed, err = appInstallRepo.GetFirst(repo.WithByID(req.InstallId))
518+
if err != nil {
519+
return err
520+
}
521+
if err := guardClusterRestore(installed); err != nil {
522+
return err
523+
}
524+
}
498525
if installed.App.Key == "ollama" || installed.App.Key == "vllm" {
499526
if _, loaded := appUpgradeLocks.LoadOrStore(installed.ID, struct{}{}); loaded {
500527
return buserr.New("TaskIsExecuting")

‎agent/app/service/backup_redis.go‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"path"
7+
"strconv"
78
"strings"
89
"time"
910

@@ -122,23 +123,36 @@ func handleRedisBackup(redisInfo *repo.RootInfo, parentTask *task.Task, recordID
122123
}
123124
return nil
124125
}
125-
itemTask.AddSubTask(i18n.GetMsgByKey("TaskBackup"), backupDatabase, nil)
126126
if parentTask != nil {
127127
return backupDatabase(parentTask)
128128
}
129129

130130
itemTask.AddSubTaskWithOps(i18n.GetMsgByKey("TaskBackup"), backupDatabase, nil, 3, time.Hour)
131-
go func() {
132-
if err := itemTask.Execute(); err != nil {
133-
markBackupFailed(recordID, err)
134-
return
135-
}
131+
if err := itemTask.Execute(); err != nil {
132+
return err
133+
}
134+
if recordID != 0 {
136135
backupRepo.UpdateRecordByMap(recordID, map[string]interface{}{"status": constant.StatusSuccess})
137-
}()
138-
return itemTask.Execute()
136+
}
137+
return nil
138+
}
139+
140+
func redisNeedsAOFRestoreGuard(version string) bool {
141+
major, _, _ := strings.Cut(version, ".")
142+
number, err := strconv.Atoi(major)
143+
return err != nil || number >= 8
139144
}
140145

141146
func handleRedisRecover(redisInfo *repo.RootInfo, parentTask *task.Task, recoverFile string, isRollback bool, secret, taskID string) error {
147+
if redisNeedsAOFRestoreGuard(redisInfo.Version) {
148+
appendonly, err := configGetStr(redisInfo.ContainerName, redisInfo.Password, "appendonly")
149+
if err != nil {
150+
return err
151+
}
152+
if appendonly == "yes" {
153+
return buserr.New("ErrRedisAOFRestoreUnavailable")
154+
}
155+
}
142156
var (
143157
err error
144158
itemTask *task.Task

0 commit comments

Comments
 (0)