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.

228 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. IsDirectory: 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) (foundExistingEntry bool, err error) {
  109. ctx := context.Background()
  110. dir, name := filer2.FullPath(key).DirAndName()
  111. // read existing entry
  112. var existingEntry *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 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. existingEntry = resp.Entry
  125. return nil
  126. })
  127. if err != nil {
  128. return false, fmt.Errorf("lookup %s: %v", key, err)
  129. }
  130. glog.V(0).Infof("oldEntry %+v, newEntry %+v, existingEntry: %+v", oldEntry, newEntry, existingEntry)
  131. if existingEntry.Attributes.Mtime > newEntry.Attributes.Mtime {
  132. // skip if already changed
  133. // this usually happens when the messages are not ordered
  134. glog.V(0).Infof("late updates %s", key)
  135. } else if filer2.ETag(newEntry.Chunks) == filer2.ETag(existingEntry.Chunks) {
  136. // skip if no change
  137. // this usually happens when retrying the replication
  138. glog.V(0).Infof("already replicated %s", key)
  139. } else {
  140. // find out what changed
  141. deletedChunks, newChunks := compareChunks(oldEntry, newEntry)
  142. // delete the chunks that are deleted from the source
  143. if deleteIncludeChunks {
  144. // remove the deleted chunks. Actual data deletion happens in filer UpdateEntry FindUnusedFileChunks
  145. existingEntry.Chunks = minusChunks(existingEntry.Chunks, deletedChunks)
  146. }
  147. // replicate the chunks that are new in the source
  148. replicatedChunks, err := fs.replicateChunks(newChunks)
  149. if err != nil {
  150. return true, fmt.Errorf("replicte %s chunks error: %v", key, err)
  151. }
  152. existingEntry.Chunks = append(existingEntry.Chunks, replicatedChunks...)
  153. }
  154. // save updated meta data
  155. return true, fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  156. request := &filer_pb.UpdateEntryRequest{
  157. Directory: dir,
  158. Entry: existingEntry,
  159. }
  160. if _, err := client.UpdateEntry(ctx, request); err != nil {
  161. return fmt.Errorf("update existingEntry %s: %v", key, err)
  162. }
  163. return nil
  164. })
  165. }
  166. func compareChunks(oldEntry, newEntry *filer_pb.Entry) (deletedChunks, newChunks []*filer_pb.FileChunk) {
  167. deletedChunks = minusChunks(oldEntry.Chunks, newEntry.Chunks)
  168. newChunks = minusChunks(newEntry.Chunks, oldEntry.Chunks)
  169. return
  170. }
  171. func minusChunks(as, bs []*filer_pb.FileChunk) (delta []*filer_pb.FileChunk) {
  172. for _, a := range as {
  173. found := false
  174. for _, b := range bs {
  175. if a.FileId == b.FileId {
  176. found = true
  177. break
  178. }
  179. }
  180. if !found {
  181. delta = append(delta, a)
  182. }
  183. }
  184. return
  185. }