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.

427 lines
15 KiB

2 years ago
3 years ago
3 years ago
2 years ago
3 years ago
  1. package command
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/filer"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. "github.com/seaweedfs/seaweedfs/weed/pb"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/remote_pb"
  9. "github.com/seaweedfs/seaweedfs/weed/remote_storage"
  10. "github.com/seaweedfs/seaweedfs/weed/replication/source"
  11. "github.com/seaweedfs/seaweedfs/weed/util"
  12. "google.golang.org/protobuf/proto"
  13. "math"
  14. "math/rand"
  15. "path/filepath"
  16. "strings"
  17. "time"
  18. )
  19. func (option *RemoteGatewayOptions) followBucketUpdatesAndUploadToRemote(filerSource *source.FilerSource) error {
  20. // read filer remote storage mount mappings
  21. if detectErr := option.collectRemoteStorageConf(); detectErr != nil {
  22. return fmt.Errorf("read mount info: %v", detectErr)
  23. }
  24. eachEntryFunc, err := option.makeBucketedEventProcessor(filerSource)
  25. if err != nil {
  26. return err
  27. }
  28. lastOffsetTs := collectLastSyncOffset(option, option.grpcDialOption, pb.ServerAddress(*option.filerAddress), option.bucketsDir, *option.timeAgo)
  29. processor := NewMetadataProcessor(eachEntryFunc, 128, lastOffsetTs.UnixNano())
  30. var lastLogTsNs = time.Now().UnixNano()
  31. processEventFnWithOffset := pb.AddOffsetFunc(func(resp *filer_pb.SubscribeMetadataResponse) error {
  32. processor.AddSyncJob(resp)
  33. return nil
  34. }, 3*time.Second, func(counter int64, lastTsNs int64) error {
  35. offsetTsNs := processor.processedTsWatermark.Load()
  36. if offsetTsNs == 0 {
  37. return nil
  38. }
  39. now := time.Now().UnixNano()
  40. glog.V(0).Infof("remote sync %s progressed to %v %0.2f/sec", *option.filerAddress, time.Unix(0, offsetTsNs), float64(counter)/(float64(now-lastLogTsNs)/1e9))
  41. lastLogTsNs = now
  42. return remote_storage.SetSyncOffset(option.grpcDialOption, pb.ServerAddress(*option.filerAddress), option.bucketsDir, offsetTsNs)
  43. })
  44. option.clientEpoch++
  45. metadataFollowOption := &pb.MetadataFollowOption{
  46. ClientName: "filer.remote.sync",
  47. ClientId: option.clientId,
  48. ClientEpoch: option.clientEpoch,
  49. SelfSignature: 0,
  50. PathPrefix: option.bucketsDir,
  51. AdditionalPathPrefixes: []string{filer.DirectoryEtcRemote},
  52. DirectoriesToWatch: nil,
  53. StartTsNs: lastOffsetTs.UnixNano(),
  54. StopTsNs: 0,
  55. EventErrorType: pb.TrivialOnError,
  56. }
  57. return pb.FollowMetadata(pb.ServerAddress(*option.filerAddress), option.grpcDialOption, metadataFollowOption, processEventFnWithOffset)
  58. }
  59. func (option *RemoteGatewayOptions) makeBucketedEventProcessor(filerSource *source.FilerSource) (pb.ProcessMetadataFunc, error) {
  60. handleCreateBucket := func(entry *filer_pb.Entry) error {
  61. if !entry.IsDirectory {
  62. return nil
  63. }
  64. if entry.RemoteEntry != nil {
  65. // this directory is imported from "remote.mount.buckets" or "remote.mount"
  66. return nil
  67. }
  68. if option.mappings.PrimaryBucketStorageName != "" && *option.createBucketAt == "" {
  69. *option.createBucketAt = option.mappings.PrimaryBucketStorageName
  70. glog.V(0).Infof("%s is set as the primary remote storage", *option.createBucketAt)
  71. }
  72. if len(option.mappings.Mappings) == 1 && *option.createBucketAt == "" {
  73. for k := range option.mappings.Mappings {
  74. *option.createBucketAt = k
  75. glog.V(0).Infof("%s is set as the only remote storage", *option.createBucketAt)
  76. }
  77. }
  78. if *option.createBucketAt == "" {
  79. return nil
  80. }
  81. remoteConf, found := option.remoteConfs[*option.createBucketAt]
  82. if !found {
  83. return fmt.Errorf("un-configured remote storage %s", *option.createBucketAt)
  84. }
  85. client, err := remote_storage.GetRemoteStorage(remoteConf)
  86. if err != nil {
  87. return err
  88. }
  89. bucketName := strings.ToLower(entry.Name)
  90. if *option.include != "" {
  91. if ok, _ := filepath.Match(*option.include, entry.Name); !ok {
  92. return nil
  93. }
  94. }
  95. if *option.exclude != "" {
  96. if ok, _ := filepath.Match(*option.exclude, entry.Name); ok {
  97. return nil
  98. }
  99. }
  100. bucketPath := util.FullPath(option.bucketsDir).Child(entry.Name)
  101. remoteLocation, found := option.mappings.Mappings[string(bucketPath)]
  102. if !found {
  103. if *option.createBucketRandomSuffix {
  104. // https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html
  105. if len(bucketName)+5 > 63 {
  106. bucketName = bucketName[:58]
  107. }
  108. bucketName = fmt.Sprintf("%s-%04d", bucketName, rand.Uint32()%10000)
  109. }
  110. remoteLocation = &remote_pb.RemoteStorageLocation{
  111. Name: *option.createBucketAt,
  112. Bucket: bucketName,
  113. Path: "/",
  114. }
  115. // need to add new mapping here before getting updates from metadata tailing
  116. option.mappings.Mappings[string(bucketPath)] = remoteLocation
  117. } else {
  118. bucketName = remoteLocation.Bucket
  119. }
  120. glog.V(0).Infof("create bucket %s", bucketName)
  121. if err := client.CreateBucket(bucketName); err != nil {
  122. return fmt.Errorf("create bucket %s in %s: %v", bucketName, remoteConf.Name, err)
  123. }
  124. return filer.InsertMountMapping(option, string(bucketPath), remoteLocation)
  125. }
  126. handleDeleteBucket := func(entry *filer_pb.Entry) error {
  127. if !entry.IsDirectory {
  128. return nil
  129. }
  130. client, remoteStorageMountLocation, err := option.findRemoteStorageClient(entry.Name)
  131. if err != nil {
  132. return fmt.Errorf("findRemoteStorageClient %s: %v", entry.Name, err)
  133. }
  134. glog.V(0).Infof("delete remote bucket %s", remoteStorageMountLocation.Bucket)
  135. if err := client.DeleteBucket(remoteStorageMountLocation.Bucket); err != nil {
  136. return fmt.Errorf("delete remote bucket %s: %v", remoteStorageMountLocation.Bucket, err)
  137. }
  138. bucketPath := util.FullPath(option.bucketsDir).Child(entry.Name)
  139. return filer.DeleteMountMapping(option, string(bucketPath))
  140. }
  141. handleEtcRemoteChanges := func(resp *filer_pb.SubscribeMetadataResponse) error {
  142. message := resp.EventNotification
  143. if message.NewEntry != nil {
  144. // update
  145. if message.NewEntry.Name == filer.REMOTE_STORAGE_MOUNT_FILE {
  146. newMappings, readErr := filer.UnmarshalRemoteStorageMappings(message.NewEntry.Content)
  147. if readErr != nil {
  148. return fmt.Errorf("unmarshal mappings: %v", readErr)
  149. }
  150. option.mappings = newMappings
  151. }
  152. if strings.HasSuffix(message.NewEntry.Name, filer.REMOTE_STORAGE_CONF_SUFFIX) {
  153. conf := &remote_pb.RemoteConf{}
  154. if err := proto.Unmarshal(message.NewEntry.Content, conf); err != nil {
  155. return fmt.Errorf("unmarshal %s/%s: %v", filer.DirectoryEtcRemote, message.NewEntry.Name, err)
  156. }
  157. option.remoteConfs[conf.Name] = conf
  158. }
  159. } else if message.OldEntry != nil {
  160. // deletion
  161. if strings.HasSuffix(message.OldEntry.Name, filer.REMOTE_STORAGE_CONF_SUFFIX) {
  162. conf := &remote_pb.RemoteConf{}
  163. if err := proto.Unmarshal(message.OldEntry.Content, conf); err != nil {
  164. return fmt.Errorf("unmarshal %s/%s: %v", filer.DirectoryEtcRemote, message.OldEntry.Name, err)
  165. }
  166. delete(option.remoteConfs, conf.Name)
  167. }
  168. }
  169. return nil
  170. }
  171. eachEntryFunc := func(resp *filer_pb.SubscribeMetadataResponse) error {
  172. message := resp.EventNotification
  173. if strings.HasPrefix(resp.Directory, filer.DirectoryEtcRemote) {
  174. return handleEtcRemoteChanges(resp)
  175. }
  176. if filer_pb.IsEmpty(resp) {
  177. return nil
  178. }
  179. if filer_pb.IsCreate(resp) {
  180. if message.NewParentPath == option.bucketsDir {
  181. return handleCreateBucket(message.NewEntry)
  182. }
  183. if isMultipartUploadFile(message.NewParentPath, message.NewEntry.Name) {
  184. return nil
  185. }
  186. if !filer.HasData(message.NewEntry) {
  187. return nil
  188. }
  189. bucket, remoteStorageMountLocation, remoteStorage, ok := option.detectBucketInfo(message.NewParentPath)
  190. if !ok {
  191. return nil
  192. }
  193. client, err := remote_storage.GetRemoteStorage(remoteStorage)
  194. if err != nil {
  195. return err
  196. }
  197. glog.V(2).Infof("create: %+v", resp)
  198. if !shouldSendToRemote(message.NewEntry) {
  199. glog.V(2).Infof("skipping creating: %+v", resp)
  200. return nil
  201. }
  202. dest := toRemoteStorageLocation(bucket, util.NewFullPath(message.NewParentPath, message.NewEntry.Name), remoteStorageMountLocation)
  203. if message.NewEntry.IsDirectory {
  204. glog.V(0).Infof("mkdir %s", remote_storage.FormatLocation(dest))
  205. return client.WriteDirectory(dest, message.NewEntry)
  206. }
  207. glog.V(0).Infof("create %s", remote_storage.FormatLocation(dest))
  208. remoteEntry, writeErr := retriedWriteFile(client, filerSource, message.NewEntry, dest)
  209. if writeErr != nil {
  210. return writeErr
  211. }
  212. return updateLocalEntry(option, message.NewParentPath, message.NewEntry, remoteEntry)
  213. }
  214. if filer_pb.IsDelete(resp) {
  215. if resp.Directory == option.bucketsDir {
  216. return handleDeleteBucket(message.OldEntry)
  217. }
  218. bucket, remoteStorageMountLocation, remoteStorage, ok := option.detectBucketInfo(resp.Directory)
  219. if !ok {
  220. return nil
  221. }
  222. client, err := remote_storage.GetRemoteStorage(remoteStorage)
  223. if err != nil {
  224. return err
  225. }
  226. glog.V(2).Infof("delete: %+v", resp)
  227. dest := toRemoteStorageLocation(bucket, util.NewFullPath(resp.Directory, message.OldEntry.Name), remoteStorageMountLocation)
  228. if message.OldEntry.IsDirectory {
  229. glog.V(0).Infof("rmdir %s", remote_storage.FormatLocation(dest))
  230. return client.RemoveDirectory(dest)
  231. }
  232. glog.V(0).Infof("delete %s", remote_storage.FormatLocation(dest))
  233. return client.DeleteFile(dest)
  234. }
  235. if message.OldEntry != nil && message.NewEntry != nil {
  236. if resp.Directory == option.bucketsDir {
  237. if message.NewParentPath == option.bucketsDir {
  238. if message.OldEntry.Name == message.NewEntry.Name {
  239. return nil
  240. }
  241. if err := handleCreateBucket(message.NewEntry); err != nil {
  242. return err
  243. }
  244. if err := handleDeleteBucket(message.OldEntry); err != nil {
  245. return err
  246. }
  247. }
  248. }
  249. oldBucket, oldRemoteStorageMountLocation, oldRemoteStorage, oldOk := option.detectBucketInfo(resp.Directory)
  250. newBucket, newRemoteStorageMountLocation, newRemoteStorage, newOk := option.detectBucketInfo(message.NewParentPath)
  251. if oldOk && newOk {
  252. if !shouldSendToRemote(message.NewEntry) {
  253. glog.V(2).Infof("skipping updating: %+v", resp)
  254. return nil
  255. }
  256. client, err := remote_storage.GetRemoteStorage(oldRemoteStorage)
  257. if err != nil {
  258. return err
  259. }
  260. if resp.Directory == message.NewParentPath && message.OldEntry.Name == message.NewEntry.Name {
  261. // update the same entry
  262. if message.NewEntry.IsDirectory {
  263. // update directory property
  264. return nil
  265. }
  266. if message.OldEntry.RemoteEntry != nil && filer.IsSameData(message.OldEntry, message.NewEntry) {
  267. glog.V(2).Infof("update meta: %+v", resp)
  268. oldDest := toRemoteStorageLocation(oldBucket, util.NewFullPath(resp.Directory, message.OldEntry.Name), oldRemoteStorageMountLocation)
  269. return client.UpdateFileMetadata(oldDest, message.OldEntry, message.NewEntry)
  270. } else {
  271. newDest := toRemoteStorageLocation(newBucket, util.NewFullPath(message.NewParentPath, message.NewEntry.Name), newRemoteStorageMountLocation)
  272. remoteEntry, writeErr := retriedWriteFile(client, filerSource, message.NewEntry, newDest)
  273. if writeErr != nil {
  274. return writeErr
  275. }
  276. return updateLocalEntry(option, message.NewParentPath, message.NewEntry, remoteEntry)
  277. }
  278. }
  279. }
  280. // the following is entry rename
  281. if oldOk {
  282. client, err := remote_storage.GetRemoteStorage(oldRemoteStorage)
  283. if err != nil {
  284. return err
  285. }
  286. oldDest := toRemoteStorageLocation(oldBucket, util.NewFullPath(resp.Directory, message.OldEntry.Name), oldRemoteStorageMountLocation)
  287. if message.OldEntry.IsDirectory {
  288. return client.RemoveDirectory(oldDest)
  289. }
  290. glog.V(0).Infof("delete %s", remote_storage.FormatLocation(oldDest))
  291. if err := client.DeleteFile(oldDest); err != nil {
  292. return err
  293. }
  294. }
  295. if newOk {
  296. if !shouldSendToRemote(message.NewEntry) {
  297. glog.V(2).Infof("skipping updating: %+v", resp)
  298. return nil
  299. }
  300. client, err := remote_storage.GetRemoteStorage(newRemoteStorage)
  301. if err != nil {
  302. return err
  303. }
  304. newDest := toRemoteStorageLocation(newBucket, util.NewFullPath(message.NewParentPath, message.NewEntry.Name), newRemoteStorageMountLocation)
  305. if message.NewEntry.IsDirectory {
  306. return client.WriteDirectory(newDest, message.NewEntry)
  307. }
  308. remoteEntry, writeErr := retriedWriteFile(client, filerSource, message.NewEntry, newDest)
  309. if writeErr != nil {
  310. return writeErr
  311. }
  312. return updateLocalEntry(option, message.NewParentPath, message.NewEntry, remoteEntry)
  313. }
  314. }
  315. return nil
  316. }
  317. return eachEntryFunc, nil
  318. }
  319. func (option *RemoteGatewayOptions) findRemoteStorageClient(bucketName string) (client remote_storage.RemoteStorageClient, remoteStorageMountLocation *remote_pb.RemoteStorageLocation, err error) {
  320. bucket := util.FullPath(option.bucketsDir).Child(bucketName)
  321. var isMounted bool
  322. remoteStorageMountLocation, isMounted = option.mappings.Mappings[string(bucket)]
  323. if !isMounted {
  324. return nil, remoteStorageMountLocation, fmt.Errorf("%s is not mounted", bucket)
  325. }
  326. remoteConf, hasClient := option.remoteConfs[remoteStorageMountLocation.Name]
  327. if !hasClient {
  328. return nil, remoteStorageMountLocation, fmt.Errorf("%s mounted to un-configured %+v", bucket, remoteStorageMountLocation)
  329. }
  330. client, err = remote_storage.GetRemoteStorage(remoteConf)
  331. if err != nil {
  332. return nil, remoteStorageMountLocation, err
  333. }
  334. return client, remoteStorageMountLocation, nil
  335. }
  336. func (option *RemoteGatewayOptions) detectBucketInfo(actualDir string) (bucket util.FullPath, remoteStorageMountLocation *remote_pb.RemoteStorageLocation, remoteConf *remote_pb.RemoteConf, ok bool) {
  337. bucket, ok = extractBucketPath(option.bucketsDir, actualDir)
  338. if !ok {
  339. return "", nil, nil, false
  340. }
  341. var isMounted bool
  342. remoteStorageMountLocation, isMounted = option.mappings.Mappings[string(bucket)]
  343. if !isMounted {
  344. glog.Warningf("%s is not mounted", bucket)
  345. return "", nil, nil, false
  346. }
  347. var hasClient bool
  348. remoteConf, hasClient = option.remoteConfs[remoteStorageMountLocation.Name]
  349. if !hasClient {
  350. glog.Warningf("%s mounted to un-configured %+v", bucket, remoteStorageMountLocation)
  351. return "", nil, nil, false
  352. }
  353. return bucket, remoteStorageMountLocation, remoteConf, true
  354. }
  355. func extractBucketPath(bucketsDir, dir string) (util.FullPath, bool) {
  356. if !strings.HasPrefix(dir, bucketsDir+"/") {
  357. return "", false
  358. }
  359. parts := strings.SplitN(dir[len(bucketsDir)+1:], "/", 2)
  360. return util.FullPath(bucketsDir).Child(parts[0]), true
  361. }
  362. func (option *RemoteGatewayOptions) collectRemoteStorageConf() (err error) {
  363. if mappings, err := filer.ReadMountMappings(option.grpcDialOption, pb.ServerAddress(*option.filerAddress)); err != nil {
  364. if err == filer_pb.ErrNotFound {
  365. return fmt.Errorf("remote storage is not configured in filer server")
  366. }
  367. return err
  368. } else {
  369. option.mappings = mappings
  370. }
  371. option.remoteConfs = make(map[string]*remote_pb.RemoteConf)
  372. var lastConfName string
  373. err = filer_pb.List(option, filer.DirectoryEtcRemote, "", func(entry *filer_pb.Entry, isLast bool) error {
  374. if !strings.HasSuffix(entry.Name, filer.REMOTE_STORAGE_CONF_SUFFIX) {
  375. return nil
  376. }
  377. conf := &remote_pb.RemoteConf{}
  378. if err := proto.Unmarshal(entry.Content, conf); err != nil {
  379. return fmt.Errorf("unmarshal %s/%s: %v", filer.DirectoryEtcRemote, entry.Name, err)
  380. }
  381. option.remoteConfs[conf.Name] = conf
  382. lastConfName = conf.Name
  383. return nil
  384. }, "", false, math.MaxUint32)
  385. if option.mappings.PrimaryBucketStorageName == "" && len(option.remoteConfs) == 1 {
  386. glog.V(0).Infof("%s is set to the default remote storage", lastConfName)
  387. option.mappings.PrimaryBucketStorageName = lastConfName
  388. }
  389. return
  390. }