You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.3 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package command
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "time"
  7. "google.golang.org/grpc/reflection"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/messaging/broker"
  10. "github.com/chrislusf/seaweedfs/weed/pb"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "github.com/chrislusf/seaweedfs/weed/pb/messaging_pb"
  13. "github.com/chrislusf/seaweedfs/weed/security"
  14. "github.com/chrislusf/seaweedfs/weed/util"
  15. )
  16. var (
  17. messageBrokerStandaloneOptions QueueOptions
  18. )
  19. type QueueOptions struct {
  20. filer *string
  21. port *int
  22. cpuprofile *string
  23. memprofile *string
  24. }
  25. func init() {
  26. cmdMsgBroker.Run = runMsgBroker // break init cycle
  27. messageBrokerStandaloneOptions.filer = cmdMsgBroker.Flag.String("filer", "localhost:8888", "filer server address")
  28. messageBrokerStandaloneOptions.port = cmdMsgBroker.Flag.Int("port", 17777, "queue server gRPC listen port")
  29. messageBrokerStandaloneOptions.cpuprofile = cmdMsgBroker.Flag.String("cpuprofile", "", "cpu profile output file")
  30. messageBrokerStandaloneOptions.memprofile = cmdMsgBroker.Flag.String("memprofile", "", "memory profile output file")
  31. }
  32. var cmdMsgBroker = &Command{
  33. UsageLine: "msg.broker [-port=17777] [-filer=<ip:port>]",
  34. Short: "<WIP> start a message queue broker",
  35. Long: `start a message queue broker
  36. The broker can accept gRPC calls to write or read messages. The messages are stored via filer.
  37. The brokers are stateless. To scale up, just add more brokers.
  38. `,
  39. }
  40. func runMsgBroker(cmd *Command, args []string) bool {
  41. util.LoadConfiguration("security", false)
  42. return messageBrokerStandaloneOptions.startQueueServer()
  43. }
  44. func (msgBrokerOpt *QueueOptions) startQueueServer() bool {
  45. util.SetupProfiling(*messageBrokerStandaloneOptions.cpuprofile, *messageBrokerStandaloneOptions.memprofile)
  46. filerGrpcAddress, err := pb.ParseFilerGrpcAddress(*msgBrokerOpt.filer)
  47. if err != nil {
  48. glog.Fatal(err)
  49. return false
  50. }
  51. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.msg_broker")
  52. cipher := false
  53. for {
  54. err = pb.WithGrpcFilerClient(filerGrpcAddress, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  55. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  56. if err != nil {
  57. return fmt.Errorf("get filer %s configuration: %v", filerGrpcAddress, err)
  58. }
  59. cipher = resp.Cipher
  60. return nil
  61. })
  62. if err != nil {
  63. glog.V(0).Infof("wait to connect to filer %s grpc address %s", *msgBrokerOpt.filer, filerGrpcAddress)
  64. time.Sleep(time.Second)
  65. } else {
  66. glog.V(0).Infof("connected to filer %s grpc address %s", *msgBrokerOpt.filer, filerGrpcAddress)
  67. break
  68. }
  69. }
  70. qs, err := broker.NewMessageBroker(&broker.MessageBrokerOption{
  71. Filers: []string{*msgBrokerOpt.filer},
  72. DefaultReplication: "",
  73. MaxMB: 0,
  74. Port: *msgBrokerOpt.port,
  75. Cipher: cipher,
  76. }, grpcDialOption)
  77. // start grpc listener
  78. grpcL, err := util.NewListener(":"+strconv.Itoa(*msgBrokerOpt.port), 0)
  79. if err != nil {
  80. glog.Fatalf("failed to listen on grpc port %d: %v", *msgBrokerOpt.port, err)
  81. }
  82. grpcS := pb.NewGrpcServer(security.LoadServerTLS(util.GetViper(), "grpc.msg_broker"))
  83. messaging_pb.RegisterSeaweedMessagingServer(grpcS, qs)
  84. reflection.Register(grpcS)
  85. grpcS.Serve(grpcL)
  86. return true
  87. }