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.

203 lines
6.3 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
  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, tsNs int64) (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. oldestChunkIndex, oldestTs := LogicChunkIndex(-1), int64(0)
  60. for lci, mc := range up.writableChunks {
  61. chunkModifiedTsNs := mc.LastModifiedTsNs()
  62. if oldestTs < chunkModifiedTsNs {
  63. oldestChunkIndex = lci
  64. oldestTs = chunkModifiedTsNs
  65. }
  66. }
  67. up.moveToSealed(up.writableChunks[oldestChunkIndex], oldestChunkIndex)
  68. // fmt.Printf("flush chunk %d with %d bytes written\n", logicChunkIndex, oldestTs)
  69. }
  70. if false && 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. if _, foundSealed := up.sealedChunks[logicChunkIndex]; foundSealed {
  80. println("found already sealed chunk", logicChunkIndex)
  81. }
  82. if _, foundReading := up.activeReadChunks[logicChunkIndex]; foundReading {
  83. println("found active read chunk", logicChunkIndex)
  84. }
  85. n = pageChunk.WriteDataAt(p, off, tsNs)
  86. up.maybeMoveToSealed(pageChunk, logicChunkIndex)
  87. return
  88. }
  89. func (up *UploadPipeline) MaybeReadDataAt(p []byte, off int64, tsNs int64) (maxStop int64) {
  90. logicChunkIndex := LogicChunkIndex(off / up.ChunkSize)
  91. up.chunksLock.Lock()
  92. defer func() {
  93. up.readerCountCond.Signal()
  94. up.chunksLock.Unlock()
  95. }()
  96. // read from sealed chunks first
  97. sealedChunk, found := up.sealedChunks[logicChunkIndex]
  98. if found {
  99. sealedChunk.referenceCounter++
  100. }
  101. if found {
  102. maxStop = sealedChunk.chunk.ReadDataAt(p, off, tsNs)
  103. glog.V(4).Infof("%s read sealed memchunk [%d,%d)", up.filepath, off, maxStop)
  104. sealedChunk.FreeReference(fmt.Sprintf("%s finish reading chunk %d", up.filepath, logicChunkIndex))
  105. }
  106. // read from writable chunks last
  107. writableChunk, found := up.writableChunks[logicChunkIndex]
  108. if !found {
  109. return
  110. }
  111. writableMaxStop := writableChunk.ReadDataAt(p, off, tsNs)
  112. glog.V(4).Infof("%s read writable memchunk [%d,%d)", up.filepath, off, writableMaxStop)
  113. maxStop = max(maxStop, writableMaxStop)
  114. return
  115. }
  116. func (up *UploadPipeline) FlushAll() {
  117. up.chunksLock.Lock()
  118. defer up.chunksLock.Unlock()
  119. for logicChunkIndex, memChunk := range up.writableChunks {
  120. up.moveToSealed(memChunk, logicChunkIndex)
  121. }
  122. up.waitForCurrentWritersToComplete()
  123. }
  124. func (up *UploadPipeline) maybeMoveToSealed(memChunk PageChunk, logicChunkIndex LogicChunkIndex) {
  125. if memChunk.IsComplete() {
  126. up.moveToSealed(memChunk, logicChunkIndex)
  127. }
  128. }
  129. func (up *UploadPipeline) moveToSealed(memChunk PageChunk, logicChunkIndex LogicChunkIndex) {
  130. atomic.AddInt32(&up.uploaderCount, 1)
  131. glog.V(4).Infof("%s uploaderCount %d ++> %d", up.filepath, up.uploaderCount-1, up.uploaderCount)
  132. if oldMemChunk, found := up.sealedChunks[logicChunkIndex]; found {
  133. oldMemChunk.FreeReference(fmt.Sprintf("%s replace chunk %d", up.filepath, logicChunkIndex))
  134. }
  135. sealedChunk := &SealedChunk{
  136. chunk: memChunk,
  137. referenceCounter: 1, // default 1 is for uploading process
  138. }
  139. up.sealedChunks[logicChunkIndex] = sealedChunk
  140. delete(up.writableChunks, logicChunkIndex)
  141. // unlock before submitting the uploading jobs
  142. up.chunksLock.Unlock()
  143. up.uploaders.Execute(func() {
  144. // first add to the file chunks
  145. sealedChunk.chunk.SaveContent(up.saveToStorageFn)
  146. // notify waiting process
  147. atomic.AddInt32(&up.uploaderCount, -1)
  148. glog.V(4).Infof("%s uploaderCount %d --> %d", up.filepath, up.uploaderCount+1, up.uploaderCount)
  149. // Lock and Unlock are not required,
  150. // but it may signal multiple times during one wakeup,
  151. // and the waiting goroutine may miss some of them!
  152. up.uploaderCountCond.L.Lock()
  153. up.uploaderCountCond.Broadcast()
  154. up.uploaderCountCond.L.Unlock()
  155. // wait for readers
  156. up.chunksLock.Lock()
  157. defer up.chunksLock.Unlock()
  158. for up.IsLocked(logicChunkIndex) {
  159. up.readerCountCond.Wait()
  160. }
  161. // then remove from sealed chunks
  162. delete(up.sealedChunks, logicChunkIndex)
  163. sealedChunk.FreeReference(fmt.Sprintf("%s finished uploading chunk %d", up.filepath, logicChunkIndex))
  164. })
  165. up.chunksLock.Lock()
  166. }
  167. func (up *UploadPipeline) Shutdown() {
  168. up.swapFile.FreeResource()
  169. up.chunksLock.Lock()
  170. defer up.chunksLock.Unlock()
  171. for logicChunkIndex, sealedChunk := range up.sealedChunks {
  172. sealedChunk.FreeReference(fmt.Sprintf("%s uploadpipeline shutdown chunk %d", up.filepath, logicChunkIndex))
  173. }
  174. }