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"
|
|
|
|
|
2020-04-18 22:17:27 +00:00
|
|
|
"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
|
|
|
|
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-04-18 08:12:01 +00:00
|
|
|
topicLocks *TopicLocks
|
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-04-19 07:18:32 +00:00
|
|
|
messageBroker.topicLocks = NewTopicLocks(messageBroker)
|
|
|
|
|
2020-04-18 22:17:27 +00:00
|
|
|
messageBroker.checkPeers()
|
|
|
|
|
|
|
|
// go messageBroker.loopForEver()
|
2020-03-04 08:39:47 +00:00
|
|
|
|
|
|
|
return messageBroker, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (broker *MessageBroker) loopForEver() {
|
|
|
|
|
|
|
|
for {
|
|
|
|
broker.checkPeers()
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (broker *MessageBroker) checkPeers() {
|
|
|
|
|
|
|
|
// contact a filer about masters
|
|
|
|
var masters []string
|
2020-04-18 22:17:27 +00:00
|
|
|
found := false
|
|
|
|
for !found {
|
|
|
|
for _, filer := range broker.option.Filers {
|
|
|
|
err := broker.withFilerClient(filer, func(client filer_pb.SeaweedFilerClient) error {
|
|
|
|
resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
masters = append(masters, resp.Masters...)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
found = true
|
|
|
|
break
|
2020-03-04 08:39:47 +00:00
|
|
|
}
|
2020-04-18 22:17:27 +00:00
|
|
|
glog.V(0).Infof("failed to read masters from %+v: %v", broker.option.Filers, err)
|
|
|
|
time.Sleep(time.Second)
|
2020-03-04 08:39:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-18 22:17:27 +00:00
|
|
|
glog.V(0).Infof("received master list: %s", masters)
|
2020-03-04 08:39:47 +00:00
|
|
|
|
|
|
|
// contact each masters for filers
|
|
|
|
var filers []string
|
2020-04-18 22:17:27 +00:00
|
|
|
found = false
|
|
|
|
for !found {
|
|
|
|
for _, master := range masters {
|
|
|
|
err := broker.withMasterClient(master, func(client master_pb.SeaweedClient) error {
|
|
|
|
resp, err := client.ListMasterClients(context.Background(), &master_pb.ListMasterClientsRequest{
|
|
|
|
ClientType: "filer",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
filers = append(filers, resp.GrpcAddresses...)
|
|
|
|
|
|
|
|
return nil
|
2020-03-04 08:39:47 +00:00
|
|
|
})
|
2020-04-18 22:17:27 +00:00
|
|
|
if err == nil {
|
|
|
|
found = true
|
|
|
|
break
|
2020-03-04 08:39:47 +00:00
|
|
|
}
|
2020-04-18 22:17:27 +00:00
|
|
|
glog.V(0).Infof("failed to list filers: %v", err)
|
|
|
|
time.Sleep(time.Second)
|
2020-03-04 08:39:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-18 22:17:27 +00:00
|
|
|
glog.V(0).Infof("received filer list: %s", filers)
|
2020-03-04 08:39:47 +00:00
|
|
|
|
2020-04-18 22:17:27 +00:00
|
|
|
broker.option.Filers = filers
|
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)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|