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.

146 lines
4.0 KiB

4 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. "os"
  9. "sync"
  10. "time"
  11. )
  12. type TempFileDirtyPages struct {
  13. f *File
  14. tf *os.File
  15. writtenIntervals *page_writer.WrittenContinuousIntervals
  16. writeWaitGroup sync.WaitGroup
  17. pageAddLock sync.Mutex
  18. chunkAddLock sync.Mutex
  19. lastErr error
  20. collection string
  21. replication string
  22. }
  23. func newTempFileDirtyPages(file *File) *TempFileDirtyPages {
  24. tempFile := &TempFileDirtyPages{
  25. f: file,
  26. writtenIntervals: &page_writer.WrittenContinuousIntervals{},
  27. }
  28. return tempFile
  29. }
  30. func (pages *TempFileDirtyPages) AddPage(offset int64, data []byte) {
  31. pages.pageAddLock.Lock()
  32. defer pages.pageAddLock.Unlock()
  33. if pages.tf == nil {
  34. tf, err := os.CreateTemp(pages.f.wfs.option.getTempFilePageDir(), "")
  35. if err != nil {
  36. glog.Errorf("create temp file: %v", err)
  37. pages.lastErr = err
  38. return
  39. }
  40. pages.tf = tf
  41. pages.writtenIntervals.TempFile = tf
  42. pages.writtenIntervals.LastOffset = 0
  43. }
  44. writtenOffset := pages.writtenIntervals.LastOffset
  45. dataSize := int64(len(data))
  46. // glog.V(4).Infof("%s AddPage %v at %d [%d,%d)", pages.f.fullpath(), pages.tf.Name(), writtenOffset, offset, offset+dataSize)
  47. if _, err := pages.tf.WriteAt(data, writtenOffset); err != nil {
  48. pages.lastErr = err
  49. } else {
  50. pages.writtenIntervals.AddInterval(writtenOffset, len(data), offset)
  51. pages.writtenIntervals.LastOffset += dataSize
  52. }
  53. // pages.writtenIntervals.debug()
  54. return
  55. }
  56. func (pages *TempFileDirtyPages) FlushData() error {
  57. pages.saveExistingPagesToStorage()
  58. pages.writeWaitGroup.Wait()
  59. if pages.lastErr != nil {
  60. return fmt.Errorf("flush data: %v", pages.lastErr)
  61. }
  62. pages.pageAddLock.Lock()
  63. defer pages.pageAddLock.Unlock()
  64. if pages.tf != nil {
  65. pages.writtenIntervals.TempFile = nil
  66. pages.writtenIntervals.Lists = nil
  67. pages.tf.Close()
  68. os.Remove(pages.tf.Name())
  69. pages.tf = nil
  70. }
  71. return nil
  72. }
  73. func (pages *TempFileDirtyPages) saveExistingPagesToStorage() {
  74. pageSize := pages.f.wfs.option.ChunkSizeLimit
  75. // glog.V(4).Infof("%v saveExistingPagesToStorage %d lists", pages.f.Name, len(pages.writtenIntervals.lists))
  76. for _, list := range pages.writtenIntervals.Lists {
  77. listStopOffset := list.Offset() + list.Size()
  78. for uploadedOffset := int64(0); uploadedOffset < listStopOffset; uploadedOffset += pageSize {
  79. start, stop := max(list.Offset(), uploadedOffset), min(listStopOffset, uploadedOffset+pageSize)
  80. if start >= stop {
  81. continue
  82. }
  83. // glog.V(4).Infof("uploading %v [%d,%d) %d/%d", pages.f.Name, start, stop, i, len(pages.writtenIntervals.lists))
  84. pages.saveToStorage(list.ToReader(start, stop), start, stop-start)
  85. }
  86. }
  87. }
  88. func (pages *TempFileDirtyPages) saveToStorage(reader io.Reader, offset int64, size int64) {
  89. mtime := time.Now().UnixNano()
  90. pages.writeWaitGroup.Add(1)
  91. writer := func() {
  92. defer pages.writeWaitGroup.Done()
  93. reader = io.LimitReader(reader, size)
  94. chunk, collection, replication, err := pages.f.wfs.saveDataAsChunk(pages.f.fullpath())(reader, pages.f.Name, offset)
  95. if err != nil {
  96. glog.V(0).Infof("%s saveToStorage [%d,%d): %v", pages.f.fullpath(), offset, offset+size, err)
  97. pages.lastErr = err
  98. return
  99. }
  100. chunk.Mtime = mtime
  101. pages.collection, pages.replication = collection, replication
  102. pages.chunkAddLock.Lock()
  103. defer pages.chunkAddLock.Unlock()
  104. pages.f.addChunks([]*filer_pb.FileChunk{chunk})
  105. glog.V(3).Infof("%s saveToStorage %s [%d,%d)", pages.f.fullpath(), chunk.FileId, offset, offset+size)
  106. }
  107. if pages.f.wfs.concurrentWriters != nil {
  108. pages.f.wfs.concurrentWriters.Execute(writer)
  109. } else {
  110. go writer()
  111. }
  112. }
  113. func (pages *TempFileDirtyPages) ReadDirtyDataAt(data []byte, startOffset int64) (maxStop int64) {
  114. return pages.writtenIntervals.ReadDataAt(data, startOffset)
  115. }
  116. func (pages *TempFileDirtyPages) GetStorageOptions() (collection, replication string) {
  117. return pages.collection, pages.replication
  118. }