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.

192 lines
6.1 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
  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. bufferChunkLimit int
  25. swapFile *SwapFile
  26. }
  27. type SealedChunk struct {
  28. chunk PageChunk
  29. referenceCounter int // track uploading or reading processes
  30. }
  31. func (sc *SealedChunk) FreeReference(messageOnFree string) {
  32. sc.referenceCounter--
  33. if sc.referenceCounter == 0 {
  34. glog.V(4).Infof("Free sealed chunk: %s", messageOnFree)
  35. sc.chunk.FreeResource()
  36. }
  37. }
  38. func NewUploadPipeline(writers *util.LimitedConcurrentExecutor, chunkSize int64, saveToStorageFn SaveToStorageFunc, bufferChunkLimit int, swapFileDir string) *UploadPipeline {
  39. return &UploadPipeline{
  40. ChunkSize: chunkSize,
  41. writableChunks: make(map[LogicChunkIndex]PageChunk),
  42. sealedChunks: make(map[LogicChunkIndex]*SealedChunk),
  43. uploaders: writers,
  44. uploaderCountCond: sync.NewCond(&sync.Mutex{}),
  45. saveToStorageFn: saveToStorageFn,
  46. activeReadChunks: make(map[LogicChunkIndex]int),
  47. bufferChunkLimit: bufferChunkLimit,
  48. swapFile: NewSwapFile(swapFileDir, chunkSize),
  49. }
  50. }
  51. func (up *UploadPipeline) SaveDataAt(p []byte, off int64) (n int) {
  52. up.writableChunksLock.Lock()
  53. defer up.writableChunksLock.Unlock()
  54. logicChunkIndex := LogicChunkIndex(off / up.ChunkSize)
  55. pageChunk, found := up.writableChunks[logicChunkIndex]
  56. if !found {
  57. if atomic.LoadInt64(&memChunkCounter) > 4*int64(up.bufferChunkLimit) {
  58. // if total number of chunks is over 4 times of per file buffer count limit
  59. pageChunk = up.swapFile.NewTempFileChunk(logicChunkIndex)
  60. } else if len(up.writableChunks) < up.bufferChunkLimit {
  61. // if current file chunks is still under the per file buffer count limit
  62. pageChunk = NewMemChunk(logicChunkIndex, up.ChunkSize)
  63. } else {
  64. fullestChunkIndex, fullness := LogicChunkIndex(-1), int64(0)
  65. for lci, mc := range up.writableChunks {
  66. chunkFullness := mc.WrittenSize()
  67. if fullness < chunkFullness {
  68. fullestChunkIndex = lci
  69. fullness = chunkFullness
  70. }
  71. }
  72. up.moveToSealed(up.writableChunks[fullestChunkIndex], fullestChunkIndex)
  73. delete(up.writableChunks, fullestChunkIndex)
  74. // fmt.Printf("flush chunk %d with %d bytes written\n", logicChunkIndex, fullness)
  75. pageChunk = NewMemChunk(logicChunkIndex, up.ChunkSize)
  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. // read from sealed chunks first
  86. up.sealedChunksLock.Lock()
  87. sealedChunk, found := up.sealedChunks[logicChunkIndex]
  88. if found {
  89. sealedChunk.referenceCounter++
  90. }
  91. up.sealedChunksLock.Unlock()
  92. if found {
  93. maxStop = sealedChunk.chunk.ReadDataAt(p, off)
  94. glog.V(4).Infof("%s read sealed memchunk [%d,%d)", up.filepath, off, maxStop)
  95. sealedChunk.FreeReference(fmt.Sprintf("%s finish reading chunk %d", up.filepath, logicChunkIndex))
  96. }
  97. // read from writable chunks last
  98. up.writableChunksLock.Lock()
  99. defer up.writableChunksLock.Unlock()
  100. writableChunk, found := up.writableChunks[logicChunkIndex]
  101. if !found {
  102. return
  103. }
  104. writableMaxStop := writableChunk.ReadDataAt(p, off)
  105. glog.V(4).Infof("%s read writable memchunk [%d,%d)", up.filepath, off, writableMaxStop)
  106. maxStop = max(maxStop, writableMaxStop)
  107. return
  108. }
  109. func (up *UploadPipeline) FlushAll() {
  110. up.writableChunksLock.Lock()
  111. defer up.writableChunksLock.Unlock()
  112. for logicChunkIndex, memChunk := range up.writableChunks {
  113. up.moveToSealed(memChunk, logicChunkIndex)
  114. }
  115. up.waitForCurrentWritersToComplete()
  116. }
  117. func (up *UploadPipeline) maybeMoveToSealed(memChunk PageChunk, logicChunkIndex LogicChunkIndex) {
  118. if memChunk.IsComplete() {
  119. up.moveToSealed(memChunk, logicChunkIndex)
  120. }
  121. }
  122. func (up *UploadPipeline) moveToSealed(memChunk PageChunk, logicChunkIndex LogicChunkIndex) {
  123. atomic.AddInt32(&up.uploaderCount, 1)
  124. glog.V(4).Infof("%s uploaderCount %d ++> %d", up.filepath, up.uploaderCount-1, up.uploaderCount)
  125. up.sealedChunksLock.Lock()
  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. up.sealedChunksLock.Unlock()
  136. up.uploaders.Execute(func() {
  137. // first add to the file chunks
  138. sealedChunk.chunk.SaveContent(up.saveToStorageFn)
  139. // notify waiting process
  140. atomic.AddInt32(&up.uploaderCount, -1)
  141. glog.V(4).Infof("%s uploaderCount %d --> %d", up.filepath, up.uploaderCount+1, up.uploaderCount)
  142. // Lock and Unlock are not required,
  143. // but it may signal multiple times during one wakeup,
  144. // and the waiting goroutine may miss some of them!
  145. up.uploaderCountCond.L.Lock()
  146. up.uploaderCountCond.Broadcast()
  147. up.uploaderCountCond.L.Unlock()
  148. // wait for readers
  149. for up.IsLocked(logicChunkIndex) {
  150. time.Sleep(59 * time.Millisecond)
  151. }
  152. // then remove from sealed chunks
  153. up.sealedChunksLock.Lock()
  154. defer up.sealedChunksLock.Unlock()
  155. delete(up.sealedChunks, logicChunkIndex)
  156. sealedChunk.FreeReference(fmt.Sprintf("%s finished uploading chunk %d", up.filepath, logicChunkIndex))
  157. })
  158. }
  159. func (up *UploadPipeline) Shutdown() {
  160. up.swapFile.FreeResource()
  161. for logicChunkIndex, sealedChunk := range up.sealedChunks {
  162. sealedChunk.FreeReference(fmt.Sprintf("%s uploadpipeline shutdown chunk %d", up.filepath, logicChunkIndex))
  163. }
  164. }