2024-03-11 05:34:28 +08:00
|
|
|
package broker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
|
|
|
"io"
|
|
|
|
"math/rand"
|
2024-03-16 11:36:01 +08:00
|
|
|
"sync"
|
2024-03-11 05:34:28 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-03-17 01:51:47 +08:00
|
|
|
func (b *MessageQueueBroker) PublishFollowMe(c context.Context, request *mq_pb.PublishFollowMeRequest) (*mq_pb.PublishFollowMeResponse, error) {
|
2024-03-11 05:34:28 +08:00
|
|
|
glog.V(0).Infof("PublishFollowMe %v", request)
|
2024-03-16 11:36:01 +08:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
var ret error
|
2024-03-11 05:34:28 +08:00
|
|
|
go b.withBrokerClient(true, pb.ServerAddress(request.BrokerSelf), func(client mq_pb.SeaweedMessagingClient) error {
|
|
|
|
followerId := rand.Int31()
|
|
|
|
subscribeClient, err := client.FollowInMemoryMessages(context.Background(), &mq_pb.FollowInMemoryMessagesRequest{
|
|
|
|
Message: &mq_pb.FollowInMemoryMessagesRequest_Init{
|
|
|
|
Init: &mq_pb.FollowInMemoryMessagesRequest_InitMessage{
|
|
|
|
ConsumerGroup: string(b.option.BrokerAddress()),
|
2024-03-17 08:11:18 +08:00
|
|
|
ConsumerId: fmt.Sprintf("followMe@%s-%d", b.option.BrokerAddress(), followerId),
|
2024-03-11 05:34:28 +08:00
|
|
|
FollowerId: followerId,
|
|
|
|
Topic: request.Topic,
|
|
|
|
PartitionOffset: &mq_pb.PartitionOffset{
|
|
|
|
Partition: request.Partition,
|
|
|
|
StartTsNs: 0,
|
|
|
|
StartType: mq_pb.PartitionOffsetStartType_EARLIEST_IN_MEMORY,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2024-03-16 11:36:01 +08:00
|
|
|
|
2024-03-11 05:34:28 +08:00
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("FollowInMemoryMessages error: %v", err)
|
2024-03-16 11:36:01 +08:00
|
|
|
ret = err
|
2024-03-11 05:34:28 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-16 11:36:01 +08:00
|
|
|
// receive first hello message
|
|
|
|
resp, err := subscribeClient.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("FollowInMemoryMessages recv first message error: %v", err)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
glog.V(0).Infof("doFollowInMemoryMessage recv first message nil response")
|
|
|
|
return io.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
|
2024-03-17 08:11:18 +08:00
|
|
|
b.doFollowInMemoryMessage(context.Background(), followerId, subscribeClient)
|
2024-03-11 05:34:28 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2024-03-16 11:36:01 +08:00
|
|
|
wg.Wait()
|
|
|
|
return &mq_pb.PublishFollowMeResponse{}, ret
|
2024-03-11 05:34:28 +08:00
|
|
|
}
|
|
|
|
|
2024-03-17 08:11:18 +08:00
|
|
|
func (b *MessageQueueBroker) doFollowInMemoryMessage(c context.Context, followerId int32, client mq_pb.SeaweedMessaging_FollowInMemoryMessagesClient) {
|
2024-03-11 05:34:28 +08:00
|
|
|
for {
|
|
|
|
resp, err := client.Recv()
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
glog.V(0).Infof("doFollowInMemoryMessage error: %v", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
glog.V(0).Infof("doFollowInMemoryMessage nil response")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if resp.Message != nil {
|
|
|
|
// process ctrl message or data message
|
2024-03-17 01:51:47 +08:00
|
|
|
switch m := resp.Message.(type) {
|
2024-03-11 05:34:28 +08:00
|
|
|
case *mq_pb.FollowInMemoryMessagesResponse_Data:
|
|
|
|
// process data message
|
|
|
|
print("d")
|
|
|
|
case *mq_pb.FollowInMemoryMessagesResponse_Ctrl:
|
|
|
|
// process ctrl message
|
|
|
|
if m.Ctrl.FlushedSequence > 0 {
|
|
|
|
flushTime := time.Unix(0, m.Ctrl.FlushedSequence)
|
|
|
|
glog.V(0).Infof("doFollowInMemoryMessage flushTime: %v", flushTime)
|
|
|
|
}
|
|
|
|
if m.Ctrl.FollowerChangedToId != 0 {
|
|
|
|
// follower changed
|
2024-03-17 08:11:18 +08:00
|
|
|
glog.V(0).Infof("doFollowInMemoryMessage follower changed from %d to %d", followerId, m.Ctrl.FollowerChangedToId)
|
2024-03-11 05:34:28 +08:00
|
|
|
return
|
|
|
|
}
|
2024-03-17 08:11:18 +08:00
|
|
|
default:
|
|
|
|
glog.V(0).Infof("doFollowInMemoryMessage unknown message type: %v", m)
|
2024-03-11 05:34:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|