Some checks failed
go: build dev binaries / cleanup (push) Successful in 4s
Codespell / Check for spelling errors (push) Failing after 3s
docker: build dev containers / build-rust-binaries (amd64, x86_64-unknown-linux-musl) (push) Failing after 3s
docker: build dev containers / build-dev-containers (push) Has been skipped
docker: build dev containers / build-rust-binaries (arm64, true, aarch64-unknown-linux-musl) (push) Failing after 3s
End to End / FUSE Mount (push) Failing after 2s
EC Integration Tests / EC Integration Tests (push) Failing after 30s
FUSE DLM Integration Tests / FUSE DLM Integration Tests (push) Failing after 3s
FUSE Volume Server Failover Tests / FUSE Volume Server Failover (push) Failing after 3s
FUSE Integration Tests / FUSE Integration Testing (push) Failing after 2s
FUSE P2P Peer Chunk Sharing Integration Tests / FUSE P2P Peer Chunk Sharing (push) Failing after 3s
go: build binary / Go Vet (push) Failing after 2s
go: build binary / Go Vet 32-bit (push) Failing after 3s
go: build binary / Build (push) Failing after 4s
go: build binary / Build cross-platform (freebsd/amd64) (push) Failing after 3s
go: build binary / Build cross-platform (windows/amd64) (push) Failing after 3s
go: build binary / Build cross-platform (darwin/arm64) (push) Failing after 3s
go: build binary / Build cross-platform (windows/arm64) (push) Failing after 3s
go: build binary / Test (push) Failing after 2s
go: build binary / Test 32-bit (push) Failing after 3s
helm: lint and test charts / lint-test (push) Failing after 3s
Java Client Integration Tests / Java Integration Tests (11) (push) Failing after 3s
Java Client Integration Tests / Java Integration Tests (17) (push) Failing after 3s
go: build dev binaries / build_dev_linux_windows (amd64, linux) (push) Failing after 2s
go: build dev binaries / build_dev_linux_windows (amd64, windows) (push) Failing after 3s
go: build dev binaries / build_dev_darwin (amd64, darwin) (push) Has been skipped
go: build dev binaries / build_dev_darwin (arm64, darwin) (push) Has been skipped
Java Client Unit Tests / Java Unit Tests (11) (push) Failing after 3s
Java Client Unit Tests / Java Unit Tests (17) (push) Failing after 2s
Java Client Unit Tests / Java Unit Tests (21) (push) Failing after 3s
Java Client Unit Tests / Java Unit Tests (8) (push) Failing after 3s
Kafka Quick Test (Load Test with Schema Registry) / Kafka Client Load Test (Quick) (push) Failing after 3s
Kafka Gateway Tests / Kafka Unit Tests (unit-tests-1) (push) Failing after 11s
Kafka Gateway Tests / Kafka Integration Tests (Critical) (integration-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka End-to-End Tests (with SMQ) (e2e-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka Consumer Group Tests (Highly Isolated) (consumer-group-1) (push) Failing after 2s
Kafka Gateway Tests / Kafka Client Compatibility (with SMQ) (client-compat-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka SMQ Integration Tests (Full Stack) (smq-integration-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka Protocol Tests (Isolated) (protocol-1) (push) Failing after 3s
KMS Tests / KMS Provider Integration Tests (push) Failing after 2s
KMS Tests / S3 KMS End-to-End Tests (push) Failing after 3s
Master Cold Start Tests / Master Cold Start Tests (push) Failing after 4s
Metadata Subscribe Integration Tests / Metadata Subscribe Integration Tests (push) Failing after 3s
Multi-Master Tests / Multi-Master Failover Tests (push) Failing after 3s
Performance / CPU and Heap Profile (push) Failing after 2s
Performance / S3 Read/Write Benchmark (rust volume) (push) Failing after 3s
pjdfstest POSIX Compliance / pjdfstest (push) Failing after 2s
Plugin Worker Integration Tests / Plugin Worker: erasure_coding (push) Failing after 3s
Plugin Worker Integration Tests / Plugin Worker: vacuum (push) Failing after 3s
Rust Volume Server Tests / Rust Integration Tests (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 1) (push) Failing after 2s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 1) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 2) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 2) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 3) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Distributed Tests (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Performance Tests (push) Failing after 3s
Performance / Throughput Benchmark (go volume) (push) Failing after 3s
Performance / Throughput Benchmark (rust volume) (push) Failing after 2s
Performance / S3 Read/Write Benchmark (go volume) (push) Failing after 3s
Plugin Worker Integration Tests / Plugin Worker: volume_balance (push) Failing after 3s
PostgreSQL Gateway Tests / PostgreSQL Basic Tests (push) Failing after 3s
Rust Volume Server Tests / Rust Unit Tests (push) Failing after 2s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 3) (push) Failing after 3s
Rust Plugin Worker Tests / Rust Plugin Worker Build and Unit Tests (push) Failing after 3s
rust: build dev volume server binaries / cleanup (push) Successful in 3s
S3 ETag and ACL Tests / S3 ETag + ACL Integration Tests (push) Failing after 3s
S3 IAM Integration Tests / IAM Unit Tests (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (advanced) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (basic) (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Integration Tests (group) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (policy-enforcement) (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Integration Tests (sts) (push) Failing after 4s
S3 Keycloak Integration Tests / S3 Keycloak Integration Tests (push) Failing after 3s
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.11) (push) Failing after 3s
S3 PyArrow Parquet Tests / Go Unit Tests (Implicit Directory) (push) Has been cancelled
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.12) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-darwin (darwin-amd64, x86_64-apple-darwin) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-darwin (darwin-arm64, aarch64-apple-darwin) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-linux (linux-amd64, x86_64-unknown-linux-gnu) (push) Has been cancelled
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.9) (push) Has been cancelled
S3 Proxy Signature Tests / S3 Proxy Signature Verification Tests (push) Has been cancelled
S3 SDK V2 Route Disambiguation Tests / S3 SDK V2 Routing Tests (push) Has been cancelled
S3 SSE Tests / S3 SSE Integration Tests (quick) (push) Has been cancelled
S3 SSE Tests / S3 SSE Compatibility Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Metadata Persistence Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Copy Operations Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Multipart Upload Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Performance Test (push) Has been cancelled
S3 SSE Tests / S3 Volume Encryption Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Integration Tests (comprehensive) (push) Has been cancelled
Spark Integration Tests / Spark Integration Tests (push) Failing after 2s
Telemetry Integration Tests / telemetry-integration-test (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 1) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 1) (push) Failing after 3s
Ceph S3 tests / Basic S3 tests (KV store) (push) Has been cancelled
Ceph S3 tests / S3 Versioning & Object Lock tests (push) Has been cancelled
S3 Policy Integration Tests / S3 Policy Unit Tests (push) Failing after 3s
Ceph S3 tests / S3 CORS tests (push) Has been cancelled
Ceph S3 tests / SeaweedFS Custom S3 Copy tests (push) Has been cancelled
Ceph S3 tests / Basic S3 tests (SQL store) (push) Has been cancelled
test s3 over https using aws-cli / awscli-tests (push) Has been cancelled
TLS Rotation Integration Tests / TLS Rotation Integration Tests (push) Has been cancelled
Vacuum Integration Tests / Vacuum Integration Tests (push) Has been cancelled
S3 Policy Integration Tests / S3 Policy Variables Integration Tests (push) Failing after 3s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (advanced-policy) (push) Failing after 3s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (basic-policy) (push) Failing after 2s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (contextual-policy) (push) Failing after 4s
S3 Policy Integration Tests / Trusted Proxy Detection Tests (push) Failing after 2s
S3 Policy Integration Tests / S3 Policy Shell Integration Tests (push) Failing after 3s
Samba on FUSE Integration / samba-integration (push) Failing after 2s
SFTP Integration Tests / SFTP Integration Testing (push) Failing after 3s
terraform: validate and test modules / fmt, validate, plan-level tests (push) Failing after 3s
terraform: validate and test modules / local cluster smoke test (real weed) (push) Failing after 2s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 3) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 2) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 2) (push) Failing after 2s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 3) (push) Failing after 5s
mount: windows conformance / WinFsp conformance (push) Has been cancelled
mount: windows / Mount on Windows (push) Has been cancelled
403 lines
13 KiB
Go
403 lines
13 KiB
Go
package page_writer
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"sync"
|
|
"sync/atomic"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
)
|
|
|
|
type LogicChunkIndex int
|
|
|
|
type UploadPipeline struct {
|
|
uploaderCount int32
|
|
uploaderCountCond *sync.Cond
|
|
filepath util.FullPath
|
|
ChunkSize int64
|
|
uploaders *util.LimitedConcurrentExecutor
|
|
saveToStorageFn SaveToStorageFunc
|
|
writableChunkLimit int
|
|
concurrentWriterMax int32
|
|
swapFile *SwapFile
|
|
chunksLock sync.Mutex
|
|
writableChunks map[LogicChunkIndex]PageChunk
|
|
sealedChunks map[LogicChunkIndex]*SealedChunk
|
|
activeReadChunks map[LogicChunkIndex]int
|
|
readerCountCond *sync.Cond
|
|
accountant *WriteBufferAccountant
|
|
lastWriteChunkIndex int64 // atomic: highest LogicChunkIndex written
|
|
}
|
|
|
|
type SealedChunk struct {
|
|
chunk PageChunk
|
|
referenceCounter int // track uploading or reading processes
|
|
// accountant and chunkSize are captured when the chunk is sealed so
|
|
// FreeReference can release the global write-budget slot exactly once
|
|
// regardless of which code path triggers the final deref (normal
|
|
// upload completion, Shutdown, or replace-in-place in moveToSealed).
|
|
accountant *WriteBufferAccountant
|
|
chunkSize int64
|
|
}
|
|
|
|
func (sc *SealedChunk) FreeReference(messageOnFree string) {
|
|
// Early-return guard so repeated calls (Shutdown racing the async
|
|
// uploader, or any future caller that loses track of ownership) are
|
|
// strict no-ops rather than driving referenceCounter negative.
|
|
if sc.referenceCounter <= 0 {
|
|
return
|
|
}
|
|
sc.referenceCounter--
|
|
if sc.referenceCounter == 0 {
|
|
glog.V(4).Infof("Free sealed chunk: %s", messageOnFree)
|
|
sc.chunk.FreeResource()
|
|
sc.accountant.Release(sc.chunkSize)
|
|
}
|
|
}
|
|
|
|
// NewUploadPipeline constructs an UploadPipeline. accountant may be nil,
|
|
// in which case no global write-buffer cap is enforced. When non-nil,
|
|
// creating a new page chunk (memory or swap) first reserves ChunkSize
|
|
// bytes against it, blocking the writer if the global cap is reached.
|
|
// The accountant is captured at construction so the pipeline's hot paths
|
|
// (SaveDataAt, moveToSealed, Shutdown) can read up.accountant without
|
|
// any synchronization.
|
|
func NewUploadPipeline(writers *util.LimitedConcurrentExecutor, chunkSize int64, saveToStorageFn SaveToStorageFunc, bufferChunkLimit int, swapFileDir string, accountant *WriteBufferAccountant) *UploadPipeline {
|
|
t := &UploadPipeline{
|
|
ChunkSize: chunkSize,
|
|
writableChunks: make(map[LogicChunkIndex]PageChunk),
|
|
sealedChunks: make(map[LogicChunkIndex]*SealedChunk),
|
|
uploaders: writers,
|
|
uploaderCountCond: sync.NewCond(&sync.Mutex{}),
|
|
saveToStorageFn: saveToStorageFn,
|
|
activeReadChunks: make(map[LogicChunkIndex]int),
|
|
writableChunkLimit: bufferChunkLimit,
|
|
concurrentWriterMax: int32(bufferChunkLimit),
|
|
swapFile: NewSwapFile(swapFileDir, chunkSize),
|
|
accountant: accountant,
|
|
}
|
|
t.readerCountCond = sync.NewCond(&t.chunksLock)
|
|
return t
|
|
}
|
|
|
|
func (up *UploadPipeline) SaveDataAt(p []byte, off int64, isSequential bool, tsNs int64) (n int, err error) {
|
|
|
|
up.chunksLock.Lock()
|
|
defer up.chunksLock.Unlock()
|
|
|
|
logicChunkIndex := LogicChunkIndex(off / up.ChunkSize)
|
|
|
|
// track write frontier for proactive flushing (CAS to avoid regression)
|
|
for {
|
|
old := atomic.LoadInt64(&up.lastWriteChunkIndex)
|
|
if int64(logicChunkIndex) <= old {
|
|
break
|
|
}
|
|
if atomic.CompareAndSwapInt64(&up.lastWriteChunkIndex, old, int64(logicChunkIndex)) {
|
|
break
|
|
}
|
|
}
|
|
|
|
pageChunk, found := up.writableChunks[logicChunkIndex]
|
|
if !found {
|
|
// Reserve a chunk-sized slot against the global write budget before
|
|
// allocating. Reserve may block when volumes are full and sealed
|
|
// chunks can't drain, so we must release chunksLock first — the
|
|
// uploader goroutines that eventually call Release take chunksLock.
|
|
if up.accountant != nil {
|
|
up.chunksLock.Unlock()
|
|
up.accountant.Reserve(up.ChunkSize)
|
|
up.chunksLock.Lock()
|
|
// Re-check: another writer on the same file may have created
|
|
// the chunk while we were blocked. If so, give the slot back.
|
|
if pageChunk, found = up.writableChunks[logicChunkIndex]; found {
|
|
up.accountant.Release(up.ChunkSize)
|
|
}
|
|
}
|
|
}
|
|
if !found {
|
|
if len(up.writableChunks) > up.writableChunkLimit {
|
|
// Per-pipeline soft cap. Seal the fullest gap-free chunk;
|
|
// gappy chunks must wait so in-flight FUSE writeback can fill
|
|
// the gap (issue #9330). If none qualifies, fall through —
|
|
// the memChunkCounter ceiling below redirects to swap.
|
|
candidateChunkIndex, fullness := LogicChunkIndex(-1), int64(0)
|
|
for lci, mc := range up.writableChunks {
|
|
if !mc.IsContiguouslyWritten() {
|
|
continue
|
|
}
|
|
if b := mc.WrittenSize(); b > fullness {
|
|
candidateChunkIndex = lci
|
|
fullness = b
|
|
}
|
|
}
|
|
if candidateChunkIndex >= 0 {
|
|
up.moveToSealed(up.writableChunks[candidateChunkIndex], candidateChunkIndex)
|
|
}
|
|
}
|
|
// fmt.Printf("isSequential:%v len(up.writableChunks):%v memChunkCounter:%v", isSequential, len(up.writableChunks), memChunkCounter)
|
|
if isSequential &&
|
|
len(up.writableChunks) < up.writableChunkLimit &&
|
|
atomic.LoadInt64(&memChunkCounter) < 4*int64(up.writableChunkLimit) {
|
|
pageChunk = NewMemChunk(logicChunkIndex, up.ChunkSize)
|
|
// fmt.Printf(" create mem chunk %d\n", logicChunkIndex)
|
|
} else {
|
|
pageChunk = up.swapFile.NewSwapFileChunk(logicChunkIndex)
|
|
// fmt.Printf(" create file chunk %d\n", logicChunkIndex)
|
|
if pageChunk == nil {
|
|
up.accountant.Release(up.ChunkSize)
|
|
return 0, fmt.Errorf("failed to create swap file chunk")
|
|
}
|
|
}
|
|
up.writableChunks[logicChunkIndex] = pageChunk
|
|
}
|
|
//if _, foundSealed := up.sealedChunks[logicChunkIndex]; foundSealed {
|
|
// println("found already sealed chunk", logicChunkIndex)
|
|
//}
|
|
//if _, foundReading := up.activeReadChunks[logicChunkIndex]; foundReading {
|
|
// println("found active read chunk", logicChunkIndex)
|
|
//}
|
|
n = pageChunk.WriteDataAt(p, off, tsNs)
|
|
up.maybeMoveToSealed(pageChunk, logicChunkIndex)
|
|
|
|
return
|
|
}
|
|
|
|
func (up *UploadPipeline) MaybeReadDataAt(p []byte, off int64, tsNs int64) (maxStop int64) {
|
|
logicChunkIndex := LogicChunkIndex(off / up.ChunkSize)
|
|
|
|
up.chunksLock.Lock()
|
|
defer func() {
|
|
up.readerCountCond.Signal()
|
|
up.chunksLock.Unlock()
|
|
}()
|
|
|
|
// read from sealed chunks first
|
|
sealedChunk, found := up.sealedChunks[logicChunkIndex]
|
|
if found {
|
|
maxStop = sealedChunk.chunk.ReadDataAt(p, off, tsNs)
|
|
glog.V(4).Infof("%s read sealed memchunk [%d,%d)", up.filepath, off, maxStop)
|
|
}
|
|
|
|
// read from writable chunks last
|
|
writableChunk, found := up.writableChunks[logicChunkIndex]
|
|
if !found {
|
|
return
|
|
}
|
|
writableMaxStop := writableChunk.ReadDataAt(p, off, tsNs)
|
|
glog.V(4).Infof("%s read writable memchunk [%d,%d)", up.filepath, off, writableMaxStop)
|
|
maxStop = max(maxStop, writableMaxStop)
|
|
|
|
return
|
|
}
|
|
|
|
func (up *UploadPipeline) FlushAll() {
|
|
up.flushChunks()
|
|
up.waitForCurrentWritersToComplete()
|
|
}
|
|
|
|
func (up *UploadPipeline) flushChunks() {
|
|
up.chunksLock.Lock()
|
|
defer up.chunksLock.Unlock()
|
|
|
|
for logicChunkIndex, memChunk := range up.writableChunks {
|
|
up.moveToSealed(memChunk, logicChunkIndex)
|
|
}
|
|
}
|
|
|
|
func (up *UploadPipeline) maybeMoveToSealed(memChunk PageChunk, logicChunkIndex LogicChunkIndex) {
|
|
if memChunk.IsComplete() {
|
|
up.moveToSealed(memChunk, logicChunkIndex)
|
|
}
|
|
}
|
|
|
|
func (up *UploadPipeline) moveToSealed(memChunk PageChunk, logicChunkIndex LogicChunkIndex) {
|
|
newCount := atomic.AddInt32(&up.uploaderCount, 1)
|
|
glog.V(4).Infof("%s uploaderCount %d ++> %d", up.filepath, newCount-1, newCount)
|
|
|
|
if oldMemChunk, found := up.sealedChunks[logicChunkIndex]; found {
|
|
oldMemChunk.FreeReference(fmt.Sprintf("%s replace chunk %d", up.filepath, logicChunkIndex))
|
|
}
|
|
sealedChunk := &SealedChunk{
|
|
chunk: memChunk,
|
|
// One for the sealedChunks slot, one for the upload below, which
|
|
// drops its own. Execute() returns before SaveContent reads the
|
|
// chunk, so a later seal must not free the buffer under it.
|
|
referenceCounter: 2,
|
|
accountant: up.accountant,
|
|
chunkSize: up.ChunkSize,
|
|
}
|
|
up.sealedChunks[logicChunkIndex] = sealedChunk
|
|
delete(up.writableChunks, logicChunkIndex)
|
|
|
|
// unlock before submitting the uploading jobs
|
|
up.chunksLock.Unlock()
|
|
up.uploaders.Execute(func() {
|
|
// first add to the file chunks
|
|
sealedChunk.chunk.SaveContent(up.saveToStorageFn)
|
|
|
|
// notify waiting process
|
|
newCount := atomic.AddInt32(&up.uploaderCount, -1)
|
|
glog.V(4).Infof("%s uploaderCount %d --> %d", up.filepath, newCount+1, newCount)
|
|
// Lock and Unlock are not required,
|
|
// but it may signal multiple times during one wakeup,
|
|
// and the waiting goroutine may miss some of them!
|
|
up.uploaderCountCond.L.Lock()
|
|
up.uploaderCountCond.Broadcast()
|
|
up.uploaderCountCond.L.Unlock()
|
|
|
|
// wait for readers
|
|
up.chunksLock.Lock()
|
|
defer up.chunksLock.Unlock()
|
|
for up.IsLocked(logicChunkIndex) {
|
|
up.readerCountCond.Wait()
|
|
}
|
|
|
|
// then remove from sealed chunks, unless a later seal took over the
|
|
// index — deleting that one would hide its dirty pages from readers
|
|
if up.sealedChunks[logicChunkIndex] == sealedChunk {
|
|
delete(up.sealedChunks, logicChunkIndex)
|
|
sealedChunk.FreeReference(fmt.Sprintf("%s unindex chunk %d", up.filepath, logicChunkIndex))
|
|
}
|
|
sealedChunk.FreeReference(fmt.Sprintf("%s finished uploading chunk %d", up.filepath, logicChunkIndex))
|
|
|
|
})
|
|
up.chunksLock.Lock()
|
|
}
|
|
|
|
// EvictOneWritableChunk force-seals one writable chunk so the accountant's
|
|
// Reserve loop can make progress. Strict pass picks the fullest gap-free
|
|
// chunk (issue #9330). Fallback picks the oldest non-empty writer when
|
|
// nothing is gap-free; without it Reserve deadlocks (cond.Wait only wakes
|
|
// on Release, Release only fires on upload completion). Callers must not
|
|
// hold up.chunksLock.
|
|
func (up *UploadPipeline) EvictOneWritableChunk() bool {
|
|
up.chunksLock.Lock()
|
|
defer up.chunksLock.Unlock()
|
|
if len(up.writableChunks) == 0 {
|
|
return false
|
|
}
|
|
|
|
bestIndex := LogicChunkIndex(-1)
|
|
var bestBytes int64
|
|
for lci, wc := range up.writableChunks {
|
|
if !wc.IsContiguouslyWritten() {
|
|
continue
|
|
}
|
|
if b := wc.WrittenSize(); b > bestBytes {
|
|
bestIndex = lci
|
|
bestBytes = b
|
|
}
|
|
}
|
|
|
|
if bestIndex < 0 {
|
|
oldestTsNs := int64(math.MaxInt64)
|
|
var fallbackBytes int64
|
|
for lci, wc := range up.writableChunks {
|
|
b := wc.WrittenSize()
|
|
if b == 0 {
|
|
continue
|
|
}
|
|
ts := wc.LastWriteTsNs()
|
|
if ts == 0 {
|
|
continue
|
|
}
|
|
if ts < oldestTsNs || (ts == oldestTsNs && b > fallbackBytes) {
|
|
bestIndex = lci
|
|
oldestTsNs = ts
|
|
fallbackBytes = b
|
|
}
|
|
}
|
|
}
|
|
|
|
if bestIndex < 0 {
|
|
return false
|
|
}
|
|
up.moveToSealed(up.writableChunks[bestIndex], bestIndex)
|
|
return true
|
|
}
|
|
|
|
// ProactiveFlush seals at most one idle writable chunk that is unlikely to
|
|
// receive further writes, submitting it for async upload. Returns true if a
|
|
// chunk was sealed. The caller (ChunkFlusher) invokes this periodically so
|
|
// that partially-written chunks drain continuously instead of piling up
|
|
// until fsync.
|
|
func (up *UploadPipeline) ProactiveFlush(nowNs int64, idleThresholdNs int64, maxHoldNs int64, fillRatio int64, frontierLag int, isSequential bool) bool {
|
|
if up.concurrentWriterMax <= 0 || atomic.LoadInt32(&up.uploaderCount)*2 >= up.concurrentWriterMax {
|
|
return false
|
|
}
|
|
|
|
up.chunksLock.Lock()
|
|
defer up.chunksLock.Unlock()
|
|
|
|
if len(up.writableChunks) == 0 {
|
|
return false
|
|
}
|
|
|
|
frontier := atomic.LoadInt64(&up.lastWriteChunkIndex)
|
|
isSeq := isSequential
|
|
|
|
var bestIdx LogicChunkIndex = -1
|
|
var bestBytes int64 = -1
|
|
|
|
for lci, chunk := range up.writableChunks {
|
|
lastWrite := chunk.LastWriteTsNs()
|
|
if lastWrite == 0 {
|
|
continue
|
|
}
|
|
age := nowNs - lastWrite
|
|
if age < idleThresholdNs {
|
|
continue
|
|
}
|
|
// Skip gappy chunks; FUSE writeback may still be in flight for the
|
|
// hole and SaveContent would bake it into split volume chunks
|
|
// (issue #9330). Failing here is just a missed flush — FlushAll
|
|
// at close still seals everything.
|
|
if !chunk.IsContiguouslyWritten() {
|
|
continue
|
|
}
|
|
written := chunk.WrittenSize()
|
|
nearlyFull := written >= fillRatio
|
|
behindFrontier := isSeq && int64(lci) <= frontier-int64(frontierLag)
|
|
stale := age >= maxHoldNs
|
|
|
|
if !nearlyFull && !behindFrontier && !stale {
|
|
continue
|
|
}
|
|
if written > bestBytes {
|
|
bestIdx = lci
|
|
bestBytes = written
|
|
}
|
|
}
|
|
if bestIdx < 0 {
|
|
return false
|
|
}
|
|
glog.V(3).Infof("%s proactive flush chunk %d (%d bytes written)", up.filepath, bestIdx, bestBytes)
|
|
up.moveToSealed(up.writableChunks[bestIdx], bestIdx)
|
|
return true
|
|
}
|
|
|
|
func (up *UploadPipeline) Shutdown() {
|
|
up.swapFile.FreeResource()
|
|
|
|
up.chunksLock.Lock()
|
|
defer up.chunksLock.Unlock()
|
|
// Free any writable chunks that were reserved but never sealed — on the
|
|
// Destroy() path (truncate / metadata invalidation) there is no
|
|
// preceding FlushData(), so dirty writable chunks would otherwise leak
|
|
// both their memory and their write-budget slots.
|
|
for logicChunkIndex, writableChunk := range up.writableChunks {
|
|
glog.V(4).Infof("%s uploadpipeline shutdown writable chunk %d", up.filepath, logicChunkIndex)
|
|
writableChunk.FreeResource()
|
|
up.accountant.Release(up.ChunkSize)
|
|
delete(up.writableChunks, logicChunkIndex)
|
|
}
|
|
for logicChunkIndex, sealedChunk := range up.sealedChunks {
|
|
// only the slot reference; an in-flight upload frees the chunk itself
|
|
sealedChunk.FreeReference(fmt.Sprintf("%s uploadpipeline shutdown chunk %d", up.filepath, logicChunkIndex))
|
|
delete(up.sealedChunks, logicChunkIndex)
|
|
}
|
|
}
|