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.

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