seaweedfs/weed/mq/broker/broker_server.go

117 lines
3.3 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 (
"time"
"github.com/seaweedfs/seaweedfs/weed/cluster"
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
"github.com/seaweedfs/seaweedfs/weed/wdclient"
2020-03-04 08:39:47 +00:00
"google.golang.org/grpc"
"github.com/seaweedfs/seaweedfs/weed/pb"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
2020-03-04 08:39:47 +00:00
)
2022-07-02 05:44:28 +00:00
type MessageQueueBrokerOption struct {
2022-07-02 06:34:51 +00:00
Masters map[string]pb.ServerAddress
FilerGroup string
2022-07-03 07:29:25 +00:00
DataCenter string
Rack string
2020-03-04 08:39:47 +00:00
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
}
2022-07-02 05:44:28 +00:00
type MessageQueueBroker struct {
2022-07-02 05:43:25 +00:00
mq_pb.UnimplementedSeaweedMessagingServer
2022-07-02 05:44:28 +00:00
option *MessageQueueBrokerOption
2020-03-04 08:39:47 +00:00
grpcDialOption grpc.DialOption
2022-07-02 06:34:51 +00:00
MasterClient *wdclient.MasterClient
2022-07-12 09:00:54 +00:00
filers map[pb.ServerAddress]struct{}
currentFiler pb.ServerAddress
2020-03-04 08:39:47 +00:00
}
2022-07-02 06:34:51 +00:00
func NewMessageBroker(option *MessageQueueBrokerOption, grpcDialOption grpc.DialOption) (mqBroker *MessageQueueBroker, err error) {
2020-03-04 08:39:47 +00:00
2022-07-02 06:34:51 +00:00
mqBroker = &MessageQueueBroker{
2020-03-04 08:39:47 +00:00
option: option,
2020-04-17 09:29:00 +00:00
grpcDialOption: grpcDialOption,
MasterClient: wdclient.NewMasterClient(grpcDialOption, option.FilerGroup, cluster.BrokerType, pb.NewServerAddress(option.Ip, option.Port, 0), option.DataCenter, option.Rack, option.Masters),
2022-07-12 09:00:54 +00:00
filers: make(map[pb.ServerAddress]struct{}),
2020-03-04 08:39:47 +00:00
}
mqBroker.MasterClient.SetOnPeerUpdateFn(mqBroker.OnBrokerUpdate)
2022-07-02 06:34:51 +00:00
go mqBroker.MasterClient.KeepConnectedToMaster()
2020-03-04 08:39:47 +00:00
2022-07-12 09:00:54 +00:00
existingNodes := cluster.ListExistingPeerUpdates(mqBroker.MasterClient.GetMaster(), grpcDialOption, option.FilerGroup, cluster.FilerType)
for _, newNode := range existingNodes {
mqBroker.OnBrokerUpdate(newNode, time.Now())
}
2022-07-02 06:34:51 +00:00
return mqBroker, nil
2020-03-04 08:39:47 +00:00
}
2022-07-12 09:00:54 +00:00
func (broker *MessageQueueBroker) OnBrokerUpdate(update *master_pb.ClusterNodeUpdate, startFrom time.Time) {
if update.NodeType != cluster.FilerType {
return
}
address := pb.ServerAddress(update.Address)
if update.IsAdd {
broker.filers[address] = struct{}{}
if broker.currentFiler == "" {
broker.currentFiler = address
}
} else {
delete(broker.filers, address)
if broker.currentFiler == address {
for filer := range broker.filers {
2022-07-12 09:00:54 +00:00
broker.currentFiler = filer
break
}
}
}
}
func (broker *MessageQueueBroker) GetFiler() pb.ServerAddress {
return broker.currentFiler
}
2022-07-16 17:49:34 +00:00
func (broker *MessageQueueBroker) WithFilerClient(streamingMode bool, fn func(filer_pb.SeaweedFilerClient) error) error {
2020-03-04 08:39:47 +00:00
return pb.WithFilerClient(streamingMode, 0, broker.GetFiler(), broker.grpcDialOption, fn)
2022-07-16 17:49:34 +00:00
}
func (broker *MessageQueueBroker) AdjustedUrl(location *filer_pb.Location) string {
return location.Url
2020-03-04 08:39:47 +00:00
}
func (broker *MessageQueueBroker) GetDataCenter() string {
return ""
}
2022-07-02 05:44:28 +00:00
func (broker *MessageQueueBroker) withMasterClient(streamingMode bool, master pb.ServerAddress, fn func(client master_pb.SeaweedClient) error) error {
2020-03-04 08:39:47 +00:00
return pb.WithMasterClient(streamingMode, master, broker.grpcDialOption, false, func(client master_pb.SeaweedClient) error {
2020-03-04 08:39:47 +00:00
return fn(client)
})
}
2022-07-11 07:20:27 +00:00
func (broker *MessageQueueBroker) withBrokerClient(streamingMode bool, server pb.ServerAddress, fn func(client mq_pb.SeaweedMessagingClient) error) error {
return pb.WithBrokerClient(streamingMode, server, broker.grpcDialOption, func(client mq_pb.SeaweedMessagingClient) error {
return fn(client)
})
}