filer_remote_sync_dir.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package command
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  6. "os"
  7. "strings"
  8. "time"
  9. "github.com/seaweedfs/seaweedfs/weed/filer"
  10. "github.com/seaweedfs/seaweedfs/weed/glog"
  11. "github.com/seaweedfs/seaweedfs/weed/pb"
  12. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  13. "github.com/seaweedfs/seaweedfs/weed/pb/remote_pb"
  14. "github.com/seaweedfs/seaweedfs/weed/remote_storage"
  15. "github.com/seaweedfs/seaweedfs/weed/replication/source"
  16. "github.com/seaweedfs/seaweedfs/weed/util"
  17. "google.golang.org/grpc"
  18. "google.golang.org/protobuf/proto"
  19. )
  20. func followUpdatesAndUploadToRemote(option *RemoteSyncOptions, filerSource *source.FilerSource, mountedDir string) error {
  21. // read filer remote storage mount mappings
  22. _, _, remoteStorageMountLocation, remoteStorage, detectErr := filer.DetectMountInfo(option.grpcDialOption, pb.ServerAddress(*option.filerAddress), mountedDir)
  23. if detectErr != nil {
  24. return fmt.Errorf("read mount info: %v", detectErr)
  25. }
  26. eachEntryFunc, err := makeEventProcessor(remoteStorage, mountedDir, remoteStorageMountLocation, filerSource)
  27. if err != nil {
  28. return err
  29. }
  30. processor := NewMetadataProcessor(eachEntryFunc, 128)
  31. var lastLogTsNs = time.Now().UnixNano()
  32. processEventFnWithOffset := pb.AddOffsetFunc(func(resp *filer_pb.SubscribeMetadataResponse) error {
  33. if resp.EventNotification.NewEntry != nil {
  34. if *option.storageClass == "" {
  35. if _, ok := resp.EventNotification.NewEntry.Extended[s3_constants.AmzStorageClass]; ok {
  36. delete(resp.EventNotification.NewEntry.Extended, s3_constants.AmzStorageClass)
  37. }
  38. } else {
  39. resp.EventNotification.NewEntry.Extended[s3_constants.AmzStorageClass] = []byte(*option.storageClass)
  40. }
  41. }
  42. processor.AddSyncJob(resp)
  43. return nil
  44. }, 3*time.Second, func(counter int64, lastTsNs int64) error {
  45. if processor.processedTsWatermark == 0 {
  46. return nil
  47. }
  48. // use processor.processedTsWatermark instead of the lastTsNs from the most recent job
  49. now := time.Now().UnixNano()
  50. glog.V(0).Infof("remote sync %s progressed to %v %0.2f/sec", *option.filerAddress, time.Unix(0, processor.processedTsWatermark), float64(counter)/(float64(now-lastLogTsNs)/1e9))
  51. lastLogTsNs = now
  52. return remote_storage.SetSyncOffset(option.grpcDialOption, pb.ServerAddress(*option.filerAddress), mountedDir, processor.processedTsWatermark)
  53. })
  54. lastOffsetTs := collectLastSyncOffset(option, option.grpcDialOption, pb.ServerAddress(*option.filerAddress), mountedDir, *option.timeAgo)
  55. option.clientEpoch++
  56. metadataFollowOption := &pb.MetadataFollowOption{
  57. ClientName: "filer.remote.sync",
  58. ClientId: option.clientId,
  59. ClientEpoch: option.clientEpoch,
  60. SelfSignature: 0,
  61. PathPrefix: mountedDir,
  62. AdditionalPathPrefixes: []string{filer.DirectoryEtcRemote},
  63. DirectoriesToWatch: nil,
  64. StartTsNs: lastOffsetTs.UnixNano(),
  65. StopTsNs: 0,
  66. EventErrorType: pb.TrivialOnError,
  67. }
  68. return pb.FollowMetadata(pb.ServerAddress(*option.filerAddress), option.grpcDialOption, metadataFollowOption, processEventFnWithOffset)
  69. }
  70. func makeEventProcessor(remoteStorage *remote_pb.RemoteConf, mountedDir string, remoteStorageMountLocation *remote_pb.RemoteStorageLocation, filerSource *source.FilerSource) (pb.ProcessMetadataFunc, error) {
  71. client, err := remote_storage.GetRemoteStorage(remoteStorage)
  72. if err != nil {
  73. return nil, err
  74. }
  75. handleEtcRemoteChanges := func(resp *filer_pb.SubscribeMetadataResponse) error {
  76. message := resp.EventNotification
  77. if message.NewEntry == nil {
  78. return nil
  79. }
  80. if message.NewEntry.Name == filer.REMOTE_STORAGE_MOUNT_FILE {
  81. mappings, readErr := filer.UnmarshalRemoteStorageMappings(message.NewEntry.Content)
  82. if readErr != nil {
  83. return fmt.Errorf("unmarshal mappings: %v", readErr)
  84. }
  85. if remoteLoc, found := mappings.Mappings[mountedDir]; found {
  86. if remoteStorageMountLocation.Bucket != remoteLoc.Bucket || remoteStorageMountLocation.Path != remoteLoc.Path {
  87. glog.Fatalf("Unexpected mount changes %+v => %+v", remoteStorageMountLocation, remoteLoc)
  88. }
  89. } else {
  90. glog.V(0).Infof("unmounted %s exiting ...", mountedDir)
  91. os.Exit(0)
  92. }
  93. }
  94. if message.NewEntry.Name == remoteStorage.Name+filer.REMOTE_STORAGE_CONF_SUFFIX {
  95. conf := &remote_pb.RemoteConf{}
  96. if err := proto.Unmarshal(message.NewEntry.Content, conf); err != nil {
  97. return fmt.Errorf("unmarshal %s/%s: %v", filer.DirectoryEtcRemote, message.NewEntry.Name, err)
  98. }
  99. remoteStorage = conf
  100. if newClient, err := remote_storage.GetRemoteStorage(remoteStorage); err == nil {
  101. client = newClient
  102. } else {
  103. return err
  104. }
  105. }
  106. return nil
  107. }
  108. eachEntryFunc := func(resp *filer_pb.SubscribeMetadataResponse) error {
  109. message := resp.EventNotification
  110. if strings.HasPrefix(resp.Directory, filer.DirectoryEtcRemote) {
  111. return handleEtcRemoteChanges(resp)
  112. }
  113. if filer_pb.IsEmpty(resp) {
  114. return nil
  115. }
  116. if filer_pb.IsCreate(resp) {
  117. if isMultipartUploadFile(message.NewParentPath, message.NewEntry.Name) {
  118. return nil
  119. }
  120. if !filer.HasData(message.NewEntry) {
  121. return nil
  122. }
  123. glog.V(2).Infof("create: %+v", resp)
  124. if !shouldSendToRemote(message.NewEntry) {
  125. glog.V(2).Infof("skipping creating: %+v", resp)
  126. return nil
  127. }
  128. dest := toRemoteStorageLocation(util.FullPath(mountedDir), util.NewFullPath(message.NewParentPath, message.NewEntry.Name), remoteStorageMountLocation)
  129. if message.NewEntry.IsDirectory {
  130. glog.V(0).Infof("mkdir %s", remote_storage.FormatLocation(dest))
  131. return client.WriteDirectory(dest, message.NewEntry)
  132. }
  133. glog.V(0).Infof("create %s", remote_storage.FormatLocation(dest))
  134. remoteEntry, writeErr := retriedWriteFile(client, filerSource, message.NewEntry, dest)
  135. if writeErr != nil {
  136. return writeErr
  137. }
  138. return updateLocalEntry(&remoteSyncOptions, message.NewParentPath, message.NewEntry, remoteEntry)
  139. }
  140. if filer_pb.IsDelete(resp) {
  141. glog.V(2).Infof("delete: %+v", resp)
  142. dest := toRemoteStorageLocation(util.FullPath(mountedDir), util.NewFullPath(resp.Directory, message.OldEntry.Name), remoteStorageMountLocation)
  143. if message.OldEntry.IsDirectory {
  144. glog.V(0).Infof("rmdir %s", remote_storage.FormatLocation(dest))
  145. return client.RemoveDirectory(dest)
  146. }
  147. glog.V(0).Infof("delete %s", remote_storage.FormatLocation(dest))
  148. return client.DeleteFile(dest)
  149. }
  150. if message.OldEntry != nil && message.NewEntry != nil {
  151. oldDest := toRemoteStorageLocation(util.FullPath(mountedDir), util.NewFullPath(resp.Directory, message.OldEntry.Name), remoteStorageMountLocation)
  152. dest := toRemoteStorageLocation(util.FullPath(mountedDir), util.NewFullPath(message.NewParentPath, message.NewEntry.Name), remoteStorageMountLocation)
  153. if !shouldSendToRemote(message.NewEntry) {
  154. glog.V(2).Infof("skipping updating: %+v", resp)
  155. return nil
  156. }
  157. if message.NewEntry.IsDirectory {
  158. return client.WriteDirectory(dest, message.NewEntry)
  159. }
  160. if resp.Directory == message.NewParentPath && message.OldEntry.Name == message.NewEntry.Name {
  161. if filer.IsSameData(message.OldEntry, message.NewEntry) {
  162. glog.V(2).Infof("update meta: %+v", resp)
  163. return client.UpdateFileMetadata(dest, message.OldEntry, message.NewEntry)
  164. }
  165. }
  166. glog.V(2).Infof("update: %+v", resp)
  167. glog.V(0).Infof("delete %s", remote_storage.FormatLocation(oldDest))
  168. if err := client.DeleteFile(oldDest); err != nil {
  169. if isMultipartUploadFile(resp.Directory, message.OldEntry.Name) {
  170. return nil
  171. }
  172. }
  173. remoteEntry, writeErr := retriedWriteFile(client, filerSource, message.NewEntry, dest)
  174. if writeErr != nil {
  175. return writeErr
  176. }
  177. return updateLocalEntry(&remoteSyncOptions, message.NewParentPath, message.NewEntry, remoteEntry)
  178. }
  179. return nil
  180. }
  181. return eachEntryFunc, nil
  182. }
  183. func retriedWriteFile(client remote_storage.RemoteStorageClient, filerSource *source.FilerSource, newEntry *filer_pb.Entry, dest *remote_pb.RemoteStorageLocation) (remoteEntry *filer_pb.RemoteEntry, err error) {
  184. var writeErr error
  185. err = util.Retry("writeFile", func() error {
  186. reader := filer.NewFileReader(filerSource, newEntry)
  187. glog.V(0).Infof("create %s", remote_storage.FormatLocation(dest))
  188. remoteEntry, writeErr = client.WriteFile(dest, newEntry, reader)
  189. if writeErr != nil {
  190. return writeErr
  191. }
  192. return nil
  193. })
  194. if err != nil {
  195. glog.Errorf("write to %s: %v", dest, err)
  196. }
  197. return
  198. }
  199. func collectLastSyncOffset(filerClient filer_pb.FilerClient, grpcDialOption grpc.DialOption, filerAddress pb.ServerAddress, mountedDir string, timeAgo time.Duration) time.Time {
  200. // 1. specified by timeAgo
  201. // 2. last offset timestamp for this directory
  202. // 3. directory creation time
  203. var lastOffsetTs time.Time
  204. if timeAgo == 0 {
  205. mountedDirEntry, err := filer_pb.GetEntry(filerClient, util.FullPath(mountedDir))
  206. if err != nil {
  207. glog.V(0).Infof("get mounted directory %s: %v", mountedDir, err)
  208. return time.Now()
  209. }
  210. lastOffsetTsNs, err := remote_storage.GetSyncOffset(grpcDialOption, filerAddress, mountedDir)
  211. if mountedDirEntry != nil {
  212. if err == nil && mountedDirEntry.Attributes.Crtime < lastOffsetTsNs/1000000 {
  213. lastOffsetTs = time.Unix(0, lastOffsetTsNs)
  214. glog.V(0).Infof("resume from %v", lastOffsetTs)
  215. } else {
  216. lastOffsetTs = time.Unix(mountedDirEntry.Attributes.Crtime, 0)
  217. }
  218. } else {
  219. lastOffsetTs = time.Now()
  220. }
  221. } else {
  222. lastOffsetTs = time.Now().Add(-timeAgo)
  223. }
  224. return lastOffsetTs
  225. }
  226. func toRemoteStorageLocation(mountDir, sourcePath util.FullPath, remoteMountLocation *remote_pb.RemoteStorageLocation) *remote_pb.RemoteStorageLocation {
  227. source := string(sourcePath[len(mountDir):])
  228. dest := util.FullPath(remoteMountLocation.Path).Child(source)
  229. return &remote_pb.RemoteStorageLocation{
  230. Name: remoteMountLocation.Name,
  231. Bucket: remoteMountLocation.Bucket,
  232. Path: string(dest),
  233. }
  234. }
  235. func shouldSendToRemote(entry *filer_pb.Entry) bool {
  236. if entry.RemoteEntry == nil {
  237. return true
  238. }
  239. if entry.RemoteEntry.RemoteMtime < entry.Attributes.Mtime {
  240. return true
  241. }
  242. return false
  243. }
  244. func updateLocalEntry(filerClient filer_pb.FilerClient, dir string, entry *filer_pb.Entry, remoteEntry *filer_pb.RemoteEntry) error {
  245. remoteEntry.LastLocalSyncTsNs = time.Now().UnixNano()
  246. entry.RemoteEntry = remoteEntry
  247. return filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  248. _, err := client.UpdateEntry(context.Background(), &filer_pb.UpdateEntryRequest{
  249. Directory: dir,
  250. Entry: entry,
  251. })
  252. return err
  253. })
  254. }
  255. func isMultipartUploadFile(dir string, name string) bool {
  256. return strings.HasPrefix(dir, "/buckets/") &&
  257. strings.Contains(dir, "/"+s3_constants.MultipartUploadsFolder+"/") &&
  258. strings.HasSuffix(name, ".part")
  259. }