Files
seaweedfs/weed/storage/store_volume_report.go
T
Chris Lu 3bd218e030 volume: cut idle memory at high volume counts (#10861)
* volume: start a volume's batch write worker on first use

Mounting a volume started a goroutine parked on a 128-slot channel, plus
the 128-entry batch slice it had already allocated. That is around 6.7KB
per volume the server pays whether or not the volume ever takes a write:
7231 bytes per mounted volume, of which 4101 is goroutine stack.

Only a write that asks for fsync ever reaches the worker, and a
remote-tiered or read-only volume never can. Create the channel and its
goroutine on the first such request instead, and let a write arriving
after Destroy fall back to the inline path rather than queue onto a
worker that has gone.

Measured over 20000 mounted volumes: 7231 -> 1269 bytes each.

* volume: update the heartbeat report state in place

Every heartbeat built a second map of what it was about to tell the
master, holding a freshly allocated short information message per volume,
then swapped it in over the old one -- and computed departures through a
third map of the live volume ids. A server holding 2M volumes rebuilt all
three every VolumePulsePeriod for a report that usually says nothing.

Number the heartbeats instead and mark the entry already held with the
pass that found the copy, so a quiet volume costs a map lookup and no
allocation. Departures are the entries a pass did not mark; the live-id
map is now built only when there are some, sized to them.

Measured over 10000 mounted volumes: 436 -> 196 bytes allocated per
volume per heartbeat.

* volume: fill one volume information message per heartbeat, not per volume

The heartbeat built a message for every volume held so it could hash it,
then dropped all but the few it had something to say about. At 2M volumes
that is 2M messages allocated every VolumePulsePeriod to send almost none
of them.

Fill a message the caller supplies instead, and replace it only when the
heartbeat keeps it, so a server with nothing to report fills the same one
all the way through.

Measured over 10000 mounted volumes: 196 -> 4 bytes allocated per volume
per heartbeat, and a heartbeat runs a third faster.

* volume: drop the per-volume trace from the heartbeat's status read

glog.V(4).Infof evaluates its arguments whether or not the verbosity is
on, so every volume boxed its id into a fresh interface slice on every
heartbeat: 759 of the 773 allocations a 1000-volume heartbeat made, for a
line that at this scale would print millions of unreadable rows.

Measured over 1000 mounted volumes: 4776 -> 1792 bytes and 759 -> 14
allocations per heartbeat, which no longer grows with the volume count.

* seaweed-volume: mirror the in-place heartbeat report state

Same change as the Go volume server: number the heartbeats and mark the
entry already held with the pass that found the copy, instead of building
a second map of hashes and swapping it in.

The volume snapshot must leave the reporting state as it found it, so it
keeps asking through changed() while a real heartbeat marks through
record().

* volume: refuse writes to a closed volume instead of dereferencing nil

Close and Destroy leave the needle map and data backend nil, but a caller
that already holds the volume can still reach the write path, where both
are used unguarded: a write racing a volume deletion took the server down.
syncDelete has always checked; syncWrite and the batch worker had not.

Reachable before this series and now also from the inline fallback a
durable write takes when the worker has gone.

* seaweed-volume: guard the report state with one mutex, as Go does

The full-list flag and the generation that answers it have to move
together. Split across separate atomics they cannot: a request landing
between begin's two reads returns full == false with the generation it
just raised, and one landing between commit's read and its clear is
marked answered by a heartbeat that carried no list. Either way the
resend is dropped.

Neither is reachable today -- every caller reaches this through the
store's RwLock, the flag setters under a read lock and the heartbeat
build under a write lock, so they cannot interleave. The type should not
depend on that being true two files away, and Go holds a single mutex
over exactly these fields.

* test: build the servers under test to match the harness's offset size

The mixed Go/Rust suites run both servers against one dataset, so both
have to agree on the offset width. They did not: the harness built Go
with no tags, 4-byte offsets, while the Rust crate defaults to its 5bytes
feature, and the Rust server then refused the .vif the Go server had just
written -- "bytes_offset mismatch: found 4, expected 5".

Build each side to match the offset size the test binary itself was
compiled with, so a plain `go test` and one with -tags 5BytesOffset both
get a matched pair.
2026-08-21 13:04:56 -07:00

160 lines
5.4 KiB
Go

package storage
import (
"sync"
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
)
// volumeReportKey identifies one reported copy. Keyed by disk as well as id
// because a volume id can be mounted on two disks, and reporting one of them
// would leave the other's changes untold.
type volumeReportKey struct {
diskId uint32
volumeId uint32
}
// reportedVolume is what the master was told about one volume copy: the hash
// that detects change, the heartbeat pass that last found the copy held, and
// enough identity to name the volume if it departs.
type reportedVolume struct {
hash uint64
pass uint64
short *master_pb.VolumeShortInformationMessage
}
// volumeReportState remembers what the master was last told about each volume,
// so a heartbeat can carry only what moved since.
//
// It is per-connection: a server that reconnects, or reaches a different
// master, knows nothing about what that master holds and starts again from the
// full list. The zero value has told no master anything, so it sends the whole
// list until one accepts changes.
type volumeReportState struct {
mu sync.Mutex
// deltasAccepted is set once the master says it compares digests. Until
// then the whole list goes every time, which is what an older master needs.
deltasAccepted bool
fullListNeeded bool
// fullListGeneration counts requests for the whole list, so one arriving
// while a heartbeat is being built is not marked satisfied by it.
fullListGeneration uint64
// pass numbers heartbeats, so one can mark the copies it finds held without
// building a second map of them.
pass uint64
lastReported map[volumeReportKey]reportedVolume
}
// reset drops everything known about the master's view.
func (s *volumeReportState) reset() {
s.mu.Lock()
defer s.mu.Unlock()
s.deltasAccepted = false
s.fullListNeeded = true
s.fullListGeneration++
s.lastReported = nil
}
func (s *volumeReportState) acceptDeltas() {
s.mu.Lock()
defer s.mu.Unlock()
s.deltasAccepted = true
}
func (s *volumeReportState) requestFullList() {
s.mu.Lock()
defer s.mu.Unlock()
s.fullListNeeded = true
s.fullListGeneration++
}
// begin opens a heartbeat: whether it must carry the whole list, the request it
// answers, and the pass number that marks the copies it finds still held.
func (s *volumeReportState) begin() (full bool, generation uint64, pass uint64) {
s.mu.Lock()
defer s.mu.Unlock()
s.pass++
return s.fullListNeeded || !s.deltasAccepted, s.fullListGeneration, s.pass
}
// record marks one volume copy as held by the heartbeat being built, and reports
// whether the master needs telling about it. It updates the entry already held
// rather than build a second map beside it, so a server whose volumes are quiet
// allocates nothing per volume per heartbeat.
func (s *volumeReportState) record(m *master_pb.VolumeInformationMessage, hash uint64, pass uint64) bool {
key := volumeReportKey{diskId: m.DiskId, volumeId: m.Id}
s.mu.Lock()
defer s.mu.Unlock()
if previous, known := s.lastReported[key]; known {
changed := previous.hash != hash
if changed {
// Only a departure hands a short message out, and that entry leaves
// the map in the same step, so the one held here is read by no one.
fillShortInformation(previous.short, m)
}
previous.hash, previous.pass = hash, pass
s.lastReported[key] = previous
return changed
}
if s.lastReported == nil {
s.lastReported = make(map[volumeReportKey]reportedVolume)
}
short := &master_pb.VolumeShortInformationMessage{}
fillShortInformation(short, m)
s.lastReported[key] = reportedVolume{hash: hash, pass: pass, short: short}
return true
}
func fillShortInformation(short *master_pb.VolumeShortInformationMessage, m *master_pb.VolumeInformationMessage) {
short.Id = m.Id
short.Collection = m.Collection
short.ReplicaPlacement = m.ReplicaPlacement
short.Version = m.Version
short.Ttl = m.Ttl
short.DiskType = m.DiskType
short.DiskId = m.DiskId
}
// commit closes the heartbeat. Copies this pass did not find are forgotten, so
// one that comes back is reported again, and those whose volume left the server
// altogether are returned. A delta heartbeat says nothing through silence, so
// they must be named or the master keeps counting them until a digest mismatch
// buys it a full list — long enough for a busy cluster to run its free-slot
// accounting dry. A volume that moved disks is still held, so it is not a
// departure; a full list is already the whole truth, so it names none.
func (s *volumeReportState) commit(pass uint64, generation uint64, full bool) []*master_pb.VolumeShortInformationMessage {
s.mu.Lock()
defer s.mu.Unlock()
// A request that arrived while this heartbeat was being built asked about a
// later state than it carries, so it stands.
if s.fullListGeneration == generation {
s.fullListNeeded = false
}
var goneKeys []volumeReportKey
for key, prior := range s.lastReported {
if prior.pass != pass {
goneKeys = append(goneKeys, key)
}
}
if len(goneKeys) == 0 {
return nil
}
goneIds := make(map[uint32]bool, len(goneKeys))
for _, key := range goneKeys {
goneIds[key.volumeId] = true
}
for key, prior := range s.lastReported {
if prior.pass == pass {
delete(goneIds, key.volumeId)
}
}
var gone []*master_pb.VolumeShortInformationMessage
for _, key := range goneKeys {
if !full && goneIds[key.volumeId] {
gone = append(gone, s.lastReported[key].short)
}
delete(s.lastReported, key)
}
return gone
}