package command import ( "context" "fmt" "net" "sync/atomic" "google.golang.org/grpc" "google.golang.org/grpc/reflection" "github.com/seaweedfs/seaweedfs/weed/glog" "github.com/seaweedfs/seaweedfs/weed/mq/broker" "github.com/seaweedfs/seaweedfs/weed/pb" "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb" "github.com/seaweedfs/seaweedfs/weed/security" "github.com/seaweedfs/seaweedfs/weed/util" "github.com/seaweedfs/seaweedfs/weed/util/grace" ) var ( mqBrokerStandaloneOptions MessageQueueBrokerOptions ) const ( defaultMqBrokerPort = 17777 defaultMqBrokerLogFlushInterval = 5 ) type MessageQueueBrokerOptions struct { masters map[string]pb.ServerAddress mastersString *string filerGroup *string ip *string ipBind *string listenOnLocalhost *bool port *int dataCenter *string rack *string cpuprofile *string memprofile *string logFlushInterval *int debug *bool debugPort *int // shutdownCtx, when non-nil, stops the broker once cancelled, in addition // to on interrupt. Set by commands that embed the broker; not a flag. shutdownCtx context.Context // bound records whether the gRPC port is bound, so weed mini lists the // broker only when it runs; not a flag. bound atomic.Bool // TLS options listenQueueServer loads for serveQueueServer. grpcDialOption grpc.DialOption grpcServerOptions []grpc.ServerOption } func init() { cmdMqBroker.Run = runMqBroker // break init cycle mqBrokerStandaloneOptions.mastersString = cmdMqBroker.Flag.String("master", "localhost:9333", "comma-separated master servers") mqBrokerStandaloneOptions.filerGroup = cmdMqBroker.Flag.String("filerGroup", "", "share metadata with other filers in the same filerGroup") mqBrokerStandaloneOptions.ip = cmdMqBroker.Flag.String("ip", util.DetectedHostAddress(), "broker host address") mqBrokerStandaloneOptions.ipBind = cmdMqBroker.Flag.String("ip.bind", "", "ip address to bind to. If empty, listen on all interfaces.") mqBrokerStandaloneOptions.port = cmdMqBroker.Flag.Int("port", defaultMqBrokerPort, "broker gRPC listen port") mqBrokerStandaloneOptions.dataCenter = cmdMqBroker.Flag.String("dataCenter", "", "prefer to read and write to volumes in this data center") mqBrokerStandaloneOptions.rack = cmdMqBroker.Flag.String("rack", "", "prefer to write to volumes in this rack") mqBrokerStandaloneOptions.cpuprofile = cmdMqBroker.Flag.String("cpuprofile", "", "cpu profile output file") mqBrokerStandaloneOptions.memprofile = cmdMqBroker.Flag.String("memprofile", "", "memory profile output file") mqBrokerStandaloneOptions.logFlushInterval = cmdMqBroker.Flag.Int("logFlushInterval", defaultMqBrokerLogFlushInterval, "log buffer flush interval in seconds") mqBrokerStandaloneOptions.debug = cmdMqBroker.Flag.Bool("debug", false, "serves runtime profiling data via pprof on the port specified by -debug.port") mqBrokerStandaloneOptions.debugPort = cmdMqBroker.Flag.Int("debug.port", 6060, "http port for debugging") } var cmdMqBroker = &Command{ UsageLine: "mq.broker [-port=17777] [-master=]", Short: " start a message queue broker", 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 runMqBroker(cmd *Command, args []string) bool { if *mqBrokerStandaloneOptions.debug { grace.StartDebugServer(*mqBrokerStandaloneOptions.debugPort) } util.LoadSecurityConfiguration() *mqBrokerStandaloneOptions.cpuprofile = util.ResolvePath(*mqBrokerStandaloneOptions.cpuprofile) *mqBrokerStandaloneOptions.memprofile = util.ResolvePath(*mqBrokerStandaloneOptions.memprofile) grace.SetupProfiling(*mqBrokerStandaloneOptions.cpuprofile, *mqBrokerStandaloneOptions.memprofile) mqBrokerStandaloneOptions.masters = pb.ServerAddresses(*mqBrokerStandaloneOptions.mastersString).ToAddressMap() if err := mqBrokerStandaloneOptions.startQueueServer(); err != nil { glog.Fatalf("%v", err) } return true } // startQueueServer serves the broker until it is interrupted or shutdownCtx is // cancelled. It returns an error only when the broker could not start. func (mqBrokerOpt *MessageQueueBrokerOptions) startQueueServer() error { grpcL, localL, err := mqBrokerOpt.listenQueueServer() if err != nil { return err } return mqBrokerOpt.serveQueueServer(grpcL, localL) } // listenQueueServer loads the broker's TLS config and binds its gRPC port. // Bind before creating the broker: it registers with the master as soon as // it exists, and a broker that cannot serve must not. func (mqBrokerOpt *MessageQueueBrokerOptions) listenQueueServer() (grpcL, localL net.Listener, err error) { mqBrokerOpt.bound.Store(false) // Returned, not fatal: a gateway's broker must not take the gateway down. if mqBrokerOpt.grpcDialOption, err = security.TryLoadClientTLS(util.GetViper(), "grpc.msg_broker", "grpc.msg_broker"); err != nil { return nil, nil, fmt.Errorf("message queue broker gRPC client TLS: %v", err) } tlsCreds, tlsVerify, err := security.TryLoadServerTLS(util.GetViper(), "grpc.msg_broker") if err != nil { return nil, nil, fmt.Errorf("message queue broker gRPC server TLS: %v", err) } mqBrokerOpt.grpcServerOptions = append([]grpc.ServerOption{tlsCreds, tlsVerify}, broker.GrpcServerOptions()...) // The loopback companion listener is only kept while -ip.bind is unset. // An explicit -ip.bind pins the port to exactly that address. listenOnLocalhost := *mqBrokerOpt.ipBind == "" if mqBrokerOpt.listenOnLocalhost != nil { listenOnLocalhost = *mqBrokerOpt.listenOnLocalhost } grpcL, localL, err = util.NewIpAndLocalListeners(*mqBrokerOpt.ipBind, *mqBrokerOpt.port, 0, listenOnLocalhost) if err != nil { return nil, nil, fmt.Errorf("message queue broker failed to listen on grpc port %d: %v", *mqBrokerOpt.port, err) } mqBrokerOpt.bound.Store(true) return grpcL, localL, nil } // boundAddress is the address the broker registers with the master, or empty // when it is not running. func (mqBrokerOpt *MessageQueueBrokerOptions) boundAddress() pb.ServerAddress { if mqBrokerOpt == nil || !mqBrokerOpt.bound.Load() { return "" } return pb.ServerAddress(util.JoinHostPort(*mqBrokerOpt.ip, *mqBrokerOpt.port)) } func (mqBrokerOpt *MessageQueueBrokerOptions) serveQueueServer(grpcL, localL net.Listener) error { qs, err := broker.NewMessageBroker(&broker.MessageQueueBrokerOption{ Masters: mqBrokerOpt.masters, FilerGroup: *mqBrokerOpt.filerGroup, DataCenter: *mqBrokerOpt.dataCenter, Rack: *mqBrokerOpt.rack, DefaultReplication: "", MaxMB: 0, Ip: *mqBrokerOpt.ip, Port: *mqBrokerOpt.port, LogFlushInterval: *mqBrokerOpt.logFlushInterval, }, mqBrokerOpt.grpcDialOption) if err != nil { mqBrokerOpt.bound.Store(false) grpcL.Close() if localL != nil { localL.Close() } return fmt.Errorf("failed to create new message broker for queue server: %v", err) } // Create main gRPC server serverOptions := mqBrokerOpt.grpcServerOptions grpcS := pb.NewGrpcServer(serverOptions...) mq_pb.RegisterSeaweedMessagingServer(grpcS, qs) reflection.Register(grpcS) grace.OnInterrupt(grpcS.Stop) // Start localhost listener if available var localGrpcS *grpc.Server if localL != nil { localGrpcS = pb.NewGrpcServer(serverOptions...) mq_pb.RegisterSeaweedMessagingServer(localGrpcS, qs) reflection.Register(localGrpcS) grace.OnInterrupt(localGrpcS.Stop) go func() { glog.V(0).Infof("MQ Broker listening on localhost:%d", *mqBrokerOpt.port) if err := localGrpcS.Serve(localL); err != nil && err != grpc.ErrServerStopped { glog.Errorf("MQ Broker localhost listener error: %v", err) } }() } // Hooks run last-registered first: flush the partitions, then drop the connections. grace.OnInterrupt(qs.Stop) if mqBrokerOpt.shutdownCtx != nil { go func() { <-mqBrokerOpt.shutdownCtx.Done() qs.Stop() if localGrpcS != nil { localGrpcS.Stop() } grpcS.Stop() }() } glog.V(0).Infof("MQ Broker listening on %s:%d", *mqBrokerOpt.ipBind, *mqBrokerOpt.port) if err := grpcS.Serve(grpcL); err != nil && err != grpc.ErrServerStopped { glog.Errorf("Failed to serve MQ Broker: %v", err) } return nil }