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.

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