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"
|
2024-03-11 05:34:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
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"
|
2024-01-29 14:04:42 +08:00
|
|
|
"io"
|
2023-12-12 04:05:54 +08:00
|
|
|
"math/rand"
|
|
|
|
"net"
|
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
|
|
|
|
|
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()
|
2024-03-21 03:25:40 +08:00
|
|
|
if initMessage == nil {
|
|
|
|
response.Error = fmt.Sprintf("missing init message")
|
|
|
|
glog.Errorf("missing init message")
|
|
|
|
return stream.Send(response)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get or generate a local partition
|
|
|
|
t, p := topic.FromPbTopic(initMessage.Topic), topic.FromPbPartition(initMessage.Partition)
|
2024-03-25 04:04:59 +08:00
|
|
|
localTopicPartition, getOrGenErr := b.GetOrGenerateLocalPartition(t, p)
|
|
|
|
if getOrGenErr != nil {
|
|
|
|
response.Error = fmt.Sprintf("topic %v not found: %v", t, getOrGenErr)
|
|
|
|
glog.Errorf("topic %v not found: %v", t, getOrGenErr)
|
2024-03-21 03:25:40 +08:00
|
|
|
return stream.Send(response)
|
|
|
|
}
|
2024-03-25 04:04:59 +08:00
|
|
|
|
2024-03-21 03:25:40 +08:00
|
|
|
ackInterval = int(initMessage.AckInterval)
|
|
|
|
|
|
|
|
// connect to follower brokers
|
2024-03-25 03:57:09 +08:00
|
|
|
if localTopicPartition.FollowerStream == nil && len(initMessage.FollowerBrokers) > 0 {
|
2024-03-21 03:25:40 +08:00
|
|
|
follower := initMessage.FollowerBrokers[0]
|
2024-03-28 01:24:57 +08:00
|
|
|
ctx := context.Background()
|
2024-03-27 12:52:12 +08:00
|
|
|
localTopicPartition.FollowerGrpcConnection, err = pb.GrpcDial(ctx, follower, true, b.grpcDialOption)
|
2024-01-29 07:55:26 +08:00
|
|
|
if err != nil {
|
2024-03-21 03:25:40 +08:00
|
|
|
response.Error = fmt.Sprintf("fail to dial %s: %v", follower, err)
|
|
|
|
glog.Errorf("fail to dial %s: %v", follower, err)
|
2024-01-29 07:55:26 +08:00
|
|
|
return stream.Send(response)
|
2023-08-28 08:50:59 +08:00
|
|
|
}
|
2024-03-27 12:52:12 +08:00
|
|
|
followerClient := mq_pb.NewSeaweedMessagingClient(localTopicPartition.FollowerGrpcConnection)
|
2024-03-25 03:57:09 +08:00
|
|
|
localTopicPartition.FollowerStream, err = followerClient.PublishFollowMe(ctx)
|
2024-03-21 03:25:40 +08:00
|
|
|
if err != nil {
|
|
|
|
response.Error = fmt.Sprintf("fail to create publish client: %v", err)
|
|
|
|
glog.Errorf("fail to create publish client: %v", err)
|
|
|
|
return stream.Send(response)
|
|
|
|
}
|
2024-03-25 03:57:09 +08:00
|
|
|
if err = localTopicPartition.FollowerStream.Send(&mq_pb.PublishFollowMeRequest{
|
2024-03-21 03:25:40 +08:00
|
|
|
Message: &mq_pb.PublishFollowMeRequest_Init{
|
|
|
|
Init: &mq_pb.PublishFollowMeRequest_InitMessage{
|
2024-03-17 01:51:47 +08:00
|
|
|
Topic: initMessage.Topic,
|
|
|
|
Partition: initMessage.Partition,
|
2024-03-21 03:25:40 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
2024-03-11 05:34:28 +08:00
|
|
|
}
|
2023-12-12 04:05:54 +08:00
|
|
|
|
2024-03-25 12:10:02 +08:00
|
|
|
// start receiving ack from follower
|
2024-03-21 03:25:40 +08:00
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
println("stop receiving ack from follower")
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
2024-03-25 03:57:09 +08:00
|
|
|
ack, err := localTopicPartition.FollowerStream.Recv()
|
2024-03-21 03:25:40 +08:00
|
|
|
if err != nil {
|
2024-03-25 23:15:47 +08:00
|
|
|
glog.Errorf("Error receiving follower ack: %v", err)
|
2024-03-21 03:25:40 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
println("recv ack", ack.AckTsNs)
|
|
|
|
if err := stream.Send(&mq_pb.PublishMessageResponse{
|
|
|
|
AckSequence: ack.AckTsNs,
|
|
|
|
}); err != nil {
|
2024-03-25 23:15:47 +08:00
|
|
|
glog.Errorf("Error sending publisher ack %v: %v", ack, err)
|
2024-03-21 03:25:40 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2024-03-25 12:10:02 +08:00
|
|
|
// process each published messages
|
|
|
|
clientName := fmt.Sprintf("%v-%4d/%s/%v", findClientAddress(stream.Context()), rand.Intn(10000), initMessage.Topic, initMessage.Partition)
|
|
|
|
localTopicPartition.Publishers.AddPublisher(clientName, topic.NewLocalPublisher())
|
|
|
|
|
|
|
|
ackCounter := 0
|
|
|
|
var ackSequence int64
|
|
|
|
defer func() {
|
|
|
|
// remove the publisher
|
|
|
|
localTopicPartition.Publishers.RemovePublisher(clientName)
|
|
|
|
glog.V(0).Infof("topic %v partition %v published %d messges Publisher:%d Subscriber:%d", initMessage.Topic, initMessage.Partition, ackSequence, localTopicPartition.Publishers.Size(), localTopicPartition.Subscribers.Size())
|
|
|
|
if localTopicPartition.MaybeShutdownLocalPartition() {
|
|
|
|
if localTopicPartition.FollowerStream != nil {
|
|
|
|
// send close to the follower
|
|
|
|
if followErr := localTopicPartition.FollowerStream.Send(&mq_pb.PublishFollowMeRequest{
|
|
|
|
Message: &mq_pb.PublishFollowMeRequest_Close{
|
|
|
|
Close: &mq_pb.PublishFollowMeRequest_CloseMessage{},
|
|
|
|
},
|
|
|
|
}); followErr != nil {
|
|
|
|
glog.Errorf("Error closing follower stream: %v", followErr)
|
|
|
|
}
|
|
|
|
println("closing grpcConnection to follower")
|
2024-03-27 12:52:12 +08:00
|
|
|
localTopicPartition.FollowerGrpcConnection.Close()
|
2024-03-25 12:10:02 +08:00
|
|
|
}
|
|
|
|
b.localTopicManager.RemoveTopicPartition(t, p)
|
2024-03-25 23:15:47 +08:00
|
|
|
glog.V(0).Infof("Removed local topic %v partition %v", initMessage.Topic, initMessage.Partition)
|
2024-03-25 12:10:02 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-03-21 03:25:40 +08:00
|
|
|
// send a hello message
|
|
|
|
stream.Send(&mq_pb.PublishMessageResponse{})
|
|
|
|
|
|
|
|
var receivedSequence, acknowledgedSequence int64
|
|
|
|
|
|
|
|
defer func() {
|
2024-03-25 03:57:09 +08:00
|
|
|
if localTopicPartition.FollowerStream != nil {
|
2024-03-21 03:25:40 +08:00
|
|
|
//if err := followerStream.CloseSend(); err != nil {
|
|
|
|
// glog.Errorf("Error closing follower stream: %v", err)
|
|
|
|
//}
|
|
|
|
} else {
|
|
|
|
if acknowledgedSequence < receivedSequence {
|
|
|
|
acknowledgedSequence = receivedSequence
|
|
|
|
response := &mq_pb.PublishMessageResponse{
|
|
|
|
AckSequence: acknowledgedSequence,
|
|
|
|
}
|
|
|
|
if err := stream.Send(response); err != nil {
|
|
|
|
glog.Errorf("Error sending response %v: %v", response, err)
|
|
|
|
}
|
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 {
|
2024-01-29 14:04:42 +08:00
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
glog.V(0).Infof("topic %v partition %v publish stream error: %v", initMessage.Topic, initMessage.Partition, err)
|
2023-08-21 13:53:05 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process the received message
|
2024-03-21 03:25:40 +08:00
|
|
|
dataMessage := req.GetData()
|
|
|
|
if dataMessage == nil {
|
|
|
|
continue
|
2023-08-21 13:53:05 +08:00
|
|
|
}
|
2023-09-07 13:39:46 +08:00
|
|
|
|
2024-03-21 03:25:40 +08:00
|
|
|
// send to the local partition
|
|
|
|
localTopicPartition.Publish(dataMessage)
|
|
|
|
receivedSequence = dataMessage.TsNs
|
|
|
|
|
|
|
|
// maybe send to the follower
|
2024-03-25 03:57:09 +08:00
|
|
|
if localTopicPartition.FollowerStream != nil {
|
2024-03-21 03:25:40 +08:00
|
|
|
println("recv", string(dataMessage.Key), dataMessage.TsNs)
|
2024-03-25 03:57:09 +08:00
|
|
|
if followErr := localTopicPartition.FollowerStream.Send(&mq_pb.PublishFollowMeRequest{
|
2024-03-21 03:25:40 +08:00
|
|
|
Message: &mq_pb.PublishFollowMeRequest_Data{
|
|
|
|
Data: dataMessage,
|
|
|
|
},
|
|
|
|
}); followErr != nil {
|
|
|
|
return followErr
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ackCounter++
|
|
|
|
if ackCounter >= ackInterval {
|
|
|
|
ackCounter = 0
|
|
|
|
// send back the ack directly
|
|
|
|
acknowledgedSequence = receivedSequence
|
|
|
|
response := &mq_pb.PublishMessageResponse{
|
|
|
|
AckSequence: acknowledgedSequence,
|
|
|
|
}
|
|
|
|
if err := stream.Send(response); err != nil {
|
|
|
|
glog.Errorf("Error sending response %v: %v", response, err)
|
|
|
|
}
|
2023-09-07 13:39:46 +08:00
|
|
|
}
|
2023-08-21 13:53:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-25 03:57:09 +08:00
|
|
|
if localTopicPartition.FollowerStream != nil {
|
2024-03-21 03:25:40 +08:00
|
|
|
// send close to the follower
|
2024-03-25 03:57:09 +08:00
|
|
|
if followErr := localTopicPartition.FollowerStream.Send(&mq_pb.PublishFollowMeRequest{
|
2024-03-21 03:25:40 +08:00
|
|
|
Message: &mq_pb.PublishFollowMeRequest_Close{
|
|
|
|
Close: &mq_pb.PublishFollowMeRequest_CloseMessage{},
|
|
|
|
},
|
|
|
|
}); followErr != nil {
|
|
|
|
return followErr
|
|
|
|
}
|
|
|
|
println("closing follower stream")
|
|
|
|
|
|
|
|
//if err := followerStream.CloseSend(); err != nil {
|
|
|
|
// glog.Errorf("Error closing follower stream: %v", err)
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|