Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:reload all storages #6836

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions internal/op/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package op

import (
"context"
"slices"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -190,6 +191,23 @@ func UpdateStorage(ctx context.Context, storage model.Storage) error {
log.Debugf("storage %+v is update", storageDriver)
return err
}
func DeleteMemoryStorage(dbMountPaths []string) {
keys := storagesMap.Keys()
for _, v := range keys {
if slices.Contains(dbMountPaths, v) {
continue
}
storageDriver, ok := storagesMap.Load(v)
if !ok {
continue
}
//ignore err
storageDriver.Drop(context.Background())
//delete
storagesMap.Delete(v)

}
}

func DeleteStorageById(ctx context.Context, id uint) error {
storage, err := db.GetStorageById(id)
Expand Down
10 changes: 10 additions & 0 deletions pkg/generic_sync/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,16 @@ func (m *MapOf[K, V]) Values() []V {
return values
}

// Keys returns a slice of the keys in the map.
func (m *MapOf[K, V]) Keys() []K {
var keys []K
m.Range(func(key K, value V) bool {
keys = append(keys, key)
return true
})
return keys
}

func (m *MapOf[K, V]) Count() int {
return len(m.dirty)
}
Expand Down
20 changes: 12 additions & 8 deletions server/handles/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,18 @@ func LoadAllStorages(c *gin.Context) {
}
conf.StoragesLoaded = false
go func(storages []model.Storage) {
//current mount paths in db
var mountPaths []string
for _, storage := range storages {
mountPaths = append(mountPaths, storage.MountPath)
//GetStorageByMountPath used old storagesMap,if err is not nil,
// the mountPath is added by other server , just do LoadStorage
storageDriver, err := op.GetStorageByMountPath(storage.MountPath)
if err != nil {
log.Errorf("failed get storage driver: %+v", err)
continue
}
// drop the storage in the driver
if err := storageDriver.Drop(context.Background()); err != nil {
log.Errorf("failed drop storage: %+v", err)
continue
if err == nil {
if err := storageDriver.Drop(context.Background()); err != nil {
log.Errorf("failed drop storage: %+v", err)
continue
}
}
if err := op.LoadStorage(context.Background(), storage); err != nil {
log.Errorf("failed get enabled storages: %+v", err)
Expand All @@ -146,6 +148,8 @@ func LoadAllStorages(c *gin.Context) {
log.Infof("success load storage: [%s], driver: [%s]",
storage.MountPath, storage.Driver)
}
//deal the keys in storagesMap, but not in mountPaths
op.DeleteMemoryStorage(mountPaths)
conf.StoragesLoaded = true
}(storages)
common.SuccessResp(c)
Expand Down