mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
* volume: apply a tailed size-0 record as a delete when the key exists A delete and an empty write both reach the tail receiver as a size-0 .dat record. Writing every one as a live needle turned each delete caught by volume.move's tail into a 200-empty object. Treat the record as a delete when the key already has a record on the receiver, and as an empty write otherwise. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * rust volume: apply a tailed size-0 record as a delete when the key exists Mirrors the Go tail receiver: a size-0 record for a key that already has a record here is a delete, otherwise an empty write. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * shell: volume.merge applies a size-0 record as a delete when the key exists The tail stream carries deletes as size-0 records, and volume.merge wrote each one to the merged copy as a live empty needle, so every delete in the volume came back as a 200-empty object on all replicas. A size-0 record for a key already written in the merged stream is now a delete on the target; for a new key it stays an empty write. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * shell: volume.merge reads a size-0 record as a delete only after its own replica's record Replicas append the same write at their own times, so a fresh empty write can reach the merge twice when the copies are further apart than the dedup window. Tracking keys across all replicas made the second copy a delete. Track, per source replica, the keys it has returned (duplicates the merge skips included): a size-0 record is a delete only when its own replica wrote the key before, which is the only way a replica appends a delete. A delete for a key the merged copy already deleted sends nothing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * shell: volume.merge verification counts the deletes the merge applied Applying deletes lets a correct merge hold fewer live needles than a replica that missed a delete, and the live-count check then refused it and kept the divergent replicas. Every key live on a replica is either live or deleted in the merged copy, so compare that replica's live count with the merged live count plus the keys the merge deleted. An empty merged copy is still refused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * volume: tail receiver looks a size-0 record's key up in the needle map The receiver read the needle to decide whether a size-0 record deletes an existing key. A read reports a needle past its TTL as not found, so a delete for it was written as a live empty needle, which reads ignore TTL for. Look the key up in the needle map instead (Volume.LookupNeedle). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * rust volume: tail receiver looks a size-0 record's key up in the needle map Mirror Go: write_tailed_needle read the needle to decide whether a size-0 record deletes an existing key, and a read reports a needle past its TTL as not found, so the delete became a live empty needle. Look the key up in the needle map instead (Volume::lookup_needle). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * shell: volume.merge keeps a merged delete when a late empty copy follows A replica that missed a delete can hold its copy of the empty write stamped after the other replica's delete. As that replica's first record for the key it was written, reviving the deleted key. A size-0 record now never revives a key the merged copy already deleted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * shell: volume.merge refuses more replicas than its key history has bits Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
183 lines
5.5 KiB
Go
183 lines
5.5 KiB
Go
package weed_server
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/operation"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/super_block"
|
|
)
|
|
|
|
func (vs *VolumeServer) VolumeTailSender(req *volume_server_pb.VolumeTailSenderRequest, stream volume_server_pb.VolumeServer_VolumeTailSenderServer) error {
|
|
v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
|
|
if v == nil {
|
|
return fmt.Errorf("not found volume id %d", req.VolumeId)
|
|
}
|
|
if err := v.UnavailableError(); err != nil {
|
|
return err
|
|
}
|
|
|
|
defer glog.V(1).Infof("tailing volume %d finished", v.Id)
|
|
|
|
lastTimestampNs := req.SinceNs
|
|
drainingSeconds := req.IdleTimeoutSeconds
|
|
|
|
for {
|
|
if err := v.UnavailableError(); err != nil {
|
|
return err
|
|
}
|
|
lastProcessedTimestampNs, err := sendNeedlesSince(stream, v, lastTimestampNs)
|
|
if err != nil {
|
|
glog.Infof("sendNeedlesSince: %v", err)
|
|
return fmt.Errorf("streamFollow: %w", err)
|
|
}
|
|
time.Sleep(2 * time.Second)
|
|
|
|
if req.IdleTimeoutSeconds == 0 {
|
|
lastTimestampNs = lastProcessedTimestampNs
|
|
continue
|
|
}
|
|
if lastProcessedTimestampNs == lastTimestampNs {
|
|
drainingSeconds--
|
|
if drainingSeconds <= 0 {
|
|
return nil
|
|
}
|
|
glog.V(1).Infof("tailing volume %d drains requests with %d seconds remaining", v.Id, drainingSeconds)
|
|
} else {
|
|
lastTimestampNs = lastProcessedTimestampNs
|
|
drainingSeconds = req.IdleTimeoutSeconds
|
|
glog.V(1).Infof("tailing volume %d resets draining wait time to %d seconds", v.Id, drainingSeconds)
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func sendNeedlesSince(stream volume_server_pb.VolumeServer_VolumeTailSenderServer, v *storage.Volume, lastTimestampNs uint64) (lastProcessedTimestampNs uint64, err error) {
|
|
|
|
foundOffset, isLastOne, err := v.BinarySearchByAppendAtNs(lastTimestampNs)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("fail to locate by appendAtNs %d: %s", lastTimestampNs, err)
|
|
}
|
|
|
|
// log.Printf("reading ts %d offset %d isLast %v", lastTimestampNs, foundOffset, isLastOne)
|
|
|
|
if isLastOne {
|
|
if err := v.UnavailableError(); err != nil {
|
|
return 0, err
|
|
}
|
|
// need to heart beat to the client to ensure the connection health
|
|
sendErr := stream.Send(&volume_server_pb.VolumeTailSenderResponse{IsLastChunk: true, Version: uint32(v.Version())})
|
|
return lastTimestampNs, sendErr
|
|
}
|
|
|
|
scanner := &VolumeFileScanner4Tailing{
|
|
stream: stream,
|
|
version: uint32(v.Version()),
|
|
v: v,
|
|
}
|
|
|
|
err = storage.ScanVolumeFileFrom(v.Version(), v.DataBackend, foundOffset.ToActualOffset(), scanner)
|
|
|
|
return scanner.lastProcessedTimestampNs, err
|
|
|
|
}
|
|
|
|
func (vs *VolumeServer) VolumeTailReceiver(ctx context.Context, req *volume_server_pb.VolumeTailReceiverRequest) (*volume_server_pb.VolumeTailReceiverResponse, error) {
|
|
if err := vs.checkGrpcAdminAuth(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resp := &volume_server_pb.VolumeTailReceiverResponse{}
|
|
|
|
if !vs.AllowUntrustedRemoteEndpoints {
|
|
if err := validateReplicaTarget(ctx, req.SourceVolumeServer); err != nil {
|
|
return resp, fmt.Errorf("invalid source volume server %s: %w", req.SourceVolumeServer, err)
|
|
}
|
|
}
|
|
|
|
v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
|
|
if v == nil {
|
|
return resp, fmt.Errorf("receiver not found volume id %d", req.VolumeId)
|
|
}
|
|
|
|
defer glog.V(1).Infof("receive tailing volume %d finished", v.Id)
|
|
|
|
return resp, operation.TailVolumeFromSource(pb.ServerAddress(req.SourceVolumeServer), v.Id, req.SinceNs, int(req.IdleTimeoutSeconds), func(n *needle.Needle) error {
|
|
return writeTailedNeedle(vs.store, v.Id, n)
|
|
}, vs.grpcDialOption, vs.guardedGrpcDialOption(req.SourceVolumeServer))
|
|
|
|
}
|
|
|
|
// writeTailedNeedle applies one tailed .dat record. A delete and an empty
|
|
// write both arrive as a size-0 record; it is a delete when the key already
|
|
// has a record here, and an empty write otherwise.
|
|
func writeTailedNeedle(store *storage.Store, vid needle.VolumeId, n *needle.Needle) error {
|
|
if v := store.GetVolume(vid); v != nil && n.Size == 0 {
|
|
size, found, err := v.LookupNeedle(n.Id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if found {
|
|
if !size.IsDeleted() {
|
|
_, err = store.DeleteVolumeNeedle(vid, n, false)
|
|
}
|
|
return err
|
|
}
|
|
}
|
|
_, err := store.WriteVolumeNeedle(vid, n, false, false)
|
|
return err
|
|
}
|
|
|
|
// generate the volume idx
|
|
type VolumeFileScanner4Tailing struct {
|
|
stream volume_server_pb.VolumeServer_VolumeTailSenderServer
|
|
lastProcessedTimestampNs uint64
|
|
version uint32
|
|
v *storage.Volume
|
|
}
|
|
|
|
func (scanner *VolumeFileScanner4Tailing) VisitSuperBlock(superBlock super_block.SuperBlock) error {
|
|
return nil
|
|
|
|
}
|
|
func (scanner *VolumeFileScanner4Tailing) ReadNeedleBody() bool {
|
|
return true
|
|
}
|
|
|
|
func (scanner *VolumeFileScanner4Tailing) VisitNeedle(n *needle.Needle, offset int64, needleHeader, needleBody []byte) error {
|
|
if err := scanner.v.UnavailableError(); err != nil {
|
|
return err
|
|
}
|
|
isLastChunk := false
|
|
|
|
// need to send body by chunks
|
|
for i := 0; i < len(needleBody); i += BufferSizeLimit {
|
|
stopOffset := i + BufferSizeLimit
|
|
if stopOffset >= len(needleBody) {
|
|
isLastChunk = true
|
|
stopOffset = len(needleBody)
|
|
}
|
|
|
|
sendErr := scanner.stream.Send(&volume_server_pb.VolumeTailSenderResponse{
|
|
NeedleHeader: needleHeader,
|
|
NeedleBody: needleBody[i:stopOffset],
|
|
IsLastChunk: isLastChunk,
|
|
Version: scanner.version,
|
|
})
|
|
if sendErr != nil {
|
|
return sendErr
|
|
}
|
|
}
|
|
|
|
scanner.lastProcessedTimestampNs = n.AppendAtNs
|
|
return nil
|
|
}
|