2021-09-04 11:35:46 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/remote_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/remote_storage"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/replication/source"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2022-08-17 19:05:07 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2021-09-04 11:35:46 +00:00
|
|
|
"math"
|
2021-09-05 05:46:28 +00:00
|
|
|
"math/rand"
|
2021-09-16 06:04:16 +00:00
|
|
|
"path/filepath"
|
2021-09-04 11:35:46 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-09-16 05:47:17 +00:00
|
|
|
func (option *RemoteGatewayOptions) followBucketUpdatesAndUploadToRemote(filerSource *source.FilerSource) error {
|
2021-09-04 11:35:46 +00:00
|
|
|
|
|
|
|
// read filer remote storage mount mappings
|
|
|
|
if detectErr := option.collectRemoteStorageConf(); detectErr != nil {
|
|
|
|
return fmt.Errorf("read mount info: %v", detectErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
eachEntryFunc, err := option.makeBucketedEventProcessor(filerSource)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
processEventFnWithOffset := pb.AddOffsetFunc(eachEntryFunc, 3*time.Second, func(counter int64, lastTsNs int64) error {
|
|
|
|
lastTime := time.Unix(0, lastTsNs)
|
|
|
|
glog.V(0).Infof("remote sync %s progressed to %v %0.2f/sec", *option.filerAddress, lastTime, float64(counter)/float64(3))
|
2021-09-13 05:47:52 +00:00
|
|
|
return remote_storage.SetSyncOffset(option.grpcDialOption, pb.ServerAddress(*option.filerAddress), option.bucketsDir, lastTsNs)
|
2021-09-04 11:35:46 +00:00
|
|
|
})
|
|
|
|
|
2021-09-16 05:47:17 +00:00
|
|
|
lastOffsetTs := collectLastSyncOffset(option, option.grpcDialOption, pb.ServerAddress(*option.filerAddress), option.bucketsDir, *option.timeAgo)
|
2021-09-04 11:35:46 +00:00
|
|
|
|
2022-07-23 17:50:28 +00:00
|
|
|
option.clientEpoch++
|
2023-03-22 06:01:49 +00:00
|
|
|
|
|
|
|
metadataFollowOption := &pb.MetadataFollowOption{
|
|
|
|
ClientName: "filer.remote.sync",
|
|
|
|
ClientId: option.clientId,
|
|
|
|
ClientEpoch: option.clientEpoch,
|
|
|
|
SelfSignature: 0,
|
|
|
|
PathPrefix: option.bucketsDir,
|
|
|
|
AdditionalPathPrefixes: []string{filer.DirectoryEtcRemote},
|
|
|
|
DirectoriesToWatch: nil,
|
|
|
|
StartTsNs: lastOffsetTs.UnixNano(),
|
|
|
|
StopTsNs: 0,
|
|
|
|
EventErrorType: pb.TrivialOnError,
|
|
|
|
}
|
|
|
|
|
|
|
|
return pb.FollowMetadata(pb.ServerAddress(*option.filerAddress), option.grpcDialOption, metadataFollowOption, processEventFnWithOffset)
|
2021-09-04 11:35:46 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 05:47:17 +00:00
|
|
|
func (option *RemoteGatewayOptions) makeBucketedEventProcessor(filerSource *source.FilerSource) (pb.ProcessMetadataFunc, error) {
|
2021-09-04 11:35:46 +00:00
|
|
|
|
|
|
|
handleCreateBucket := func(entry *filer_pb.Entry) error {
|
|
|
|
if !entry.IsDirectory {
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-05 18:55:52 +00:00
|
|
|
if entry.RemoteEntry != nil {
|
|
|
|
// this directory is imported from "remote.mount.buckets" or "remote.mount"
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-06 22:10:55 +00:00
|
|
|
if option.mappings.PrimaryBucketStorageName != "" && *option.createBucketAt == "" {
|
|
|
|
*option.createBucketAt = option.mappings.PrimaryBucketStorageName
|
|
|
|
glog.V(0).Infof("%s is set as the primary remote storage", *option.createBucketAt)
|
|
|
|
}
|
|
|
|
if len(option.mappings.Mappings) == 1 && *option.createBucketAt == "" {
|
|
|
|
for k := range option.mappings.Mappings {
|
|
|
|
*option.createBucketAt = k
|
|
|
|
glog.V(0).Infof("%s is set as the only remote storage", *option.createBucketAt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if *option.createBucketAt == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-04 11:54:55 +00:00
|
|
|
remoteConf, found := option.remoteConfs[*option.createBucketAt]
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("un-configured remote storage %s", *option.createBucketAt)
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := remote_storage.GetRemoteStorage(remoteConf)
|
2021-09-04 11:35:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-05 05:46:28 +00:00
|
|
|
bucketName := strings.ToLower(entry.Name)
|
2021-09-16 06:04:16 +00:00
|
|
|
if *option.include != "" {
|
|
|
|
if ok, _ := filepath.Match(*option.include, entry.Name); !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if *option.exclude != "" {
|
|
|
|
if ok, _ := filepath.Match(*option.exclude, entry.Name); ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2021-12-05 21:06:41 +00:00
|
|
|
|
|
|
|
bucketPath := util.FullPath(option.bucketsDir).Child(entry.Name)
|
|
|
|
remoteLocation, found := option.mappings.Mappings[string(bucketPath)]
|
|
|
|
if !found {
|
|
|
|
if *option.createBucketRandomSuffix {
|
|
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html
|
|
|
|
if len(bucketName)+5 > 63 {
|
|
|
|
bucketName = bucketName[:58]
|
|
|
|
}
|
|
|
|
bucketName = fmt.Sprintf("%s-%04d", bucketName, rand.Uint32()%10000)
|
|
|
|
}
|
|
|
|
remoteLocation = &remote_pb.RemoteStorageLocation{
|
|
|
|
Name: *option.createBucketAt,
|
|
|
|
Bucket: bucketName,
|
|
|
|
Path: "/",
|
2021-09-05 05:46:28 +00:00
|
|
|
}
|
2021-12-05 21:06:41 +00:00
|
|
|
// need to add new mapping here before getting updates from metadata tailing
|
|
|
|
option.mappings.Mappings[string(bucketPath)] = remoteLocation
|
|
|
|
} else {
|
|
|
|
bucketName = remoteLocation.Bucket
|
2021-09-05 05:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(0).Infof("create bucket %s", bucketName)
|
|
|
|
if err := client.CreateBucket(bucketName); err != nil {
|
2021-09-06 22:10:55 +00:00
|
|
|
return fmt.Errorf("create bucket %s in %s: %v", bucketName, remoteConf.Name, err)
|
2021-09-04 11:35:46 +00:00
|
|
|
}
|
2021-09-04 12:22:34 +00:00
|
|
|
|
|
|
|
return filer.InsertMountMapping(option, string(bucketPath), remoteLocation)
|
|
|
|
|
2021-09-04 11:35:46 +00:00
|
|
|
}
|
|
|
|
handleDeleteBucket := func(entry *filer_pb.Entry) error {
|
|
|
|
if !entry.IsDirectory {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-05 05:46:28 +00:00
|
|
|
client, remoteStorageMountLocation, err := option.findRemoteStorageClient(entry.Name)
|
2021-09-04 11:35:46 +00:00
|
|
|
if err != nil {
|
2021-09-06 22:10:55 +00:00
|
|
|
return fmt.Errorf("findRemoteStorageClient %s: %v", entry.Name, err)
|
2021-09-04 11:35:46 +00:00
|
|
|
}
|
|
|
|
|
2021-09-05 05:46:28 +00:00
|
|
|
glog.V(0).Infof("delete remote bucket %s", remoteStorageMountLocation.Bucket)
|
|
|
|
if err := client.DeleteBucket(remoteStorageMountLocation.Bucket); err != nil {
|
2021-09-06 22:10:55 +00:00
|
|
|
return fmt.Errorf("delete remote bucket %s: %v", remoteStorageMountLocation.Bucket, err)
|
2021-09-04 11:35:46 +00:00
|
|
|
}
|
2021-09-04 12:22:34 +00:00
|
|
|
|
|
|
|
bucketPath := util.FullPath(option.bucketsDir).Child(entry.Name)
|
|
|
|
|
|
|
|
return filer.DeleteMountMapping(option, string(bucketPath))
|
2021-09-04 11:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleEtcRemoteChanges := func(resp *filer_pb.SubscribeMetadataResponse) error {
|
|
|
|
message := resp.EventNotification
|
2021-09-04 12:22:34 +00:00
|
|
|
if message.NewEntry != nil {
|
|
|
|
// update
|
|
|
|
if message.NewEntry.Name == filer.REMOTE_STORAGE_MOUNT_FILE {
|
|
|
|
newMappings, readErr := filer.UnmarshalRemoteStorageMappings(message.NewEntry.Content)
|
|
|
|
if readErr != nil {
|
|
|
|
return fmt.Errorf("unmarshal mappings: %v", readErr)
|
|
|
|
}
|
|
|
|
option.mappings = newMappings
|
2021-09-04 11:35:46 +00:00
|
|
|
}
|
2021-09-04 12:22:34 +00:00
|
|
|
if strings.HasSuffix(message.NewEntry.Name, filer.REMOTE_STORAGE_CONF_SUFFIX) {
|
|
|
|
conf := &remote_pb.RemoteConf{}
|
|
|
|
if err := proto.Unmarshal(message.NewEntry.Content, conf); err != nil {
|
|
|
|
return fmt.Errorf("unmarshal %s/%s: %v", filer.DirectoryEtcRemote, message.NewEntry.Name, err)
|
|
|
|
}
|
|
|
|
option.remoteConfs[conf.Name] = conf
|
|
|
|
}
|
|
|
|
} else if message.OldEntry != nil {
|
|
|
|
// deletion
|
|
|
|
if strings.HasSuffix(message.OldEntry.Name, filer.REMOTE_STORAGE_CONF_SUFFIX) {
|
|
|
|
conf := &remote_pb.RemoteConf{}
|
|
|
|
if err := proto.Unmarshal(message.OldEntry.Content, conf); err != nil {
|
|
|
|
return fmt.Errorf("unmarshal %s/%s: %v", filer.DirectoryEtcRemote, message.OldEntry.Name, err)
|
|
|
|
}
|
|
|
|
delete(option.remoteConfs, conf.Name)
|
2021-09-04 11:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
eachEntryFunc := func(resp *filer_pb.SubscribeMetadataResponse) error {
|
|
|
|
message := resp.EventNotification
|
|
|
|
if strings.HasPrefix(resp.Directory, filer.DirectoryEtcRemote) {
|
|
|
|
return handleEtcRemoteChanges(resp)
|
|
|
|
}
|
|
|
|
|
2022-02-25 09:17:26 +00:00
|
|
|
if filer_pb.IsEmpty(resp) {
|
2021-09-04 11:35:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-02-25 09:17:26 +00:00
|
|
|
if filer_pb.IsCreate(resp) {
|
2021-09-04 11:35:46 +00:00
|
|
|
if message.NewParentPath == option.bucketsDir {
|
|
|
|
return handleCreateBucket(message.NewEntry)
|
|
|
|
}
|
2022-09-12 04:53:15 +00:00
|
|
|
if isMultipartUploadFile(message.NewParentPath, message.NewEntry.Name) {
|
2021-12-15 03:48:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-04 11:35:46 +00:00
|
|
|
if !filer.HasData(message.NewEntry) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
bucket, remoteStorageMountLocation, remoteStorage, ok := option.detectBucketInfo(message.NewParentPath)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
client, err := remote_storage.GetRemoteStorage(remoteStorage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
glog.V(2).Infof("create: %+v", resp)
|
|
|
|
if !shouldSendToRemote(message.NewEntry) {
|
|
|
|
glog.V(2).Infof("skipping creating: %+v", resp)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
dest := toRemoteStorageLocation(bucket, util.NewFullPath(message.NewParentPath, message.NewEntry.Name), remoteStorageMountLocation)
|
|
|
|
if message.NewEntry.IsDirectory {
|
|
|
|
glog.V(0).Infof("mkdir %s", remote_storage.FormatLocation(dest))
|
|
|
|
return client.WriteDirectory(dest, message.NewEntry)
|
|
|
|
}
|
|
|
|
glog.V(0).Infof("create %s", remote_storage.FormatLocation(dest))
|
2021-11-23 05:48:00 +00:00
|
|
|
remoteEntry, writeErr := retriedWriteFile(client, filerSource, message.NewEntry, dest)
|
2021-09-04 11:35:46 +00:00
|
|
|
if writeErr != nil {
|
|
|
|
return writeErr
|
|
|
|
}
|
|
|
|
return updateLocalEntry(&remoteSyncOptions, message.NewParentPath, message.NewEntry, remoteEntry)
|
|
|
|
}
|
2022-02-25 09:17:26 +00:00
|
|
|
if filer_pb.IsDelete(resp) {
|
2021-09-04 11:35:46 +00:00
|
|
|
if resp.Directory == option.bucketsDir {
|
|
|
|
return handleDeleteBucket(message.OldEntry)
|
|
|
|
}
|
|
|
|
bucket, remoteStorageMountLocation, remoteStorage, ok := option.detectBucketInfo(resp.Directory)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
client, err := remote_storage.GetRemoteStorage(remoteStorage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
glog.V(2).Infof("delete: %+v", resp)
|
|
|
|
dest := toRemoteStorageLocation(bucket, util.NewFullPath(resp.Directory, message.OldEntry.Name), remoteStorageMountLocation)
|
|
|
|
if message.OldEntry.IsDirectory {
|
|
|
|
glog.V(0).Infof("rmdir %s", remote_storage.FormatLocation(dest))
|
|
|
|
return client.RemoveDirectory(dest)
|
|
|
|
}
|
|
|
|
glog.V(0).Infof("delete %s", remote_storage.FormatLocation(dest))
|
|
|
|
return client.DeleteFile(dest)
|
|
|
|
}
|
|
|
|
if message.OldEntry != nil && message.NewEntry != nil {
|
|
|
|
if resp.Directory == option.bucketsDir {
|
|
|
|
if message.NewParentPath == option.bucketsDir {
|
|
|
|
if message.OldEntry.Name == message.NewEntry.Name {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := handleCreateBucket(message.NewEntry); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := handleDeleteBucket(message.OldEntry); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
oldBucket, oldRemoteStorageMountLocation, oldRemoteStorage, oldOk := option.detectBucketInfo(resp.Directory)
|
|
|
|
newBucket, newRemoteStorageMountLocation, newRemoteStorage, newOk := option.detectBucketInfo(message.NewParentPath)
|
|
|
|
if oldOk && newOk {
|
|
|
|
if !shouldSendToRemote(message.NewEntry) {
|
|
|
|
glog.V(2).Infof("skipping updating: %+v", resp)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
client, err := remote_storage.GetRemoteStorage(oldRemoteStorage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp.Directory == message.NewParentPath && message.OldEntry.Name == message.NewEntry.Name {
|
|
|
|
// update the same entry
|
|
|
|
if message.NewEntry.IsDirectory {
|
|
|
|
// update directory property
|
|
|
|
return nil
|
|
|
|
}
|
2022-01-31 04:23:24 +00:00
|
|
|
if message.OldEntry.RemoteEntry != nil && filer.IsSameData(message.OldEntry, message.NewEntry) {
|
2021-09-04 11:35:46 +00:00
|
|
|
glog.V(2).Infof("update meta: %+v", resp)
|
|
|
|
oldDest := toRemoteStorageLocation(oldBucket, util.NewFullPath(resp.Directory, message.OldEntry.Name), oldRemoteStorageMountLocation)
|
|
|
|
return client.UpdateFileMetadata(oldDest, message.OldEntry, message.NewEntry)
|
|
|
|
} else {
|
|
|
|
newDest := toRemoteStorageLocation(newBucket, util.NewFullPath(message.NewParentPath, message.NewEntry.Name), newRemoteStorageMountLocation)
|
2021-11-23 05:48:00 +00:00
|
|
|
remoteEntry, writeErr := retriedWriteFile(client, filerSource, message.NewEntry, newDest)
|
2021-09-04 11:35:46 +00:00
|
|
|
if writeErr != nil {
|
|
|
|
return writeErr
|
|
|
|
}
|
|
|
|
return updateLocalEntry(&remoteSyncOptions, message.NewParentPath, message.NewEntry, remoteEntry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// the following is entry rename
|
|
|
|
if oldOk {
|
|
|
|
client, err := remote_storage.GetRemoteStorage(oldRemoteStorage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
oldDest := toRemoteStorageLocation(oldBucket, util.NewFullPath(resp.Directory, message.OldEntry.Name), oldRemoteStorageMountLocation)
|
|
|
|
if message.OldEntry.IsDirectory {
|
|
|
|
return client.RemoveDirectory(oldDest)
|
|
|
|
}
|
|
|
|
glog.V(0).Infof("delete %s", remote_storage.FormatLocation(oldDest))
|
|
|
|
if err := client.DeleteFile(oldDest); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if newOk {
|
|
|
|
if !shouldSendToRemote(message.NewEntry) {
|
|
|
|
glog.V(2).Infof("skipping updating: %+v", resp)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
client, err := remote_storage.GetRemoteStorage(newRemoteStorage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newDest := toRemoteStorageLocation(newBucket, util.NewFullPath(message.NewParentPath, message.NewEntry.Name), newRemoteStorageMountLocation)
|
|
|
|
if message.NewEntry.IsDirectory {
|
|
|
|
return client.WriteDirectory(newDest, message.NewEntry)
|
|
|
|
}
|
2021-11-23 05:48:00 +00:00
|
|
|
remoteEntry, writeErr := retriedWriteFile(client, filerSource, message.NewEntry, newDest)
|
2021-09-04 11:35:46 +00:00
|
|
|
if writeErr != nil {
|
|
|
|
return writeErr
|
|
|
|
}
|
|
|
|
return updateLocalEntry(&remoteSyncOptions, message.NewParentPath, message.NewEntry, remoteEntry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return eachEntryFunc, nil
|
|
|
|
}
|
|
|
|
|
2021-09-16 05:47:17 +00:00
|
|
|
func (option *RemoteGatewayOptions) findRemoteStorageClient(bucketName string) (client remote_storage.RemoteStorageClient, remoteStorageMountLocation *remote_pb.RemoteStorageLocation, err error) {
|
2021-09-04 11:35:46 +00:00
|
|
|
bucket := util.FullPath(option.bucketsDir).Child(bucketName)
|
|
|
|
|
2021-09-05 05:46:28 +00:00
|
|
|
var isMounted bool
|
|
|
|
remoteStorageMountLocation, isMounted = option.mappings.Mappings[string(bucket)]
|
2021-09-04 11:35:46 +00:00
|
|
|
if !isMounted {
|
2021-09-05 05:46:28 +00:00
|
|
|
return nil, remoteStorageMountLocation, fmt.Errorf("%s is not mounted", bucket)
|
2021-09-04 11:35:46 +00:00
|
|
|
}
|
|
|
|
remoteConf, hasClient := option.remoteConfs[remoteStorageMountLocation.Name]
|
|
|
|
if !hasClient {
|
2021-09-05 05:46:28 +00:00
|
|
|
return nil, remoteStorageMountLocation, fmt.Errorf("%s mounted to un-configured %+v", bucket, remoteStorageMountLocation)
|
2021-09-04 11:35:46 +00:00
|
|
|
}
|
|
|
|
|
2021-09-05 05:46:28 +00:00
|
|
|
client, err = remote_storage.GetRemoteStorage(remoteConf)
|
2021-09-04 11:35:46 +00:00
|
|
|
if err != nil {
|
2021-09-05 05:46:28 +00:00
|
|
|
return nil, remoteStorageMountLocation, err
|
2021-09-04 11:35:46 +00:00
|
|
|
}
|
2021-09-05 05:46:28 +00:00
|
|
|
return client, remoteStorageMountLocation, nil
|
2021-09-04 11:35:46 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 05:47:17 +00:00
|
|
|
func (option *RemoteGatewayOptions) detectBucketInfo(actualDir string) (bucket util.FullPath, remoteStorageMountLocation *remote_pb.RemoteStorageLocation, remoteConf *remote_pb.RemoteConf, ok bool) {
|
2021-09-04 11:35:46 +00:00
|
|
|
bucket, ok = extractBucketPath(option.bucketsDir, actualDir)
|
|
|
|
if !ok {
|
|
|
|
return "", nil, nil, false
|
|
|
|
}
|
|
|
|
var isMounted bool
|
|
|
|
remoteStorageMountLocation, isMounted = option.mappings.Mappings[string(bucket)]
|
|
|
|
if !isMounted {
|
|
|
|
glog.Warningf("%s is not mounted", bucket)
|
|
|
|
return "", nil, nil, false
|
|
|
|
}
|
|
|
|
var hasClient bool
|
|
|
|
remoteConf, hasClient = option.remoteConfs[remoteStorageMountLocation.Name]
|
|
|
|
if !hasClient {
|
|
|
|
glog.Warningf("%s mounted to un-configured %+v", bucket, remoteStorageMountLocation)
|
|
|
|
return "", nil, nil, false
|
|
|
|
}
|
|
|
|
return bucket, remoteStorageMountLocation, remoteConf, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractBucketPath(bucketsDir, dir string) (util.FullPath, bool) {
|
|
|
|
if !strings.HasPrefix(dir, bucketsDir+"/") {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
parts := strings.SplitN(dir[len(bucketsDir)+1:], "/", 2)
|
|
|
|
return util.FullPath(bucketsDir).Child(parts[0]), true
|
|
|
|
}
|
|
|
|
|
2021-09-16 05:47:17 +00:00
|
|
|
func (option *RemoteGatewayOptions) collectRemoteStorageConf() (err error) {
|
2021-09-04 11:35:46 +00:00
|
|
|
|
2021-09-13 05:47:52 +00:00
|
|
|
if mappings, err := filer.ReadMountMappings(option.grpcDialOption, pb.ServerAddress(*option.filerAddress)); err != nil {
|
2021-09-04 11:35:46 +00:00
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
option.mappings = mappings
|
|
|
|
}
|
|
|
|
|
|
|
|
option.remoteConfs = make(map[string]*remote_pb.RemoteConf)
|
2021-09-06 22:10:55 +00:00
|
|
|
var lastConfName string
|
2021-09-04 11:35:46 +00:00
|
|
|
err = filer_pb.List(option, filer.DirectoryEtcRemote, "", func(entry *filer_pb.Entry, isLast bool) error {
|
|
|
|
if !strings.HasSuffix(entry.Name, filer.REMOTE_STORAGE_CONF_SUFFIX) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
conf := &remote_pb.RemoteConf{}
|
|
|
|
if err := proto.Unmarshal(entry.Content, conf); err != nil {
|
|
|
|
return fmt.Errorf("unmarshal %s/%s: %v", filer.DirectoryEtcRemote, entry.Name, err)
|
|
|
|
}
|
|
|
|
option.remoteConfs[conf.Name] = conf
|
2021-09-06 22:10:55 +00:00
|
|
|
lastConfName = conf.Name
|
2021-09-04 11:35:46 +00:00
|
|
|
return nil
|
|
|
|
}, "", false, math.MaxUint32)
|
|
|
|
|
2021-09-06 22:10:55 +00:00
|
|
|
if option.mappings.PrimaryBucketStorageName == "" && len(option.remoteConfs) == 1 {
|
|
|
|
glog.V(0).Infof("%s is set to the default remote storage", lastConfName)
|
|
|
|
option.mappings.PrimaryBucketStorageName = lastConfName
|
|
|
|
}
|
|
|
|
|
2021-09-04 11:35:46 +00:00
|
|
|
return
|
2021-09-05 05:46:28 +00:00
|
|
|
}
|