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.

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