2020-05-05 09:05:28 +00:00
|
|
|
package broker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-05-08 09:47:22 +00:00
|
|
|
"fmt"
|
2021-11-08 08:09:11 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/cluster"
|
2021-09-13 05:47:52 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb"
|
2020-05-05 09:05:28 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/messaging_pb"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
Topic discovery:
|
|
|
|
|
|
|
|
When pub or sub connects, it ask for the whole broker list, and run consistent hashing to find the broker.
|
|
|
|
|
|
|
|
The broker will check peers whether it is already hosted by some other broker, if that broker is alive and acknowledged alive, redirect to it.
|
|
|
|
Otherwise, just host the topic.
|
|
|
|
|
|
|
|
So, if the pub or sub connects around the same time, they would connect to the same broker. Everyone is happy.
|
|
|
|
If one of the pub or sub connects very late, and the system topo changed quite a bit with new servers added or old servers died, checking peers will help.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
func (broker *MessageBroker) FindBroker(c context.Context, request *messaging_pb.FindBrokerRequest) (*messaging_pb.FindBrokerResponse, error) {
|
|
|
|
|
2020-05-08 09:47:22 +00:00
|
|
|
t := &messaging_pb.FindBrokerResponse{}
|
|
|
|
var peers []string
|
|
|
|
|
|
|
|
targetTopicPartition := fmt.Sprintf(TopicPartitionFmt, request.Namespace, request.Topic, request.Parition)
|
|
|
|
|
|
|
|
for _, filer := range broker.option.Filers {
|
2021-12-26 08:15:03 +00:00
|
|
|
err := broker.withFilerClient(false, filer, func(client filer_pb.SeaweedFilerClient) error {
|
2020-05-08 09:47:22 +00:00
|
|
|
resp, err := client.LocateBroker(context.Background(), &filer_pb.LocateBrokerRequest{
|
|
|
|
Resource: targetTopicPartition,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp.Found && len(resp.Resources) > 0 {
|
|
|
|
t.Broker = resp.Resources[0].GrpcAddresses
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, b := range resp.Resources {
|
|
|
|
peers = append(peers, b.GrpcAddresses)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 09:05:28 +00:00
|
|
|
|
2020-05-08 09:47:22 +00:00
|
|
|
t.Broker = PickMember(peers, []byte(targetTopicPartition))
|
2020-05-05 09:05:28 +00:00
|
|
|
|
2020-05-08 09:47:22 +00:00
|
|
|
return t, nil
|
|
|
|
|
|
|
|
}
|
2020-05-05 09:05:28 +00:00
|
|
|
|
2020-05-08 09:47:22 +00:00
|
|
|
func (broker *MessageBroker) checkFilers() {
|
2020-05-05 09:05:28 +00:00
|
|
|
|
|
|
|
// contact a filer about masters
|
2021-09-13 05:47:52 +00:00
|
|
|
var masters []pb.ServerAddress
|
2020-05-05 09:05:28 +00:00
|
|
|
found := false
|
|
|
|
for !found {
|
|
|
|
for _, filer := range broker.option.Filers {
|
2021-12-26 08:15:03 +00:00
|
|
|
err := broker.withFilerClient(false, filer, func(client filer_pb.SeaweedFilerClient) error {
|
2020-05-05 09:05:28 +00:00
|
|
|
resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-13 05:47:52 +00:00
|
|
|
for _, m := range resp.Masters {
|
|
|
|
masters = append(masters, pb.ServerAddress(m))
|
|
|
|
}
|
2020-05-05 09:05:28 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
glog.V(0).Infof("failed to read masters from %+v: %v", broker.option.Filers, err)
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
glog.V(0).Infof("received master list: %s", masters)
|
|
|
|
|
|
|
|
// contact each masters for filers
|
2021-09-13 05:47:52 +00:00
|
|
|
var filers []pb.ServerAddress
|
2020-05-05 09:05:28 +00:00
|
|
|
found = false
|
|
|
|
for !found {
|
|
|
|
for _, master := range masters {
|
2021-12-26 08:15:03 +00:00
|
|
|
err := broker.withMasterClient(false, master, func(client master_pb.SeaweedClient) error {
|
2021-11-03 06:38:45 +00:00
|
|
|
resp, err := client.ListClusterNodes(context.Background(), &master_pb.ListClusterNodesRequest{
|
2021-11-08 08:09:11 +00:00
|
|
|
ClientType: cluster.FilerType,
|
2020-05-05 09:05:28 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-03 06:38:45 +00:00
|
|
|
for _, clusterNode := range resp.ClusterNodes {
|
|
|
|
filers = append(filers, pb.ServerAddress(clusterNode.Address))
|
|
|
|
}
|
2020-05-05 09:05:28 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
glog.V(0).Infof("failed to list filers: %v", err)
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
glog.V(0).Infof("received filer list: %s", filers)
|
|
|
|
|
|
|
|
broker.option.Filers = filers
|
|
|
|
|
|
|
|
}
|