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.

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