2020-04-18 08:12:01 +00:00
|
|
|
package broker
|
2020-03-04 08:39:47 +00:00
|
|
|
|
|
|
|
import (
|
2023-09-16 22:05:38 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
2023-12-11 20:05:54 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/pub_balancer"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/sub_coordinator"
|
2023-08-21 05:53:05 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/topic"
|
2022-08-24 06:18:21 +00:00
|
|
|
"time"
|
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"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"
|
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"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
|
2023-08-21 05:53:05 +00:00
|
|
|
option *MessageQueueBrokerOption
|
|
|
|
grpcDialOption grpc.DialOption
|
|
|
|
MasterClient *wdclient.MasterClient
|
|
|
|
filers map[pb.ServerAddress]struct{}
|
|
|
|
currentFiler pb.ServerAddress
|
|
|
|
localTopicManager *topic.LocalTopicManager
|
2023-12-11 20:05:54 +00:00
|
|
|
Balancer *pub_balancer.Balancer
|
2023-09-16 22:05:38 +00:00
|
|
|
lockAsBalancer *cluster.LiveLock
|
2023-09-19 21:08:17 +00:00
|
|
|
currentBalancer pb.ServerAddress
|
2023-12-11 20:05:54 +00:00
|
|
|
Coordinator *sub_coordinator.Coordinator
|
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
|
|
|
|
2023-12-11 20:05:54 +00:00
|
|
|
pub_broker_balancer := pub_balancer.NewBalancer()
|
2024-01-03 21:30:30 +00:00
|
|
|
coordinator := sub_coordinator.NewCoordinator(pub_broker_balancer)
|
2023-12-11 20:05:54 +00:00
|
|
|
|
2022-07-02 06:34:51 +00:00
|
|
|
mqBroker = &MessageQueueBroker{
|
2023-08-21 05:53:05 +00:00
|
|
|
option: option,
|
|
|
|
grpcDialOption: grpcDialOption,
|
2023-09-15 06:49:05 +00:00
|
|
|
MasterClient: wdclient.NewMasterClient(grpcDialOption, option.FilerGroup, cluster.BrokerType, pb.NewServerAddress(option.Ip, option.Port, 0), option.DataCenter, option.Rack, *pb.NewServiceDiscoveryFromMap(option.Masters)),
|
2023-08-21 05:53:05 +00:00
|
|
|
filers: make(map[pb.ServerAddress]struct{}),
|
|
|
|
localTopicManager: topic.NewLocalTopicManager(),
|
2023-12-11 20:05:54 +00:00
|
|
|
Balancer: pub_broker_balancer,
|
2024-01-03 21:30:30 +00:00
|
|
|
Coordinator: coordinator,
|
2020-03-04 08:39:47 +00:00
|
|
|
}
|
2022-08-26 17:18:49 +00:00
|
|
|
mqBroker.MasterClient.SetOnPeerUpdateFn(mqBroker.OnBrokerUpdate)
|
2024-01-03 21:30:30 +00:00
|
|
|
pub_broker_balancer.OnPartitionChange = mqBroker.Coordinator.OnPartitionChange
|
2020-04-18 22:17:27 +00:00
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2023-09-16 22:05:38 +00:00
|
|
|
// keep connecting to balancer
|
|
|
|
go func() {
|
|
|
|
for mqBroker.currentFiler == "" {
|
|
|
|
time.Sleep(time.Millisecond * 237)
|
|
|
|
}
|
|
|
|
self := fmt.Sprintf("%s:%d", option.Ip, option.Port)
|
2023-12-11 20:05:54 +00:00
|
|
|
glog.V(0).Infof("broker %s found filer %s", self, mqBroker.currentFiler)
|
2023-09-16 22:05:38 +00:00
|
|
|
|
|
|
|
lockClient := cluster.NewLockClient(grpcDialOption, mqBroker.currentFiler)
|
2023-12-11 20:05:54 +00:00
|
|
|
mqBroker.lockAsBalancer = lockClient.StartLock(pub_balancer.LockBrokerBalancer, self)
|
2023-09-16 22:05:38 +00:00
|
|
|
for {
|
2023-10-13 03:28:11 +00:00
|
|
|
err := mqBroker.BrokerConnectToBalancer(self)
|
2023-09-16 22:05:38 +00:00
|
|
|
if err != nil {
|
2023-10-13 03:28:11 +00:00
|
|
|
fmt.Printf("BrokerConnectToBalancer: %v\n", err)
|
2023-09-16 22:05:38 +00:00
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-07-02 06:34:51 +00:00
|
|
|
return mqBroker, nil
|
2020-03-04 08:39:47 +00:00
|
|
|
}
|
|
|
|
|
2023-12-11 20:05:54 +00:00
|
|
|
func (b *MessageQueueBroker) OnBrokerUpdate(update *master_pb.ClusterNodeUpdate, startFrom time.Time) {
|
2022-07-12 09:00:54 +00:00
|
|
|
if update.NodeType != cluster.FilerType {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
address := pb.ServerAddress(update.Address)
|
|
|
|
if update.IsAdd {
|
2023-12-11 20:05:54 +00:00
|
|
|
b.filers[address] = struct{}{}
|
|
|
|
if b.currentFiler == "" {
|
|
|
|
b.currentFiler = address
|
2022-07-12 09:00:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-12-11 20:05:54 +00:00
|
|
|
delete(b.filers, address)
|
|
|
|
if b.currentFiler == address {
|
|
|
|
for filer := range b.filers {
|
|
|
|
b.currentFiler = filer
|
2022-07-12 09:00:54 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-12-11 20:05:54 +00:00
|
|
|
func (b *MessageQueueBroker) GetFiler() pb.ServerAddress {
|
|
|
|
return b.currentFiler
|
2022-07-12 09:00:54 +00:00
|
|
|
}
|
|
|
|
|
2023-12-11 20:05:54 +00:00
|
|
|
func (b *MessageQueueBroker) WithFilerClient(streamingMode bool, fn func(filer_pb.SeaweedFilerClient) error) error {
|
2020-03-04 08:39:47 +00:00
|
|
|
|
2023-12-11 20:05:54 +00:00
|
|
|
return pb.WithFilerClient(streamingMode, 0, b.GetFiler(), b.grpcDialOption, fn)
|
2022-07-16 17:49:34 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-12-11 20:05:54 +00:00
|
|
|
func (b *MessageQueueBroker) AdjustedUrl(location *filer_pb.Location) string {
|
2022-07-16 17:49:34 +00:00
|
|
|
|
|
|
|
return location.Url
|
2020-03-04 08:39:47 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-12-11 20:05:54 +00:00
|
|
|
func (b *MessageQueueBroker) GetDataCenter() string {
|
2022-08-05 00:35:00 +00:00
|
|
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-12-11 20:05:54 +00:00
|
|
|
func (b *MessageQueueBroker) withMasterClient(streamingMode bool, master pb.ServerAddress, fn func(client master_pb.SeaweedClient) error) error {
|
2020-03-04 08:39:47 +00:00
|
|
|
|
2023-12-11 20:05:54 +00:00
|
|
|
return pb.WithMasterClient(streamingMode, master, b.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
|
|
|
|
2023-12-11 20:05:54 +00:00
|
|
|
func (b *MessageQueueBroker) withBrokerClient(streamingMode bool, server pb.ServerAddress, fn func(client mq_pb.SeaweedMessagingClient) error) error {
|
2022-07-11 07:20:27 +00:00
|
|
|
|
2023-12-11 20:05:54 +00:00
|
|
|
return pb.WithBrokerGrpcClient(streamingMode, server.String(), b.grpcDialOption, func(client mq_pb.SeaweedMessagingClient) error {
|
2022-07-11 07:20:27 +00:00
|
|
|
return fn(client)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|