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.

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