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.

163 lines
4.0 KiB

  1. package sink
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/util"
  4. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  5. "fmt"
  6. "strings"
  7. "github.com/chrislusf/seaweedfs/weed/filer2"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "context"
  10. "sync"
  11. )
  12. type ReplicationSink interface {
  13. DeleteEntry(entry *filer_pb.Entry, deleteIncludeChunks bool) error
  14. CreateEntry(entry *filer_pb.Entry) error
  15. UpdateEntry(oldEntry, newEntry *filer_pb.Entry, deleteIncludeChunks bool) error
  16. }
  17. type FilerSink struct {
  18. grpcAddress string
  19. id string
  20. dir string
  21. }
  22. func (fs *FilerSink) Initialize(configuration util.Configuration) error {
  23. return fs.initialize(
  24. configuration.GetString("grpcAddress"),
  25. configuration.GetString("id"),
  26. configuration.GetString("directory"),
  27. )
  28. }
  29. func (fs *FilerSink) initialize(grpcAddress string, id string, dir string) (err error) {
  30. fs.grpcAddress = grpcAddress
  31. fs.id = id
  32. fs.dir = dir
  33. return nil
  34. }
  35. func (fs *FilerSink) DeleteEntry(entry *filer_pb.Entry, deleteIncludeChunks bool) error {
  36. return fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  37. dir, name := filer2.FullPath(entry.Name).DirAndName()
  38. request := &filer_pb.DeleteEntryRequest{
  39. Directory: dir,
  40. Name: name,
  41. IsDirectory: entry.IsDirectory,
  42. IsDeleteData: deleteIncludeChunks,
  43. }
  44. glog.V(1).Infof("delete entry: %v", request)
  45. _, err := client.DeleteEntry(context.Background(), request)
  46. if err != nil {
  47. glog.V(0).Infof("delete entry %s: %v", entry.Name, err)
  48. return fmt.Errorf("delete entry %s: %v", entry.Name, err)
  49. }
  50. return nil
  51. })
  52. }
  53. func (fs *FilerSink) CreateEntry(entry *filer_pb.Entry) error {
  54. replicatedChunks, err := replicateChunks(entry.Chunks)
  55. if err != nil {
  56. glog.V(0).Infof("replicate entry chunks %s: %v", entry.Name, err)
  57. return fmt.Errorf("replicate entry chunks %s: %v", entry.Name, err)
  58. }
  59. return fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  60. dir, name := filer2.FullPath(entry.Name).DirAndName()
  61. request := &filer_pb.CreateEntryRequest{
  62. Directory: dir,
  63. Entry: &filer_pb.Entry{
  64. Name: name,
  65. IsDirectory: entry.IsDirectory,
  66. Attributes: entry.Attributes,
  67. Chunks: replicatedChunks,
  68. },
  69. }
  70. glog.V(1).Infof("create: %v", request)
  71. if _, err := client.CreateEntry(context.Background(), request); err != nil {
  72. glog.V(0).Infof("create entry %s: %v", entry.Name, err)
  73. return fmt.Errorf("create entry %s: %v", entry.Name, err)
  74. }
  75. return nil
  76. })
  77. }
  78. func (fs *FilerSink) UpdateEntry(oldEntry, newEntry *filer_pb.Entry, deleteIncludeChunks bool) error {
  79. return nil
  80. }
  81. func (fs *FilerSink) withFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  82. grpcConnection, err := util.GrpcDial(fs.grpcAddress)
  83. if err != nil {
  84. return fmt.Errorf("fail to dial %s: %v", fs.grpcAddress, err)
  85. }
  86. defer grpcConnection.Close()
  87. client := filer_pb.NewSeaweedFilerClient(grpcConnection)
  88. return fn(client)
  89. }
  90. func volumeId(fileId string) string {
  91. lastCommaIndex := strings.LastIndex(fileId, ",")
  92. if lastCommaIndex > 0 {
  93. return fileId[:lastCommaIndex]
  94. }
  95. return fileId
  96. }
  97. func replicateChunks(sourceChunks []*filer_pb.FileChunk) (replicatedChunks []*filer_pb.FileChunk, err error) {
  98. if len(sourceChunks) == 0 {
  99. return
  100. }
  101. var wg sync.WaitGroup
  102. for _, s := range sourceChunks {
  103. wg.Add(1)
  104. go func(chunk *filer_pb.FileChunk) {
  105. defer wg.Done()
  106. replicatedChunk, e := replicateOneChunk(chunk)
  107. if e != nil {
  108. err = e
  109. }
  110. replicatedChunks = append(replicatedChunks, replicatedChunk)
  111. }(s)
  112. }
  113. wg.Wait()
  114. return
  115. }
  116. func replicateOneChunk(sourceChunk *filer_pb.FileChunk) (*filer_pb.FileChunk, error) {
  117. fileId, err := fetchAndWrite(sourceChunk)
  118. if err != nil {
  119. return nil, fmt.Errorf("copy %s: %v", sourceChunk.FileId, err)
  120. }
  121. return &filer_pb.FileChunk{
  122. FileId: fileId,
  123. Offset: sourceChunk.Offset,
  124. Size: sourceChunk.Size,
  125. Mtime: sourceChunk.Mtime,
  126. ETag: sourceChunk.ETag,
  127. SourceFileId: sourceChunk.FileId,
  128. }, nil
  129. }
  130. func fetchAndWrite(sourceChunk *filer_pb.FileChunk) (fileId string, err error) {
  131. return
  132. }