You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.0 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package remote_storage
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  5. "github.com/chrislusf/seaweedfs/weed/pb/remote_pb"
  6. "github.com/golang/protobuf/proto"
  7. "io"
  8. "strings"
  9. "sync"
  10. )
  11. func ParseLocationName(remote string) (locationName string) {
  12. if strings.HasSuffix(string(remote), "/") {
  13. remote = remote[:len(remote)-1]
  14. }
  15. parts := strings.SplitN(string(remote), "/", 2)
  16. if len(parts) >= 1 {
  17. return parts[0]
  18. }
  19. return
  20. }
  21. func parseBucketLocation(remote string) (loc *remote_pb.RemoteStorageLocation) {
  22. loc = &remote_pb.RemoteStorageLocation{}
  23. if strings.HasSuffix(string(remote), "/") {
  24. remote = remote[:len(remote)-1]
  25. }
  26. parts := strings.SplitN(string(remote), "/", 3)
  27. if len(parts) >= 1 {
  28. loc.Name = parts[0]
  29. }
  30. if len(parts) >= 2 {
  31. loc.Bucket = parts[1]
  32. }
  33. loc.Path = string(remote[len(loc.Name)+1+len(loc.Bucket):])
  34. if loc.Path == "" {
  35. loc.Path = "/"
  36. }
  37. return
  38. }
  39. func parseNoBucketLocation(remote string) (loc *remote_pb.RemoteStorageLocation) {
  40. loc = &remote_pb.RemoteStorageLocation{}
  41. if strings.HasSuffix(string(remote), "/") {
  42. remote = remote[:len(remote)-1]
  43. }
  44. parts := strings.SplitN(string(remote), "/", 2)
  45. if len(parts) >= 1 {
  46. loc.Name = parts[0]
  47. }
  48. loc.Path = string(remote[len(loc.Name):])
  49. if loc.Path == "" {
  50. loc.Path = "/"
  51. }
  52. return
  53. }
  54. func FormatLocation(loc *remote_pb.RemoteStorageLocation) string {
  55. return fmt.Sprintf("%s/%s%s", loc.Name, loc.Bucket, loc.Path)
  56. }
  57. type VisitFunc func(dir string, name string, isDirectory bool, remoteEntry *filer_pb.RemoteEntry) error
  58. type RemoteStorageClient interface {
  59. Traverse(loc *remote_pb.RemoteStorageLocation, visitFn VisitFunc) error
  60. ReadFile(loc *remote_pb.RemoteStorageLocation, offset int64, size int64) (data []byte, err error)
  61. WriteDirectory(loc *remote_pb.RemoteStorageLocation, entry *filer_pb.Entry) (err error)
  62. RemoveDirectory(loc *remote_pb.RemoteStorageLocation) (err error)
  63. WriteFile(loc *remote_pb.RemoteStorageLocation, entry *filer_pb.Entry, reader io.Reader) (remoteEntry *filer_pb.RemoteEntry, err error)
  64. UpdateFileMetadata(loc *remote_pb.RemoteStorageLocation, oldEntry *filer_pb.Entry, newEntry *filer_pb.Entry) (err error)
  65. DeleteFile(loc *remote_pb.RemoteStorageLocation) (err error)
  66. }
  67. type RemoteStorageClientMaker interface {
  68. Make(remoteConf *remote_pb.RemoteConf) (RemoteStorageClient, error)
  69. HasBucket() bool
  70. }
  71. type CachedRemoteStorageClient struct {
  72. *remote_pb.RemoteConf
  73. RemoteStorageClient
  74. }
  75. var (
  76. RemoteStorageClientMakers = make(map[string]RemoteStorageClientMaker)
  77. remoteStorageClients = make(map[string]CachedRemoteStorageClient)
  78. remoteStorageClientsLock sync.Mutex
  79. )
  80. func ParseRemoteLocation(remoteConfType string, remote string) (remoteStorageLocation *remote_pb.RemoteStorageLocation, err error) {
  81. maker, found := RemoteStorageClientMakers[remoteConfType]
  82. if !found {
  83. return nil, fmt.Errorf("remote storage type %s not found", remoteConfType)
  84. }
  85. if !maker.HasBucket() {
  86. return parseNoBucketLocation(remote), nil
  87. }
  88. return parseBucketLocation(remote), nil
  89. }
  90. func makeRemoteStorageClient(remoteConf *remote_pb.RemoteConf) (RemoteStorageClient, error) {
  91. maker, found := RemoteStorageClientMakers[remoteConf.Type]
  92. if !found {
  93. return nil, fmt.Errorf("remote storage type %s not found", remoteConf.Type)
  94. }
  95. return maker.Make(remoteConf)
  96. }
  97. func GetRemoteStorage(remoteConf *remote_pb.RemoteConf) (RemoteStorageClient, error) {
  98. remoteStorageClientsLock.Lock()
  99. defer remoteStorageClientsLock.Unlock()
  100. existingRemoteStorageClient, found := remoteStorageClients[remoteConf.Name]
  101. if found && proto.Equal(existingRemoteStorageClient.RemoteConf, remoteConf) {
  102. return existingRemoteStorageClient.RemoteStorageClient, nil
  103. }
  104. newRemoteStorageClient, err := makeRemoteStorageClient(remoteConf)
  105. if err != nil {
  106. return nil, fmt.Errorf("make remote storage client %s: %v", remoteConf.Name, err)
  107. }
  108. remoteStorageClients[remoteConf.Name] = CachedRemoteStorageClient{
  109. RemoteConf: remoteConf,
  110. RemoteStorageClient: newRemoteStorageClient,
  111. }
  112. return newRemoteStorageClient, nil
  113. }