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.

107 lines
2.7 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package filesys
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/filesys/page_writer"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "io"
  8. "sync"
  9. "time"
  10. )
  11. type MemoryChunkPages struct {
  12. f *File
  13. writeWaitGroup sync.WaitGroup
  14. chunkAddLock sync.Mutex
  15. lastErr error
  16. collection string
  17. replication string
  18. uploadPipeline *page_writer.UploadPipeline
  19. hasWrites bool
  20. }
  21. func newMemoryChunkPages(file *File, chunkSize int64) *MemoryChunkPages {
  22. dirtyPages := &MemoryChunkPages{
  23. f: file,
  24. }
  25. dirtyPages.uploadPipeline = page_writer.NewUploadPipeline(
  26. file.wfs.concurrentWriters, chunkSize, dirtyPages.saveChunkedFileIntevalToStorage)
  27. return dirtyPages
  28. }
  29. func (pages *MemoryChunkPages) AddPage(offset int64, data []byte) {
  30. pages.hasWrites = true
  31. glog.V(4).Infof("%v memory AddPage [%d, %d)", pages.f.fullpath(), offset, offset+int64(len(data)))
  32. pages.uploadPipeline.SaveDataAt(data, offset)
  33. return
  34. }
  35. func (pages *MemoryChunkPages) FlushData() error {
  36. if !pages.hasWrites {
  37. return nil
  38. }
  39. pages.saveChunkedFileToStorage()
  40. pages.writeWaitGroup.Wait()
  41. if pages.lastErr != nil {
  42. return fmt.Errorf("flush data: %v", pages.lastErr)
  43. }
  44. return nil
  45. }
  46. func (pages *MemoryChunkPages) ReadDirtyDataAt(data []byte, startOffset int64) (maxStop int64) {
  47. if !pages.hasWrites {
  48. return
  49. }
  50. return pages.uploadPipeline.MaybeReadDataAt(data, startOffset)
  51. }
  52. func (pages *MemoryChunkPages) GetStorageOptions() (collection, replication string) {
  53. return pages.collection, pages.replication
  54. }
  55. func (pages *MemoryChunkPages) saveChunkedFileToStorage() {
  56. pages.uploadPipeline.FlushAll()
  57. }
  58. func (pages *MemoryChunkPages) saveChunkedFileIntevalToStorage(reader io.Reader, offset int64, size int64, cleanupFn func()) {
  59. mtime := time.Now().UnixNano()
  60. pages.writeWaitGroup.Add(1)
  61. writer := func() {
  62. defer pages.writeWaitGroup.Done()
  63. defer cleanupFn()
  64. chunk, collection, replication, err := pages.f.wfs.saveDataAsChunk(pages.f.fullpath())(reader, pages.f.Name, offset)
  65. if err != nil {
  66. glog.V(0).Infof("%s saveToStorage [%d,%d): %v", pages.f.fullpath(), offset, offset+size, err)
  67. pages.lastErr = err
  68. return
  69. }
  70. chunk.Mtime = mtime
  71. pages.collection, pages.replication = collection, replication
  72. pages.chunkAddLock.Lock()
  73. pages.f.addChunks([]*filer_pb.FileChunk{chunk})
  74. glog.V(3).Infof("%s saveToStorage %s [%d,%d)", pages.f.fullpath(), chunk.FileId, offset, offset+size)
  75. pages.chunkAddLock.Unlock()
  76. }
  77. if pages.f.wfs.concurrentWriters != nil {
  78. pages.f.wfs.concurrentWriters.Execute(writer)
  79. } else {
  80. go writer()
  81. }
  82. }
  83. func (pages MemoryChunkPages) Destroy() {
  84. pages.uploadPipeline.Shutdown()
  85. }