seaweedfs/weed/command/msg_broker.go

115 lines
3.5 KiB
Go
Raw Normal View History

2020-03-04 16:39:47 +08:00
package command
import (
"context"
"fmt"
"strconv"
"time"
"google.golang.org/grpc/reflection"
2020-05-05 17:05:28 +08:00
"github.com/chrislusf/seaweedfs/weed/util/grace"
2020-04-16 17:55:09 +08:00
"github.com/chrislusf/seaweedfs/weed/glog"
2020-04-18 16:12:01 +08:00
"github.com/chrislusf/seaweedfs/weed/messaging/broker"
2020-03-04 16:39:47 +08:00
"github.com/chrislusf/seaweedfs/weed/pb"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
2020-04-16 17:21:23 +08:00
"github.com/chrislusf/seaweedfs/weed/pb/messaging_pb"
2020-03-04 16:39:47 +08:00
"github.com/chrislusf/seaweedfs/weed/security"
"github.com/chrislusf/seaweedfs/weed/util"
)
var (
messageBrokerStandaloneOptions MessageBrokerOptions
2020-03-04 16:39:47 +08:00
)
type MessageBrokerOptions struct {
2020-04-19 18:03:40 +08:00
filer *string
2020-05-05 17:05:28 +08:00
ip *string
2020-04-19 18:03:40 +08:00
port *int
cpuprofile *string
memprofile *string
2020-03-04 16:39:47 +08:00
}
func init() {
cmdMsgBroker.Run = runMsgBroker // break init cycle
messageBrokerStandaloneOptions.filer = cmdMsgBroker.Flag.String("filer", "localhost:8888", "filer server address")
2020-05-05 17:05:28 +08:00
messageBrokerStandaloneOptions.ip = cmdMsgBroker.Flag.String("ip", util.DetectedHostAddress(), "broker host address")
messageBrokerStandaloneOptions.port = cmdMsgBroker.Flag.Int("port", 17777, "broker gRPC listen port")
2020-04-19 18:03:40 +08:00
messageBrokerStandaloneOptions.cpuprofile = cmdMsgBroker.Flag.String("cpuprofile", "", "cpu profile output file")
messageBrokerStandaloneOptions.memprofile = cmdMsgBroker.Flag.String("memprofile", "", "memory profile output file")
2020-03-04 16:39:47 +08:00
}
var cmdMsgBroker = &Command{
UsageLine: "msgBroker [-port=17777] [-filer=<ip:port>]",
Short: "start a message queue broker",
2020-03-04 16:39:47 +08:00
Long: `start a message queue broker
The broker can accept gRPC calls to write or read messages. The messages are stored via filer.
The brokers are stateless. To scale up, just add more brokers.
`,
}
func runMsgBroker(cmd *Command, args []string) bool {
util.LoadConfiguration("security", false)
return messageBrokerStandaloneOptions.startQueueServer()
}
func (msgBrokerOpt *MessageBrokerOptions) startQueueServer() bool {
2020-03-04 16:39:47 +08:00
grace.SetupProfiling(*messageBrokerStandaloneOptions.cpuprofile, *messageBrokerStandaloneOptions.memprofile)
2020-04-19 18:03:40 +08:00
2021-03-03 12:59:39 +08:00
filerGrpcAddress, err := pb.ParseServerToGrpcAddress(*msgBrokerOpt.filer)
2020-03-04 16:39:47 +08:00
if err != nil {
glog.Fatal(err)
return false
}
2020-04-17 17:29:00 +08:00
grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.msg_broker")
cipher := false
2020-03-04 16:39:47 +08:00
for {
err = pb.WithGrpcFilerClient(filerGrpcAddress, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
2020-04-17 17:29:00 +08:00
resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
2020-03-04 16:39:47 +08:00
if err != nil {
return fmt.Errorf("get filer %s configuration: %v", filerGrpcAddress, err)
}
2020-04-17 17:29:00 +08:00
cipher = resp.Cipher
2020-03-04 16:39:47 +08:00
return nil
})
if err != nil {
glog.V(0).Infof("wait to connect to filer %s grpc address %s", *msgBrokerOpt.filer, filerGrpcAddress)
time.Sleep(time.Second)
} else {
glog.V(0).Infof("connected to filer %s grpc address %s", *msgBrokerOpt.filer, filerGrpcAddress)
break
}
}
2020-04-18 16:12:01 +08:00
qs, err := broker.NewMessageBroker(&broker.MessageBrokerOption{
2020-03-04 16:39:47 +08:00
Filers: []string{*msgBrokerOpt.filer},
DefaultReplication: "",
MaxMB: 0,
2020-05-05 17:05:28 +08:00
Ip: *msgBrokerOpt.ip,
2020-03-04 16:39:47 +08:00
Port: *msgBrokerOpt.port,
2020-04-17 17:29:00 +08:00
Cipher: cipher,
}, grpcDialOption)
2020-03-04 16:39:47 +08:00
// start grpc listener
grpcL, err := util.NewListener(":"+strconv.Itoa(*msgBrokerOpt.port), 0)
if err != nil {
glog.Fatalf("failed to listen on grpc port %d: %v", *msgBrokerOpt.port, err)
}
grpcS := pb.NewGrpcServer(security.LoadServerTLS(util.GetViper(), "grpc.msg_broker"))
2020-04-16 17:21:23 +08:00
messaging_pb.RegisterSeaweedMessagingServer(grpcS, qs)
2020-03-04 16:39:47 +08:00
reflection.Register(grpcS)
grpcS.Serve(grpcL)
return true
}