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.

232 lines
6.2 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package filersink
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/security"
  6. "github.com/spf13/viper"
  7. "google.golang.org/grpc"
  8. "github.com/chrislusf/seaweedfs/weed/filer2"
  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) error {
  35. return fs.initialize(
  36. configuration.GetString("grpcAddress"),
  37. configuration.GetString("directory"),
  38. configuration.GetString("replication"),
  39. configuration.GetString("collection"),
  40. configuration.GetInt("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(viper.Sub("grpc"), "client")
  54. return nil
  55. }
  56. func (fs *FilerSink) DeleteEntry(key string, isDirectory, deleteIncludeChunks bool) error {
  57. return fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  58. dir, name := filer2.FullPath(key).DirAndName()
  59. request := &filer_pb.DeleteEntryRequest{
  60. Directory: dir,
  61. Name: name,
  62. IsDeleteData: deleteIncludeChunks,
  63. }
  64. glog.V(1).Infof("delete entry: %v", request)
  65. _, err := client.DeleteEntry(context.Background(), request)
  66. if err != nil {
  67. glog.V(0).Infof("delete entry %s: %v", key, err)
  68. return fmt.Errorf("delete entry %s: %v", key, err)
  69. }
  70. return nil
  71. })
  72. }
  73. func (fs *FilerSink) CreateEntry(key string, entry *filer_pb.Entry) error {
  74. return fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  75. dir, name := filer2.FullPath(key).DirAndName()
  76. ctx := context.Background()
  77. // look up existing entry
  78. lookupRequest := &filer_pb.LookupDirectoryEntryRequest{
  79. Directory: dir,
  80. Name: name,
  81. }
  82. glog.V(1).Infof("lookup: %v", lookupRequest)
  83. if resp, err := client.LookupDirectoryEntry(ctx, lookupRequest); err == nil {
  84. if filer2.ETag(resp.Entry.Chunks) == filer2.ETag(entry.Chunks) {
  85. glog.V(0).Infof("already replicated %s", key)
  86. return nil
  87. }
  88. }
  89. replicatedChunks, err := fs.replicateChunks(entry.Chunks)
  90. if err != nil {
  91. glog.V(0).Infof("replicate entry chunks %s: %v", key, err)
  92. return fmt.Errorf("replicate entry chunks %s: %v", key, err)
  93. }
  94. glog.V(0).Infof("replicated %s %+v ===> %+v", key, entry.Chunks, replicatedChunks)
  95. request := &filer_pb.CreateEntryRequest{
  96. Directory: dir,
  97. Entry: &filer_pb.Entry{
  98. Name: name,
  99. IsDirectory: entry.IsDirectory,
  100. Attributes: entry.Attributes,
  101. Chunks: replicatedChunks,
  102. },
  103. }
  104. glog.V(1).Infof("create: %v", request)
  105. if _, err := client.CreateEntry(ctx, request); err != nil {
  106. glog.V(0).Infof("create entry %s: %v", key, err)
  107. return fmt.Errorf("create entry %s: %v", key, err)
  108. }
  109. return nil
  110. })
  111. }
  112. func (fs *FilerSink) UpdateEntry(key string, oldEntry, newEntry *filer_pb.Entry, deleteIncludeChunks bool) (foundExistingEntry bool, err error) {
  113. ctx := context.Background()
  114. dir, name := filer2.FullPath(key).DirAndName()
  115. // read existing entry
  116. var existingEntry *filer_pb.Entry
  117. err = fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  118. request := &filer_pb.LookupDirectoryEntryRequest{
  119. Directory: dir,
  120. Name: name,
  121. }
  122. glog.V(4).Infof("lookup entry: %v", request)
  123. resp, err := client.LookupDirectoryEntry(ctx, request)
  124. if err != nil {
  125. glog.V(0).Infof("lookup %s: %v", key, err)
  126. return err
  127. }
  128. existingEntry = resp.Entry
  129. return nil
  130. })
  131. if err != nil {
  132. return false, fmt.Errorf("lookup %s: %v", key, err)
  133. }
  134. glog.V(0).Infof("oldEntry %+v, newEntry %+v, existingEntry: %+v", oldEntry, newEntry, existingEntry)
  135. if existingEntry.Attributes.Mtime > newEntry.Attributes.Mtime {
  136. // skip if already changed
  137. // this usually happens when the messages are not ordered
  138. glog.V(0).Infof("late updates %s", key)
  139. } else if filer2.ETag(newEntry.Chunks) == filer2.ETag(existingEntry.Chunks) {
  140. // skip if no change
  141. // this usually happens when retrying the replication
  142. glog.V(0).Infof("already replicated %s", key)
  143. } else {
  144. // find out what changed
  145. deletedChunks, newChunks := compareChunks(oldEntry, newEntry)
  146. // delete the chunks that are deleted from the source
  147. if deleteIncludeChunks {
  148. // remove the deleted chunks. Actual data deletion happens in filer UpdateEntry FindUnusedFileChunks
  149. existingEntry.Chunks = minusChunks(existingEntry.Chunks, deletedChunks)
  150. }
  151. // replicate the chunks that are new in the source
  152. replicatedChunks, err := fs.replicateChunks(newChunks)
  153. if err != nil {
  154. return true, fmt.Errorf("replicte %s chunks error: %v", key, err)
  155. }
  156. existingEntry.Chunks = append(existingEntry.Chunks, replicatedChunks...)
  157. }
  158. // save updated meta data
  159. return true, fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  160. request := &filer_pb.UpdateEntryRequest{
  161. Directory: dir,
  162. Entry: existingEntry,
  163. }
  164. if _, err := client.UpdateEntry(ctx, request); err != nil {
  165. return fmt.Errorf("update existingEntry %s: %v", key, err)
  166. }
  167. return nil
  168. })
  169. }
  170. func compareChunks(oldEntry, newEntry *filer_pb.Entry) (deletedChunks, newChunks []*filer_pb.FileChunk) {
  171. deletedChunks = minusChunks(oldEntry.Chunks, newEntry.Chunks)
  172. newChunks = minusChunks(newEntry.Chunks, oldEntry.Chunks)
  173. return
  174. }
  175. func minusChunks(as, bs []*filer_pb.FileChunk) (delta []*filer_pb.FileChunk) {
  176. for _, a := range as {
  177. found := false
  178. for _, b := range bs {
  179. if a.FileId == b.FileId {
  180. found = true
  181. break
  182. }
  183. }
  184. if !found {
  185. delta = append(delta, a)
  186. }
  187. }
  188. return
  189. }