2019-04-18 07:18:29 +00:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
2019-04-20 18:35:20 +00:00
|
|
|
"context"
|
2019-04-18 07:18:29 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2020-11-17 06:26:58 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util/log"
|
2019-04-20 18:35:20 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/operation"
|
2019-04-18 07:18:29 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage"
|
2019-04-19 04:43:36 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
2019-12-23 20:48:20 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/super_block"
|
2019-04-18 07:18:29 +00:00
|
|
|
)
|
|
|
|
|
2019-04-20 18:35:20 +00:00
|
|
|
func (vs *VolumeServer) VolumeTailSender(req *volume_server_pb.VolumeTailSenderRequest, stream volume_server_pb.VolumeServer_VolumeTailSenderServer) error {
|
2019-04-18 07:18:29 +00:00
|
|
|
|
2019-04-19 04:43:36 +00:00
|
|
|
v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
|
2019-04-18 07:18:29 +00:00
|
|
|
if v == nil {
|
|
|
|
return fmt.Errorf("not found volume id %d", req.VolumeId)
|
|
|
|
}
|
|
|
|
|
2020-11-17 06:26:58 +00:00
|
|
|
defer log.Debugf("tailing volume %d finished", v.Id)
|
2019-04-18 18:05:02 +00:00
|
|
|
|
2019-04-18 07:18:29 +00:00
|
|
|
lastTimestampNs := req.SinceNs
|
2019-04-20 19:05:28 +00:00
|
|
|
drainingSeconds := req.IdleTimeoutSeconds
|
2019-04-18 07:18:29 +00:00
|
|
|
|
|
|
|
for {
|
2019-04-18 18:05:02 +00:00
|
|
|
lastProcessedTimestampNs, err := sendNeedlesSince(stream, v, lastTimestampNs)
|
|
|
|
if err != nil {
|
2020-11-17 06:26:58 +00:00
|
|
|
log.Infof("sendNeedlesSince: %v", err)
|
2019-04-18 18:05:02 +00:00
|
|
|
return fmt.Errorf("streamFollow: %v", err)
|
|
|
|
}
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
2019-04-20 19:05:28 +00:00
|
|
|
if req.IdleTimeoutSeconds == 0 {
|
2019-04-19 02:22:13 +00:00
|
|
|
lastTimestampNs = lastProcessedTimestampNs
|
2019-04-18 18:05:02 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if lastProcessedTimestampNs == lastTimestampNs {
|
|
|
|
drainingSeconds--
|
|
|
|
if drainingSeconds <= 0 {
|
|
|
|
return nil
|
2019-04-18 07:18:29 +00:00
|
|
|
}
|
2020-11-17 06:26:58 +00:00
|
|
|
log.Debugf("tailing volume %d drains requests with %d seconds remaining", v.Id, drainingSeconds)
|
2019-04-18 18:05:02 +00:00
|
|
|
} else {
|
|
|
|
lastTimestampNs = lastProcessedTimestampNs
|
2019-04-20 19:05:28 +00:00
|
|
|
drainingSeconds = req.IdleTimeoutSeconds
|
2020-11-17 06:26:58 +00:00
|
|
|
log.Debugf("tailing volume %d resets draining wait time to %d seconds", v.Id, drainingSeconds)
|
2019-04-18 07:18:29 +00:00
|
|
|
}
|
2019-04-18 18:05:02 +00:00
|
|
|
|
2019-04-18 07:18:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-20 18:35:20 +00:00
|
|
|
func sendNeedlesSince(stream volume_server_pb.VolumeServer_VolumeTailSenderServer, v *storage.Volume, lastTimestampNs uint64) (lastProcessedTimestampNs uint64, err error) {
|
2019-04-18 07:18:29 +00:00
|
|
|
|
|
|
|
foundOffset, isLastOne, err := v.BinarySearchByAppendAtNs(lastTimestampNs)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("fail to locate by appendAtNs %d: %s", lastTimestampNs, err)
|
|
|
|
}
|
|
|
|
|
2019-04-18 18:05:02 +00:00
|
|
|
// log.Printf("reading ts %d offset %d isLast %v", lastTimestampNs, foundOffset, isLastOne)
|
|
|
|
|
2019-04-18 07:18:29 +00:00
|
|
|
if isLastOne {
|
2019-04-18 18:05:02 +00:00
|
|
|
// need to heart beat to the client to ensure the connection health
|
2019-04-20 18:35:20 +00:00
|
|
|
sendErr := stream.Send(&volume_server_pb.VolumeTailSenderResponse{IsLastChunk: true})
|
2019-04-18 18:05:02 +00:00
|
|
|
return lastTimestampNs, sendErr
|
2019-04-18 07:18:29 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 07:50:30 +00:00
|
|
|
scanner := &VolumeFileScanner4Tailing{
|
2019-10-22 07:50:50 +00:00
|
|
|
stream: stream,
|
2019-10-22 07:50:30 +00:00
|
|
|
}
|
2019-04-18 07:18:29 +00:00
|
|
|
|
2019-10-29 07:35:16 +00:00
|
|
|
err = storage.ScanVolumeFileFrom(v.Version(), v.DataBackend, foundOffset.ToAcutalOffset(), scanner)
|
2019-04-18 07:18:29 +00:00
|
|
|
|
2019-10-22 07:50:30 +00:00
|
|
|
return scanner.lastProcessedTimestampNs, err
|
2019-04-18 07:18:29 +00:00
|
|
|
|
|
|
|
}
|
2019-04-20 18:35:20 +00: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)
|
|
|
|
}
|
|
|
|
|
2020-11-17 06:26:58 +00:00
|
|
|
defer log.Debugf("receive tailing volume %d finished", v.Id)
|
2019-04-20 18:35:20 +00:00
|
|
|
|
2019-04-20 19:05:28 +00:00
|
|
|
return resp, operation.TailVolumeFromSource(req.SourceVolumeServer, vs.grpcDialOption, v.Id, req.SinceNs, int(req.IdleTimeoutSeconds), func(n *needle.Needle) error {
|
2020-04-12 04:39:16 +00:00
|
|
|
_, err := vs.store.WriteVolumeNeedle(v.Id, n, false)
|
2019-04-20 18:35:20 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
2019-10-22 07:50:30 +00:00
|
|
|
|
|
|
|
// generate the volume idx
|
|
|
|
type VolumeFileScanner4Tailing struct {
|
2019-10-22 07:50:50 +00:00
|
|
|
stream volume_server_pb.VolumeServer_VolumeTailSenderServer
|
2019-10-22 07:50:30 +00:00
|
|
|
lastProcessedTimestampNs uint64
|
|
|
|
}
|
|
|
|
|
2019-12-23 20:48:20 +00:00
|
|
|
func (scanner *VolumeFileScanner4Tailing) VisitSuperBlock(superBlock super_block.SuperBlock) error {
|
2019-10-22 07:50:30 +00:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
if sendErr != nil {
|
|
|
|
return sendErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner.lastProcessedTimestampNs = n.AppendAtNs
|
|
|
|
return nil
|
|
|
|
}
|