mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-08-17 12:46:59 +00:00
* volume: gate the admin RPCs that only shell and workers call checkGrpcAdminAuth covered 19 of the 48 VolumeServer RPCs, so an operator who sets -whiteList expecting it to cover the gRPC surface gets partial coverage. Extend it to ten that mutate state and are only ever called by the shell or a worker: SetState, VolumeCopy, the EC generate/rebuild/copy/unmount/to-volume pair, both tier moves, and VolumeTailReceiver. That is safe because the same callers already reach gated RPCs today -- VolumeMarkReadonly, VacuumVolume*, VolumeEcShardsDelete, VolumeDelete -- so a whitelist deployment already lists those hosts. Nothing here is on a master or peer path, which is what made the earlier fail-closed gate break multi-host clusters. The split is by caller rather than by blast radius: the guard matches a peer IP against the whitelist, and a whitelist holds masters, shell hosts and workers, not every peer volume server. Gating a call one volume server makes to another would break replication, EC and tiering, so those stay open. Two test fakes embedded a nil grpc.ServerStream and only implemented Send; they now implement Context, which the streaming RPCs read to authorize. * volume: fail the build when a gRPC method skips the admin gate The admin gate is an opt-in list in a 48-method service, which is how it drifted down to covering 19 of them: nothing tied adding an RPC to deciding whether it needed the gate. Parse volume_server.proto, walk the AST of every *VolumeServer method, and require each RPC to either call checkGrpcAdminAuth or appear in ungatedVolumeServerRPCs with the reason it stays open. A stale entry naming an RPC that no longer exists fails too, so the list can't quietly stop exempting anything. The exemptions are the cluster-internal calls -- replica sync, EC shard distribution, vacuum reads, backup, tailing -- plus the read-only and liveness RPCs. Closing the cluster-internal ones needs a peer identity rather than an IP whitelist; recording them here makes that a visible decision instead of an omission. The AST walk also corrects the count: a line-window scan credits VacuumVolumeCheck and VolumeServerStatus with a neighbouring function's guard.
144 lines
4.3 KiB
Go
144 lines
4.3 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{}
|
|
|
|
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), vs.grpcDialOption, v.Id, req.SinceNs, int(req.IdleTimeoutSeconds), func(n *needle.Needle) error {
|
|
_, err := vs.store.WriteVolumeNeedle(v.Id, n, false, false)
|
|
return err
|
|
})
|
|
|
|
}
|
|
|
|
// 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
|
|
}
|