2022-01-17 09:53:56 +00:00
|
|
|
package page_writer
|
|
|
|
|
|
|
|
import (
|
2022-01-17 22:15:10 +00:00
|
|
|
"fmt"
|
2022-01-17 09:53:56 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2022-01-18 06:24:44 +00:00
|
|
|
"time"
|
2022-01-17 09:53:56 +00:00
|
|
|
)
|
|
|
|
|
2022-01-18 06:24:44 +00:00
|
|
|
type LogicChunkIndex int
|
|
|
|
|
2022-01-17 09:53:56 +00:00
|
|
|
type UploadPipeline struct {
|
2022-01-18 06:24:44 +00:00
|
|
|
filepath util.FullPath
|
|
|
|
ChunkSize int64
|
2022-01-22 12:18:54 +00:00
|
|
|
writableChunks map[LogicChunkIndex]PageChunk
|
2022-01-18 06:24:44 +00:00
|
|
|
writableChunksLock sync.Mutex
|
|
|
|
sealedChunks map[LogicChunkIndex]*SealedChunk
|
|
|
|
sealedChunksLock sync.Mutex
|
2022-01-22 09:46:10 +00:00
|
|
|
uploaders *util.LimitedConcurrentExecutor
|
|
|
|
uploaderCount int32
|
|
|
|
uploaderCountCond *sync.Cond
|
2022-01-22 09:43:14 +00:00
|
|
|
saveToStorageFn SaveToStorageFunc
|
2022-01-18 06:24:44 +00:00
|
|
|
activeReadChunks map[LogicChunkIndex]int
|
|
|
|
activeReadChunksLock sync.Mutex
|
2022-01-26 08:36:19 +00:00
|
|
|
bufferChunkLimit int
|
2022-01-17 09:53:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SealedChunk struct {
|
2022-01-22 12:18:54 +00:00
|
|
|
chunk PageChunk
|
2022-01-17 09:53:56 +00:00
|
|
|
referenceCounter int // track uploading or reading processes
|
|
|
|
}
|
|
|
|
|
2022-01-17 22:15:10 +00:00
|
|
|
func (sc *SealedChunk) FreeReference(messageOnFree string) {
|
2022-01-17 09:53:56 +00:00
|
|
|
sc.referenceCounter--
|
|
|
|
if sc.referenceCounter == 0 {
|
2022-01-17 22:15:10 +00:00
|
|
|
glog.V(4).Infof("Free sealed chunk: %s", messageOnFree)
|
2022-01-22 12:18:54 +00:00
|
|
|
sc.chunk.FreeResource()
|
2022-01-17 09:53:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 08:36:19 +00:00
|
|
|
func NewUploadPipeline(writers *util.LimitedConcurrentExecutor, chunkSize int64, saveToStorageFn SaveToStorageFunc, bufferChunkLimit int) *UploadPipeline {
|
2022-01-17 09:53:56 +00:00
|
|
|
return &UploadPipeline{
|
2022-01-22 09:46:10 +00:00
|
|
|
ChunkSize: chunkSize,
|
2022-01-22 12:18:54 +00:00
|
|
|
writableChunks: make(map[LogicChunkIndex]PageChunk),
|
2022-01-22 09:46:10 +00:00
|
|
|
sealedChunks: make(map[LogicChunkIndex]*SealedChunk),
|
|
|
|
uploaders: writers,
|
|
|
|
uploaderCountCond: sync.NewCond(&sync.Mutex{}),
|
|
|
|
saveToStorageFn: saveToStorageFn,
|
|
|
|
activeReadChunks: make(map[LogicChunkIndex]int),
|
2022-01-26 08:36:19 +00:00
|
|
|
bufferChunkLimit: bufferChunkLimit,
|
2022-01-17 09:53:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 16:06:53 +00:00
|
|
|
func (up *UploadPipeline) SaveDataAt(p []byte, off int64) (n int) {
|
|
|
|
up.writableChunksLock.Lock()
|
|
|
|
defer up.writableChunksLock.Unlock()
|
2022-01-17 09:53:56 +00:00
|
|
|
|
2022-01-22 16:06:53 +00:00
|
|
|
logicChunkIndex := LogicChunkIndex(off / up.ChunkSize)
|
2022-01-17 09:53:56 +00:00
|
|
|
|
2022-01-22 16:06:53 +00:00
|
|
|
memChunk, found := up.writableChunks[logicChunkIndex]
|
2022-01-17 09:53:56 +00:00
|
|
|
if !found {
|
2022-01-26 08:36:19 +00:00
|
|
|
if len(up.writableChunks) < up.bufferChunkLimit {
|
2022-01-22 16:06:53 +00:00
|
|
|
memChunk = NewMemChunk(logicChunkIndex, up.ChunkSize)
|
|
|
|
} else {
|
2022-01-24 07:02:05 +00:00
|
|
|
fullestChunkIndex, fullness := LogicChunkIndex(-1), int64(0)
|
|
|
|
for lci, mc := range up.writableChunks {
|
|
|
|
chunkFullness := mc.WrittenSize()
|
|
|
|
if fullness < chunkFullness {
|
|
|
|
fullestChunkIndex = lci
|
|
|
|
fullness = chunkFullness
|
|
|
|
}
|
2022-01-22 16:06:53 +00:00
|
|
|
}
|
2022-01-24 07:02:05 +00:00
|
|
|
up.moveToSealed(up.writableChunks[fullestChunkIndex], fullestChunkIndex)
|
|
|
|
delete(up.writableChunks, fullestChunkIndex)
|
|
|
|
fmt.Printf("flush chunk %d with %d bytes written", logicChunkIndex, fullness)
|
|
|
|
memChunk = NewMemChunk(logicChunkIndex, up.ChunkSize)
|
2022-01-22 16:06:53 +00:00
|
|
|
}
|
|
|
|
up.writableChunks[logicChunkIndex] = memChunk
|
2022-01-17 09:53:56 +00:00
|
|
|
}
|
2022-01-22 14:34:29 +00:00
|
|
|
n = memChunk.WriteDataAt(p, off)
|
2022-01-22 16:06:53 +00:00
|
|
|
up.maybeMoveToSealed(memChunk, logicChunkIndex)
|
2022-01-17 09:53:56 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-22 16:06:53 +00:00
|
|
|
func (up *UploadPipeline) MaybeReadDataAt(p []byte, off int64) (maxStop int64) {
|
|
|
|
logicChunkIndex := LogicChunkIndex(off / up.ChunkSize)
|
2022-01-17 09:53:56 +00:00
|
|
|
|
|
|
|
// read from sealed chunks first
|
2022-01-22 16:06:53 +00:00
|
|
|
up.sealedChunksLock.Lock()
|
|
|
|
sealedChunk, found := up.sealedChunks[logicChunkIndex]
|
2022-01-17 09:53:56 +00:00
|
|
|
if found {
|
|
|
|
sealedChunk.referenceCounter++
|
|
|
|
}
|
2022-01-22 16:06:53 +00:00
|
|
|
up.sealedChunksLock.Unlock()
|
2022-01-17 09:53:56 +00:00
|
|
|
if found {
|
2022-01-22 13:40:10 +00:00
|
|
|
maxStop = sealedChunk.chunk.ReadDataAt(p, off)
|
2022-01-22 16:06:53 +00:00
|
|
|
glog.V(4).Infof("%s read sealed memchunk [%d,%d)", up.filepath, off, maxStop)
|
|
|
|
sealedChunk.FreeReference(fmt.Sprintf("%s finish reading chunk %d", up.filepath, logicChunkIndex))
|
2022-01-17 09:53:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// read from writable chunks last
|
2022-01-22 16:06:53 +00:00
|
|
|
up.writableChunksLock.Lock()
|
|
|
|
defer up.writableChunksLock.Unlock()
|
|
|
|
writableChunk, found := up.writableChunks[logicChunkIndex]
|
2022-01-17 09:53:56 +00:00
|
|
|
if !found {
|
|
|
|
return
|
|
|
|
}
|
2022-01-22 13:40:10 +00:00
|
|
|
writableMaxStop := writableChunk.ReadDataAt(p, off)
|
2022-01-22 16:06:53 +00:00
|
|
|
glog.V(4).Infof("%s read writable memchunk [%d,%d)", up.filepath, off, writableMaxStop)
|
2022-01-18 04:41:00 +00:00
|
|
|
maxStop = max(maxStop, writableMaxStop)
|
2022-01-17 09:53:56 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-22 16:06:53 +00:00
|
|
|
func (up *UploadPipeline) FlushAll() {
|
|
|
|
up.writableChunksLock.Lock()
|
|
|
|
defer up.writableChunksLock.Unlock()
|
2022-01-17 09:53:56 +00:00
|
|
|
|
2022-01-22 16:06:53 +00:00
|
|
|
for logicChunkIndex, memChunk := range up.writableChunks {
|
|
|
|
up.moveToSealed(memChunk, logicChunkIndex)
|
2022-01-17 09:53:56 +00:00
|
|
|
}
|
|
|
|
|
2022-01-22 16:06:53 +00:00
|
|
|
up.waitForCurrentWritersToComplete()
|
2022-01-17 09:53:56 +00:00
|
|
|
}
|
|
|
|
|
2022-01-22 16:06:53 +00:00
|
|
|
func (up *UploadPipeline) maybeMoveToSealed(memChunk PageChunk, logicChunkIndex LogicChunkIndex) {
|
2022-01-22 13:40:10 +00:00
|
|
|
if memChunk.IsComplete() {
|
2022-01-22 16:06:53 +00:00
|
|
|
up.moveToSealed(memChunk, logicChunkIndex)
|
2022-01-17 09:53:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 16:06:53 +00:00
|
|
|
func (up *UploadPipeline) moveToSealed(memChunk PageChunk, logicChunkIndex LogicChunkIndex) {
|
|
|
|
atomic.AddInt32(&up.uploaderCount, 1)
|
|
|
|
glog.V(4).Infof("%s uploaderCount %d ++> %d", up.filepath, up.uploaderCount-1, up.uploaderCount)
|
2022-01-17 09:53:56 +00:00
|
|
|
|
2022-01-22 16:06:53 +00:00
|
|
|
up.sealedChunksLock.Lock()
|
2022-01-17 09:53:56 +00:00
|
|
|
|
2022-01-22 16:06:53 +00:00
|
|
|
if oldMemChunk, found := up.sealedChunks[logicChunkIndex]; found {
|
|
|
|
oldMemChunk.FreeReference(fmt.Sprintf("%s replace chunk %d", up.filepath, logicChunkIndex))
|
2022-01-17 09:53:56 +00:00
|
|
|
}
|
|
|
|
sealedChunk := &SealedChunk{
|
|
|
|
chunk: memChunk,
|
|
|
|
referenceCounter: 1, // default 1 is for uploading process
|
|
|
|
}
|
2022-01-22 16:06:53 +00:00
|
|
|
up.sealedChunks[logicChunkIndex] = sealedChunk
|
|
|
|
delete(up.writableChunks, logicChunkIndex)
|
2022-01-17 09:53:56 +00:00
|
|
|
|
2022-01-22 16:06:53 +00:00
|
|
|
up.sealedChunksLock.Unlock()
|
2022-01-17 09:53:56 +00:00
|
|
|
|
2022-01-22 16:06:53 +00:00
|
|
|
up.uploaders.Execute(func() {
|
2022-01-17 21:40:41 +00:00
|
|
|
// first add to the file chunks
|
2022-01-22 16:06:53 +00:00
|
|
|
sealedChunk.chunk.SaveContent(up.saveToStorageFn)
|
2022-01-17 09:53:56 +00:00
|
|
|
|
2022-01-18 06:24:44 +00:00
|
|
|
// notify waiting process
|
2022-01-22 16:06:53 +00:00
|
|
|
atomic.AddInt32(&up.uploaderCount, -1)
|
|
|
|
glog.V(4).Infof("%s uploaderCount %d --> %d", up.filepath, up.uploaderCount+1, up.uploaderCount)
|
2022-01-17 09:53:56 +00:00
|
|
|
// Lock and Unlock are not required,
|
|
|
|
// but it may signal multiple times during one wakeup,
|
|
|
|
// and the waiting goroutine may miss some of them!
|
2022-01-22 16:06:53 +00:00
|
|
|
up.uploaderCountCond.L.Lock()
|
|
|
|
up.uploaderCountCond.Broadcast()
|
|
|
|
up.uploaderCountCond.L.Unlock()
|
2022-01-18 06:24:44 +00:00
|
|
|
|
|
|
|
// wait for readers
|
2022-01-22 16:06:53 +00:00
|
|
|
for up.IsLocked(logicChunkIndex) {
|
2022-01-18 06:24:44 +00:00
|
|
|
time.Sleep(59 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
// then remove from sealed chunks
|
2022-01-22 16:06:53 +00:00
|
|
|
up.sealedChunksLock.Lock()
|
|
|
|
defer up.sealedChunksLock.Unlock()
|
|
|
|
delete(up.sealedChunks, logicChunkIndex)
|
|
|
|
sealedChunk.FreeReference(fmt.Sprintf("%s finished uploading chunk %d", up.filepath, logicChunkIndex))
|
2022-01-18 06:24:44 +00:00
|
|
|
|
2022-01-17 09:53:56 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-22 16:06:53 +00:00
|
|
|
func (up *UploadPipeline) Shutdown() {
|
2022-01-17 09:53:56 +00:00
|
|
|
}
|