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.

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