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.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. "github.com/chrislusf/seaweedfs/weed/wdclient"
  6. "google.golang.org/grpc"
  7. "github.com/chrislusf/seaweedfs/weed/security"
  8. "github.com/chrislusf/seaweedfs/weed/filer"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. "github.com/chrislusf/seaweedfs/weed/replication/sink"
  12. "github.com/chrislusf/seaweedfs/weed/replication/source"
  13. "github.com/chrislusf/seaweedfs/weed/util"
  14. )
  15. type FilerSink struct {
  16. filerSource *source.FilerSource
  17. grpcAddress string
  18. dir string
  19. replication string
  20. collection string
  21. ttlSec int32
  22. dataCenter string
  23. grpcDialOption grpc.DialOption
  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.DoInitialize(
  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. security.LoadClientTLS(util.GetViper(), "grpc.client"))
  42. }
  43. func (fs *FilerSink) SetSourceFiler(s *source.FilerSource) {
  44. fs.filerSource = s
  45. }
  46. func (fs *FilerSink) DoInitialize(grpcAddress string, dir string,
  47. replication string, collection string, ttlSec int, grpcDialOption grpc.DialOption) (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 = grpcDialOption
  54. return nil
  55. }
  56. func (fs *FilerSink) DeleteEntry(key string, isDirectory, deleteIncludeChunks bool, signatures []int32) error {
  57. dir, name := util.FullPath(key).DirAndName()
  58. glog.V(4).Infof("delete entry: %v", key)
  59. err := filer_pb.Remove(fs, dir, name, deleteIncludeChunks, true, true, true, signatures)
  60. if err != nil {
  61. glog.V(0).Infof("delete entry %s: %v", key, err)
  62. return fmt.Errorf("delete entry %s: %v", key, err)
  63. }
  64. return nil
  65. }
  66. func (fs *FilerSink) CreateEntry(key string, entry *filer_pb.Entry, signatures []int32) error {
  67. return fs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  68. dir, name := util.FullPath(key).DirAndName()
  69. // look up existing entry
  70. lookupRequest := &filer_pb.LookupDirectoryEntryRequest{
  71. Directory: dir,
  72. Name: name,
  73. }
  74. glog.V(1).Infof("lookup: %v", lookupRequest)
  75. if resp, err := filer_pb.LookupEntry(client, lookupRequest); err == nil {
  76. if filer.ETag(resp.Entry) == filer.ETag(entry) {
  77. glog.V(3).Infof("already replicated %s", key)
  78. return nil
  79. }
  80. }
  81. replicatedChunks, err := fs.replicateChunks(entry.Chunks, key)
  82. if err != nil {
  83. // only warning here since the source chunk may have been deleted already
  84. glog.Warningf("replicate entry chunks %s: %v", key, err)
  85. }
  86. glog.V(4).Infof("replicated %s %+v ===> %+v", key, entry.Chunks, replicatedChunks)
  87. request := &filer_pb.CreateEntryRequest{
  88. Directory: dir,
  89. Entry: &filer_pb.Entry{
  90. Name: name,
  91. IsDirectory: entry.IsDirectory,
  92. Attributes: entry.Attributes,
  93. Chunks: replicatedChunks,
  94. Content: entry.Content,
  95. },
  96. IsFromOtherCluster: true,
  97. Signatures: signatures,
  98. }
  99. glog.V(3).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, signatures []int32) (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(4).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(2).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(3).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, key)
  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: signatures,
  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 wdclient.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. }