seaweedfs/weed/messaging/broker/broker_server.go

115 lines
3.2 KiB
Go
Raw Normal View History

2020-04-18 08:12:01 +00:00
package broker
2020-03-04 08:39:47 +00:00
import (
"context"
"time"
"google.golang.org/grpc"
"github.com/chrislusf/seaweedfs/weed/glog"
2020-03-04 08:39:47 +00:00
"github.com/chrislusf/seaweedfs/weed/pb"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
)
type MessageBrokerOption struct {
Filers []string
DefaultReplication string
MaxMB int
2020-05-05 09:05:28 +00:00
Ip string
2020-03-04 08:39:47 +00:00
Port int
2020-04-17 09:29:00 +00:00
Cipher bool
2020-03-04 08:39:47 +00:00
}
type MessageBroker struct {
option *MessageBrokerOption
grpcDialOption grpc.DialOption
2020-05-12 15:48:00 +00:00
topicManager *TopicManager
2020-03-04 08:39:47 +00:00
}
2020-04-17 09:29:00 +00:00
func NewMessageBroker(option *MessageBrokerOption, grpcDialOption grpc.DialOption) (messageBroker *MessageBroker, err error) {
2020-03-04 08:39:47 +00:00
messageBroker = &MessageBroker{
option: option,
2020-04-17 09:29:00 +00:00
grpcDialOption: grpcDialOption,
2020-03-04 08:39:47 +00:00
}
2020-05-12 15:48:00 +00:00
messageBroker.topicManager = NewTopicManager(messageBroker)
2020-04-19 07:18:32 +00:00
2020-05-08 09:47:22 +00:00
messageBroker.checkFilers()
2020-05-05 09:05:28 +00:00
go messageBroker.keepConnectedToOneFiler()
2020-03-04 08:39:47 +00:00
return messageBroker, nil
}
2020-05-05 09:05:28 +00:00
func (broker *MessageBroker) keepConnectedToOneFiler() {
2020-03-04 08:39:47 +00:00
for {
for _, filer := range broker.option.Filers {
2020-05-05 09:05:28 +00:00
broker.withFilerClient(filer, func(client filer_pb.SeaweedFilerClient) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
stream, err := client.KeepConnected(ctx)
if err != nil {
2020-05-05 09:05:28 +00:00
glog.V(0).Infof("%s:%d failed to keep connected to %s: %v", broker.option.Ip, broker.option.Port, filer, err)
return err
}
2020-05-08 09:47:22 +00:00
initRequest := &filer_pb.KeepConnectedRequest{
Name: broker.option.Ip,
GrpcPort: uint32(broker.option.Port),
}
2020-05-12 15:48:00 +00:00
for _, tp := range broker.topicManager.ListTopicPartitions() {
2020-05-08 09:47:22 +00:00
initRequest.Resources = append(initRequest.Resources, tp.String())
}
if err := stream.Send(&filer_pb.KeepConnectedRequest{
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 09:05:28 +00:00
glog.V(0).Infof("conntected with filer: %v", filer)
for {
if err := stream.Send(&filer_pb.KeepConnectedRequest{
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 09:47:22 +00:00
time.Sleep(11 * time.Second)
2020-05-05 09:05:28 +00:00
// println("woke up")
}
return nil
2020-03-04 08:39:47 +00:00
})
2020-05-08 09:47:22 +00:00
time.Sleep(3 * time.Second)
2020-03-04 08:39:47 +00:00
}
}
}
func (broker *MessageBroker) withFilerClient(filer string, fn func(filer_pb.SeaweedFilerClient) error) error {
return pb.WithFilerClient(filer, broker.grpcDialOption, fn)
}
func (broker *MessageBroker) withMasterClient(master string, fn func(client master_pb.SeaweedClient) error) error {
return pb.WithMasterClient(master, broker.grpcDialOption, func(client master_pb.SeaweedClient) error {
return fn(client)
})
}