2020-04-18 16:12:01 +08:00
|
|
|
package broker
|
2020-03-04 16:39:47 +08:00
|
|
|
|
|
|
|
import (
|
2023-09-17 06:05:38 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
2023-12-12 04:05:54 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/pub_balancer"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/sub_coordinator"
|
2023-08-21 13:53:05 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/topic"
|
2024-01-16 13:22:41 +08:00
|
|
|
"sync"
|
2022-08-24 14:18:21 +08:00
|
|
|
"time"
|
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/cluster"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/wdclient"
|
2020-03-04 16:39:47 +08:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2022-07-29 15:17:28 +08: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 16:39:47 +08:00
|
|
|
)
|
|
|
|
|
2022-07-02 13:44:28 +08:00
|
|
|
type MessageQueueBrokerOption struct {
|
2022-07-02 14:34:51 +08:00
|
|
|
Masters map[string]pb.ServerAddress
|
|
|
|
FilerGroup string
|
2022-07-03 15:29:25 +08:00
|
|
|
DataCenter string
|
|
|
|
Rack string
|
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
|
2024-01-09 15:27:02 +08:00
|
|
|
VolumeServerAccess string // how to access volume servers
|
2020-03-04 16:39:47 +08:00
|
|
|
}
|
|
|
|
|
2024-01-17 16:15:52 +08:00
|
|
|
func (option *MessageQueueBrokerOption) BrokerAddress() pb.ServerAddress {
|
|
|
|
return pb.NewServerAddress(option.Ip, option.Port, 0)
|
|
|
|
}
|
|
|
|
|
2022-07-02 13:44:28 +08:00
|
|
|
type MessageQueueBroker struct {
|
2022-07-02 13:43:25 +08:00
|
|
|
mq_pb.UnimplementedSeaweedMessagingServer
|
2023-08-21 13:53:05 +08:00
|
|
|
option *MessageQueueBrokerOption
|
|
|
|
grpcDialOption grpc.DialOption
|
|
|
|
MasterClient *wdclient.MasterClient
|
|
|
|
filers map[pb.ServerAddress]struct{}
|
|
|
|
currentFiler pb.ServerAddress
|
|
|
|
localTopicManager *topic.LocalTopicManager
|
2024-05-22 01:02:07 +08:00
|
|
|
PubBalancer *pub_balancer.PubBalancer
|
2024-05-23 23:25:53 +08:00
|
|
|
lockAsBalancer *cluster.LiveLock
|
|
|
|
SubCoordinator *sub_coordinator.SubCoordinator
|
|
|
|
accessLock sync.Mutex
|
2024-05-21 02:05:18 +08:00
|
|
|
fca *sub_coordinator.FilerClientAccessor
|
2020-03-04 16:39:47 +08:00
|
|
|
}
|
|
|
|
|
2022-07-02 14:34:51 +08:00
|
|
|
func NewMessageBroker(option *MessageQueueBrokerOption, grpcDialOption grpc.DialOption) (mqBroker *MessageQueueBroker, err error) {
|
2020-03-04 16:39:47 +08:00
|
|
|
|
2024-05-22 01:02:07 +08:00
|
|
|
pubBalancer := pub_balancer.NewPubBalancer()
|
|
|
|
subCoordinator := sub_coordinator.NewSubCoordinator(pubBalancer)
|
2023-12-12 04:05:54 +08:00
|
|
|
|
2022-07-02 14:34:51 +08:00
|
|
|
mqBroker = &MessageQueueBroker{
|
2023-08-21 13:53:05 +08:00
|
|
|
option: option,
|
|
|
|
grpcDialOption: grpcDialOption,
|
2024-01-17 16:15:52 +08:00
|
|
|
MasterClient: wdclient.NewMasterClient(grpcDialOption, option.FilerGroup, cluster.BrokerType, option.BrokerAddress(), option.DataCenter, option.Rack, *pb.NewServiceDiscoveryFromMap(option.Masters)),
|
2023-08-21 13:53:05 +08:00
|
|
|
filers: make(map[pb.ServerAddress]struct{}),
|
|
|
|
localTopicManager: topic.NewLocalTopicManager(),
|
2024-05-22 01:02:07 +08:00
|
|
|
PubBalancer: pubBalancer,
|
|
|
|
SubCoordinator: subCoordinator,
|
2020-03-04 16:39:47 +08:00
|
|
|
}
|
2024-05-14 23:50:17 +08:00
|
|
|
fca := &sub_coordinator.FilerClientAccessor{
|
2024-05-21 02:05:18 +08:00
|
|
|
GetFiler: mqBroker.GetFiler,
|
2024-05-15 00:09:36 +08:00
|
|
|
GetGrpcDialOption: mqBroker.GetGrpcDialOption,
|
2024-05-14 23:50:17 +08:00
|
|
|
}
|
|
|
|
mqBroker.fca = fca
|
2024-05-22 01:02:07 +08:00
|
|
|
subCoordinator.FilerClientAccessor = fca
|
2024-05-14 23:50:17 +08:00
|
|
|
|
2022-08-27 01:18:49 +08:00
|
|
|
mqBroker.MasterClient.SetOnPeerUpdateFn(mqBroker.OnBrokerUpdate)
|
2024-05-22 01:02:07 +08:00
|
|
|
pubBalancer.OnPartitionChange = mqBroker.SubCoordinator.OnPartitionChange
|
|
|
|
pubBalancer.OnAddBroker = mqBroker.SubCoordinator.OnSubAddBroker
|
|
|
|
pubBalancer.OnRemoveBroker = mqBroker.SubCoordinator.OnSubRemoveBroker
|
2020-04-19 06:17:27 +08:00
|
|
|
|
2022-07-02 14:34:51 +08:00
|
|
|
go mqBroker.MasterClient.KeepConnectedToMaster()
|
2020-03-04 16:39:47 +08:00
|
|
|
|
2022-07-12 17:00:54 +08:00
|
|
|
existingNodes := cluster.ListExistingPeerUpdates(mqBroker.MasterClient.GetMaster(), grpcDialOption, option.FilerGroup, cluster.FilerType)
|
|
|
|
for _, newNode := range existingNodes {
|
|
|
|
mqBroker.OnBrokerUpdate(newNode, time.Now())
|
|
|
|
}
|
|
|
|
|
2023-09-17 06:05:38 +08:00
|
|
|
// keep connecting to balancer
|
|
|
|
go func() {
|
|
|
|
for mqBroker.currentFiler == "" {
|
|
|
|
time.Sleep(time.Millisecond * 237)
|
|
|
|
}
|
2024-01-17 16:15:52 +08:00
|
|
|
self := option.BrokerAddress()
|
2023-12-12 04:05:54 +08:00
|
|
|
glog.V(0).Infof("broker %s found filer %s", self, mqBroker.currentFiler)
|
2023-09-17 06:05:38 +08:00
|
|
|
|
2024-03-10 04:56:49 +08:00
|
|
|
newBrokerBalancerCh := make(chan string, 1)
|
2023-09-17 06:05:38 +08:00
|
|
|
lockClient := cluster.NewLockClient(grpcDialOption, mqBroker.currentFiler)
|
2024-02-03 07:54:57 +08:00
|
|
|
mqBroker.lockAsBalancer = lockClient.StartLongLivedLock(pub_balancer.LockBrokerBalancer, string(self), func(newLockOwner string) {
|
2024-03-10 04:56:49 +08:00
|
|
|
glog.V(0).Infof("broker %s found balanacer %s", self, newLockOwner)
|
|
|
|
newBrokerBalancerCh <- newLockOwner
|
2024-02-03 07:54:57 +08:00
|
|
|
})
|
2024-03-10 04:56:49 +08:00
|
|
|
mqBroker.KeepConnectedToBrokerBalancer(newBrokerBalancerCh)
|
2023-09-17 06:05:38 +08:00
|
|
|
}()
|
|
|
|
|
2022-07-02 14:34:51 +08:00
|
|
|
return mqBroker, nil
|
2020-03-04 16:39:47 +08:00
|
|
|
}
|
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
func (b *MessageQueueBroker) OnBrokerUpdate(update *master_pb.ClusterNodeUpdate, startFrom time.Time) {
|
2022-07-12 17:00:54 +08:00
|
|
|
if update.NodeType != cluster.FilerType {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
address := pb.ServerAddress(update.Address)
|
|
|
|
if update.IsAdd {
|
2023-12-12 04:05:54 +08:00
|
|
|
b.filers[address] = struct{}{}
|
|
|
|
if b.currentFiler == "" {
|
|
|
|
b.currentFiler = address
|
2022-07-12 17:00:54 +08:00
|
|
|
}
|
|
|
|
} else {
|
2023-12-12 04:05:54 +08:00
|
|
|
delete(b.filers, address)
|
|
|
|
if b.currentFiler == address {
|
|
|
|
for filer := range b.filers {
|
|
|
|
b.currentFiler = filer
|
2022-07-12 17:00:54 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-05-15 00:09:36 +08:00
|
|
|
func (b *MessageQueueBroker) GetGrpcDialOption() grpc.DialOption {
|
|
|
|
return b.grpcDialOption
|
|
|
|
}
|
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
func (b *MessageQueueBroker) GetFiler() pb.ServerAddress {
|
|
|
|
return b.currentFiler
|
2022-07-12 17:00:54 +08:00
|
|
|
}
|
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
func (b *MessageQueueBroker) WithFilerClient(streamingMode bool, fn func(filer_pb.SeaweedFilerClient) error) error {
|
2020-03-04 16:39:47 +08:00
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
return pb.WithFilerClient(streamingMode, 0, b.GetFiler(), b.grpcDialOption, fn)
|
2022-07-17 01:49:34 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
func (b *MessageQueueBroker) AdjustedUrl(location *filer_pb.Location) string {
|
2022-07-17 01:49:34 +08:00
|
|
|
|
|
|
|
return location.Url
|
2020-03-04 16:39:47 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
func (b *MessageQueueBroker) GetDataCenter() string {
|
2022-08-05 08:35:00 +08:00
|
|
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
func (b *MessageQueueBroker) withMasterClient(streamingMode bool, master pb.ServerAddress, fn func(client master_pb.SeaweedClient) error) error {
|
2020-03-04 16:39:47 +08:00
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
return pb.WithMasterClient(streamingMode, master, b.grpcDialOption, false, func(client master_pb.SeaweedClient) error {
|
2020-03-04 16:39:47 +08:00
|
|
|
return fn(client)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
2022-07-11 15:20:27 +08:00
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
func (b *MessageQueueBroker) withBrokerClient(streamingMode bool, server pb.ServerAddress, fn func(client mq_pb.SeaweedMessagingClient) error) error {
|
2022-07-11 15:20:27 +08:00
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
return pb.WithBrokerGrpcClient(streamingMode, server.String(), b.grpcDialOption, func(client mq_pb.SeaweedMessagingClient) error {
|
2022-07-11 15:20:27 +08:00
|
|
|
return fn(client)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|