You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.0 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
5 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/operation"
  8. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  9. "github.com/chrislusf/seaweedfs/weed/storage"
  10. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  11. )
  12. func (vs *VolumeServer) VolumeTailSender(req *volume_server_pb.VolumeTailSenderRequest, stream volume_server_pb.VolumeServer_VolumeTailSenderServer) error {
  13. v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
  14. if v == nil {
  15. return fmt.Errorf("not found volume id %d", req.VolumeId)
  16. }
  17. defer glog.V(1).Infof("tailing volume %d finished", v.Id)
  18. lastTimestampNs := req.SinceNs
  19. drainingSeconds := req.IdleTimeoutSeconds
  20. for {
  21. lastProcessedTimestampNs, err := sendNeedlesSince(stream, v, lastTimestampNs)
  22. if err != nil {
  23. glog.Infof("sendNeedlesSince: %v", err)
  24. return fmt.Errorf("streamFollow: %v", err)
  25. }
  26. time.Sleep(2 * time.Second)
  27. if req.IdleTimeoutSeconds == 0 {
  28. lastTimestampNs = lastProcessedTimestampNs
  29. continue
  30. }
  31. if lastProcessedTimestampNs == lastTimestampNs {
  32. drainingSeconds--
  33. if drainingSeconds <= 0 {
  34. return nil
  35. }
  36. glog.V(1).Infof("tailing volume %d drains requests with %d seconds remaining", v.Id, drainingSeconds)
  37. } else {
  38. lastTimestampNs = lastProcessedTimestampNs
  39. drainingSeconds = req.IdleTimeoutSeconds
  40. glog.V(1).Infof("tailing volume %d resets draining wait time to %d seconds", v.Id, drainingSeconds)
  41. }
  42. }
  43. }
  44. func sendNeedlesSince(stream volume_server_pb.VolumeServer_VolumeTailSenderServer, v *storage.Volume, lastTimestampNs uint64) (lastProcessedTimestampNs uint64, err error) {
  45. foundOffset, isLastOne, err := v.BinarySearchByAppendAtNs(lastTimestampNs)
  46. if err != nil {
  47. return 0, fmt.Errorf("fail to locate by appendAtNs %d: %s", lastTimestampNs, err)
  48. }
  49. // log.Printf("reading ts %d offset %d isLast %v", lastTimestampNs, foundOffset, isLastOne)
  50. if isLastOne {
  51. // need to heart beat to the client to ensure the connection health
  52. sendErr := stream.Send(&volume_server_pb.VolumeTailSenderResponse{IsLastChunk: true})
  53. return lastTimestampNs, sendErr
  54. }
  55. scanner := &VolumeFileScanner4Tailing{
  56. stream: stream,
  57. }
  58. err = storage.ScanVolumeFileFrom(v.Version(), v.DataBackend, foundOffset.ToAcutalOffset(), scanner)
  59. return scanner.lastProcessedTimestampNs, err
  60. }
  61. func (vs *VolumeServer) VolumeTailReceiver(ctx context.Context, req *volume_server_pb.VolumeTailReceiverRequest) (*volume_server_pb.VolumeTailReceiverResponse, error) {
  62. resp := &volume_server_pb.VolumeTailReceiverResponse{}
  63. v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
  64. if v == nil {
  65. return resp, fmt.Errorf("receiver not found volume id %d", req.VolumeId)
  66. }
  67. defer glog.V(1).Infof("receive tailing volume %d finished", v.Id)
  68. return resp, operation.TailVolumeFromSource(req.SourceVolumeServer, vs.grpcDialOption, v.Id, req.SinceNs, int(req.IdleTimeoutSeconds), func(n *needle.Needle) error {
  69. _, _, err := vs.store.WriteVolumeNeedle(v.Id, n)
  70. return err
  71. })
  72. }
  73. // generate the volume idx
  74. type VolumeFileScanner4Tailing struct {
  75. stream volume_server_pb.VolumeServer_VolumeTailSenderServer
  76. lastProcessedTimestampNs uint64
  77. }
  78. func (scanner *VolumeFileScanner4Tailing) VisitSuperBlock(superBlock storage.SuperBlock) error {
  79. return nil
  80. }
  81. func (scanner *VolumeFileScanner4Tailing) ReadNeedleBody() bool {
  82. return true
  83. }
  84. func (scanner *VolumeFileScanner4Tailing) VisitNeedle(n *needle.Needle, offset int64, needleHeader, needleBody []byte) error {
  85. isLastChunk := false
  86. // need to send body by chunks
  87. for i := 0; i < len(needleBody); i += BufferSizeLimit {
  88. stopOffset := i + BufferSizeLimit
  89. if stopOffset >= len(needleBody) {
  90. isLastChunk = true
  91. stopOffset = len(needleBody)
  92. }
  93. sendErr := scanner.stream.Send(&volume_server_pb.VolumeTailSenderResponse{
  94. NeedleHeader: needleHeader,
  95. NeedleBody: needleBody[i:stopOffset],
  96. IsLastChunk: isLastChunk,
  97. })
  98. if sendErr != nil {
  99. return sendErr
  100. }
  101. }
  102. scanner.lastProcessedTimestampNs = n.AppendAtNs
  103. return nil
  104. }