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.

255 lines
7.8 KiB

7 years ago
7 years ago
4 years ago
4 years ago
4 years ago
6 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
  1. package filersink
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/pb"
  6. "github.com/seaweedfs/seaweedfs/weed/wdclient"
  7. "math"
  8. "google.golang.org/grpc"
  9. "github.com/seaweedfs/seaweedfs/weed/security"
  10. "github.com/seaweedfs/seaweedfs/weed/filer"
  11. "github.com/seaweedfs/seaweedfs/weed/glog"
  12. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  13. "github.com/seaweedfs/seaweedfs/weed/replication/sink"
  14. "github.com/seaweedfs/seaweedfs/weed/replication/source"
  15. "github.com/seaweedfs/seaweedfs/weed/util"
  16. )
  17. type FilerSink struct {
  18. filerSource *source.FilerSource
  19. grpcAddress string
  20. dir string
  21. replication string
  22. collection string
  23. ttlSec int32
  24. diskType string
  25. dataCenter string
  26. grpcDialOption grpc.DialOption
  27. address string
  28. writeChunkByFiler bool
  29. isIncremental bool
  30. executor *util.LimitedConcurrentExecutor
  31. }
  32. func init() {
  33. sink.Sinks = append(sink.Sinks, &FilerSink{})
  34. }
  35. func (fs *FilerSink) GetName() string {
  36. return "filer"
  37. }
  38. func (fs *FilerSink) GetSinkToDirectory() string {
  39. return fs.dir
  40. }
  41. func (fs *FilerSink) IsIncremental() bool {
  42. return fs.isIncremental
  43. }
  44. func (fs *FilerSink) Initialize(configuration util.Configuration, prefix string) error {
  45. fs.isIncremental = configuration.GetBool(prefix + "is_incremental")
  46. fs.dataCenter = configuration.GetString(prefix + "dataCenter")
  47. filerAddress := pb.ServerAddress(configuration.GetString(prefix + "address"))
  48. return fs.DoInitialize(
  49. filerAddress.ToHttpAddress(),
  50. filerAddress.ToGrpcAddress(),
  51. configuration.GetString(prefix+"directory"),
  52. configuration.GetString(prefix+"replication"),
  53. configuration.GetString(prefix+"collection"),
  54. configuration.GetInt(prefix+"ttlSec"),
  55. configuration.GetString(prefix+"disk"),
  56. security.LoadClientTLS(util.GetViper(), "grpc.client"),
  57. false)
  58. }
  59. func (fs *FilerSink) SetSourceFiler(s *source.FilerSource) {
  60. fs.filerSource = s
  61. }
  62. func (fs *FilerSink) DoInitialize(address, grpcAddress string, dir string,
  63. replication string, collection string, ttlSec int, diskType string, grpcDialOption grpc.DialOption, writeChunkByFiler bool) (err error) {
  64. fs.address = address
  65. if fs.address == "" {
  66. fs.address = pb.GrpcAddressToServerAddress(grpcAddress)
  67. }
  68. fs.grpcAddress = grpcAddress
  69. fs.dir = dir
  70. fs.replication = replication
  71. fs.collection = collection
  72. fs.ttlSec = int32(ttlSec)
  73. fs.diskType = diskType
  74. fs.grpcDialOption = grpcDialOption
  75. fs.writeChunkByFiler = writeChunkByFiler
  76. fs.executor = util.NewLimitedConcurrentExecutor(32)
  77. return nil
  78. }
  79. func (fs *FilerSink) DeleteEntry(key string, isDirectory, deleteIncludeChunks bool, signatures []int32) error {
  80. dir, name := util.FullPath(key).DirAndName()
  81. glog.V(4).Infof("delete entry: %v", key)
  82. err := filer_pb.Remove(fs, dir, name, deleteIncludeChunks, true, true, true, signatures)
  83. if err != nil {
  84. glog.V(0).Infof("delete entry %s: %v", key, err)
  85. return fmt.Errorf("delete entry %s: %v", key, err)
  86. }
  87. return nil
  88. }
  89. func (fs *FilerSink) CreateEntry(key string, entry *filer_pb.Entry, signatures []int32) error {
  90. return fs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  91. dir, name := util.FullPath(key).DirAndName()
  92. // look up existing entry
  93. lookupRequest := &filer_pb.LookupDirectoryEntryRequest{
  94. Directory: dir,
  95. Name: name,
  96. }
  97. glog.V(1).Infof("lookup: %v", lookupRequest)
  98. if resp, err := filer_pb.LookupEntry(client, lookupRequest); err == nil {
  99. if filer.ETag(resp.Entry) == filer.ETag(entry) {
  100. glog.V(3).Infof("already replicated %s", key)
  101. return nil
  102. }
  103. }
  104. replicatedChunks, err := fs.replicateChunks(entry.GetChunks(), key)
  105. if err != nil {
  106. // only warning here since the source chunk may have been deleted already
  107. glog.Warningf("replicate entry chunks %s: %v", key, err)
  108. }
  109. glog.V(4).Infof("replicated %s %+v ===> %+v", key, entry.GetChunks(), replicatedChunks)
  110. request := &filer_pb.CreateEntryRequest{
  111. Directory: dir,
  112. Entry: &filer_pb.Entry{
  113. Name: name,
  114. IsDirectory: entry.IsDirectory,
  115. Attributes: entry.Attributes,
  116. Extended: entry.Extended,
  117. Chunks: replicatedChunks,
  118. Content: entry.Content,
  119. RemoteEntry: entry.RemoteEntry,
  120. },
  121. IsFromOtherCluster: true,
  122. Signatures: signatures,
  123. }
  124. glog.V(3).Infof("create: %v", request)
  125. if err := filer_pb.CreateEntry(client, request); err != nil {
  126. glog.V(0).Infof("create entry %s: %v", key, err)
  127. return fmt.Errorf("create entry %s: %v", key, err)
  128. }
  129. return nil
  130. })
  131. }
  132. func (fs *FilerSink) UpdateEntry(key string, oldEntry *filer_pb.Entry, newParentPath string, newEntry *filer_pb.Entry, deleteIncludeChunks bool, signatures []int32) (foundExistingEntry bool, err error) {
  133. dir, name := util.FullPath(key).DirAndName()
  134. // read existing entry
  135. var existingEntry *filer_pb.Entry
  136. err = fs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  137. request := &filer_pb.LookupDirectoryEntryRequest{
  138. Directory: dir,
  139. Name: name,
  140. }
  141. glog.V(4).Infof("lookup entry: %v", request)
  142. resp, err := filer_pb.LookupEntry(client, request)
  143. if err != nil {
  144. glog.V(0).Infof("lookup %s: %v", key, err)
  145. return err
  146. }
  147. existingEntry = resp.Entry
  148. return nil
  149. })
  150. if err != nil {
  151. return false, fmt.Errorf("lookup %s: %v", key, err)
  152. }
  153. glog.V(4).Infof("oldEntry %+v, newEntry %+v, existingEntry: %+v", oldEntry, newEntry, existingEntry)
  154. if existingEntry.Attributes.Mtime > newEntry.Attributes.Mtime {
  155. // skip if already changed
  156. // this usually happens when the messages are not ordered
  157. glog.V(2).Infof("late updates %s", key)
  158. } else {
  159. // find out what changed
  160. deletedChunks, newChunks, err := compareChunks(filer.LookupFn(fs), oldEntry, newEntry)
  161. if err != nil {
  162. return true, fmt.Errorf("replicate %s compare chunks error: %v", key, err)
  163. }
  164. // delete the chunks that are deleted from the source
  165. if deleteIncludeChunks {
  166. // remove the deleted chunks. Actual data deletion happens in filer UpdateEntry FindUnusedFileChunks
  167. existingEntry.Chunks = filer.DoMinusChunksBySourceFileId(existingEntry.GetChunks(), deletedChunks)
  168. }
  169. // replicate the chunks that are new in the source
  170. replicatedChunks, err := fs.replicateChunks(newChunks, key)
  171. if err != nil {
  172. return true, fmt.Errorf("replicate %s chunks error: %v", key, err)
  173. }
  174. existingEntry.Chunks = append(existingEntry.GetChunks(), replicatedChunks...)
  175. existingEntry.Attributes = newEntry.Attributes
  176. existingEntry.Extended = newEntry.Extended
  177. existingEntry.HardLinkId = newEntry.HardLinkId
  178. existingEntry.HardLinkCounter = newEntry.HardLinkCounter
  179. existingEntry.Content = newEntry.Content
  180. existingEntry.RemoteEntry = newEntry.RemoteEntry
  181. }
  182. // save updated meta data
  183. return true, fs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  184. request := &filer_pb.UpdateEntryRequest{
  185. Directory: newParentPath,
  186. Entry: existingEntry,
  187. IsFromOtherCluster: true,
  188. Signatures: signatures,
  189. }
  190. if _, err := client.UpdateEntry(context.Background(), request); err != nil {
  191. return fmt.Errorf("update existingEntry %s: %v", key, err)
  192. }
  193. return nil
  194. })
  195. }
  196. func compareChunks(lookupFileIdFn wdclient.LookupFileIdFunctionType, oldEntry, newEntry *filer_pb.Entry) (deletedChunks, newChunks []*filer_pb.FileChunk, err error) {
  197. aData, aMeta, aErr := filer.ResolveChunkManifest(lookupFileIdFn, oldEntry.GetChunks(), 0, math.MaxInt64)
  198. if aErr != nil {
  199. return nil, nil, aErr
  200. }
  201. bData, bMeta, bErr := filer.ResolveChunkManifest(lookupFileIdFn, newEntry.GetChunks(), 0, math.MaxInt64)
  202. if bErr != nil {
  203. return nil, nil, bErr
  204. }
  205. deletedChunks = append(deletedChunks, filer.DoMinusChunks(aData, bData)...)
  206. deletedChunks = append(deletedChunks, filer.DoMinusChunks(aMeta, bMeta)...)
  207. newChunks = append(newChunks, filer.DoMinusChunks(bData, aData)...)
  208. newChunks = append(newChunks, filer.DoMinusChunks(bMeta, aMeta)...)
  209. return
  210. }