seaweedfs/weed/mq/broker/broker_grpc_pub_follow.go

148 lines
4.2 KiB
Go
Raw Normal View History

2024-03-11 05:34:28 +08:00
package broker
import (
"fmt"
2024-03-11 05:34:28 +08:00
"github.com/seaweedfs/seaweedfs/weed/glog"
2024-04-02 13:32:30 +08:00
"github.com/seaweedfs/seaweedfs/weed/mq/topic"
2024-03-11 05:34:28 +08:00
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
2024-04-02 13:32:30 +08:00
"github.com/seaweedfs/seaweedfs/weed/util/buffered_queue"
"github.com/seaweedfs/seaweedfs/weed/util/log_buffer"
"io"
2024-04-02 13:32:30 +08:00
"time"
2024-03-11 05:34:28 +08:00
)
2024-04-02 13:32:30 +08:00
type memBuffer struct {
2024-05-21 02:03:56 +08:00
buf []byte
startTime time.Time
stopTime time.Time
2024-04-02 13:32:30 +08:00
}
2024-05-21 02:03:56 +08:00
2024-04-02 13:32:30 +08:00
func (b *MessageQueueBroker) PublishFollowMe(stream mq_pb.SeaweedMessaging_PublishFollowMeServer) (err error) {
var req *mq_pb.PublishFollowMeRequest
req, err = stream.Recv()
if err != nil {
return err
}
initMessage := req.GetInit()
if initMessage == nil {
return fmt.Errorf("missing init message")
}
2024-04-02 13:32:30 +08:00
// create an in-memory queue of buffered messages
inMemoryBuffers := buffered_queue.NewBufferedQueue[memBuffer](4)
logBuffer := b.buildFollowerLogBuffer(inMemoryBuffers)
lastFlushTsNs := time.Now().UnixNano()
// follow each published messages
for {
// receive a message
2024-04-02 13:32:30 +08:00
req, err = stream.Recv()
if err != nil {
if err == io.EOF {
2024-04-02 13:32:30 +08:00
err = nil
break
}
glog.V(0).Infof("topic %v partition %v publish stream error: %v", initMessage.Topic, initMessage.Partition, err)
2024-04-02 13:32:30 +08:00
break
}
// Process the received message
if dataMessage := req.GetData(); dataMessage != nil {
2024-04-02 13:32:30 +08:00
// TODO: change this to DataMessage
// log the message
2024-04-03 07:25:43 +08:00
logBuffer.AddToBuffer(dataMessage)
2024-04-02 13:32:30 +08:00
// send back the ack
if err := stream.Send(&mq_pb.PublishFollowMeResponse{
AckTsNs: dataMessage.TsNs,
}); err != nil {
glog.Errorf("Error sending response %v: %v", dataMessage, err)
}
2024-04-03 06:31:31 +08:00
// println("ack", string(dataMessage.Key), dataMessage.TsNs)
} else if closeMessage := req.GetClose(); closeMessage != nil {
glog.V(0).Infof("topic %v partition %v publish stream closed: %v", initMessage.Topic, initMessage.Partition, closeMessage)
break
} else if flushMessage := req.GetFlush(); flushMessage != nil {
glog.V(0).Infof("topic %v partition %v publish stream flushed: %v", initMessage.Topic, initMessage.Partition, flushMessage)
2024-04-02 13:32:30 +08:00
lastFlushTsNs = flushMessage.TsNs
// drop already flushed messages
for mem, found := inMemoryBuffers.PeekHead(); found; mem, found = inMemoryBuffers.PeekHead() {
if mem.stopTime.UnixNano() <= flushMessage.TsNs {
inMemoryBuffers.Dequeue()
2024-04-03 06:31:31 +08:00
// println("dropping flushed messages: ", mem.startTime.UnixNano(), mem.stopTime.UnixNano(), len(mem.buf))
2024-04-02 13:32:30 +08:00
} else {
break
}
}
} else {
glog.Errorf("unknown message: %v", req)
}
}
2024-04-02 13:32:30 +08:00
t, p := topic.FromPbTopic(initMessage.Topic), topic.FromPbPartition(initMessage.Partition)
logBuffer.ShutdownLogBuffer()
// wait until all messages are sent to inMemoryBuffers
for !logBuffer.IsAllFlushed() {
time.Sleep(113 * time.Millisecond)
}
partitionDir := topic.PartitionDir(t, p)
2024-04-02 13:32:30 +08:00
// flush the remaining messages
inMemoryBuffers.CloseInput()
for mem, found := inMemoryBuffers.Dequeue(); found; mem, found = inMemoryBuffers.Dequeue() {
if len(mem.buf) == 0 {
continue
}
startTime, stopTime := mem.startTime.UTC(), mem.stopTime.UTC()
if stopTime.UnixNano() <= lastFlushTsNs {
glog.V(0).Infof("dropping remaining data at %v %v", t, p)
continue
}
// TODO trim data earlier than lastFlushTsNs
targetFile := fmt.Sprintf("%s/%s", partitionDir, startTime.Format(topic.TIME_FORMAT))
for {
if err := b.appendToFile(targetFile, mem.buf); err != nil {
glog.V(0).Infof("metadata log write failed %s: %v", targetFile, err)
time.Sleep(737 * time.Millisecond)
} else {
break
}
}
glog.V(0).Infof("flushed remaining data at %v to %s size %d", mem.stopTime.UnixNano(), targetFile, len(mem.buf))
}
glog.V(0).Infof("shut down follower for %v %v", t, p)
return err
}
func (b *MessageQueueBroker) buildFollowerLogBuffer(inMemoryBuffers *buffered_queue.BufferedQueue[memBuffer]) *log_buffer.LogBuffer {
lb := log_buffer.NewLogBuffer("follower",
2*time.Minute, func(logBuffer *log_buffer.LogBuffer, startTime, stopTime time.Time, buf []byte) {
if len(buf) == 0 {
return
}
inMemoryBuffers.Enqueue(memBuffer{
buf: buf,
startTime: startTime,
stopTime: stopTime,
})
glog.V(0).Infof("queue up %d~%d size %d", startTime.UnixNano(), stopTime.UnixNano(), len(buf))
}, nil, func() {
})
return lb
2024-03-11 05:34:28 +08:00
}