mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
* filer, mount: read a manifest's chunks only within its span Until #13042, a truncate through a manifest only shortened the manifest chunk; the chunks it holds stayed whole. Reads resolve those chunks at their own offsets and sizes, so once such a file grows over the cut again without writing it, by an extending truncate or a write past the end, the cut bytes come back where zeros belong. #13042 keeps new truncates from leaving such manifests, but files a mount truncated between #12462 and #13042 still have them. Reproduced with weed mount on a local cluster: a mount from before #13042 appends 700 lines with one close() each, so the first 256 fold into a manifest, and truncates the file to 100 lines; a mount from current master then extends it past its old end with ftruncate. The extended range read 8,580 bytes of the cut lines instead of zeros, through that mount and through the filer. Reads now cut each manifest's chunks to the manifest's span: ResolveChunkManifestForRead, used by NonOverlappingVisibleIntervals (filer and S3 streaming, WebDAV, the mount's merge reads), the S3 ranged reads and the mount's peer reads, and ChunkGroup for the mount's reads. A manifest made by MaybeManifestize spans its chunks exactly, so only shortened ones change. ResolveChunkManifest still returns the chunks whole: MinusChunks, deletions and the other chunk-list comparisons need every chunk a manifest references, or the chunks past the cut would never be deleted. * sw-client: read a manifest's chunks only within its span Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * java client: read a manifest's chunks only within its span Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: drop PR numbers from the manifest span comments Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: fold ResolveChunkManifestForRead into ResolveChunkManifest ResolveChunkManifest takes cutToSpan: the read paths pass true, the deletion and chunk-list comparison paths pass false. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: cut manifests to their span when flattening an object's chunks A full-object SSE GET, CopyObject and UploadPartCopy flatten the source's manifests into a plain chunk list, which drops each manifest's span, so the chunks a truncate left past a shortened manifest came back as object data. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * shell: purge a volume.fsck path only for chunks within a manifest's span A chunk past a shortened manifest's span is still referenced but never read, so a missing volume under it loses nothing. The purge decision now uses the span-cut chunks while the inventory keeps every reference. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * shell: cut manifests to their span in fs.distribute_chunks The entry is rewritten with the resolved chunks as a flat list or a new manifest that spans them, so chunks past a shortened manifest's span became readable again. The filer's update still deletes them, since it compares against the old entry's uncut chunks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * shell: verify only the chunks within a manifest's span in fs.verify A chunk past a shortened manifest's span is never read, so a missing needle under it failed the entry and, with -pruneEntries, deleted a file whose readable bytes were intact. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * shell: mark the chunks past a manifest's span instead of allowing the visible ones The purge gate now skips only the resolved chunks that the span cut drops, so chunks that come from outside the manifests keep their purge behavior. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * shell: skip the purge for a missing past-span chunk on a listed volume volume.fsck -findMissingChunksInFiler queued a path for deletion when a listed volume's index lacked any chunk the uncut inventory recorded, including a chunk past a shortened manifest's span that no read reaches. Record those chunks per path during the filer walk and let only a missing chunk within the span queue the purge. The path is still reported. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
327 lines
12 KiB
Go
327 lines
12 KiB
Go
package mount
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"io"
|
|
"sort"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mount_peer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
// errPeerReadSkipped signals that the read didn't go through the peer
|
|
// path for a benign reason (local cache hit, no peer owner, etc.) rather
|
|
// than a genuine failure. Callers fall through to the volume path just
|
|
// as they would for a real error, but suppress the "peer read failed"
|
|
// log that would otherwise mislead operators into thinking peer sharing
|
|
// is broken.
|
|
var errPeerReadSkipped = fmt.Errorf("peer read skipped (not an error)")
|
|
|
|
// peerLookupTimeout bounds the ChunkLookup RPC. Short because the result
|
|
// is consumed on the read critical path.
|
|
const peerLookupTimeout = 500 * time.Millisecond
|
|
|
|
// peerFetchTimeout bounds a single FetchChunk stream; on expiry we fall
|
|
// through to the next holder or the volume server.
|
|
const peerFetchTimeout = 5 * time.Second
|
|
|
|
// maxPeerFetchChunkBytes caps how much we will accept from a single
|
|
// FetchChunk stream. gRPC per-message size is already capped by the
|
|
// server option; this is belt-and-suspenders against a runaway peer.
|
|
const maxPeerFetchChunkBytes = 64 * 1024 * 1024
|
|
|
|
// tryPeerRead attempts to satisfy a read from a peer mount's chunk cache.
|
|
// Returns (bytesRead, modifiedTsNs, nil) on success. On any failure it
|
|
// returns (0, 0, err) so the caller falls through to
|
|
// entryChunkGroup.ReadDataAt (the volume-server path).
|
|
//
|
|
// Flow:
|
|
// 1. Resolve the offset's leaf chunk (flattening manifests).
|
|
// 2. Ask the HRW owner mount for current holders via ChunkLookup.
|
|
// 3. For each holder (LRU order from PR #5), open a FetchChunk stream,
|
|
// assemble frames into a size-bounded buffer, and verify MD5
|
|
// end-to-end against FileChunk.ETag.
|
|
// 4. On success, populate chunk_cache and enqueue an announce so
|
|
// other mounts can discover us as a new holder.
|
|
//
|
|
// chunks is a snapshot captured under the LockedEntry lock by the caller.
|
|
func (fh *FileHandle) tryPeerRead(ctx context.Context, fileSize int64, buff []byte, offset int64, chunks []*filer_pb.FileChunk) (int64, int64, error) {
|
|
if fh.wfs.peerRegistrar == nil || fh.wfs.peerConnPool == nil {
|
|
return 0, 0, fmt.Errorf("peer sharing not configured")
|
|
}
|
|
|
|
// Resolve offset → leaf chunk, flattening any manifest indirection.
|
|
readStop := offset + int64(len(buff))
|
|
if readStop > fileSize {
|
|
readStop = fileSize
|
|
}
|
|
dataChunks, _, err := filer.ResolveChunkManifest(ctx, fh.wfs.LookupFn(), chunks, offset, readStop, fh.wfs.CacheInvalidator(), true)
|
|
if err != nil {
|
|
return 0, 0, fmt.Errorf("resolve manifest: %w", err)
|
|
}
|
|
targetChunk, chunkOffset := findChunkContaining(dataChunks, offset)
|
|
if targetChunk == nil {
|
|
return 0, 0, fmt.Errorf("no leaf chunk for offset %d", offset)
|
|
}
|
|
// Reject reads that cross a chunk boundary. We only fetch one chunk
|
|
// here, and the caller (readFromChunks) maps a short non-error
|
|
// return to "success, zero-fill the rest" — which would silently
|
|
// corrupt reads that should actually span two chunks. Bail so the
|
|
// fallback ReadDataAt path can handle the multi-chunk case
|
|
// correctly. See weedfs_file_read.go short-read semantics.
|
|
chunkEnd := targetChunk.Offset + int64(targetChunk.Size)
|
|
if readStop > chunkEnd {
|
|
return 0, 0, errPeerReadSkipped
|
|
}
|
|
|
|
// Fail fast when the chunk is already cached locally: the fallback
|
|
// ReadDataAt path will satisfy the read from chunkCache with no RPCs,
|
|
// so dialing a peer (ChunkLookup + FetchChunk) would be pure overhead.
|
|
if fh.wfs.chunkCache != nil && fh.wfs.chunkCache.IsInCache(targetChunk.FileId, true) {
|
|
return 0, 0, errPeerReadSkipped
|
|
}
|
|
|
|
selfAddr := ""
|
|
if fh.wfs.peerGrpcServer != nil {
|
|
selfAddr = fh.wfs.peerGrpcServer.SelfAddr()
|
|
}
|
|
|
|
owner := fh.wfs.peerRegistrar.OwnerFor(targetChunk.FileId)
|
|
if owner == "" || owner == selfAddr {
|
|
return 0, 0, fmt.Errorf("no peer owner for fid %s", targetChunk.FileId)
|
|
}
|
|
|
|
holders, err := peerLookupHolders(ctx, fh.wfs.peerConnPool.Dialer(), owner, targetChunk.FileId)
|
|
if err != nil {
|
|
return 0, 0, fmt.Errorf("peer lookup: %w", err)
|
|
}
|
|
if len(holders) == 0 {
|
|
return 0, 0, fmt.Errorf("no peer holder for fid %s", targetChunk.FileId)
|
|
}
|
|
|
|
// Re-rank holders by locality (same rack > same DC > elsewhere), keeping
|
|
// the server's LRU order stable within each bucket. The server caps the
|
|
// list at maxLookupHolders, so this is always a small N.
|
|
sortHoldersByLocality(holders, fh.wfs.option.PeerDataCenter, fh.wfs.option.PeerRack)
|
|
|
|
dial := fh.wfs.peerConnPool.Dialer()
|
|
for _, h := range holders {
|
|
if h.addr == selfAddr {
|
|
continue
|
|
}
|
|
data, ferr := fetchChunkFromPeer(ctx, dial, h.addr, targetChunk.FileId, targetChunk.Size, targetChunk.ETag)
|
|
if ferr != nil {
|
|
glog.V(2).Infof("peer-fetch %s from %s: %v", targetChunk.FileId, h.addr, ferr)
|
|
continue
|
|
}
|
|
if fh.wfs.chunkCache != nil {
|
|
fh.wfs.chunkCache.SetChunk(targetChunk.FileId, data)
|
|
}
|
|
if fh.wfs.peerAnnouncer != nil {
|
|
fh.wfs.peerAnnouncer.EnqueueAnnounce(targetChunk.FileId)
|
|
}
|
|
if chunkOffset >= int64(len(data)) {
|
|
return 0, 0, fmt.Errorf("peer returned short chunk")
|
|
}
|
|
// Cap the copy to whichever is smaller: the caller's buffer, or
|
|
// the remaining bytes before logical EOF (readStop already
|
|
// clamped to fileSize). FileChunk.Size can legitimately exceed
|
|
// the logical file length when the last chunk is partially
|
|
// written and the filer stored the full padded buffer, so a
|
|
// naïve copy(buff, data[chunkOffset:]) would return bytes past
|
|
// EOF. The caller treats a short non-error return as success
|
|
// and zero-fills the tail, so returning too many bytes here is
|
|
// a correctness bug, not just wasted I/O.
|
|
remaining := readStop - offset
|
|
available := int64(len(data)) - chunkOffset
|
|
maxCopy := int64(len(buff))
|
|
if remaining < maxCopy {
|
|
maxCopy = remaining
|
|
}
|
|
if available < maxCopy {
|
|
maxCopy = available
|
|
}
|
|
copied := copy(buff[:maxCopy], data[chunkOffset:chunkOffset+maxCopy])
|
|
return int64(copied), targetChunk.ModifiedTsNs, nil
|
|
}
|
|
return 0, 0, fmt.Errorf("no peer served fid %s", targetChunk.FileId)
|
|
}
|
|
|
|
func findChunkContaining(chunks []*filer_pb.FileChunk, offset int64) (*filer_pb.FileChunk, int64) {
|
|
for _, c := range chunks {
|
|
start := c.Offset
|
|
stop := c.Offset + int64(c.Size)
|
|
if offset >= start && offset < stop {
|
|
return c, offset - start
|
|
}
|
|
}
|
|
return nil, 0
|
|
}
|
|
|
|
// peerHolder is a holder entry carried through the fetcher: addr for
|
|
// dialing plus the DC/Rack labels the owner recorded at announce time, so
|
|
// the fetcher can re-rank by its own locality before dialing.
|
|
type peerHolder struct {
|
|
addr string
|
|
dc string
|
|
rack string
|
|
}
|
|
|
|
// peerLookupHolders calls ChunkLookup on the given HRW owner and returns
|
|
// the holders in the server-reported (LRU) order along with their locality
|
|
// labels. The pooled dialer is passed in from the caller.
|
|
func peerLookupHolders(ctx context.Context, dial MountPeerDialer, ownerAddr, fid string) ([]peerHolder, error) {
|
|
client, closeFn, err := dial(ctx, ownerAddr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer closeFn()
|
|
|
|
callCtx, cancel := context.WithTimeout(ctx, peerLookupTimeout)
|
|
defer cancel()
|
|
resp, err := client.ChunkLookup(callCtx, &mount_peer_pb.ChunkLookupRequest{FileIds: []string{fid}})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
set, ok := resp.PeersByFid[fid]
|
|
if !ok || set == nil {
|
|
return nil, nil
|
|
}
|
|
out := make([]peerHolder, 0, len(set.Peers))
|
|
for _, p := range set.Peers {
|
|
out = append(out, peerHolder{addr: p.PeerAddr, dc: p.DataCenter, rack: p.Rack})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// localityBucket scores how "close" a peer is to self. Lower is better:
|
|
//
|
|
// 0 = same rack in same DC (shortest hop)
|
|
// 1 = same DC, different rack (cross-rack but still intra-DC)
|
|
// 2 = different DC, or unknown (anything else)
|
|
//
|
|
// Missing labels on either side fall to bucket 2 — we don't claim locality
|
|
// we can't prove.
|
|
func localityBucket(selfDC, selfRack, peerDC, peerRack string) int {
|
|
if selfDC == "" || peerDC == "" || selfDC != peerDC {
|
|
return 2
|
|
}
|
|
if selfRack != "" && peerRack != "" && selfRack == peerRack {
|
|
return 0
|
|
}
|
|
return 1
|
|
}
|
|
|
|
// sortHoldersByLocality stable-sorts holders so the most local peers are
|
|
// tried first. The server-returned order is LRU (freshest holder first),
|
|
// and sort.SliceStable preserves that ordering within each locality
|
|
// bucket, so among equally-local peers we still prefer the freshest.
|
|
func sortHoldersByLocality(holders []peerHolder, selfDC, selfRack string) {
|
|
if len(holders) < 2 {
|
|
return
|
|
}
|
|
sort.SliceStable(holders, func(i, j int) bool {
|
|
return localityBucket(selfDC, selfRack, holders[i].dc, holders[i].rack) <
|
|
localityBucket(selfDC, selfRack, holders[j].dc, holders[j].rack)
|
|
})
|
|
}
|
|
|
|
// fetchChunkFromPeer server-streams a chunk from the given peer and
|
|
// verifies the MD5 of the assembled bytes against expectedETag.
|
|
//
|
|
// expectedSize (when > 0) is the authoritative chunk length from the
|
|
// filer entry; we pre-allocate exactly that to avoid slice growth and
|
|
// reject streams that overshoot it. A zero expectedSize falls back to
|
|
// maxPeerFetchChunkBytes as a safety ceiling.
|
|
func fetchChunkFromPeer(ctx context.Context, dial MountPeerDialer, peerAddr, fid string, expectedSize uint64, expectedETag string) ([]byte, error) {
|
|
client, closeFn, err := dial(ctx, peerAddr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer closeFn()
|
|
|
|
callCtx, cancel := context.WithTimeout(ctx, peerFetchTimeout)
|
|
defer cancel()
|
|
|
|
stream, err := client.FetchChunk(callCtx, &mount_peer_pb.FetchChunkRequest{
|
|
FileId: fid,
|
|
ExpectedEtag: expectedETag,
|
|
ExpectedSize: expectedSize,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// capHint pre-sizes the assembly buffer from the filer-reported chunk
|
|
// size. When the caller didn't know the size (expectedSize == 0), we
|
|
// let append grow the buffer rather than reserve the 64 MiB ceiling —
|
|
// typical chunks are a few MiB, and the maxPeerFetchChunkBytes check
|
|
// during Recv is the real safety ceiling.
|
|
capHint := expectedSize
|
|
if capHint > maxPeerFetchChunkBytes {
|
|
capHint = maxPeerFetchChunkBytes
|
|
}
|
|
buf := make([]byte, 0, capHint)
|
|
|
|
for {
|
|
resp, rerr := stream.Recv()
|
|
if rerr == io.EOF {
|
|
break
|
|
}
|
|
if rerr != nil {
|
|
if status.Code(rerr) == codes.NotFound {
|
|
return nil, fmt.Errorf("peer not cached")
|
|
}
|
|
return nil, rerr
|
|
}
|
|
if len(buf)+len(resp.Data) > int(maxPeerFetchChunkBytes) {
|
|
return nil, fmt.Errorf("peer response exceeds max chunk size %d", maxPeerFetchChunkBytes)
|
|
}
|
|
buf = append(buf, resp.Data...)
|
|
}
|
|
|
|
if expectedSize > 0 && uint64(len(buf)) != expectedSize {
|
|
return nil, fmt.Errorf("peer returned %d bytes, expected %d", len(buf), expectedSize)
|
|
}
|
|
|
|
// Per-chunk integrity check — the peer is treated as untrusted.
|
|
// FileChunk.ETag is UploadResult.ContentMd5, which is base64 of the
|
|
// raw 16-byte MD5 (see weed/operation/upload_content.go:64 and
|
|
// filer's ETagChunks which decodes via util.Base64Md5ToBytes).
|
|
// Compare raw digests so we don't reject every valid peer response
|
|
// because of an encoding mismatch.
|
|
if expectedETag != "" {
|
|
got := util.Md5(buf)
|
|
if !etagMatchesMD5(expectedETag, got) {
|
|
return nil, fmt.Errorf("etag mismatch: peer=%s got=%x want=%s", peerAddr, got, expectedETag)
|
|
}
|
|
}
|
|
return buf, nil
|
|
}
|
|
|
|
// etagMatchesMD5 compares a stored FileChunk.ETag string to a raw 16-byte
|
|
// MD5 digest. FileChunk.ETag is produced as base64(md5) on upload, but
|
|
// some older chunks (and tests) use hex(md5). Accept either.
|
|
func etagMatchesMD5(etag string, rawMD5 []byte) bool {
|
|
if len(rawMD5) != md5.Size {
|
|
return false
|
|
}
|
|
if dec := util.Base64Md5ToBytes(etag); len(dec) == md5.Size {
|
|
return bytes.Equal(dec, rawMD5)
|
|
}
|
|
if dec, err := hex.DecodeString(etag); err == nil && len(dec) == md5.Size {
|
|
return bytes.Equal(dec, rawMD5)
|
|
}
|
|
return false
|
|
}
|