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.

227 lines
6.0 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package filersink
  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/sink"
  9. "github.com/chrislusf/seaweedfs/weed/replication/source"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. )
  12. type FilerSink struct {
  13. filerSource *source.FilerSource
  14. grpcAddress string
  15. dir string
  16. replication string
  17. collection string
  18. ttlSec int32
  19. dataCenter string
  20. }
  21. func init() {
  22. sink.Sinks = append(sink.Sinks, &FilerSink{})
  23. }
  24. func (fs *FilerSink) GetName() string {
  25. return "filer"
  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, isDirectory, 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. IsDeleteData: deleteIncludeChunks,
  58. }
  59. glog.V(1).Infof("delete entry: %v", request)
  60. _, err := client.DeleteEntry(context.Background(), request)
  61. if err != nil {
  62. glog.V(0).Infof("delete entry %s: %v", key, err)
  63. return fmt.Errorf("delete entry %s: %v", key, err)
  64. }
  65. return nil
  66. })
  67. }
  68. func (fs *FilerSink) CreateEntry(key string, entry *filer_pb.Entry) error {
  69. return fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  70. dir, name := filer2.FullPath(key).DirAndName()
  71. ctx := context.Background()
  72. // look up existing entry
  73. lookupRequest := &filer_pb.LookupDirectoryEntryRequest{
  74. Directory: dir,
  75. Name: name,
  76. }
  77. glog.V(1).Infof("lookup: %v", lookupRequest)
  78. if resp, err := client.LookupDirectoryEntry(ctx, lookupRequest); err == nil {
  79. if filer2.ETag(resp.Entry.Chunks) == filer2.ETag(entry.Chunks) {
  80. glog.V(0).Infof("already replicated %s", key)
  81. return nil
  82. }
  83. }
  84. replicatedChunks, err := fs.replicateChunks(entry.Chunks)
  85. if err != nil {
  86. glog.V(0).Infof("replicate entry chunks %s: %v", key, err)
  87. return fmt.Errorf("replicate entry chunks %s: %v", key, err)
  88. }
  89. glog.V(0).Infof("replicated %s %+v ===> %+v", key, entry.Chunks, replicatedChunks)
  90. request := &filer_pb.CreateEntryRequest{
  91. Directory: dir,
  92. Entry: &filer_pb.Entry{
  93. Name: name,
  94. IsDirectory: entry.IsDirectory,
  95. Attributes: entry.Attributes,
  96. Chunks: replicatedChunks,
  97. },
  98. }
  99. glog.V(1).Infof("create: %v", request)
  100. if _, err := client.CreateEntry(ctx, request); err != nil {
  101. glog.V(0).Infof("create entry %s: %v", key, err)
  102. return fmt.Errorf("create entry %s: %v", key, err)
  103. }
  104. return nil
  105. })
  106. }
  107. func (fs *FilerSink) UpdateEntry(key string, oldEntry, newEntry *filer_pb.Entry, deleteIncludeChunks bool) (foundExistingEntry bool, err error) {
  108. ctx := context.Background()
  109. dir, name := filer2.FullPath(key).DirAndName()
  110. // read existing entry
  111. var existingEntry *filer_pb.Entry
  112. err = fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  113. request := &filer_pb.LookupDirectoryEntryRequest{
  114. Directory: dir,
  115. Name: name,
  116. }
  117. glog.V(4).Infof("lookup entry: %v", request)
  118. resp, err := client.LookupDirectoryEntry(ctx, request)
  119. if err != nil {
  120. glog.V(0).Infof("lookup %s: %v", key, err)
  121. return err
  122. }
  123. existingEntry = resp.Entry
  124. return nil
  125. })
  126. if err != nil {
  127. return false, fmt.Errorf("lookup %s: %v", key, err)
  128. }
  129. glog.V(0).Infof("oldEntry %+v, newEntry %+v, existingEntry: %+v", oldEntry, newEntry, existingEntry)
  130. if existingEntry.Attributes.Mtime > newEntry.Attributes.Mtime {
  131. // skip if already changed
  132. // this usually happens when the messages are not ordered
  133. glog.V(0).Infof("late updates %s", key)
  134. } else if filer2.ETag(newEntry.Chunks) == filer2.ETag(existingEntry.Chunks) {
  135. // skip if no change
  136. // this usually happens when retrying the replication
  137. glog.V(0).Infof("already replicated %s", key)
  138. } else {
  139. // find out what changed
  140. deletedChunks, newChunks := compareChunks(oldEntry, newEntry)
  141. // delete the chunks that are deleted from the source
  142. if deleteIncludeChunks {
  143. // remove the deleted chunks. Actual data deletion happens in filer UpdateEntry FindUnusedFileChunks
  144. existingEntry.Chunks = minusChunks(existingEntry.Chunks, deletedChunks)
  145. }
  146. // replicate the chunks that are new in the source
  147. replicatedChunks, err := fs.replicateChunks(newChunks)
  148. if err != nil {
  149. return true, fmt.Errorf("replicte %s chunks error: %v", key, err)
  150. }
  151. existingEntry.Chunks = append(existingEntry.Chunks, replicatedChunks...)
  152. }
  153. // save updated meta data
  154. return true, fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  155. request := &filer_pb.UpdateEntryRequest{
  156. Directory: dir,
  157. Entry: existingEntry,
  158. }
  159. if _, err := client.UpdateEntry(ctx, request); err != nil {
  160. return fmt.Errorf("update existingEntry %s: %v", key, err)
  161. }
  162. return nil
  163. })
  164. }
  165. func compareChunks(oldEntry, newEntry *filer_pb.Entry) (deletedChunks, newChunks []*filer_pb.FileChunk) {
  166. deletedChunks = minusChunks(oldEntry.Chunks, newEntry.Chunks)
  167. newChunks = minusChunks(newEntry.Chunks, oldEntry.Chunks)
  168. return
  169. }
  170. func minusChunks(as, bs []*filer_pb.FileChunk) (delta []*filer_pb.FileChunk) {
  171. for _, a := range as {
  172. found := false
  173. for _, b := range bs {
  174. if a.FileId == b.FileId {
  175. found = true
  176. break
  177. }
  178. }
  179. if !found {
  180. delta = append(delta, a)
  181. }
  182. }
  183. return
  184. }