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.

176 lines
5.2 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
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
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
3 years ago
3 years ago
3 years ago
3 years ago
  1. package page_writer
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/util"
  6. "sync"
  7. "sync/atomic"
  8. "time"
  9. )
  10. type LogicChunkIndex int
  11. type UploadPipeline struct {
  12. filepath util.FullPath
  13. ChunkSize int64
  14. writableChunks map[LogicChunkIndex]PageChunk
  15. writableChunksLock sync.Mutex
  16. sealedChunks map[LogicChunkIndex]*SealedChunk
  17. sealedChunksLock sync.Mutex
  18. uploaders *util.LimitedConcurrentExecutor
  19. uploaderCount int32
  20. uploaderCountCond *sync.Cond
  21. saveToStorageFn SaveToStorageFunc
  22. activeReadChunks map[LogicChunkIndex]int
  23. activeReadChunksLock sync.Mutex
  24. swapFile *SwapFile
  25. }
  26. type SealedChunk struct {
  27. chunk PageChunk
  28. referenceCounter int // track uploading or reading processes
  29. }
  30. func (sc *SealedChunk) FreeReference(messageOnFree string) {
  31. sc.referenceCounter--
  32. if sc.referenceCounter == 0 {
  33. glog.V(4).Infof("Free sealed chunk: %s", messageOnFree)
  34. sc.chunk.FreeResource()
  35. }
  36. }
  37. func NewUploadPipeline(filepath util.FullPath, writers *util.LimitedConcurrentExecutor, chunkSize int64, saveToStorageFn SaveToStorageFunc, swapFileDir string) *UploadPipeline {
  38. return &UploadPipeline{
  39. ChunkSize: chunkSize,
  40. writableChunks: make(map[LogicChunkIndex]PageChunk),
  41. sealedChunks: make(map[LogicChunkIndex]*SealedChunk),
  42. uploaders: writers,
  43. uploaderCountCond: sync.NewCond(&sync.Mutex{}),
  44. saveToStorageFn: saveToStorageFn,
  45. filepath: filepath,
  46. activeReadChunks: make(map[LogicChunkIndex]int),
  47. swapFile: NewSwapFile(swapFileDir, chunkSize),
  48. }
  49. }
  50. func (up *UploadPipeline) SaveDataAt(p []byte, off int64) (n int) {
  51. up.writableChunksLock.Lock()
  52. defer up.writableChunksLock.Unlock()
  53. logicChunkIndex := LogicChunkIndex(off / up.ChunkSize)
  54. memChunk, found := up.writableChunks[logicChunkIndex]
  55. if !found {
  56. if len(up.writableChunks) < 16 {
  57. memChunk = NewMemChunk(logicChunkIndex, up.ChunkSize)
  58. } else {
  59. memChunk = up.swapFile.NewTempFileChunk(logicChunkIndex)
  60. if memChunk == nil {
  61. memChunk = NewMemChunk(logicChunkIndex, up.ChunkSize)
  62. }
  63. }
  64. up.writableChunks[logicChunkIndex] = memChunk
  65. }
  66. n = memChunk.WriteDataAt(p, off)
  67. up.maybeMoveToSealed(memChunk, logicChunkIndex)
  68. return
  69. }
  70. func (up *UploadPipeline) MaybeReadDataAt(p []byte, off int64) (maxStop int64) {
  71. logicChunkIndex := LogicChunkIndex(off / up.ChunkSize)
  72. // read from sealed chunks first
  73. up.sealedChunksLock.Lock()
  74. sealedChunk, found := up.sealedChunks[logicChunkIndex]
  75. if found {
  76. sealedChunk.referenceCounter++
  77. }
  78. up.sealedChunksLock.Unlock()
  79. if found {
  80. maxStop = sealedChunk.chunk.ReadDataAt(p, off)
  81. glog.V(4).Infof("%s read sealed memchunk [%d,%d)", up.filepath, off, maxStop)
  82. sealedChunk.FreeReference(fmt.Sprintf("%s finish reading chunk %d", up.filepath, logicChunkIndex))
  83. }
  84. // read from writable chunks last
  85. up.writableChunksLock.Lock()
  86. defer up.writableChunksLock.Unlock()
  87. writableChunk, found := up.writableChunks[logicChunkIndex]
  88. if !found {
  89. return
  90. }
  91. writableMaxStop := writableChunk.ReadDataAt(p, off)
  92. glog.V(4).Infof("%s read writable memchunk [%d,%d)", up.filepath, off, writableMaxStop)
  93. maxStop = max(maxStop, writableMaxStop)
  94. return
  95. }
  96. func (up *UploadPipeline) FlushAll() {
  97. up.writableChunksLock.Lock()
  98. defer up.writableChunksLock.Unlock()
  99. for logicChunkIndex, memChunk := range up.writableChunks {
  100. up.moveToSealed(memChunk, logicChunkIndex)
  101. }
  102. up.waitForCurrentWritersToComplete()
  103. }
  104. func (up *UploadPipeline) maybeMoveToSealed(memChunk PageChunk, logicChunkIndex LogicChunkIndex) {
  105. if memChunk.IsComplete() {
  106. up.moveToSealed(memChunk, logicChunkIndex)
  107. }
  108. }
  109. func (up *UploadPipeline) moveToSealed(memChunk PageChunk, logicChunkIndex LogicChunkIndex) {
  110. atomic.AddInt32(&up.uploaderCount, 1)
  111. glog.V(4).Infof("%s uploaderCount %d ++> %d", up.filepath, up.uploaderCount-1, up.uploaderCount)
  112. up.sealedChunksLock.Lock()
  113. if oldMemChunk, found := up.sealedChunks[logicChunkIndex]; found {
  114. oldMemChunk.FreeReference(fmt.Sprintf("%s replace chunk %d", up.filepath, logicChunkIndex))
  115. }
  116. sealedChunk := &SealedChunk{
  117. chunk: memChunk,
  118. referenceCounter: 1, // default 1 is for uploading process
  119. }
  120. up.sealedChunks[logicChunkIndex] = sealedChunk
  121. delete(up.writableChunks, logicChunkIndex)
  122. up.sealedChunksLock.Unlock()
  123. up.uploaders.Execute(func() {
  124. // first add to the file chunks
  125. sealedChunk.chunk.SaveContent(up.saveToStorageFn)
  126. // notify waiting process
  127. atomic.AddInt32(&up.uploaderCount, -1)
  128. glog.V(4).Infof("%s uploaderCount %d --> %d", up.filepath, up.uploaderCount+1, up.uploaderCount)
  129. // Lock and Unlock are not required,
  130. // but it may signal multiple times during one wakeup,
  131. // and the waiting goroutine may miss some of them!
  132. up.uploaderCountCond.L.Lock()
  133. up.uploaderCountCond.Broadcast()
  134. up.uploaderCountCond.L.Unlock()
  135. // wait for readers
  136. for up.IsLocked(logicChunkIndex) {
  137. time.Sleep(59 * time.Millisecond)
  138. }
  139. // then remove from sealed chunks
  140. up.sealedChunksLock.Lock()
  141. defer up.sealedChunksLock.Unlock()
  142. delete(up.sealedChunks, logicChunkIndex)
  143. sealedChunk.FreeReference(fmt.Sprintf("%s finished uploading chunk %d", up.filepath, logicChunkIndex))
  144. })
  145. }
  146. func (up *UploadPipeline) Shutdown() {
  147. up.swapFile.FreeResource()
  148. }