mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-09-20 13:30:46 +02:00
* pb: stop exiting the process on malformed server addresses ServerToGrpcAddress and GrpcAddressToServerAddress called glog.Fatalf when hostAndPort could not parse the port, which os.Exit(255)ed the whole process. A caller-supplied copy or tail source address reached this path synchronously in the serving goroutine, so one anonymous VolumeCopy with a non-numeric port terminated the volume server. Log the parse error and return the input unchanged instead: the dial or request that consumes the address then fails as an ordinary error. * volume: validate copy and tail source addresses before dialing VolumeCopy, VolumeEcShardsCopy and VolumeTailReceiver dial a caller-supplied source address (SourceDataNode / SourceVolumeServer) with no endpoint validation, so an anonymous caller could aim the volume server at loopback, link-local (cloud metadata) or other unintended destinations and read dial behavior back as a connectivity oracle. Apply the same peer-target deny list FetchAndWriteNeedle uses for replica targets: the source must be a bare host:port whose host is not loopback, link-local or unspecified; cluster peers stay reachable on private networks, and -volume.allowUntrustedRemoteEndpoints opts out. The loopback-using copy tests set the flag to keep exercising the copy path in process. * rust volume: validate copy and tail source addresses before dialing Mirror the Go guard on the Rust volume server: volume_copy, volume_ec_shards_copy and volume_tail_receiver dial a caller-supplied source address, so run it through validate_replica_target first (bare host:port; no loopback, link-local or unspecified hosts; private peers stay allowed). --volume.allowUntrustedRemoteEndpoints opts out; the test fixture and the Rust test-cluster launcher set it so loopback sources in tests keep working. * volume: pin validated copy/tail source addresses at dial time validateReplicaTarget resolves the source hostname once, but the gRPC client resolved it again at connect, leaving a DNS-rebinding window for hostname sources. The copy and tail source dials now run through the same guardedDialerPolicy the remote-storage path uses, so every resolved address is re-checked against the replica deny list (private peers allowed) immediately before the TCP connect. guardedDialerPolicy also moves to util.OutboundDialContext so the guarded path keeps the -ip.bind source binding the default gRPC dialer had. The Rust volume server mirrors this with connect_guarded, a tonic connector that resolves, re-checks each address, and connects to the first passing IP; handlers use it whenever the untrusted-endpoint opt-out is off. A handler-level test now exercises the enabled validation branches for all three source-taking RPCs. * pb: return empty server address for malformed grpc addresses GrpcAddressToServerAddress used to return the unparseable input on a hostAndPort failure, so a malformed raft address (e.g. "host:abc") flowed into admin dashboard master maps unchanged. Return an empty string instead, skip empty conversions at the two raft-cluster merge sites, and drop the now-stale comment about the fatal exit the earlier commit removed. * test: opt erasure-coding loopback clusters out of the remote endpoint guard The erasure-coding suites drive VolumeEcShardsCopy / VolumeCopy between volume servers bound to 127.0.0.1, which the copy/tail source guard now rejects by default. Pass -volume.allowUntrustedRemoteEndpoints to the test volume launches, matching what the volume_server framework harnesses already do. * admin: only claim fallback master leadership on an empty raft response A nonempty RaftListClusterServers response whose entries were all rejected left masterMap empty, so the fallback marked the reachable current master as leader the same way a genuinely empty (non-raft) response does. Track whether the successful response returned zero servers and only promote the fallback master then.
150 lines
4.6 KiB
Go
150 lines
4.6 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)
|
|
}
|
|
|
|
defer glog.V(1).Infof("tailing volume %d finished", v.Id)
|
|
|
|
lastTimestampNs := req.SinceNs
|
|
drainingSeconds := req.IdleTimeoutSeconds
|
|
|
|
for {
|
|
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 {
|
|
// 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()),
|
|
}
|
|
|
|
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 {
|
|
_, err := vs.store.WriteVolumeNeedle(v.Id, n, false, false)
|
|
return err
|
|
}, vs.grpcDialOption, vs.guardedGrpcDialOption(req.SourceVolumeServer))
|
|
|
|
}
|
|
|
|
// generate the volume idx
|
|
type VolumeFileScanner4Tailing struct {
|
|
stream volume_server_pb.VolumeServer_VolumeTailSenderServer
|
|
lastProcessedTimestampNs uint64
|
|
version uint32
|
|
}
|
|
|
|
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 {
|
|
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
|
|
}
|