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.

221 lines
5.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. 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. return fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  71. dir, name := filer2.FullPath(key).DirAndName()
  72. ctx := context.Background()
  73. // look up existing entry
  74. lookupRequest := &filer_pb.LookupDirectoryEntryRequest{
  75. Directory: dir,
  76. Name: name,
  77. }
  78. glog.V(1).Infof("lookup: %v", lookupRequest)
  79. if resp, err := client.LookupDirectoryEntry(ctx, lookupRequest); err == nil {
  80. if filer2.ETag(resp.Entry.Chunks) == filer2.ETag(entry.Chunks) {
  81. glog.V(0).Infof("already replicated %s", key)
  82. return nil
  83. }
  84. }
  85. replicatedChunks, err := fs.replicateChunks(entry.Chunks)
  86. if err != nil {
  87. glog.V(0).Infof("replicate entry chunks %s: %v", key, err)
  88. return fmt.Errorf("replicate entry chunks %s: %v", key, err)
  89. }
  90. glog.V(0).Infof("replicated %s %+v ===> %+v", key, entry.Chunks, replicatedChunks)
  91. request := &filer_pb.CreateEntryRequest{
  92. Directory: dir,
  93. Entry: &filer_pb.Entry{
  94. Name: name,
  95. IsDirectory: entry.IsDirectory,
  96. Attributes: entry.Attributes,
  97. Chunks: replicatedChunks,
  98. },
  99. }
  100. glog.V(1).Infof("create: %v", request)
  101. if _, err := client.CreateEntry(ctx, request); err != nil {
  102. glog.V(0).Infof("create entry %s: %v", key, err)
  103. return fmt.Errorf("create entry %s: %v", key, err)
  104. }
  105. return nil
  106. })
  107. }
  108. func (fs *FilerSink) UpdateEntry(key string, oldEntry, newEntry *filer_pb.Entry, deleteIncludeChunks bool) (err error) {
  109. ctx := context.Background()
  110. dir, name := filer2.FullPath(key).DirAndName()
  111. // read existing entry
  112. var entry *filer_pb.Entry
  113. err = fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  114. request := &filer_pb.LookupDirectoryEntryRequest{
  115. Directory: dir,
  116. Name: name,
  117. }
  118. glog.V(4).Infof("lookup directory entry: %v", request)
  119. resp, err := client.LookupDirectoryEntry(ctx, request)
  120. if err != nil {
  121. glog.V(0).Infof("lookup %s: %v", key, err)
  122. return err
  123. }
  124. entry = resp.Entry
  125. return nil
  126. })
  127. if err != nil {
  128. return fmt.Errorf("lookup when updating %s: %v", key, err)
  129. }
  130. if filer2.ETag(newEntry.Chunks) == filer2.ETag(entry.Chunks) {
  131. // skip if no change
  132. // this usually happens when retrying the replication
  133. glog.V(0).Infof("already replicated %s", key)
  134. } else {
  135. // find out what changed
  136. deletedChunks, newChunks := compareChunks(oldEntry, newEntry)
  137. // delete the chunks that are deleted from the source
  138. if deleteIncludeChunks {
  139. // remove the deleted chunks. Actual data deletion happens in filer UpdateEntry FindUnusedFileChunks
  140. entry.Chunks = minusChunks(entry.Chunks, deletedChunks)
  141. }
  142. // replicate the chunks that are new in the source
  143. replicatedChunks, err := fs.replicateChunks(newChunks)
  144. if err != nil {
  145. return fmt.Errorf("replicte %s chunks error: %v", key, err)
  146. }
  147. entry.Chunks = append(entry.Chunks, replicatedChunks...)
  148. }
  149. // save updated meta data
  150. return fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  151. request := &filer_pb.UpdateEntryRequest{
  152. Directory: dir,
  153. Entry: entry,
  154. }
  155. if _, err := client.UpdateEntry(ctx, request); err != nil {
  156. return fmt.Errorf("update entry %s: %v", key, err)
  157. }
  158. return nil
  159. })
  160. }
  161. func compareChunks(oldEntry, newEntry *filer_pb.Entry) (deletedChunks, newChunks []*filer_pb.FileChunk) {
  162. deletedChunks = minusChunks(oldEntry.Chunks, newEntry.Chunks)
  163. newChunks = minusChunks(newEntry.Chunks, oldEntry.Chunks)
  164. return
  165. }
  166. func minusChunks(as, bs []*filer_pb.FileChunk) (delta []*filer_pb.FileChunk) {
  167. for _, a := range as {
  168. found := false
  169. for _, b := range bs {
  170. if a.FileId == b.FileId {
  171. found = true
  172. break
  173. }
  174. }
  175. if !found {
  176. delta = append(delta, a)
  177. }
  178. }
  179. return
  180. }