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.

198 lines
5.1 KiB

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