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.

224 lines
5.8 KiB

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/source"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. "github.com/chrislusf/seaweedfs/weed/replication/sink"
  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 filer2.ETag(newEntry.Chunks) == filer2.ETag(existingEntry.Chunks) {
  132. // skip if no change
  133. // this usually happens when retrying the replication
  134. glog.V(0).Infof("already replicated %s", key)
  135. } else {
  136. // find out what changed
  137. deletedChunks, newChunks := compareChunks(oldEntry, newEntry)
  138. // delete the chunks that are deleted from the source
  139. if deleteIncludeChunks {
  140. // remove the deleted chunks. Actual data deletion happens in filer UpdateEntry FindUnusedFileChunks
  141. existingEntry.Chunks = minusChunks(existingEntry.Chunks, deletedChunks)
  142. }
  143. // replicate the chunks that are new in the source
  144. replicatedChunks, err := fs.replicateChunks(newChunks)
  145. if err != nil {
  146. return true, fmt.Errorf("replicte %s chunks error: %v", key, err)
  147. }
  148. existingEntry.Chunks = append(existingEntry.Chunks, replicatedChunks...)
  149. }
  150. // save updated meta data
  151. return true, fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  152. request := &filer_pb.UpdateEntryRequest{
  153. Directory: dir,
  154. Entry: existingEntry,
  155. }
  156. if _, err := client.UpdateEntry(ctx, request); err != nil {
  157. return fmt.Errorf("update existingEntry %s: %v", key, err)
  158. }
  159. return nil
  160. })
  161. }
  162. func compareChunks(oldEntry, newEntry *filer_pb.Entry) (deletedChunks, newChunks []*filer_pb.FileChunk) {
  163. deletedChunks = minusChunks(oldEntry.Chunks, newEntry.Chunks)
  164. newChunks = minusChunks(newEntry.Chunks, oldEntry.Chunks)
  165. return
  166. }
  167. func minusChunks(as, bs []*filer_pb.FileChunk) (delta []*filer_pb.FileChunk) {
  168. for _, a := range as {
  169. found := false
  170. for _, b := range bs {
  171. if a.FileId == b.FileId {
  172. found = true
  173. break
  174. }
  175. }
  176. if !found {
  177. delta = append(delta, a)
  178. }
  179. }
  180. return
  181. }