From 2acedf65c401311e78cf442758df55c44e449f82 Mon Sep 17 00:00:00 2001 From: Sandy Xu Date: Wed, 17 Apr 2024 16:27:25 +0800 Subject: [PATCH] cmd: shutdown object storage before service exits (#4716) --- cmd/gateway.go | 4 +++- cmd/mount.go | 9 +++++++-- cmd/mount_unix.go | 4 +++- cmd/mount_windows.go | 3 ++- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/cmd/gateway.go b/cmd/gateway.go index 81867196bd88..8f1953c97a2d 100644 --- a/cmd/gateway.go +++ b/cmd/gateway.go @@ -22,7 +22,6 @@ package cmd import ( "context" "errors" - "github.com/juicedata/juicefs/pkg/utils" _ "net/http/pprof" "os" "os/signal" @@ -33,6 +32,8 @@ import ( "github.com/juicedata/juicefs/pkg/chunk" "github.com/juicedata/juicefs/pkg/fs" "github.com/juicedata/juicefs/pkg/meta" + "github.com/juicedata/juicefs/pkg/object" + "github.com/juicedata/juicefs/pkg/utils" "github.com/juicedata/juicefs/pkg/vfs" jfsgateway "github.com/juicedata/juicefs/pkg/gateway" @@ -247,6 +248,7 @@ func initForSvc(c *cli.Context, mp string, metaUrl string) (*vfs.Config, *fs.Fil if err := metaCli.CloseSession(); err != nil { logger.Fatalf("close session failed: %s", err) } + object.Shutdown(blob) os.Exit(0) }() vfsConf := getVfsConf(c, metaConf, format, chunkConf) diff --git a/cmd/mount.go b/cmd/mount.go index a7b2374efaa0..d45a6aad6c0c 100644 --- a/cmd/mount.go +++ b/cmd/mount.go @@ -379,6 +379,10 @@ type storageHolder struct { fmt meta.Format } +func (h *storageHolder) Shutdown() { + object.Shutdown(h.ObjectStorage) +} + func NewReloadableStorage(format *meta.Format, cli meta.Meta, patch func(*meta.Format)) (object.ObjectStorage, error) { if patch != nil { patch(format) @@ -597,7 +601,7 @@ func mount(c *cli.Context) error { if err = metaCli.Shutdown(); err != nil { logger.Errorf("[pid=%d] meta shutdown: %s", os.Getpid(), err) } - object.Shutdown(blob.(*storageHolder).ObjectStorage) + object.Shutdown(blob) var foreground bool if runtime.GOOS == "windows" || !c.Bool("background") || os.Getenv("JFS_FOREGROUND") != "" { foreground = true @@ -640,7 +644,7 @@ func mount(c *cli.Context) error { store.UpdateLimit(fmt.UploadLimit, fmt.DownloadLimit) }) v := vfs.NewVFS(vfsConf, metaCli, store, registerer, registry) - installHandler(mp, v) + installHandler(mp, v, blob) v.UpdateFormat = updateFormat(c) initBackgroundTasks(c, vfsConf, metaConf, metaCli, blob, registerer, registry) mountMain(v, c) @@ -648,6 +652,7 @@ func mount(c *cli.Context) error { logger.Errorf("flush all delayed data: %s", err) } err = metaCli.CloseSession() + object.Shutdown(blob) logger.Infof("The juicefs mount process exit successfully, mountpoint: %s", metaConf.MountPoint) return err } diff --git a/cmd/mount_unix.go b/cmd/mount_unix.go index 6f105ae2a73f..613721b5f7b0 100644 --- a/cmd/mount_unix.go +++ b/cmd/mount_unix.go @@ -44,6 +44,7 @@ import ( "github.com/juicedata/godaemon" "github.com/juicedata/juicefs/pkg/fuse" "github.com/juicedata/juicefs/pkg/meta" + "github.com/juicedata/juicefs/pkg/object" "github.com/juicedata/juicefs/pkg/utils" "github.com/juicedata/juicefs/pkg/vfs" "github.com/urfave/cli/v2" @@ -619,7 +620,7 @@ func adjustOOMKiller(score int) { } } -func installHandler(mp string, v *vfs.VFS) { +func installHandler(mp string, v *vfs.VFS, blob object.ObjectStorage) { // Go will catch all the signals signal.Ignore(syscall.SIGPIPE) signalChan := make(chan os.Signal, 10) @@ -636,6 +637,7 @@ func installHandler(mp string, v *vfs.VFS) { if err != nil { logger.Fatalf("flush buffered data failed: %s", err) } + object.Shutdown(blob) logger.Warnf("exit with code 1") os.Exit(1) } else { diff --git a/cmd/mount_windows.go b/cmd/mount_windows.go index 673af614751f..f4c696004fd8 100644 --- a/cmd/mount_windows.go +++ b/cmd/mount_windows.go @@ -18,6 +18,7 @@ package cmd import ( "github.com/juicedata/juicefs/pkg/meta" + "github.com/juicedata/juicefs/pkg/object" "github.com/juicedata/juicefs/pkg/vfs" "github.com/juicedata/juicefs/pkg/winfsp" "github.com/urfave/cli/v2" @@ -68,4 +69,4 @@ func setFuseOption(c *cli.Context, format *meta.Format, vfsConf *vfs.Config) {} func launchMount(mp string, conf *vfs.Config) error { return nil } -func installHandler(mp string, v *vfs.VFS) {} +func installHandler(mp string, v *vfs.VFS, blob object.ObjectStorage) {}