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.

106 lines
3.0 KiB

  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. writeWaitGroup sync.WaitGroup
  15. pageAddLock sync.Mutex
  16. chunkAddLock sync.Mutex
  17. lastErr error
  18. collection string
  19. replication string
  20. chunkedFile *page_writer.ChunkedFileWriter
  21. }
  22. func newTempFileDirtyPages(file *File, chunkSize int64) *TempFileDirtyPages {
  23. tempFile := &TempFileDirtyPages{
  24. f: file,
  25. chunkedFile: page_writer.NewChunkedFileWriter(file.wfs.option.getTempFilePageDir(), chunkSize),
  26. }
  27. return tempFile
  28. }
  29. func (pages *TempFileDirtyPages) AddPage(offset int64, data []byte) {
  30. pages.pageAddLock.Lock()
  31. defer pages.pageAddLock.Unlock()
  32. glog.V(4).Infof("%v tempfile AddPage [%d, %d)", pages.f.fullpath(), offset, offset+int64(len(data)))
  33. if _, err := pages.chunkedFile.WriteAt(data, offset); err != nil {
  34. pages.lastErr = err
  35. }
  36. return
  37. }
  38. func (pages *TempFileDirtyPages) FlushData() error {
  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 *TempFileDirtyPages) ReadDirtyDataAt(data []byte, startOffset int64) (maxStop int64) {
  47. return pages.chunkedFile.ReadDataAt(data, startOffset)
  48. }
  49. func (pages *TempFileDirtyPages) GetStorageOptions() (collection, replication string) {
  50. return pages.collection, pages.replication
  51. }
  52. func (pages *TempFileDirtyPages) saveChunkedFileToStorage() {
  53. pages.chunkedFile.ProcessEachInterval(func(file *os.File, logicChunkIndex page_writer.LogicChunkIndex, interval *page_writer.ChunkWrittenInterval) {
  54. reader := page_writer.NewFileIntervalReader(pages.chunkedFile, logicChunkIndex, interval)
  55. pages.saveChunkedFileIntevalToStorage(reader, int64(logicChunkIndex)*pages.chunkedFile.ChunkSize+interval.StartOffset, interval.Size())
  56. interval.MarkFlushed()
  57. })
  58. }
  59. func (pages *TempFileDirtyPages) saveChunkedFileIntevalToStorage(reader io.Reader, offset int64, size int64) {
  60. mtime := time.Now().UnixNano()
  61. pages.writeWaitGroup.Add(1)
  62. writer := func() {
  63. defer pages.writeWaitGroup.Done()
  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. defer pages.chunkAddLock.Unlock()
  74. pages.f.addChunks([]*filer_pb.FileChunk{chunk})
  75. glog.V(3).Infof("%s saveToStorage %s [%d,%d)", pages.f.fullpath(), chunk.FileId, offset, offset+size)
  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 TempFileDirtyPages) Destroy() {
  84. pages.chunkedFile.Destroy()
  85. }