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.6 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package filersink
  2. import (
  3. "context"
  4. "fmt"
  5. "google.golang.org/grpc"
  6. "github.com/chrislusf/seaweedfs/weed/security"
  7. "github.com/chrislusf/seaweedfs/weed/filer"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/chrislusf/seaweedfs/weed/replication/sink"
  11. "github.com/chrislusf/seaweedfs/weed/replication/source"
  12. "github.com/chrislusf/seaweedfs/weed/util"
  13. )
  14. type FilerSink struct {
  15. filerSource *source.FilerSource
  16. grpcAddress string
  17. dir string
  18. replication string
  19. collection string
  20. ttlSec int32
  21. dataCenter string
  22. grpcDialOption grpc.DialOption
  23. signature int32
  24. }
  25. func init() {
  26. sink.Sinks = append(sink.Sinks, &FilerSink{})
  27. }
  28. func (fs *FilerSink) GetName() string {
  29. return "filer"
  30. }
  31. func (fs *FilerSink) GetSinkToDirectory() string {
  32. return fs.dir
  33. }
  34. func (fs *FilerSink) Initialize(configuration util.Configuration, prefix string) error {
  35. return fs.initialize(
  36. configuration.GetString(prefix+"grpcAddress"),
  37. configuration.GetString(prefix+"directory"),
  38. configuration.GetString(prefix+"replication"),
  39. configuration.GetString(prefix+"collection"),
  40. configuration.GetInt(prefix+"ttlSec"),
  41. )
  42. }
  43. func (fs *FilerSink) SetSourceFiler(s *source.FilerSource) {
  44. fs.filerSource = s
  45. }
  46. func (fs *FilerSink) initialize(grpcAddress string, dir string,
  47. replication string, collection string, ttlSec int) (err error) {
  48. fs.grpcAddress = grpcAddress
  49. fs.dir = dir
  50. fs.replication = replication
  51. fs.collection = collection
  52. fs.ttlSec = int32(ttlSec)
  53. fs.grpcDialOption = security.LoadClientTLS(util.GetViper(), "grpc.client")
  54. fs.signature = util.RandomInt32()
  55. return nil
  56. }
  57. func (fs *FilerSink) DeleteEntry(key string, isDirectory, deleteIncludeChunks bool) error {
  58. dir, name := util.FullPath(key).DirAndName()
  59. glog.V(1).Infof("delete entry: %v", key)
  60. err := filer_pb.Remove(fs, dir, name, deleteIncludeChunks, false, false, true, fs.signature)
  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. func (fs *FilerSink) CreateEntry(key string, entry *filer_pb.Entry) error {
  68. return fs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  69. dir, name := util.FullPath(key).DirAndName()
  70. // look up existing entry
  71. lookupRequest := &filer_pb.LookupDirectoryEntryRequest{
  72. Directory: dir,
  73. Name: name,
  74. }
  75. glog.V(1).Infof("lookup: %v", lookupRequest)
  76. if resp, err := filer_pb.LookupEntry(client, lookupRequest); err == nil {
  77. if filer.ETag(resp.Entry) == filer.ETag(entry) {
  78. glog.V(0).Infof("already replicated %s", key)
  79. return nil
  80. }
  81. }
  82. replicatedChunks, err := fs.replicateChunks(entry.Chunks, dir)
  83. if err != nil {
  84. glog.V(0).Infof("replicate entry chunks %s: %v", key, err)
  85. return fmt.Errorf("replicate entry chunks %s: %v", key, err)
  86. }
  87. glog.V(0).Infof("replicated %s %+v ===> %+v", key, entry.Chunks, replicatedChunks)
  88. request := &filer_pb.CreateEntryRequest{
  89. Directory: dir,
  90. Entry: &filer_pb.Entry{
  91. Name: name,
  92. IsDirectory: entry.IsDirectory,
  93. Attributes: entry.Attributes,
  94. Chunks: replicatedChunks,
  95. },
  96. IsFromOtherCluster: true,
  97. Signatures: []int32{fs.signature},
  98. }
  99. glog.V(1).Infof("create: %v", request)
  100. if err := filer_pb.CreateEntry(client, 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 *filer_pb.Entry, newParentPath string, newEntry *filer_pb.Entry, deleteIncludeChunks bool) (foundExistingEntry bool, err error) {
  108. dir, name := util.FullPath(key).DirAndName()
  109. // read existing entry
  110. var existingEntry *filer_pb.Entry
  111. err = fs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  112. request := &filer_pb.LookupDirectoryEntryRequest{
  113. Directory: dir,
  114. Name: name,
  115. }
  116. glog.V(4).Infof("lookup entry: %v", request)
  117. resp, err := filer_pb.LookupEntry(client, request)
  118. if err != nil {
  119. glog.V(0).Infof("lookup %s: %v", key, err)
  120. return err
  121. }
  122. existingEntry = resp.Entry
  123. return nil
  124. })
  125. if err != nil {
  126. return false, fmt.Errorf("lookup %s: %v", key, err)
  127. }
  128. glog.V(0).Infof("oldEntry %+v, newEntry %+v, existingEntry: %+v", oldEntry, newEntry, existingEntry)
  129. if existingEntry.Attributes.Mtime > newEntry.Attributes.Mtime {
  130. // skip if already changed
  131. // this usually happens when the messages are not ordered
  132. glog.V(0).Infof("late updates %s", key)
  133. } else if filer.ETag(newEntry) == filer.ETag(existingEntry) {
  134. // skip if no change
  135. // this usually happens when retrying the replication
  136. glog.V(0).Infof("already replicated %s", key)
  137. } else {
  138. // find out what changed
  139. deletedChunks, newChunks, err := compareChunks(filer.LookupFn(fs), oldEntry, newEntry)
  140. if err != nil {
  141. return true, fmt.Errorf("replicte %s compare chunks error: %v", key, err)
  142. }
  143. // delete the chunks that are deleted from the source
  144. if deleteIncludeChunks {
  145. // remove the deleted chunks. Actual data deletion happens in filer UpdateEntry FindUnusedFileChunks
  146. existingEntry.Chunks = filer.DoMinusChunks(existingEntry.Chunks, deletedChunks)
  147. }
  148. // replicate the chunks that are new in the source
  149. replicatedChunks, err := fs.replicateChunks(newChunks, newParentPath)
  150. if err != nil {
  151. return true, fmt.Errorf("replicte %s chunks error: %v", key, err)
  152. }
  153. existingEntry.Chunks = append(existingEntry.Chunks, replicatedChunks...)
  154. }
  155. // save updated meta data
  156. return true, fs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  157. request := &filer_pb.UpdateEntryRequest{
  158. Directory: newParentPath,
  159. Entry: existingEntry,
  160. IsFromOtherCluster: true,
  161. Signatures: []int32{fs.signature},
  162. }
  163. if _, err := client.UpdateEntry(context.Background(), request); err != nil {
  164. return fmt.Errorf("update existingEntry %s: %v", key, err)
  165. }
  166. return nil
  167. })
  168. }
  169. func compareChunks(lookupFileIdFn filer.LookupFileIdFunctionType, oldEntry, newEntry *filer_pb.Entry) (deletedChunks, newChunks []*filer_pb.FileChunk, err error) {
  170. aData, aMeta, aErr := filer.ResolveChunkManifest(lookupFileIdFn, oldEntry.Chunks)
  171. if aErr != nil {
  172. return nil, nil, aErr
  173. }
  174. bData, bMeta, bErr := filer.ResolveChunkManifest(lookupFileIdFn, newEntry.Chunks)
  175. if bErr != nil {
  176. return nil, nil, bErr
  177. }
  178. deletedChunks = append(deletedChunks, filer.DoMinusChunks(aData, bData)...)
  179. deletedChunks = append(deletedChunks, filer.DoMinusChunks(aMeta, bMeta)...)
  180. newChunks = append(newChunks, filer.DoMinusChunks(bData, aData)...)
  181. newChunks = append(newChunks, filer.DoMinusChunks(bMeta, aMeta)...)
  182. return
  183. }