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.

284 lines
11 KiB

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