2022-08-01 04:23:44 +08:00
|
|
|
package broker
|
|
|
|
|
|
|
|
import (
|
2023-08-21 13:53:05 +08:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/topic"
|
2022-08-01 04:23:44 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
2023-12-12 04:05:54 +08:00
|
|
|
"google.golang.org/grpc/peer"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
2023-09-07 14:16:41 +08:00
|
|
|
"sync/atomic"
|
2023-09-07 13:39:46 +08:00
|
|
|
"time"
|
2022-08-01 04:23:44 +08:00
|
|
|
)
|
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
// PUB
|
|
|
|
// 1. gRPC API to configure a topic
|
|
|
|
// 1.1 create a topic with existing partition count
|
|
|
|
// 1.2 assign partitions to brokers
|
|
|
|
// 2. gRPC API to lookup topic partitions
|
|
|
|
// 3. gRPC API to publish by topic partitions
|
2023-08-21 13:53:05 +08:00
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
// SUB
|
|
|
|
// 1. gRPC API to lookup a topic partitions
|
2023-08-21 13:53:05 +08:00
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
// Re-balance topic partitions for publishing
|
|
|
|
// 1. collect stats from all the brokers
|
|
|
|
// 2. Rebalance and configure new generation of partitions on brokers
|
|
|
|
// 3. Tell brokers to close current gneration of publishing.
|
|
|
|
// Publishers needs to lookup again and publish to the new generation of partitions.
|
2023-08-21 13:53:05 +08:00
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
// Re-balance topic partitions for subscribing
|
|
|
|
// 1. collect stats from all the brokers
|
|
|
|
// Subscribers needs to listen for new partitions and connect to the brokers.
|
|
|
|
// Each subscription may not get data. It can act as a backup.
|
2023-08-21 13:53:05 +08:00
|
|
|
|
2024-01-06 07:14:25 +08:00
|
|
|
func (b *MessageQueueBroker) PublishMessage(stream mq_pb.SeaweedMessaging_PublishMessageServer) error {
|
2023-08-21 13:53:05 +08:00
|
|
|
// 1. write to the volume server
|
|
|
|
// 2. find the topic metadata owning filer
|
|
|
|
// 3. write to the filer
|
|
|
|
|
|
|
|
var localTopicPartition *topic.LocalPartition
|
2023-08-28 08:50:59 +08:00
|
|
|
req, err := stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-06 07:14:25 +08:00
|
|
|
response := &mq_pb.PublishMessageResponse{}
|
2023-08-28 08:50:59 +08:00
|
|
|
// TODO check whether current broker should be the leader for the topic partition
|
2023-09-07 13:39:46 +08:00
|
|
|
ackInterval := 1
|
2023-08-29 00:02:12 +08:00
|
|
|
initMessage := req.GetInit()
|
|
|
|
if initMessage != nil {
|
|
|
|
t, p := topic.FromPbTopic(initMessage.Topic), topic.FromPbPartition(initMessage.Partition)
|
2023-12-12 04:05:54 +08:00
|
|
|
localTopicPartition = b.localTopicManager.GetTopicPartition(t, p)
|
2023-08-28 08:50:59 +08:00
|
|
|
if localTopicPartition == nil {
|
2023-12-12 04:05:54 +08:00
|
|
|
response.Error = fmt.Sprintf("topic %v partition %v not setup", initMessage.Topic, initMessage.Partition)
|
|
|
|
glog.Errorf("topic %v partition %v not setup", initMessage.Topic, initMessage.Partition)
|
|
|
|
return stream.Send(response)
|
2023-08-28 08:50:59 +08:00
|
|
|
}
|
2023-09-07 13:39:46 +08:00
|
|
|
ackInterval = int(initMessage.AckInterval)
|
2023-09-05 12:43:30 +08:00
|
|
|
stream.Send(response)
|
2023-08-29 00:02:12 +08:00
|
|
|
} else {
|
2023-12-12 04:05:54 +08:00
|
|
|
response.Error = fmt.Sprintf("missing init message")
|
|
|
|
glog.Errorf("missing init message")
|
2023-08-29 00:02:12 +08:00
|
|
|
return stream.Send(response)
|
2023-08-28 08:50:59 +08:00
|
|
|
}
|
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
clientName := fmt.Sprintf("%v-%4d/%s/%v", findClientAddress(stream.Context()), rand.Intn(10000), initMessage.Topic, initMessage.Partition)
|
|
|
|
localTopicPartition.Publishers.AddPublisher(clientName, topic.NewLocalPublisher())
|
|
|
|
|
2023-09-07 13:39:46 +08:00
|
|
|
ackCounter := 0
|
|
|
|
var ackSequence int64
|
2023-09-07 14:16:41 +08:00
|
|
|
var isStopping int32
|
2024-01-06 07:14:25 +08:00
|
|
|
respChan := make(chan *mq_pb.PublishMessageResponse, 128)
|
2023-09-07 14:16:41 +08:00
|
|
|
defer func() {
|
|
|
|
atomic.StoreInt32(&isStopping, 1)
|
|
|
|
close(respChan)
|
2023-12-12 04:05:54 +08:00
|
|
|
localTopicPartition.Publishers.RemovePublisher(clientName)
|
2023-09-07 14:16:41 +08:00
|
|
|
}()
|
2023-09-07 13:39:46 +08:00
|
|
|
go func() {
|
2023-09-07 14:16:41 +08:00
|
|
|
ticker := time.NewTicker(1 * time.Second)
|
2023-09-07 13:39:46 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case resp := <-respChan:
|
2023-09-07 14:16:41 +08:00
|
|
|
if resp != nil {
|
|
|
|
if err := stream.Send(resp); err != nil {
|
2023-09-08 14:55:19 +08:00
|
|
|
glog.Errorf("Error sending response %v: %v", resp, err)
|
2023-09-07 14:16:41 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return
|
2023-09-07 13:39:46 +08:00
|
|
|
}
|
2023-09-07 14:16:41 +08:00
|
|
|
case <-ticker.C:
|
|
|
|
if atomic.LoadInt32(&isStopping) == 0 {
|
2024-01-06 07:14:25 +08:00
|
|
|
response := &mq_pb.PublishMessageResponse{
|
2023-09-07 14:16:41 +08:00
|
|
|
AckSequence: ackSequence,
|
|
|
|
}
|
|
|
|
respChan <- response
|
|
|
|
} else {
|
|
|
|
return
|
2023-09-07 13:39:46 +08:00
|
|
|
}
|
2023-12-12 04:05:54 +08:00
|
|
|
case <-localTopicPartition.StopPublishersCh:
|
2024-01-06 07:14:25 +08:00
|
|
|
respChan <- &mq_pb.PublishMessageResponse{
|
2023-12-12 04:05:54 +08:00
|
|
|
AckSequence: ackSequence,
|
|
|
|
ShouldClose: true,
|
|
|
|
}
|
2023-09-07 13:39:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-08-28 08:50:59 +08:00
|
|
|
// process each published messages
|
2023-08-21 13:53:05 +08:00
|
|
|
for {
|
2023-09-07 13:39:46 +08:00
|
|
|
// receive a message
|
2023-08-21 13:53:05 +08:00
|
|
|
req, err := stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process the received message
|
|
|
|
if dataMessage := req.GetData(); dataMessage != nil {
|
2023-08-28 08:50:59 +08:00
|
|
|
localTopicPartition.Publish(dataMessage)
|
2023-08-21 13:53:05 +08:00
|
|
|
}
|
2023-09-07 13:39:46 +08:00
|
|
|
|
|
|
|
ackCounter++
|
|
|
|
ackSequence++
|
|
|
|
if ackCounter >= ackInterval {
|
|
|
|
ackCounter = 0
|
|
|
|
// send back the ack
|
2024-01-06 07:14:25 +08:00
|
|
|
response := &mq_pb.PublishMessageResponse{
|
2023-09-07 13:39:46 +08:00
|
|
|
AckSequence: ackSequence,
|
|
|
|
}
|
|
|
|
respChan <- response
|
2023-08-21 13:53:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
glog.V(0).Infof("topic %v partition %v publish stream closed.", initMessage.Topic, initMessage.Partition)
|
2023-09-05 12:43:30 +08:00
|
|
|
|
2022-08-01 04:23:44 +08:00
|
|
|
return nil
|
|
|
|
}
|
2023-08-21 13:53:05 +08:00
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
// duplicated from master_grpc_server.go
|
|
|
|
func findClientAddress(ctx context.Context) string {
|
|
|
|
// fmt.Printf("FromContext %+v\n", ctx)
|
|
|
|
pr, ok := peer.FromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
glog.Error("failed to get peer from ctx")
|
|
|
|
return ""
|
2023-08-21 13:53:05 +08:00
|
|
|
}
|
2023-12-12 04:05:54 +08:00
|
|
|
if pr.Addr == net.Addr(nil) {
|
|
|
|
glog.Error("failed to get peer address")
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return pr.Addr.String()
|
2023-08-21 13:53:05 +08:00
|
|
|
}
|