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.

327 lines
12 KiB

  1. package command
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/filer"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/pb"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "github.com/chrislusf/seaweedfs/weed/pb/remote_pb"
  9. "github.com/chrislusf/seaweedfs/weed/remote_storage"
  10. "github.com/chrislusf/seaweedfs/weed/replication/source"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. "github.com/golang/protobuf/proto"
  13. "math"
  14. "strings"
  15. "time"
  16. )
  17. func (option *RemoteSyncOptions) followBucketUpdatesAndUploadToRemote(filerSource *source.FilerSource) error {
  18. // read filer remote storage mount mappings
  19. if detectErr := option.collectRemoteStorageConf(); detectErr != nil {
  20. return fmt.Errorf("read mount info: %v", detectErr)
  21. }
  22. eachEntryFunc, err := option.makeBucketedEventProcessor(filerSource)
  23. if err != nil {
  24. return err
  25. }
  26. processEventFnWithOffset := pb.AddOffsetFunc(eachEntryFunc, 3*time.Second, func(counter int64, lastTsNs int64) error {
  27. lastTime := time.Unix(0, lastTsNs)
  28. glog.V(0).Infof("remote sync %s progressed to %v %0.2f/sec", *option.filerAddress, lastTime, float64(counter)/float64(3))
  29. return remote_storage.SetSyncOffset(option.grpcDialOption, *option.filerAddress, option.bucketsDir, lastTsNs)
  30. })
  31. lastOffsetTs := collectLastSyncOffset(option, option.bucketsDir)
  32. return pb.FollowMetadata(*option.filerAddress, option.grpcDialOption, "filer.remote.sync",
  33. option.bucketsDir, []string{filer.DirectoryEtcRemote}, lastOffsetTs.UnixNano(), 0, processEventFnWithOffset, false)
  34. }
  35. func (option *RemoteSyncOptions) makeBucketedEventProcessor(filerSource *source.FilerSource) (pb.ProcessMetadataFunc, error) {
  36. handleCreateBucket := func(entry *filer_pb.Entry) error {
  37. if !entry.IsDirectory {
  38. return nil
  39. }
  40. remoteConf, found := option.remoteConfs[*option.createBucketAt]
  41. if !found {
  42. return fmt.Errorf("un-configured remote storage %s", *option.createBucketAt)
  43. }
  44. client, err := remote_storage.GetRemoteStorage(remoteConf)
  45. if err != nil {
  46. return err
  47. }
  48. glog.V(0).Infof("create bucket %s", entry.Name)
  49. if err := client.CreateBucket(entry.Name); err != nil {
  50. return err
  51. }
  52. return nil
  53. }
  54. handleDeleteBucket := func(entry *filer_pb.Entry) error {
  55. if !entry.IsDirectory {
  56. return nil
  57. }
  58. client, err := option.findRemoteStorageClient(entry.Name)
  59. if err != nil {
  60. return err
  61. }
  62. glog.V(0).Infof("delete bucket %s", entry.Name)
  63. if err := client.DeleteBucket(entry.Name); err != nil {
  64. return err
  65. }
  66. return nil
  67. }
  68. handleEtcRemoteChanges := func(resp *filer_pb.SubscribeMetadataResponse) error {
  69. message := resp.EventNotification
  70. if message.NewEntry == nil {
  71. return nil
  72. }
  73. if message.NewEntry.Name == filer.REMOTE_STORAGE_MOUNT_FILE {
  74. newMappings, readErr := filer.UnmarshalRemoteStorageMappings(message.NewEntry.Content)
  75. if readErr != nil {
  76. return fmt.Errorf("unmarshal mappings: %v", readErr)
  77. }
  78. option.mappings = newMappings
  79. }
  80. if strings.HasSuffix(message.NewEntry.Name, filer.REMOTE_STORAGE_CONF_SUFFIX) {
  81. conf := &remote_pb.RemoteConf{}
  82. if err := proto.Unmarshal(message.NewEntry.Content, conf); err != nil {
  83. return fmt.Errorf("unmarshal %s/%s: %v", filer.DirectoryEtcRemote, message.NewEntry.Name, err)
  84. }
  85. option.remoteConfs[conf.Name] = conf
  86. }
  87. return nil
  88. }
  89. eachEntryFunc := func(resp *filer_pb.SubscribeMetadataResponse) error {
  90. message := resp.EventNotification
  91. if strings.HasPrefix(resp.Directory, filer.DirectoryEtcRemote) {
  92. return handleEtcRemoteChanges(resp)
  93. }
  94. if message.OldEntry == nil && message.NewEntry == nil {
  95. return nil
  96. }
  97. if message.OldEntry == nil && message.NewEntry != nil {
  98. if message.NewParentPath == option.bucketsDir {
  99. return handleCreateBucket(message.NewEntry)
  100. }
  101. if !filer.HasData(message.NewEntry) {
  102. return nil
  103. }
  104. bucket, remoteStorageMountLocation, remoteStorage, ok := option.detectBucketInfo(message.NewParentPath)
  105. if !ok {
  106. return nil
  107. }
  108. client, err := remote_storage.GetRemoteStorage(remoteStorage)
  109. if err != nil {
  110. return err
  111. }
  112. glog.V(2).Infof("create: %+v", resp)
  113. if !shouldSendToRemote(message.NewEntry) {
  114. glog.V(2).Infof("skipping creating: %+v", resp)
  115. return nil
  116. }
  117. dest := toRemoteStorageLocation(bucket, util.NewFullPath(message.NewParentPath, message.NewEntry.Name), remoteStorageMountLocation)
  118. if message.NewEntry.IsDirectory {
  119. glog.V(0).Infof("mkdir %s", remote_storage.FormatLocation(dest))
  120. return client.WriteDirectory(dest, message.NewEntry)
  121. }
  122. glog.V(0).Infof("create %s", remote_storage.FormatLocation(dest))
  123. reader := filer.NewFileReader(filerSource, message.NewEntry)
  124. remoteEntry, writeErr := client.WriteFile(dest, message.NewEntry, reader)
  125. if writeErr != nil {
  126. return writeErr
  127. }
  128. return updateLocalEntry(&remoteSyncOptions, message.NewParentPath, message.NewEntry, remoteEntry)
  129. }
  130. if message.OldEntry != nil && message.NewEntry == nil {
  131. if resp.Directory == option.bucketsDir {
  132. return handleDeleteBucket(message.OldEntry)
  133. }
  134. bucket, remoteStorageMountLocation, remoteStorage, ok := option.detectBucketInfo(resp.Directory)
  135. if !ok {
  136. return nil
  137. }
  138. client, err := remote_storage.GetRemoteStorage(remoteStorage)
  139. if err != nil {
  140. return err
  141. }
  142. glog.V(2).Infof("delete: %+v", resp)
  143. dest := toRemoteStorageLocation(bucket, 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. if resp.Directory == option.bucketsDir {
  153. if message.NewParentPath == option.bucketsDir {
  154. if message.OldEntry.Name == message.NewEntry.Name {
  155. return nil
  156. }
  157. if err := handleCreateBucket(message.NewEntry); err != nil {
  158. return err
  159. }
  160. if err := handleDeleteBucket(message.OldEntry); err != nil {
  161. return err
  162. }
  163. }
  164. }
  165. oldBucket, oldRemoteStorageMountLocation, oldRemoteStorage, oldOk := option.detectBucketInfo(resp.Directory)
  166. newBucket, newRemoteStorageMountLocation, newRemoteStorage, newOk := option.detectBucketInfo(message.NewParentPath)
  167. if oldOk && newOk {
  168. if !shouldSendToRemote(message.NewEntry) {
  169. glog.V(2).Infof("skipping updating: %+v", resp)
  170. return nil
  171. }
  172. client, err := remote_storage.GetRemoteStorage(oldRemoteStorage)
  173. if err != nil {
  174. return err
  175. }
  176. if resp.Directory == message.NewParentPath && message.OldEntry.Name == message.NewEntry.Name {
  177. // update the same entry
  178. if message.NewEntry.IsDirectory {
  179. // update directory property
  180. return nil
  181. }
  182. if filer.IsSameData(message.OldEntry, message.NewEntry) {
  183. glog.V(2).Infof("update meta: %+v", resp)
  184. oldDest := toRemoteStorageLocation(oldBucket, util.NewFullPath(resp.Directory, message.OldEntry.Name), oldRemoteStorageMountLocation)
  185. return client.UpdateFileMetadata(oldDest, message.OldEntry, message.NewEntry)
  186. } else {
  187. newDest := toRemoteStorageLocation(newBucket, util.NewFullPath(message.NewParentPath, message.NewEntry.Name), newRemoteStorageMountLocation)
  188. reader := filer.NewFileReader(filerSource, message.NewEntry)
  189. glog.V(0).Infof("create %s", remote_storage.FormatLocation(newDest))
  190. remoteEntry, writeErr := client.WriteFile(newDest, message.NewEntry, reader)
  191. if writeErr != nil {
  192. return writeErr
  193. }
  194. return updateLocalEntry(&remoteSyncOptions, message.NewParentPath, message.NewEntry, remoteEntry)
  195. }
  196. }
  197. }
  198. // the following is entry rename
  199. if oldOk {
  200. client, err := remote_storage.GetRemoteStorage(oldRemoteStorage)
  201. if err != nil {
  202. return err
  203. }
  204. oldDest := toRemoteStorageLocation(oldBucket, util.NewFullPath(resp.Directory, message.OldEntry.Name), oldRemoteStorageMountLocation)
  205. if message.OldEntry.IsDirectory {
  206. return client.RemoveDirectory(oldDest)
  207. }
  208. glog.V(0).Infof("delete %s", remote_storage.FormatLocation(oldDest))
  209. if err := client.DeleteFile(oldDest); err != nil {
  210. return err
  211. }
  212. }
  213. if newOk {
  214. if !shouldSendToRemote(message.NewEntry) {
  215. glog.V(2).Infof("skipping updating: %+v", resp)
  216. return nil
  217. }
  218. client, err := remote_storage.GetRemoteStorage(newRemoteStorage)
  219. if err != nil {
  220. return err
  221. }
  222. newDest := toRemoteStorageLocation(newBucket, util.NewFullPath(message.NewParentPath, message.NewEntry.Name), newRemoteStorageMountLocation)
  223. if message.NewEntry.IsDirectory {
  224. return client.WriteDirectory(newDest, message.NewEntry)
  225. }
  226. reader := filer.NewFileReader(filerSource, message.NewEntry)
  227. glog.V(0).Infof("create %s", remote_storage.FormatLocation(newDest))
  228. remoteEntry, writeErr := client.WriteFile(newDest, message.NewEntry, reader)
  229. if writeErr != nil {
  230. return writeErr
  231. }
  232. return updateLocalEntry(&remoteSyncOptions, message.NewParentPath, message.NewEntry, remoteEntry)
  233. }
  234. }
  235. return nil
  236. }
  237. return eachEntryFunc, nil
  238. }
  239. func (option *RemoteSyncOptions)findRemoteStorageClient(bucketName string) (remote_storage.RemoteStorageClient, error) {
  240. bucket := util.FullPath(option.bucketsDir).Child(bucketName)
  241. remoteStorageMountLocation, isMounted := option.mappings.Mappings[string(bucket)]
  242. if !isMounted {
  243. return nil, fmt.Errorf("%s is not mounted", bucket)
  244. }
  245. remoteConf, hasClient := option.remoteConfs[remoteStorageMountLocation.Name]
  246. if !hasClient {
  247. return nil, fmt.Errorf("%s mounted to un-configured %+v", bucket, remoteStorageMountLocation)
  248. }
  249. client, err := remote_storage.GetRemoteStorage(remoteConf)
  250. if err != nil {
  251. return nil, err
  252. }
  253. return client, nil
  254. }
  255. func (option *RemoteSyncOptions) detectBucketInfo(actualDir string) (bucket util.FullPath, remoteStorageMountLocation *remote_pb.RemoteStorageLocation, remoteConf *remote_pb.RemoteConf, ok bool) {
  256. bucket, ok = extractBucketPath(option.bucketsDir, actualDir)
  257. if !ok {
  258. return "", nil, nil, false
  259. }
  260. var isMounted bool
  261. remoteStorageMountLocation, isMounted = option.mappings.Mappings[string(bucket)]
  262. if !isMounted {
  263. glog.Warningf("%s is not mounted", bucket)
  264. return "", nil, nil, false
  265. }
  266. var hasClient bool
  267. remoteConf, hasClient = option.remoteConfs[remoteStorageMountLocation.Name]
  268. if !hasClient {
  269. glog.Warningf("%s mounted to un-configured %+v", bucket, remoteStorageMountLocation)
  270. return "", nil, nil, false
  271. }
  272. return bucket, remoteStorageMountLocation, remoteConf, true
  273. }
  274. func extractBucketPath(bucketsDir, dir string) (util.FullPath, bool) {
  275. if !strings.HasPrefix(dir, bucketsDir+"/") {
  276. return "", false
  277. }
  278. parts := strings.SplitN(dir[len(bucketsDir)+1:], "/", 2)
  279. return util.FullPath(bucketsDir).Child(parts[0]), true
  280. }
  281. func (option *RemoteSyncOptions) collectRemoteStorageConf() (err error) {
  282. if mappings, err := filer.ReadMountMappings(option.grpcDialOption, *option.filerAddress); err != nil {
  283. return err
  284. } else {
  285. option.mappings = mappings
  286. }
  287. option.remoteConfs = make(map[string]*remote_pb.RemoteConf)
  288. err = filer_pb.List(option, filer.DirectoryEtcRemote, "", func(entry *filer_pb.Entry, isLast bool) error {
  289. if !strings.HasSuffix(entry.Name, filer.REMOTE_STORAGE_CONF_SUFFIX) {
  290. return nil
  291. }
  292. conf := &remote_pb.RemoteConf{}
  293. if err := proto.Unmarshal(entry.Content, conf); err != nil {
  294. return fmt.Errorf("unmarshal %s/%s: %v", filer.DirectoryEtcRemote, entry.Name, err)
  295. }
  296. option.remoteConfs[conf.Name] = conf
  297. return nil
  298. }, "", false, math.MaxUint32)
  299. return
  300. }