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.

502 lines
19 KiB

4 years ago
4 years ago
2 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
  1. package command
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "github.com/seaweedfs/seaweedfs/weed/glog"
  7. "github.com/seaweedfs/seaweedfs/weed/pb"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  9. "github.com/seaweedfs/seaweedfs/weed/replication"
  10. "github.com/seaweedfs/seaweedfs/weed/replication/sink"
  11. "github.com/seaweedfs/seaweedfs/weed/replication/sink/filersink"
  12. "github.com/seaweedfs/seaweedfs/weed/replication/source"
  13. "github.com/seaweedfs/seaweedfs/weed/security"
  14. statsCollect "github.com/seaweedfs/seaweedfs/weed/stats"
  15. "github.com/seaweedfs/seaweedfs/weed/util"
  16. "github.com/seaweedfs/seaweedfs/weed/util/grace"
  17. "google.golang.org/grpc"
  18. "os"
  19. "regexp"
  20. "strings"
  21. "time"
  22. )
  23. type SyncOptions struct {
  24. isActivePassive *bool
  25. filerA *string
  26. filerB *string
  27. aPath *string
  28. aExcludePaths *string
  29. bPath *string
  30. bExcludePaths *string
  31. aReplication *string
  32. bReplication *string
  33. aCollection *string
  34. bCollection *string
  35. aTtlSec *int
  36. bTtlSec *int
  37. aDiskType *string
  38. bDiskType *string
  39. aDebug *bool
  40. bDebug *bool
  41. aFromTsMs *int64
  42. bFromTsMs *int64
  43. aProxyByFiler *bool
  44. bProxyByFiler *bool
  45. metricsHttpIp *string
  46. metricsHttpPort *int
  47. concurrency *int
  48. clientId int32
  49. clientEpoch int32
  50. }
  51. const (
  52. SyncKeyPrefix = "sync."
  53. DefaultConcurrencyLimit = 32
  54. )
  55. var (
  56. syncOptions SyncOptions
  57. syncCpuProfile *string
  58. syncMemProfile *string
  59. )
  60. func init() {
  61. cmdFilerSynchronize.Run = runFilerSynchronize // break init cycle
  62. syncOptions.isActivePassive = cmdFilerSynchronize.Flag.Bool("isActivePassive", false, "one directional follow from A to B if true")
  63. syncOptions.filerA = cmdFilerSynchronize.Flag.String("a", "", "filer A in one SeaweedFS cluster")
  64. syncOptions.filerB = cmdFilerSynchronize.Flag.String("b", "", "filer B in the other SeaweedFS cluster")
  65. syncOptions.aPath = cmdFilerSynchronize.Flag.String("a.path", "/", "directory to sync on filer A")
  66. syncOptions.aExcludePaths = cmdFilerSynchronize.Flag.String("a.excludePaths", "", "exclude directories to sync on filer A")
  67. syncOptions.bPath = cmdFilerSynchronize.Flag.String("b.path", "/", "directory to sync on filer B")
  68. syncOptions.bExcludePaths = cmdFilerSynchronize.Flag.String("b.excludePaths", "", "exclude directories to sync on filer B")
  69. syncOptions.aReplication = cmdFilerSynchronize.Flag.String("a.replication", "", "replication on filer A")
  70. syncOptions.bReplication = cmdFilerSynchronize.Flag.String("b.replication", "", "replication on filer B")
  71. syncOptions.aCollection = cmdFilerSynchronize.Flag.String("a.collection", "", "collection on filer A")
  72. syncOptions.bCollection = cmdFilerSynchronize.Flag.String("b.collection", "", "collection on filer B")
  73. syncOptions.aTtlSec = cmdFilerSynchronize.Flag.Int("a.ttlSec", 0, "ttl in seconds on filer A")
  74. syncOptions.bTtlSec = cmdFilerSynchronize.Flag.Int("b.ttlSec", 0, "ttl in seconds on filer B")
  75. syncOptions.aDiskType = cmdFilerSynchronize.Flag.String("a.disk", "", "[hdd|ssd|<tag>] hard drive or solid state drive or any tag on filer A")
  76. syncOptions.bDiskType = cmdFilerSynchronize.Flag.String("b.disk", "", "[hdd|ssd|<tag>] hard drive or solid state drive or any tag on filer B")
  77. syncOptions.aProxyByFiler = cmdFilerSynchronize.Flag.Bool("a.filerProxy", false, "read and write file chunks by filer A instead of volume servers")
  78. syncOptions.bProxyByFiler = cmdFilerSynchronize.Flag.Bool("b.filerProxy", false, "read and write file chunks by filer B instead of volume servers")
  79. syncOptions.aDebug = cmdFilerSynchronize.Flag.Bool("a.debug", false, "debug mode to print out filer A received files")
  80. syncOptions.bDebug = cmdFilerSynchronize.Flag.Bool("b.debug", false, "debug mode to print out filer B received files")
  81. syncOptions.aFromTsMs = cmdFilerSynchronize.Flag.Int64("a.fromTsMs", 0, "synchronization from timestamp on filer A. The unit is millisecond")
  82. syncOptions.bFromTsMs = cmdFilerSynchronize.Flag.Int64("b.fromTsMs", 0, "synchronization from timestamp on filer B. The unit is millisecond")
  83. syncOptions.concurrency = cmdFilerSynchronize.Flag.Int("concurrency", DefaultConcurrencyLimit, "The maximum number of files that will be synced concurrently.")
  84. syncCpuProfile = cmdFilerSynchronize.Flag.String("cpuprofile", "", "cpu profile output file")
  85. syncMemProfile = cmdFilerSynchronize.Flag.String("memprofile", "", "memory profile output file")
  86. syncOptions.metricsHttpIp = cmdFilerSynchronize.Flag.String("metricsIp", "", "metrics listen ip")
  87. syncOptions.metricsHttpPort = cmdFilerSynchronize.Flag.Int("metricsPort", 0, "metrics listen port")
  88. syncOptions.clientId = util.RandomInt32()
  89. }
  90. var cmdFilerSynchronize = &Command{
  91. UsageLine: "filer.sync -a=<oneFilerHost>:<oneFilerPort> -b=<otherFilerHost>:<otherFilerPort>",
  92. Short: "resumable continuous synchronization between two active-active or active-passive SeaweedFS clusters",
  93. Long: `resumable continuous synchronization for file changes between two active-active or active-passive filers
  94. filer.sync listens on filer notifications. If any file is updated, it will fetch the updated content,
  95. and write to the other destination. Different from filer.replicate:
  96. * filer.sync only works between two filers.
  97. * filer.sync does not need any special message queue setup.
  98. * filer.sync supports both active-active and active-passive modes.
  99. If restarted, the synchronization will resume from the previous checkpoints, persisted every minute.
  100. A fresh sync will start from the earliest metadata logs.
  101. `,
  102. }
  103. func runFilerSynchronize(cmd *Command, args []string) bool {
  104. util.LoadConfiguration("security", false)
  105. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
  106. grace.SetupProfiling(*syncCpuProfile, *syncMemProfile)
  107. filerA := pb.ServerAddress(*syncOptions.filerA)
  108. filerB := pb.ServerAddress(*syncOptions.filerB)
  109. // start filer.sync metrics server
  110. go statsCollect.StartMetricsServer(*syncOptions.metricsHttpIp, *syncOptions.metricsHttpPort)
  111. // read a filer signature
  112. aFilerSignature, aFilerErr := replication.ReadFilerSignature(grpcDialOption, filerA)
  113. if aFilerErr != nil {
  114. glog.Errorf("get filer 'a' signature %d error from %s to %s: %v", aFilerSignature, *syncOptions.filerA, *syncOptions.filerB, aFilerErr)
  115. return true
  116. }
  117. // read b filer signature
  118. bFilerSignature, bFilerErr := replication.ReadFilerSignature(grpcDialOption, filerB)
  119. if bFilerErr != nil {
  120. glog.Errorf("get filer 'b' signature %d error from %s to %s: %v", bFilerSignature, *syncOptions.filerA, *syncOptions.filerB, bFilerErr)
  121. return true
  122. }
  123. go func() {
  124. // a->b
  125. // set synchronization start timestamp to offset
  126. initOffsetError := initOffsetFromTsMs(grpcDialOption, filerB, aFilerSignature, *syncOptions.bFromTsMs, getSignaturePrefixByPath(*syncOptions.aPath))
  127. if initOffsetError != nil {
  128. glog.Errorf("init offset from timestamp %d error from %s to %s: %v", *syncOptions.bFromTsMs, *syncOptions.filerA, *syncOptions.filerB, initOffsetError)
  129. os.Exit(2)
  130. }
  131. for {
  132. syncOptions.clientEpoch++
  133. err := doSubscribeFilerMetaChanges(
  134. syncOptions.clientId,
  135. syncOptions.clientEpoch,
  136. grpcDialOption,
  137. filerA,
  138. *syncOptions.aPath,
  139. util.StringSplit(*syncOptions.aExcludePaths, ","),
  140. *syncOptions.aProxyByFiler,
  141. filerB,
  142. *syncOptions.bPath,
  143. *syncOptions.bReplication,
  144. *syncOptions.bCollection,
  145. *syncOptions.bTtlSec,
  146. *syncOptions.bProxyByFiler,
  147. *syncOptions.bDiskType,
  148. *syncOptions.bDebug,
  149. *syncOptions.concurrency,
  150. aFilerSignature,
  151. bFilerSignature)
  152. if err != nil {
  153. glog.Errorf("sync from %s to %s: %v", *syncOptions.filerA, *syncOptions.filerB, err)
  154. time.Sleep(1747 * time.Millisecond)
  155. }
  156. }
  157. }()
  158. if !*syncOptions.isActivePassive {
  159. // b->a
  160. // set synchronization start timestamp to offset
  161. initOffsetError := initOffsetFromTsMs(grpcDialOption, filerA, bFilerSignature, *syncOptions.aFromTsMs, getSignaturePrefixByPath(*syncOptions.bPath))
  162. if initOffsetError != nil {
  163. glog.Errorf("init offset from timestamp %d error from %s to %s: %v", *syncOptions.aFromTsMs, *syncOptions.filerB, *syncOptions.filerA, initOffsetError)
  164. os.Exit(2)
  165. }
  166. go func() {
  167. for {
  168. syncOptions.clientEpoch++
  169. err := doSubscribeFilerMetaChanges(
  170. syncOptions.clientId,
  171. syncOptions.clientEpoch,
  172. grpcDialOption,
  173. filerB,
  174. *syncOptions.bPath,
  175. util.StringSplit(*syncOptions.bExcludePaths, ","),
  176. *syncOptions.bProxyByFiler,
  177. filerA,
  178. *syncOptions.aPath,
  179. *syncOptions.aReplication,
  180. *syncOptions.aCollection,
  181. *syncOptions.aTtlSec,
  182. *syncOptions.aProxyByFiler,
  183. *syncOptions.aDiskType,
  184. *syncOptions.aDebug,
  185. *syncOptions.concurrency,
  186. bFilerSignature,
  187. aFilerSignature)
  188. if err != nil {
  189. glog.Errorf("sync from %s to %s: %v", *syncOptions.filerB, *syncOptions.filerA, err)
  190. time.Sleep(2147 * time.Millisecond)
  191. }
  192. }
  193. }()
  194. }
  195. select {}
  196. return true
  197. }
  198. // initOffsetFromTsMs Initialize offset
  199. func initOffsetFromTsMs(grpcDialOption grpc.DialOption, targetFiler pb.ServerAddress, sourceFilerSignature int32, fromTsMs int64, signaturePrefix string) error {
  200. if fromTsMs <= 0 {
  201. return nil
  202. }
  203. // convert to nanosecond
  204. fromTsNs := fromTsMs * 1000_000
  205. // If not successful, exit the program.
  206. setOffsetErr := setOffset(grpcDialOption, targetFiler, signaturePrefix, sourceFilerSignature, fromTsNs)
  207. if setOffsetErr != nil {
  208. return setOffsetErr
  209. }
  210. glog.Infof("setOffset from timestamp ms success! start offset: %d from %s to %s", fromTsNs, *syncOptions.filerA, *syncOptions.filerB)
  211. return nil
  212. }
  213. func doSubscribeFilerMetaChanges(clientId int32, clientEpoch int32, grpcDialOption grpc.DialOption, sourceFiler pb.ServerAddress, sourcePath string, sourceExcludePaths []string, sourceReadChunkFromFiler bool, targetFiler pb.ServerAddress, targetPath string,
  214. replicationStr, collection string, ttlSec int, sinkWriteChunkByFiler bool, diskType string, debug bool, concurrency int, sourceFilerSignature int32, targetFilerSignature int32) error {
  215. // if first time, start from now
  216. // if has previously synced, resume from that point of time
  217. sourceFilerOffsetTsNs, err := getOffset(grpcDialOption, targetFiler, getSignaturePrefixByPath(sourcePath), sourceFilerSignature)
  218. if err != nil {
  219. return err
  220. }
  221. glog.V(0).Infof("start sync %s(%d) => %s(%d) from %v(%d)", sourceFiler, sourceFilerSignature, targetFiler, targetFilerSignature, time.Unix(0, sourceFilerOffsetTsNs), sourceFilerOffsetTsNs)
  222. // create filer sink
  223. filerSource := &source.FilerSource{}
  224. filerSource.DoInitialize(sourceFiler.ToHttpAddress(), sourceFiler.ToGrpcAddress(), sourcePath, sourceReadChunkFromFiler)
  225. filerSink := &filersink.FilerSink{}
  226. filerSink.DoInitialize(targetFiler.ToHttpAddress(), targetFiler.ToGrpcAddress(), targetPath, replicationStr, collection, ttlSec, diskType, grpcDialOption, sinkWriteChunkByFiler)
  227. filerSink.SetSourceFiler(filerSource)
  228. persistEventFn := genProcessFunction(sourcePath, targetPath, sourceExcludePaths, nil, filerSink, true, debug)
  229. processEventFn := func(resp *filer_pb.SubscribeMetadataResponse) error {
  230. message := resp.EventNotification
  231. for _, sig := range message.Signatures {
  232. if sig == targetFilerSignature && targetFilerSignature != 0 {
  233. fmt.Printf("%s skipping %s change to %v\n", targetFiler, sourceFiler, message)
  234. return nil
  235. }
  236. }
  237. return persistEventFn(resp)
  238. }
  239. if concurrency < 0 || concurrency > 1024 {
  240. glog.Warningf("invalid concurrency value, using default: %d", DefaultConcurrencyLimit)
  241. concurrency = DefaultConcurrencyLimit
  242. }
  243. processor := NewMetadataProcessor(processEventFn, concurrency)
  244. var lastLogTsNs = time.Now().UnixNano()
  245. var clientName = fmt.Sprintf("syncFrom_%s_To_%s", string(sourceFiler), string(targetFiler))
  246. processEventFnWithOffset := pb.AddOffsetFunc(func(resp *filer_pb.SubscribeMetadataResponse) error {
  247. processor.AddSyncJob(resp)
  248. return nil
  249. }, 3*time.Second, func(counter int64, lastTsNs int64) error {
  250. if processor.processedTsWatermark == 0 {
  251. return nil
  252. }
  253. // use processor.processedTsWatermark instead of the lastTsNs from the most recent job
  254. now := time.Now().UnixNano()
  255. glog.V(0).Infof("sync %s to %s progressed to %v %0.2f/sec", sourceFiler, targetFiler, time.Unix(0, processor.processedTsWatermark), float64(counter)/(float64(now-lastLogTsNs)/1e9))
  256. lastLogTsNs = now
  257. // collect synchronous offset
  258. statsCollect.FilerSyncOffsetGauge.WithLabelValues(sourceFiler.String(), targetFiler.String(), clientName, sourcePath).Set(float64(processor.processedTsWatermark))
  259. return setOffset(grpcDialOption, targetFiler, getSignaturePrefixByPath(sourcePath), sourceFilerSignature, processor.processedTsWatermark)
  260. })
  261. metadataFollowOption := &pb.MetadataFollowOption{
  262. ClientName: clientName,
  263. ClientId: clientId,
  264. ClientEpoch: clientEpoch,
  265. SelfSignature: targetFilerSignature,
  266. PathPrefix: sourcePath,
  267. AdditionalPathPrefixes: nil,
  268. DirectoriesToWatch: nil,
  269. StartTsNs: sourceFilerOffsetTsNs,
  270. StopTsNs: 0,
  271. EventErrorType: pb.RetryForeverOnError,
  272. }
  273. return pb.FollowMetadata(sourceFiler, grpcDialOption, metadataFollowOption, processEventFnWithOffset)
  274. }
  275. // When each business is distinguished according to path, and offsets need to be maintained separately.
  276. func getSignaturePrefixByPath(path string) string {
  277. // compatible historical version
  278. if path == "/" {
  279. return SyncKeyPrefix
  280. } else {
  281. return SyncKeyPrefix + path
  282. }
  283. }
  284. func getOffset(grpcDialOption grpc.DialOption, filer pb.ServerAddress, signaturePrefix string, signature int32) (lastOffsetTsNs int64, readErr error) {
  285. readErr = pb.WithFilerClient(false, signature, filer, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  286. syncKey := []byte(signaturePrefix + "____")
  287. util.Uint32toBytes(syncKey[len(signaturePrefix):len(signaturePrefix)+4], uint32(signature))
  288. resp, err := client.KvGet(context.Background(), &filer_pb.KvGetRequest{Key: syncKey})
  289. if err != nil {
  290. return err
  291. }
  292. if len(resp.Error) != 0 {
  293. return errors.New(resp.Error)
  294. }
  295. if len(resp.Value) < 8 {
  296. return nil
  297. }
  298. lastOffsetTsNs = int64(util.BytesToUint64(resp.Value))
  299. return nil
  300. })
  301. return
  302. }
  303. func setOffset(grpcDialOption grpc.DialOption, filer pb.ServerAddress, signaturePrefix string, signature int32, offsetTsNs int64) error {
  304. return pb.WithFilerClient(false, signature, filer, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  305. syncKey := []byte(signaturePrefix + "____")
  306. util.Uint32toBytes(syncKey[len(signaturePrefix):len(signaturePrefix)+4], uint32(signature))
  307. valueBuf := make([]byte, 8)
  308. util.Uint64toBytes(valueBuf, uint64(offsetTsNs))
  309. resp, err := client.KvPut(context.Background(), &filer_pb.KvPutRequest{
  310. Key: syncKey,
  311. Value: valueBuf,
  312. })
  313. if err != nil {
  314. return err
  315. }
  316. if len(resp.Error) != 0 {
  317. return errors.New(resp.Error)
  318. }
  319. return nil
  320. })
  321. }
  322. func genProcessFunction(sourcePath string, targetPath string, excludePaths []string, reExcludeFileName *regexp.Regexp, dataSink sink.ReplicationSink, doDeleteFiles bool, debug bool) func(resp *filer_pb.SubscribeMetadataResponse) error {
  323. // process function
  324. processEventFn := func(resp *filer_pb.SubscribeMetadataResponse) error {
  325. message := resp.EventNotification
  326. var sourceOldKey, sourceNewKey util.FullPath
  327. if message.OldEntry != nil {
  328. sourceOldKey = util.FullPath(resp.Directory).Child(message.OldEntry.Name)
  329. }
  330. if message.NewEntry != nil {
  331. sourceNewKey = util.FullPath(message.NewParentPath).Child(message.NewEntry.Name)
  332. }
  333. if debug {
  334. glog.V(0).Infof("received %v", resp)
  335. }
  336. if !strings.HasPrefix(resp.Directory, sourcePath) {
  337. return nil
  338. }
  339. for _, excludePath := range excludePaths {
  340. if strings.HasPrefix(resp.Directory, excludePath) {
  341. return nil
  342. }
  343. }
  344. if reExcludeFileName != nil && reExcludeFileName.MatchString(message.NewEntry.Name) {
  345. return nil
  346. }
  347. if dataSink.IsIncremental() {
  348. doDeleteFiles = false
  349. }
  350. // handle deletions
  351. if filer_pb.IsDelete(resp) {
  352. if doDeleteFiles {
  353. return nil
  354. }
  355. if !strings.HasPrefix(string(sourceOldKey), sourcePath) {
  356. return nil
  357. }
  358. key := buildKey(dataSink, message, targetPath, sourceOldKey, sourcePath)
  359. return dataSink.DeleteEntry(key, message.OldEntry.IsDirectory, message.DeleteChunks, message.Signatures)
  360. }
  361. // handle new entries
  362. if filer_pb.IsCreate(resp) {
  363. if !strings.HasPrefix(string(sourceNewKey), sourcePath) {
  364. return nil
  365. }
  366. key := buildKey(dataSink, message, targetPath, sourceNewKey, sourcePath)
  367. if err := dataSink.CreateEntry(key, message.NewEntry, message.Signatures); err != nil {
  368. return fmt.Errorf("create entry1 : %v", err)
  369. } else {
  370. return nil
  371. }
  372. }
  373. // this is something special?
  374. if filer_pb.IsEmpty(resp) {
  375. return nil
  376. }
  377. // handle updates
  378. if strings.HasPrefix(string(sourceOldKey), sourcePath) {
  379. // old key is in the watched directory
  380. if strings.HasPrefix(string(sourceNewKey), sourcePath) {
  381. // new key is also in the watched directory
  382. if doDeleteFiles {
  383. oldKey := util.Join(targetPath, string(sourceOldKey)[len(sourcePath):])
  384. message.NewParentPath = util.Join(targetPath, message.NewParentPath[len(sourcePath):])
  385. foundExisting, err := dataSink.UpdateEntry(string(oldKey), message.OldEntry, message.NewParentPath, message.NewEntry, message.DeleteChunks, message.Signatures)
  386. if foundExisting {
  387. return err
  388. }
  389. // not able to find old entry
  390. if err = dataSink.DeleteEntry(string(oldKey), message.OldEntry.IsDirectory, false, message.Signatures); err != nil {
  391. return fmt.Errorf("delete old entry %v: %v", oldKey, err)
  392. }
  393. }
  394. // create the new entry
  395. newKey := buildKey(dataSink, message, targetPath, sourceNewKey, sourcePath)
  396. if err := dataSink.CreateEntry(newKey, message.NewEntry, message.Signatures); err != nil {
  397. return fmt.Errorf("create entry2 : %v", err)
  398. } else {
  399. return nil
  400. }
  401. } else {
  402. // new key is outside of the watched directory
  403. if doDeleteFiles {
  404. key := buildKey(dataSink, message, targetPath, sourceOldKey, sourcePath)
  405. return dataSink.DeleteEntry(key, message.OldEntry.IsDirectory, message.DeleteChunks, message.Signatures)
  406. }
  407. }
  408. } else {
  409. // old key is outside of the watched directory
  410. if strings.HasPrefix(string(sourceNewKey), sourcePath) {
  411. // new key is in the watched directory
  412. key := buildKey(dataSink, message, targetPath, sourceNewKey, sourcePath)
  413. if err := dataSink.CreateEntry(key, message.NewEntry, message.Signatures); err != nil {
  414. return fmt.Errorf("create entry3 : %v", err)
  415. } else {
  416. return nil
  417. }
  418. } else {
  419. // new key is also outside of the watched directory
  420. // skip
  421. }
  422. }
  423. return nil
  424. }
  425. return processEventFn
  426. }
  427. func buildKey(dataSink sink.ReplicationSink, message *filer_pb.EventNotification, targetPath string, sourceKey util.FullPath, sourcePath string) (key string) {
  428. if !dataSink.IsIncremental() {
  429. key = util.Join(targetPath, string(sourceKey)[len(sourcePath):])
  430. } else {
  431. var mTime int64
  432. if message.NewEntry != nil {
  433. mTime = message.NewEntry.Attributes.Mtime
  434. } else if message.OldEntry != nil {
  435. mTime = message.OldEntry.Attributes.Mtime
  436. }
  437. dateKey := time.Unix(mTime, 0).Format("2006-01-02")
  438. key = util.Join(targetPath, dateKey, string(sourceKey)[len(sourcePath):])
  439. }
  440. return escapeKey(key)
  441. }