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.

191 lines
4.8 KiB

6 years ago
6 years ago
6 years ago
  1. package sink
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/filer2"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "github.com/chrislusf/seaweedfs/weed/replication/source"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. )
  11. type ReplicationSink interface {
  12. DeleteEntry(key string, entry *filer_pb.Entry, deleteIncludeChunks bool) error
  13. CreateEntry(key string, entry *filer_pb.Entry) error
  14. UpdateEntry(key string, oldEntry, newEntry *filer_pb.Entry, deleteIncludeChunks bool) error
  15. GetDirectory() string
  16. SetSourceFiler(s *source.FilerSource)
  17. }
  18. type FilerSink struct {
  19. grpcAddress string
  20. dir string
  21. filerSource *source.FilerSource
  22. replication string
  23. collection string
  24. ttlSec int32
  25. dataCenter string
  26. }
  27. func (fs *FilerSink) GetDirectory() string {
  28. return fs.dir
  29. }
  30. func (fs *FilerSink) Initialize(configuration util.Configuration) error {
  31. return fs.initialize(
  32. configuration.GetString("grpcAddress"),
  33. configuration.GetString("directory"),
  34. )
  35. }
  36. func (fs *FilerSink) SetSourceFiler(s *source.FilerSource) {
  37. fs.filerSource = s
  38. }
  39. func (fs *FilerSink) initialize(grpcAddress string, dir string) (err error) {
  40. fs.grpcAddress = grpcAddress
  41. fs.dir = dir
  42. return nil
  43. }
  44. func (fs *FilerSink) DeleteEntry(key string, entry *filer_pb.Entry, deleteIncludeChunks bool) error {
  45. return fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  46. dir, name := filer2.FullPath(key).DirAndName()
  47. request := &filer_pb.DeleteEntryRequest{
  48. Directory: dir,
  49. Name: name,
  50. IsDirectory: entry.IsDirectory,
  51. IsDeleteData: deleteIncludeChunks,
  52. }
  53. glog.V(1).Infof("delete entry: %v", request)
  54. _, err := client.DeleteEntry(context.Background(), request)
  55. if err != nil {
  56. glog.V(0).Infof("delete entry %s: %v", key, err)
  57. return fmt.Errorf("delete entry %s: %v", key, err)
  58. }
  59. return nil
  60. })
  61. }
  62. func (fs *FilerSink) CreateEntry(key string, entry *filer_pb.Entry) error {
  63. replicatedChunks, err := fs.replicateChunks(entry.Chunks)
  64. if err != nil {
  65. glog.V(0).Infof("replicate entry chunks %s: %v", key, err)
  66. return fmt.Errorf("replicate entry chunks %s: %v", key, err)
  67. }
  68. glog.V(0).Infof("replicated %s %+v ===> %+v", key, entry.Chunks, replicatedChunks)
  69. return fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  70. dir, name := filer2.FullPath(key).DirAndName()
  71. request := &filer_pb.CreateEntryRequest{
  72. Directory: dir,
  73. Entry: &filer_pb.Entry{
  74. Name: name,
  75. IsDirectory: entry.IsDirectory,
  76. Attributes: entry.Attributes,
  77. Chunks: replicatedChunks,
  78. },
  79. }
  80. glog.V(1).Infof("create: %v", request)
  81. if _, err := client.CreateEntry(context.Background(), request); err != nil {
  82. glog.V(0).Infof("create entry %s: %v", key, err)
  83. return fmt.Errorf("create entry %s: %v", key, err)
  84. }
  85. return nil
  86. })
  87. }
  88. func (fs *FilerSink) UpdateEntry(key string, oldEntry, newEntry *filer_pb.Entry, deleteIncludeChunks bool) (err error) {
  89. ctx := context.Background()
  90. dir, name := filer2.FullPath(key).DirAndName()
  91. // find out what changed
  92. deletedChunks, newChunks := compareChunks(oldEntry, newEntry)
  93. // read existing entry
  94. var entry *filer_pb.Entry
  95. err = fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  96. request := &filer_pb.LookupDirectoryEntryRequest{
  97. Directory: dir,
  98. Name: name,
  99. }
  100. glog.V(4).Infof("lookup directory entry: %v", request)
  101. resp, err := client.LookupDirectoryEntry(ctx, request)
  102. if err != nil {
  103. glog.V(0).Infof("lookup %s: %v", key, err)
  104. return err
  105. }
  106. entry = resp.Entry
  107. return nil
  108. })
  109. if err != nil {
  110. return err
  111. }
  112. // delete the chunks that are deleted from the source
  113. if deleteIncludeChunks {
  114. // remove the deleted chunks. Actual data deletion happens in filer UpdateEntry FindUnusedFileChunks
  115. entry.Chunks = minusChunks(entry.Chunks, deletedChunks)
  116. }
  117. // replicate the chunks that are new in the source
  118. replicatedChunks, err := fs.replicateChunks(newChunks)
  119. entry.Chunks = append(entry.Chunks, replicatedChunks...)
  120. // save updated meta data
  121. return fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  122. request := &filer_pb.UpdateEntryRequest{
  123. Directory: dir,
  124. Entry: entry,
  125. }
  126. if _, err := client.UpdateEntry(ctx, request); err != nil {
  127. return fmt.Errorf("update entry %s: %v", key, err)
  128. }
  129. return nil
  130. })
  131. }
  132. func compareChunks(oldEntry, newEntry *filer_pb.Entry) (deletedChunks, newChunks []*filer_pb.FileChunk) {
  133. deletedChunks = minusChunks(oldEntry.Chunks, newEntry.Chunks)
  134. newChunks = minusChunks(newEntry.Chunks, oldEntry.Chunks)
  135. return
  136. }
  137. func minusChunks(as, bs []*filer_pb.FileChunk) (delta []*filer_pb.FileChunk) {
  138. for _, a := range as {
  139. found := false
  140. for _, b := range bs {
  141. if a.FileId == b.FileId {
  142. found = true
  143. break
  144. }
  145. }
  146. if !found {
  147. delta = append(delta, a)
  148. }
  149. }
  150. return
  151. }