2021-07-27 13:53:44 +08:00
|
|
|
package filer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/remote_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/remote_storage"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2021-08-05 07:25:46 +08:00
|
|
|
"google.golang.org/grpc"
|
2022-08-18 03:05:07 +08:00
|
|
|
"google.golang.org/protobuf/proto"
|
2021-07-27 13:53:44 +08:00
|
|
|
"math"
|
2021-07-27 16:16:28 +08:00
|
|
|
"strings"
|
2021-07-27 13:53:44 +08:00
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
2021-07-27 13:53:44 +08:00
|
|
|
"github.com/viant/ptrie"
|
|
|
|
)
|
|
|
|
|
2021-07-27 16:16:28 +08:00
|
|
|
const REMOTE_STORAGE_CONF_SUFFIX = ".conf"
|
|
|
|
const REMOTE_STORAGE_MOUNT_FILE = "mount.mapping"
|
|
|
|
|
2021-07-27 13:53:44 +08:00
|
|
|
type FilerRemoteStorage struct {
|
2024-04-30 09:42:31 +08:00
|
|
|
rules ptrie.Trie[*remote_pb.RemoteStorageLocation]
|
2021-08-27 06:18:34 +08:00
|
|
|
storageNameToConf map[string]*remote_pb.RemoteConf
|
2021-07-27 13:53:44 +08:00
|
|
|
}
|
|
|
|
|
2021-07-27 16:16:28 +08:00
|
|
|
func NewFilerRemoteStorage() (rs *FilerRemoteStorage) {
|
|
|
|
rs = &FilerRemoteStorage{
|
2024-04-30 09:42:31 +08:00
|
|
|
rules: ptrie.New[*remote_pb.RemoteStorageLocation](),
|
2021-08-27 06:18:34 +08:00
|
|
|
storageNameToConf: make(map[string]*remote_pb.RemoteConf),
|
2021-07-27 13:53:44 +08:00
|
|
|
}
|
2021-07-27 16:16:28 +08:00
|
|
|
return rs
|
2021-07-27 13:53:44 +08:00
|
|
|
}
|
|
|
|
|
2021-07-29 13:43:12 +08:00
|
|
|
func (rs *FilerRemoteStorage) LoadRemoteStorageConfigurationsAndMapping(filer *Filer) (err error) {
|
2021-07-27 16:16:28 +08:00
|
|
|
// execute this on filer
|
|
|
|
|
2021-08-12 13:22:49 +08:00
|
|
|
limit := int64(math.MaxInt32)
|
|
|
|
|
|
|
|
entries, _, err := filer.ListDirectoryEntries(context.Background(), DirectoryEtcRemote, "", false, limit, "", "", "")
|
2021-07-27 13:53:44 +08:00
|
|
|
if err != nil {
|
|
|
|
if err == filer_pb.ErrNotFound {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
glog.Errorf("read remote storage %s: %v", DirectoryEtcRemote, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range entries {
|
2021-07-27 16:16:28 +08:00
|
|
|
if entry.Name() == REMOTE_STORAGE_MOUNT_FILE {
|
2021-08-01 13:39:38 +08:00
|
|
|
if err := rs.loadRemoteStorageMountMapping(entry.Content); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
2021-07-27 16:16:28 +08:00
|
|
|
}
|
|
|
|
if !strings.HasSuffix(entry.Name(), REMOTE_STORAGE_CONF_SUFFIX) {
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-27 06:18:34 +08:00
|
|
|
conf := &remote_pb.RemoteConf{}
|
2021-07-27 13:53:44 +08:00
|
|
|
if err := proto.Unmarshal(entry.Content, conf); err != nil {
|
2021-07-27 16:16:28 +08:00
|
|
|
return fmt.Errorf("unmarshal %s/%s: %v", DirectoryEtcRemote, entry.Name(), err)
|
2021-07-27 13:53:44 +08:00
|
|
|
}
|
2021-07-27 16:16:28 +08:00
|
|
|
rs.storageNameToConf[conf.Name] = conf
|
2021-07-27 13:53:44 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-07-27 16:16:28 +08:00
|
|
|
|
|
|
|
func (rs *FilerRemoteStorage) loadRemoteStorageMountMapping(data []byte) (err error) {
|
2021-08-27 06:18:34 +08:00
|
|
|
mappings := &remote_pb.RemoteStorageMapping{}
|
2021-07-27 16:16:28 +08:00
|
|
|
if err := proto.Unmarshal(data, mappings); err != nil {
|
|
|
|
return fmt.Errorf("unmarshal %s/%s: %v", DirectoryEtcRemote, REMOTE_STORAGE_MOUNT_FILE, err)
|
|
|
|
}
|
2021-07-27 18:26:35 +08:00
|
|
|
for dir, storageLocation := range mappings.Mappings {
|
|
|
|
rs.mapDirectoryToRemoteStorage(util.FullPath(dir), storageLocation)
|
2021-07-27 16:16:28 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-27 06:18:34 +08:00
|
|
|
func (rs *FilerRemoteStorage) mapDirectoryToRemoteStorage(dir util.FullPath, loc *remote_pb.RemoteStorageLocation) {
|
2021-07-29 17:08:55 +08:00
|
|
|
rs.rules.Put([]byte(dir+"/"), loc)
|
2021-07-27 16:16:28 +08:00
|
|
|
}
|
|
|
|
|
2021-08-27 06:18:34 +08:00
|
|
|
func (rs *FilerRemoteStorage) FindMountDirectory(p util.FullPath) (mountDir util.FullPath, remoteLocation *remote_pb.RemoteStorageLocation) {
|
2024-04-30 09:42:31 +08:00
|
|
|
rs.rules.MatchPrefix([]byte(p), func(key []byte, value *remote_pb.RemoteStorageLocation) bool {
|
2021-08-01 13:39:38 +08:00
|
|
|
mountDir = util.FullPath(string(key[:len(key)-1]))
|
2024-04-30 09:42:31 +08:00
|
|
|
remoteLocation = value
|
2021-07-29 13:43:12 +08:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-27 06:18:34 +08:00
|
|
|
func (rs *FilerRemoteStorage) FindRemoteStorageClient(p util.FullPath) (client remote_storage.RemoteStorageClient, remoteConf *remote_pb.RemoteConf, found bool) {
|
|
|
|
var storageLocation *remote_pb.RemoteStorageLocation
|
2024-04-30 09:42:31 +08:00
|
|
|
rs.rules.MatchPrefix([]byte(p), func(key []byte, value *remote_pb.RemoteStorageLocation) bool {
|
|
|
|
storageLocation = value
|
2021-07-27 16:16:28 +08:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
2021-08-04 15:31:06 +08:00
|
|
|
if storageLocation == nil {
|
|
|
|
found = false
|
2021-07-27 16:16:28 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-04 15:31:06 +08:00
|
|
|
return rs.GetRemoteStorageClient(storageLocation.Name)
|
2021-07-29 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2021-08-27 06:18:34 +08:00
|
|
|
func (rs *FilerRemoteStorage) GetRemoteStorageClient(storageName string) (client remote_storage.RemoteStorageClient, remoteConf *remote_pb.RemoteConf, found bool) {
|
2021-07-29 13:43:12 +08:00
|
|
|
remoteConf, found = rs.storageNameToConf[storageName]
|
|
|
|
if !found {
|
2021-07-27 16:16:28 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if client, err = remote_storage.GetRemoteStorage(remoteConf); err == nil {
|
|
|
|
found = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-07-27 18:26:35 +08:00
|
|
|
|
2021-08-27 06:18:34 +08:00
|
|
|
func UnmarshalRemoteStorageMappings(oldContent []byte) (mappings *remote_pb.RemoteStorageMapping, err error) {
|
|
|
|
mappings = &remote_pb.RemoteStorageMapping{
|
|
|
|
Mappings: make(map[string]*remote_pb.RemoteStorageLocation),
|
2021-07-27 18:26:35 +08:00
|
|
|
}
|
|
|
|
if len(oldContent) > 0 {
|
|
|
|
if err = proto.Unmarshal(oldContent, mappings); err != nil {
|
2021-07-29 17:08:55 +08:00
|
|
|
glog.Warningf("unmarshal existing mappings: %v", err)
|
2021-07-27 18:26:35 +08:00
|
|
|
}
|
|
|
|
}
|
2021-08-01 13:39:38 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-13 13:47:52 +08:00
|
|
|
func ReadRemoteStorageConf(grpcDialOption grpc.DialOption, filerAddress pb.ServerAddress, storageName string) (conf *remote_pb.RemoteConf, readErr error) {
|
2021-08-05 07:25:46 +08:00
|
|
|
var oldContent []byte
|
2023-01-20 17:48:12 +08:00
|
|
|
if readErr = pb.WithFilerClient(false, 0, filerAddress, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
|
2021-08-05 07:25:46 +08:00
|
|
|
oldContent, readErr = ReadInsideFiler(client, DirectoryEtcRemote, storageName+REMOTE_STORAGE_CONF_SUFFIX)
|
|
|
|
return readErr
|
|
|
|
}); readErr != nil {
|
|
|
|
return nil, readErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshal storage configuration
|
2021-08-27 06:18:34 +08:00
|
|
|
conf = &remote_pb.RemoteConf{}
|
2021-08-05 07:25:46 +08:00
|
|
|
if unMarshalErr := proto.Unmarshal(oldContent, conf); unMarshalErr != nil {
|
|
|
|
readErr = fmt.Errorf("unmarshal %s/%s: %v", DirectoryEtcRemote, storageName+REMOTE_STORAGE_CONF_SUFFIX, unMarshalErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2021-08-16 03:38:26 +08:00
|
|
|
|
2021-09-13 13:47:52 +08:00
|
|
|
func DetectMountInfo(grpcDialOption grpc.DialOption, filerAddress pb.ServerAddress, dir string) (*remote_pb.RemoteStorageMapping, string, *remote_pb.RemoteStorageLocation, *remote_pb.RemoteConf, error) {
|
2021-08-16 03:38:26 +08:00
|
|
|
|
|
|
|
mappings, listErr := ReadMountMappings(grpcDialOption, filerAddress)
|
|
|
|
if listErr != nil {
|
|
|
|
return nil, "", nil, nil, listErr
|
|
|
|
}
|
|
|
|
if dir == "" {
|
|
|
|
return mappings, "", nil, nil, fmt.Errorf("need to specify '-dir' option")
|
|
|
|
}
|
|
|
|
|
|
|
|
var localMountedDir string
|
2021-08-27 06:18:34 +08:00
|
|
|
var remoteStorageMountedLocation *remote_pb.RemoteStorageLocation
|
2021-08-16 03:38:26 +08:00
|
|
|
for k, loc := range mappings.Mappings {
|
|
|
|
if strings.HasPrefix(dir, k) {
|
|
|
|
localMountedDir, remoteStorageMountedLocation = k, loc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if localMountedDir == "" {
|
|
|
|
return mappings, localMountedDir, remoteStorageMountedLocation, nil, fmt.Errorf("%s is not mounted", dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// find remote storage configuration
|
|
|
|
remoteStorageConf, err := ReadRemoteStorageConf(grpcDialOption, filerAddress, remoteStorageMountedLocation.Name)
|
|
|
|
if err != nil {
|
|
|
|
return mappings, localMountedDir, remoteStorageMountedLocation, remoteStorageConf, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return mappings, localMountedDir, remoteStorageMountedLocation, remoteStorageConf, nil
|
|
|
|
}
|