seaweedfs/weed/server/volume_grpc_tail.go

118 lines
3.5 KiB
Go
Raw Normal View History

package weed_server
import (
2019-04-21 02:35:20 +08:00
"context"
"fmt"
"time"
"github.com/chrislusf/seaweedfs/weed/glog"
2019-04-21 02:35:20 +08:00
"github.com/chrislusf/seaweedfs/weed/operation"
"github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
"github.com/chrislusf/seaweedfs/weed/storage"
2019-04-19 12:43:36 +08:00
"github.com/chrislusf/seaweedfs/weed/storage/needle"
)
2019-04-21 02:35:20 +08:00
func (vs *VolumeServer) VolumeTailSender(req *volume_server_pb.VolumeTailSenderRequest, stream volume_server_pb.VolumeServer_VolumeTailSenderServer) error {
2019-04-19 12:43:36 +08:00
v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
if v == nil {
return fmt.Errorf("not found volume id %d", req.VolumeId)
}
2019-04-19 02:05:02 +08:00
defer glog.V(1).Infof("tailing volume %d finished", v.Id)
lastTimestampNs := req.SinceNs
2019-04-21 03:05:28 +08:00
drainingSeconds := req.IdleTimeoutSeconds
for {
2019-04-19 02:05:02 +08:00
lastProcessedTimestampNs, err := sendNeedlesSince(stream, v, lastTimestampNs)
if err != nil {
glog.Infof("sendNeedlesSince: %v", err)
return fmt.Errorf("streamFollow: %v", err)
}
time.Sleep(2 * time.Second)
2019-04-21 03:05:28 +08:00
if req.IdleTimeoutSeconds == 0 {
2019-04-19 10:22:13 +08:00
lastTimestampNs = lastProcessedTimestampNs
2019-04-19 02:05:02 +08:00
continue
}
if lastProcessedTimestampNs == lastTimestampNs {
drainingSeconds--
if drainingSeconds <= 0 {
return nil
}
2019-04-19 02:05:02 +08:00
glog.V(1).Infof("tailing volume %d drains requests with %d seconds remaining", v.Id, drainingSeconds)
} else {
lastTimestampNs = lastProcessedTimestampNs
2019-04-21 03:05:28 +08:00
drainingSeconds = req.IdleTimeoutSeconds
2019-04-19 02:05:02 +08:00
glog.V(1).Infof("tailing volume %d resets draining wait time to %d seconds", v.Id, drainingSeconds)
}
2019-04-19 02:05:02 +08:00
}
}
2019-04-21 02:35:20 +08:00
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)
}
2019-04-19 02:05:02 +08:00
// log.Printf("reading ts %d offset %d isLast %v", lastTimestampNs, foundOffset, isLastOne)
if isLastOne {
2019-04-19 02:05:02 +08:00
// need to heart beat to the client to ensure the connection health
2019-04-21 02:35:20 +08:00
sendErr := stream.Send(&volume_server_pb.VolumeTailSenderResponse{IsLastChunk: true})
2019-04-19 02:05:02 +08:00
return lastTimestampNs, sendErr
}
err = storage.ScanVolumeFileNeedleFrom(v.Version(), v.DataFile(), foundOffset.ToAcutalOffset(), func(needleHeader, needleBody []byte, needleAppendAtNs uint64) error {
2019-04-19 10:22:13 +08:00
isLastChunk := false
// need to send body by chunks
2019-05-28 02:59:03 +08:00
for i := 0; i < len(needleBody); i += BufferSizeLimit {
stopOffset := i + BufferSizeLimit
2019-04-19 10:22:13 +08:00
if stopOffset >= len(needleBody) {
isLastChunk = true
stopOffset = len(needleBody)
}
2019-04-21 02:35:20 +08:00
sendErr := stream.Send(&volume_server_pb.VolumeTailSenderResponse{
2019-04-19 10:22:13 +08:00
NeedleHeader: needleHeader,
NeedleBody: needleBody[i:stopOffset],
IsLastChunk: isLastChunk,
})
if sendErr != nil {
return sendErr
}
}
lastProcessedTimestampNs = needleAppendAtNs
return nil
})
return
}
2019-04-21 02:35:20 +08:00
func (vs *VolumeServer) VolumeTailReceiver(ctx context.Context, req *volume_server_pb.VolumeTailReceiverRequest) (*volume_server_pb.VolumeTailReceiverResponse, error) {
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)
2019-04-21 03:05:28 +08:00
return resp, operation.TailVolumeFromSource(req.SourceVolumeServer, vs.grpcDialOption, v.Id, req.SinceNs, int(req.IdleTimeoutSeconds), func(n *needle.Needle) error {
2019-07-18 14:43:48 +08:00
_, _, err := vs.store.WriteVolumeNeedle(v.Id, n)
2019-04-21 02:35:20 +08:00
return err
})
}