2020-04-18 16:12:01 +08:00
|
|
|
package broker
|
2020-03-04 16:39:47 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-07-02 13:43:25 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/mq_pb"
|
2020-03-04 16:39:47 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2020-04-19 06:17:27 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2020-03-04 16:39:47 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
|
|
|
|
)
|
|
|
|
|
2022-07-02 13:44:28 +08:00
|
|
|
type MessageQueueBrokerOption struct {
|
2021-09-13 13:47:52 +08:00
|
|
|
Filers []pb.ServerAddress
|
2020-03-04 16:39:47 +08:00
|
|
|
DefaultReplication string
|
|
|
|
MaxMB int
|
2020-05-05 17:05:28 +08:00
|
|
|
Ip string
|
2020-03-04 16:39:47 +08:00
|
|
|
Port int
|
2020-04-17 17:29:00 +08:00
|
|
|
Cipher bool
|
2020-03-04 16:39:47 +08:00
|
|
|
}
|
|
|
|
|
2022-07-02 13:44:28 +08:00
|
|
|
type MessageQueueBroker struct {
|
2022-07-02 13:43:25 +08:00
|
|
|
mq_pb.UnimplementedSeaweedMessagingServer
|
2022-07-02 13:44:28 +08:00
|
|
|
option *MessageQueueBrokerOption
|
2020-03-04 16:39:47 +08:00
|
|
|
grpcDialOption grpc.DialOption
|
2020-05-12 23:48:00 +08:00
|
|
|
topicManager *TopicManager
|
2020-03-04 16:39:47 +08:00
|
|
|
}
|
|
|
|
|
2022-07-02 13:44:28 +08:00
|
|
|
func NewMessageBroker(option *MessageQueueBrokerOption, grpcDialOption grpc.DialOption) (messageBroker *MessageQueueBroker, err error) {
|
2020-03-04 16:39:47 +08:00
|
|
|
|
2022-07-02 13:44:28 +08:00
|
|
|
messageBroker = &MessageQueueBroker{
|
2020-03-04 16:39:47 +08:00
|
|
|
option: option,
|
2020-04-17 17:29:00 +08:00
|
|
|
grpcDialOption: grpcDialOption,
|
2020-03-04 16:39:47 +08:00
|
|
|
}
|
|
|
|
|
2020-05-12 23:48:00 +08:00
|
|
|
messageBroker.topicManager = NewTopicManager(messageBroker)
|
2020-04-19 15:18:32 +08:00
|
|
|
|
2020-05-08 17:47:22 +08:00
|
|
|
messageBroker.checkFilers()
|
2020-04-19 06:17:27 +08:00
|
|
|
|
2020-05-05 17:05:28 +08:00
|
|
|
go messageBroker.keepConnectedToOneFiler()
|
2020-03-04 16:39:47 +08:00
|
|
|
|
|
|
|
return messageBroker, nil
|
|
|
|
}
|
|
|
|
|
2022-07-02 13:44:28 +08:00
|
|
|
func (broker *MessageQueueBroker) keepConnectedToOneFiler() {
|
2020-03-04 16:39:47 +08:00
|
|
|
|
|
|
|
for {
|
2020-04-19 06:17:27 +08:00
|
|
|
for _, filer := range broker.option.Filers {
|
2021-12-26 16:15:03 +08:00
|
|
|
broker.withFilerClient(false, filer, func(client filer_pb.SeaweedFilerClient) error {
|
2020-09-10 03:07:15 +08:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2021-11-06 08:52:15 +08:00
|
|
|
stream, err := client.KeepConnected(ctx)
|
2020-04-19 06:17:27 +08:00
|
|
|
if err != nil {
|
2020-05-05 17:05:28 +08:00
|
|
|
glog.V(0).Infof("%s:%d failed to keep connected to %s: %v", broker.option.Ip, broker.option.Port, filer, err)
|
2020-04-19 06:17:27 +08:00
|
|
|
return err
|
|
|
|
}
|
2020-05-08 17:47:22 +08:00
|
|
|
|
2021-11-06 08:52:15 +08:00
|
|
|
initRequest := &filer_pb.KeepConnectedRequest{
|
2020-05-08 17:47:22 +08:00
|
|
|
Name: broker.option.Ip,
|
|
|
|
GrpcPort: uint32(broker.option.Port),
|
|
|
|
}
|
2020-05-12 23:48:00 +08:00
|
|
|
for _, tp := range broker.topicManager.ListTopicPartitions() {
|
2020-05-08 17:47:22 +08:00
|
|
|
initRequest.Resources = append(initRequest.Resources, tp.String())
|
|
|
|
}
|
2021-11-06 08:52:15 +08:00
|
|
|
if err := stream.Send(&filer_pb.KeepConnectedRequest{
|
2020-05-08 17:47:22 +08:00
|
|
|
Name: broker.option.Ip,
|
|
|
|
GrpcPort: uint32(broker.option.Port),
|
|
|
|
}); err != nil {
|
|
|
|
glog.V(0).Infof("broker %s:%d failed to init at %s: %v", broker.option.Ip, broker.option.Port, filer, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO send events of adding/removing topics
|
|
|
|
|
2020-05-05 17:05:28 +08:00
|
|
|
glog.V(0).Infof("conntected with filer: %v", filer)
|
|
|
|
for {
|
2021-11-06 08:52:15 +08:00
|
|
|
if err := stream.Send(&filer_pb.KeepConnectedRequest{
|
2020-05-05 17:05:28 +08:00
|
|
|
Name: broker.option.Ip,
|
|
|
|
GrpcPort: uint32(broker.option.Port),
|
|
|
|
}); err != nil {
|
|
|
|
glog.V(0).Infof("%s:%d failed to sendto %s: %v", broker.option.Ip, broker.option.Port, filer, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// println("send heartbeat")
|
|
|
|
if _, err := stream.Recv(); err != nil {
|
|
|
|
glog.V(0).Infof("%s:%d failed to receive from %s: %v", broker.option.Ip, broker.option.Port, filer, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// println("received reply")
|
2020-05-08 17:47:22 +08:00
|
|
|
time.Sleep(11 * time.Second)
|
2020-05-05 17:05:28 +08:00
|
|
|
// println("woke up")
|
2020-04-19 06:17:27 +08:00
|
|
|
}
|
|
|
|
return nil
|
2020-03-04 16:39:47 +08:00
|
|
|
})
|
2020-05-08 17:47:22 +08:00
|
|
|
time.Sleep(3 * time.Second)
|
2020-03-04 16:39:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-07-02 13:44:28 +08:00
|
|
|
func (broker *MessageQueueBroker) withFilerClient(streamingMode bool, filer pb.ServerAddress, fn func(filer_pb.SeaweedFilerClient) error) error {
|
2020-03-04 16:39:47 +08:00
|
|
|
|
2021-12-26 16:15:03 +08:00
|
|
|
return pb.WithFilerClient(streamingMode, filer, broker.grpcDialOption, fn)
|
2020-03-04 16:39:47 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-07-02 13:44:28 +08:00
|
|
|
func (broker *MessageQueueBroker) withMasterClient(streamingMode bool, master pb.ServerAddress, fn func(client master_pb.SeaweedClient) error) error {
|
2020-03-04 16:39:47 +08:00
|
|
|
|
2021-12-26 16:15:03 +08:00
|
|
|
return pb.WithMasterClient(streamingMode, master, broker.grpcDialOption, func(client master_pb.SeaweedClient) error {
|
2020-03-04 16:39:47 +08:00
|
|
|
return fn(client)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|