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.

165 lines
4.3 KiB

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