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.

255 lines
9.7 KiB

3 years ago
3 years ago
3 years ago
  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. processor.AddSyncJob(resp)
  34. return nil
  35. }, 3*time.Second, func(counter int64, lastTsNs int64) error {
  36. if processor.processedTsWatermark == 0 {
  37. return nil
  38. }
  39. // use processor.processedTsWatermark instead of the lastTsNs from the most recent job
  40. now := time.Now().UnixNano()
  41. 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))
  42. lastLogTsNs = now
  43. return remote_storage.SetSyncOffset(option.grpcDialOption, pb.ServerAddress(*option.filerAddress), mountedDir, processor.processedTsWatermark)
  44. })
  45. lastOffsetTs := collectLastSyncOffset(option, option.grpcDialOption, pb.ServerAddress(*option.filerAddress), mountedDir, *option.timeAgo)
  46. option.clientEpoch++
  47. return pb.FollowMetadata(pb.ServerAddress(*option.filerAddress), option.grpcDialOption, "filer.remote.sync", option.clientId, option.clientEpoch,
  48. mountedDir, []string{filer.DirectoryEtcRemote}, lastOffsetTs.UnixNano(), 0, 0, processEventFnWithOffset, pb.TrivialOnError)
  49. }
  50. func makeEventProcessor(remoteStorage *remote_pb.RemoteConf, mountedDir string, remoteStorageMountLocation *remote_pb.RemoteStorageLocation, filerSource *source.FilerSource) (pb.ProcessMetadataFunc, error) {
  51. client, err := remote_storage.GetRemoteStorage(remoteStorage)
  52. if err != nil {
  53. return nil, err
  54. }
  55. handleEtcRemoteChanges := func(resp *filer_pb.SubscribeMetadataResponse) error {
  56. message := resp.EventNotification
  57. if message.NewEntry == nil {
  58. return nil
  59. }
  60. if message.NewEntry.Name == filer.REMOTE_STORAGE_MOUNT_FILE {
  61. mappings, readErr := filer.UnmarshalRemoteStorageMappings(message.NewEntry.Content)
  62. if readErr != nil {
  63. return fmt.Errorf("unmarshal mappings: %v", readErr)
  64. }
  65. if remoteLoc, found := mappings.Mappings[mountedDir]; found {
  66. if remoteStorageMountLocation.Bucket != remoteLoc.Bucket || remoteStorageMountLocation.Path != remoteLoc.Path {
  67. glog.Fatalf("Unexpected mount changes %+v => %+v", remoteStorageMountLocation, remoteLoc)
  68. }
  69. } else {
  70. glog.V(0).Infof("unmounted %s exiting ...", mountedDir)
  71. os.Exit(0)
  72. }
  73. }
  74. if message.NewEntry.Name == remoteStorage.Name+filer.REMOTE_STORAGE_CONF_SUFFIX {
  75. conf := &remote_pb.RemoteConf{}
  76. if err := proto.Unmarshal(message.NewEntry.Content, conf); err != nil {
  77. return fmt.Errorf("unmarshal %s/%s: %v", filer.DirectoryEtcRemote, message.NewEntry.Name, err)
  78. }
  79. remoteStorage = conf
  80. if newClient, err := remote_storage.GetRemoteStorage(remoteStorage); err == nil {
  81. client = newClient
  82. } else {
  83. return err
  84. }
  85. }
  86. return nil
  87. }
  88. eachEntryFunc := func(resp *filer_pb.SubscribeMetadataResponse) error {
  89. message := resp.EventNotification
  90. if strings.HasPrefix(resp.Directory, filer.DirectoryEtcRemote) {
  91. return handleEtcRemoteChanges(resp)
  92. }
  93. if filer_pb.IsEmpty(resp) {
  94. return nil
  95. }
  96. if filer_pb.IsCreate(resp) {
  97. if strings.Contains(message.NewParentPath, "/"+s3_constants.MultipartUploadsFolder+"/") {
  98. return nil
  99. }
  100. if !filer.HasData(message.NewEntry) {
  101. return nil
  102. }
  103. glog.V(2).Infof("create: %+v", resp)
  104. if !shouldSendToRemote(message.NewEntry) {
  105. glog.V(2).Infof("skipping creating: %+v", resp)
  106. return nil
  107. }
  108. dest := toRemoteStorageLocation(util.FullPath(mountedDir), util.NewFullPath(message.NewParentPath, message.NewEntry.Name), remoteStorageMountLocation)
  109. if message.NewEntry.IsDirectory {
  110. glog.V(0).Infof("mkdir %s", remote_storage.FormatLocation(dest))
  111. return client.WriteDirectory(dest, message.NewEntry)
  112. }
  113. glog.V(0).Infof("create %s", remote_storage.FormatLocation(dest))
  114. remoteEntry, writeErr := retriedWriteFile(client, filerSource, message.NewEntry, dest)
  115. if writeErr != nil {
  116. return writeErr
  117. }
  118. return updateLocalEntry(&remoteSyncOptions, message.NewParentPath, message.NewEntry, remoteEntry)
  119. }
  120. if filer_pb.IsDelete(resp) {
  121. glog.V(2).Infof("delete: %+v", resp)
  122. dest := toRemoteStorageLocation(util.FullPath(mountedDir), util.NewFullPath(resp.Directory, message.OldEntry.Name), remoteStorageMountLocation)
  123. if message.OldEntry.IsDirectory {
  124. glog.V(0).Infof("rmdir %s", remote_storage.FormatLocation(dest))
  125. return client.RemoveDirectory(dest)
  126. }
  127. glog.V(0).Infof("delete %s", remote_storage.FormatLocation(dest))
  128. return client.DeleteFile(dest)
  129. }
  130. if message.OldEntry != nil && message.NewEntry != nil {
  131. oldDest := toRemoteStorageLocation(util.FullPath(mountedDir), util.NewFullPath(resp.Directory, message.OldEntry.Name), remoteStorageMountLocation)
  132. dest := toRemoteStorageLocation(util.FullPath(mountedDir), util.NewFullPath(message.NewParentPath, message.NewEntry.Name), remoteStorageMountLocation)
  133. if !shouldSendToRemote(message.NewEntry) {
  134. glog.V(2).Infof("skipping updating: %+v", resp)
  135. return nil
  136. }
  137. if message.NewEntry.IsDirectory {
  138. return client.WriteDirectory(dest, message.NewEntry)
  139. }
  140. if resp.Directory == message.NewParentPath && message.OldEntry.Name == message.NewEntry.Name {
  141. if filer.IsSameData(message.OldEntry, message.NewEntry) {
  142. glog.V(2).Infof("update meta: %+v", resp)
  143. return client.UpdateFileMetadata(dest, message.OldEntry, message.NewEntry)
  144. }
  145. }
  146. glog.V(2).Infof("update: %+v", resp)
  147. glog.V(0).Infof("delete %s", remote_storage.FormatLocation(oldDest))
  148. if err := client.DeleteFile(oldDest); err != nil {
  149. if !strings.Contains(resp.Directory, "/"+s3_constants.MultipartUploadsFolder+"/") {
  150. return err
  151. }
  152. }
  153. remoteEntry, writeErr := retriedWriteFile(client, filerSource, message.NewEntry, dest)
  154. if writeErr != nil {
  155. return writeErr
  156. }
  157. return updateLocalEntry(&remoteSyncOptions, message.NewParentPath, message.NewEntry, remoteEntry)
  158. }
  159. return nil
  160. }
  161. return eachEntryFunc, nil
  162. }
  163. func retriedWriteFile(client remote_storage.RemoteStorageClient, filerSource *source.FilerSource, newEntry *filer_pb.Entry, dest *remote_pb.RemoteStorageLocation) (remoteEntry *filer_pb.RemoteEntry, err error) {
  164. var writeErr error
  165. err = util.Retry("writeFile", func() error {
  166. reader := filer.NewFileReader(filerSource, newEntry)
  167. glog.V(0).Infof("create %s", remote_storage.FormatLocation(dest))
  168. remoteEntry, writeErr = client.WriteFile(dest, newEntry, reader)
  169. if writeErr != nil {
  170. return writeErr
  171. }
  172. return nil
  173. })
  174. if err != nil {
  175. glog.Errorf("write to %s: %v", dest, err)
  176. }
  177. return
  178. }
  179. func collectLastSyncOffset(filerClient filer_pb.FilerClient, grpcDialOption grpc.DialOption, filerAddress pb.ServerAddress, mountedDir string, timeAgo time.Duration) time.Time {
  180. // 1. specified by timeAgo
  181. // 2. last offset timestamp for this directory
  182. // 3. directory creation time
  183. var lastOffsetTs time.Time
  184. if timeAgo == 0 {
  185. mountedDirEntry, err := filer_pb.GetEntry(filerClient, util.FullPath(mountedDir))
  186. if err != nil {
  187. glog.V(0).Infof("get mounted directory %s: %v", mountedDir, err)
  188. return time.Now()
  189. }
  190. lastOffsetTsNs, err := remote_storage.GetSyncOffset(grpcDialOption, filerAddress, mountedDir)
  191. if mountedDirEntry != nil {
  192. if err == nil && mountedDirEntry.Attributes.Crtime < lastOffsetTsNs/1000000 {
  193. lastOffsetTs = time.Unix(0, lastOffsetTsNs)
  194. glog.V(0).Infof("resume from %v", lastOffsetTs)
  195. } else {
  196. lastOffsetTs = time.Unix(mountedDirEntry.Attributes.Crtime, 0)
  197. }
  198. } else {
  199. lastOffsetTs = time.Now()
  200. }
  201. } else {
  202. lastOffsetTs = time.Now().Add(-timeAgo)
  203. }
  204. return lastOffsetTs
  205. }
  206. func toRemoteStorageLocation(mountDir, sourcePath util.FullPath, remoteMountLocation *remote_pb.RemoteStorageLocation) *remote_pb.RemoteStorageLocation {
  207. source := string(sourcePath[len(mountDir):])
  208. dest := util.FullPath(remoteMountLocation.Path).Child(source)
  209. return &remote_pb.RemoteStorageLocation{
  210. Name: remoteMountLocation.Name,
  211. Bucket: remoteMountLocation.Bucket,
  212. Path: string(dest),
  213. }
  214. }
  215. func shouldSendToRemote(entry *filer_pb.Entry) bool {
  216. if entry.RemoteEntry == nil {
  217. return true
  218. }
  219. if entry.RemoteEntry.RemoteMtime < entry.Attributes.Mtime {
  220. return true
  221. }
  222. return false
  223. }
  224. func updateLocalEntry(filerClient filer_pb.FilerClient, dir string, entry *filer_pb.Entry, remoteEntry *filer_pb.RemoteEntry) error {
  225. remoteEntry.LastLocalSyncTsNs = time.Now().UnixNano()
  226. entry.RemoteEntry = remoteEntry
  227. return filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  228. _, err := client.UpdateEntry(context.Background(), &filer_pb.UpdateEntryRequest{
  229. Directory: dir,
  230. Entry: entry,
  231. })
  232. return err
  233. })
  234. }