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.

187 lines
5.5 KiB

3 years ago
  1. package page_writer
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/glog"
  4. "github.com/chrislusf/seaweedfs/weed/util"
  5. "github.com/chrislusf/seaweedfs/weed/util/mem"
  6. "sync"
  7. "sync/atomic"
  8. )
  9. type UploadPipeline struct {
  10. writableChunks map[LogicChunkIndex]*MemChunk
  11. writableChunksLock sync.Mutex
  12. sealedChunks map[LogicChunkIndex]*SealedChunk
  13. sealedChunksLock sync.Mutex
  14. ChunkSize int64
  15. writers *util.LimitedConcurrentExecutor
  16. activeWriterCond *sync.Cond
  17. activeWriterCount int32
  18. saveToStorageFn SaveToStorageFunc
  19. }
  20. type SealedChunk struct {
  21. chunk *MemChunk
  22. referenceCounter int // track uploading or reading processes
  23. }
  24. func (sc *SealedChunk) FreeReference() {
  25. sc.referenceCounter--
  26. if sc.referenceCounter == 0 {
  27. mem.Free(sc.chunk.buf)
  28. }
  29. }
  30. func NewUploadPipeline(writers *util.LimitedConcurrentExecutor, chunkSize int64, saveToStorageFn SaveToStorageFunc) *UploadPipeline {
  31. return &UploadPipeline{
  32. ChunkSize: chunkSize,
  33. writableChunks: make(map[LogicChunkIndex]*MemChunk),
  34. sealedChunks: make(map[LogicChunkIndex]*SealedChunk),
  35. writers: writers,
  36. activeWriterCond: sync.NewCond(&sync.Mutex{}),
  37. saveToStorageFn: saveToStorageFn,
  38. }
  39. }
  40. func (cw *UploadPipeline) SaveDataAt(p []byte, off int64) (n int) {
  41. cw.writableChunksLock.Lock()
  42. defer cw.writableChunksLock.Unlock()
  43. logicChunkIndex := LogicChunkIndex(off / cw.ChunkSize)
  44. offsetRemainder := off % cw.ChunkSize
  45. memChunk, found := cw.writableChunks[logicChunkIndex]
  46. if !found {
  47. memChunk = &MemChunk{
  48. buf: mem.Allocate(int(cw.ChunkSize)),
  49. usage: newChunkWrittenIntervalList(),
  50. }
  51. cw.writableChunks[logicChunkIndex] = memChunk
  52. }
  53. n = copy(memChunk.buf[offsetRemainder:], p)
  54. memChunk.usage.MarkWritten(offsetRemainder, offsetRemainder+int64(n))
  55. cw.maybeMoveToSealed(memChunk, logicChunkIndex)
  56. return
  57. }
  58. func (cw *UploadPipeline) MaybeReadDataAt(p []byte, off int64) (maxStop int64) {
  59. logicChunkIndex := LogicChunkIndex(off / cw.ChunkSize)
  60. // read from sealed chunks first
  61. cw.sealedChunksLock.Lock()
  62. sealedChunk, found := cw.sealedChunks[logicChunkIndex]
  63. if found {
  64. sealedChunk.referenceCounter++
  65. }
  66. cw.sealedChunksLock.Unlock()
  67. if found {
  68. maxStop = readMemChunk(sealedChunk.chunk, p, off, logicChunkIndex, cw.ChunkSize)
  69. sealedChunk.FreeReference()
  70. }
  71. // read from writable chunks last
  72. cw.writableChunksLock.Lock()
  73. defer cw.writableChunksLock.Unlock()
  74. writableChunk, found := cw.writableChunks[logicChunkIndex]
  75. if !found {
  76. return
  77. }
  78. maxStop = max(maxStop, readMemChunk(writableChunk, p, off, logicChunkIndex, cw.ChunkSize))
  79. return
  80. }
  81. func (cw *UploadPipeline) FlushAll() {
  82. cw.writableChunksLock.Lock()
  83. defer cw.writableChunksLock.Unlock()
  84. for logicChunkIndex, memChunk := range cw.writableChunks {
  85. cw.moveToSealed(memChunk, logicChunkIndex)
  86. }
  87. cw.waitForCurrentWritersToComplete()
  88. }
  89. func (cw *UploadPipeline) waitForCurrentWritersToComplete() {
  90. cw.activeWriterCond.L.Lock()
  91. t := int32(100)
  92. for {
  93. t = atomic.LoadInt32(&cw.activeWriterCount)
  94. if t <= 0 {
  95. break
  96. }
  97. glog.V(4).Infof("activeWriterCond is %d", t)
  98. cw.activeWriterCond.Wait()
  99. }
  100. cw.activeWriterCond.L.Unlock()
  101. }
  102. func (cw *UploadPipeline) maybeMoveToSealed(memChunk *MemChunk, logicChunkIndex LogicChunkIndex) {
  103. if memChunk.usage.IsComplete(cw.ChunkSize) {
  104. cw.moveToSealed(memChunk, logicChunkIndex)
  105. }
  106. }
  107. func (cw *UploadPipeline) moveToSealed(memChunk *MemChunk, logicChunkIndex LogicChunkIndex) {
  108. atomic.AddInt32(&cw.activeWriterCount, 1)
  109. glog.V(4).Infof("activeWriterCount %d ++> %d", cw.activeWriterCount-1, cw.activeWriterCount)
  110. cw.sealedChunksLock.Lock()
  111. if oldMemChunk, found := cw.sealedChunks[logicChunkIndex]; found {
  112. oldMemChunk.FreeReference()
  113. }
  114. sealedChunk := &SealedChunk{
  115. chunk: memChunk,
  116. referenceCounter: 1, // default 1 is for uploading process
  117. }
  118. cw.sealedChunks[logicChunkIndex] = sealedChunk
  119. delete(cw.writableChunks, logicChunkIndex)
  120. cw.sealedChunksLock.Unlock()
  121. cw.writers.Execute(func() {
  122. cw.saveOneChunk(sealedChunk.chunk, logicChunkIndex)
  123. // remove from sealed chunks
  124. sealedChunk.FreeReference()
  125. cw.sealedChunksLock.Lock()
  126. defer cw.sealedChunksLock.Unlock()
  127. delete(cw.sealedChunks, logicChunkIndex)
  128. atomic.AddInt32(&cw.activeWriterCount, -1)
  129. glog.V(4).Infof("activeWriterCount %d --> %d", cw.activeWriterCount+1, cw.activeWriterCount)
  130. // Lock and Unlock are not required,
  131. // but it may signal multiple times during one wakeup,
  132. // and the waiting goroutine may miss some of them!
  133. cw.activeWriterCond.L.Lock()
  134. cw.activeWriterCond.Broadcast()
  135. cw.activeWriterCond.L.Unlock()
  136. })
  137. }
  138. func (cw *UploadPipeline) saveOneChunk(memChunk *MemChunk, logicChunkIndex LogicChunkIndex) {
  139. for t := memChunk.usage.head.next; t != memChunk.usage.tail; t = t.next {
  140. reader := util.NewBytesReader(memChunk.buf[t.StartOffset:t.stopOffset])
  141. cw.saveToStorageFn(reader, int64(logicChunkIndex)*cw.ChunkSize+t.StartOffset, t.Size(), func() {
  142. })
  143. }
  144. }
  145. func readMemChunk(memChunk *MemChunk, p []byte, off int64, logicChunkIndex LogicChunkIndex, chunkSize int64) (maxStop int64) {
  146. memChunkBaseOffset := int64(logicChunkIndex) * chunkSize
  147. for t := memChunk.usage.head.next; t != memChunk.usage.tail; t = t.next {
  148. logicStart := max(off, int64(logicChunkIndex)*chunkSize+t.StartOffset)
  149. logicStop := min(off+int64(len(p)), memChunkBaseOffset+t.stopOffset)
  150. if logicStart < logicStop {
  151. copy(p[logicStart-off:logicStop-off], memChunk.buf[logicStart-memChunkBaseOffset:logicStop-memChunkBaseOffset])
  152. maxStop = max(maxStop, logicStop)
  153. }
  154. }
  155. return
  156. }
  157. func (p2 *UploadPipeline) Shutdown() {
  158. }