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.

154 lines
3.8 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. }
  50. writtenOffset := pages.writtenIntervals.TotalSize()
  51. glog.V(4).Infof("%s AddPage %v at %d [%d,%d)", pages.f.fullpath(), pages.tf.Name(), writtenOffset, offset, offset+int64(len(data)))
  52. if _, err := pages.tf.WriteAt(data, writtenOffset); err != nil {
  53. pages.lastErr = err
  54. } else {
  55. pages.writtenIntervals.AddInterval(writtenOffset, len(data), offset)
  56. }
  57. return
  58. }
  59. func (pages *TempFileDirtyPages) FlushData() error {
  60. pages.saveExistingPagesToStorage()
  61. pages.writeWaitGroup.Wait()
  62. if pages.lastErr != nil {
  63. return fmt.Errorf("flush data: %v", pages.lastErr)
  64. }
  65. pages.pageAddLock.Lock()
  66. defer pages.pageAddLock.Unlock()
  67. if pages.tf != nil {
  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. uploadedSize := int64(0)
  76. for _, list := range pages.writtenIntervals.lists {
  77. for {
  78. start, stop := max(list.Offset(), uploadedSize), min(list.Offset()+list.Size(), uploadedSize+pageSize)
  79. if start >= stop {
  80. break
  81. }
  82. uploadedSize = stop
  83. glog.V(4).Infof("uploading %v [%d,%d)", pages.f.Name, start, stop)
  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(), pages.writeOnly)(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. }
  119. func (pages *TempFileDirtyPages) SetWriteOnly(writeOnly bool) {
  120. if pages.writeOnly {
  121. pages.writeOnly = writeOnly
  122. }
  123. }
  124. func (pages *TempFileDirtyPages) GetWriteOnly() (writeOnly bool) {
  125. return pages.writeOnly
  126. }