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.

101 lines
2.7 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
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. fh *FileHandle
  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. var (
  22. _ = page_writer.DirtyPages(&MemoryChunkPages{})
  23. )
  24. func newMemoryChunkPages(fh *FileHandle, chunkSize int64) *MemoryChunkPages {
  25. dirtyPages := &MemoryChunkPages{
  26. fh: fh,
  27. }
  28. dirtyPages.uploadPipeline = page_writer.NewUploadPipeline(fh.f.fullpath(),
  29. fh.f.wfs.concurrentWriters, chunkSize, dirtyPages.saveChunkedFileIntevalToStorage)
  30. return dirtyPages
  31. }
  32. func (pages *MemoryChunkPages) AddPage(offset int64, data []byte) {
  33. pages.hasWrites = true
  34. glog.V(4).Infof("%v memory AddPage [%d, %d)", pages.fh.f.fullpath(), offset, offset+int64(len(data)))
  35. pages.uploadPipeline.SaveDataAt(data, offset)
  36. return
  37. }
  38. func (pages *MemoryChunkPages) FlushData() error {
  39. if !pages.hasWrites {
  40. return nil
  41. }
  42. pages.uploadPipeline.FlushAll()
  43. if pages.lastErr != nil {
  44. return fmt.Errorf("flush data: %v", pages.lastErr)
  45. }
  46. return nil
  47. }
  48. func (pages *MemoryChunkPages) ReadDirtyDataAt(data []byte, startOffset int64) (maxStop int64) {
  49. if !pages.hasWrites {
  50. return
  51. }
  52. return pages.uploadPipeline.MaybeReadDataAt(data, startOffset)
  53. }
  54. func (pages *MemoryChunkPages) GetStorageOptions() (collection, replication string) {
  55. return pages.collection, pages.replication
  56. }
  57. func (pages *MemoryChunkPages) saveChunkedFileIntevalToStorage(reader io.Reader, offset int64, size int64, cleanupFn func()) {
  58. mtime := time.Now().UnixNano()
  59. defer cleanupFn()
  60. chunk, collection, replication, err := pages.fh.f.wfs.saveDataAsChunk(pages.fh.f.fullpath())(reader, pages.fh.f.Name, offset)
  61. if err != nil {
  62. glog.V(0).Infof("%s saveToStorage [%d,%d): %v", pages.fh.f.fullpath(), offset, offset+size, err)
  63. pages.lastErr = err
  64. return
  65. }
  66. chunk.Mtime = mtime
  67. pages.collection, pages.replication = collection, replication
  68. pages.chunkAddLock.Lock()
  69. pages.fh.f.addChunks([]*filer_pb.FileChunk{chunk})
  70. pages.fh.entryViewCache = nil
  71. glog.V(3).Infof("%s saveToStorage %s [%d,%d)", pages.fh.f.fullpath(), chunk.FileId, offset, offset+size)
  72. pages.chunkAddLock.Unlock()
  73. }
  74. func (pages MemoryChunkPages) Destroy() {
  75. pages.uploadPipeline.Shutdown()
  76. }
  77. func (pages *MemoryChunkPages) LockForRead(startOffset, stopOffset int64) {
  78. pages.uploadPipeline.LockForRead(startOffset, stopOffset)
  79. }
  80. func (pages *MemoryChunkPages) UnlockForRead(startOffset, stopOffset int64) {
  81. pages.uploadPipeline.UnlockForRead(startOffset, stopOffset)
  82. }