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.

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