|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/uber-go/tally" |
| 8 | + "github.com/urfave/cli/v2" |
| 9 | + "go.uber.org/fx" |
| 10 | + "go.uber.org/yarpc" |
| 11 | + "go.uber.org/yarpc/transport/grpc" |
| 12 | + "go.uber.org/zap" |
| 13 | + |
| 14 | + "github.com/uber/cadence/common/clock" |
| 15 | + "github.com/uber/cadence/common/log" |
| 16 | + "github.com/uber/cadence/service/sharddistributor/canary" |
| 17 | + "github.com/uber/cadence/service/sharddistributor/executorclient" |
| 18 | + "github.com/uber/cadence/tools/common/commoncli" |
| 19 | +) |
| 20 | + |
| 21 | +const ( |
| 22 | + // Default configuration |
| 23 | + defaultShardDistributorEndpoint = "127.0.0.1:7943" |
| 24 | + defaultFixedNamespace = "shard-distributor-canary" |
| 25 | + defaultEphemeralNamespace = "shard-distributor-canary-ephemeral" |
| 26 | + |
| 27 | + shardDistributorServiceName = "cadence-shard-distributor" |
| 28 | +) |
| 29 | + |
| 30 | +func runApp(c *cli.Context) { |
| 31 | + endpoint := c.String("endpoint") |
| 32 | + fixedNamespace := c.String("fixed-namespace") |
| 33 | + ephemeralNamespace := c.String("ephemeral-namespace") |
| 34 | + |
| 35 | + fx.New(opts(fixedNamespace, ephemeralNamespace, endpoint)).Run() |
| 36 | +} |
| 37 | + |
| 38 | +func opts(fixedNamespace, ephemeralNamespace, endpoint string) fx.Option { |
| 39 | + config := executorclient.Config{ |
| 40 | + Namespaces: []executorclient.NamespaceConfig{ |
| 41 | + {Namespace: fixedNamespace, HeartBeatInterval: 1 * time.Second}, |
| 42 | + {Namespace: ephemeralNamespace, HeartBeatInterval: 1 * time.Second}, |
| 43 | + }, |
| 44 | + } |
| 45 | + |
| 46 | + transport := grpc.NewTransport() |
| 47 | + yarpcConfig := yarpc.Config{ |
| 48 | + Name: "shard-distributor-canary", |
| 49 | + Outbounds: yarpc.Outbounds{ |
| 50 | + shardDistributorServiceName: { |
| 51 | + Unary: transport.NewSingleOutbound(endpoint), |
| 52 | + }, |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + return fx.Options( |
| 57 | + fx.Supply( |
| 58 | + fx.Annotate(tally.NoopScope, fx.As(new(tally.Scope))), |
| 59 | + fx.Annotate(clock.NewRealTimeSource(), fx.As(new(clock.TimeSource))), |
| 60 | + yarpcConfig, |
| 61 | + config, |
| 62 | + ), |
| 63 | + fx.Provide( |
| 64 | + yarpc.NewDispatcher, |
| 65 | + func(d *yarpc.Dispatcher) yarpc.ClientConfig { return d }, // Reprovide the dispatcher as a client config |
| 66 | + ), |
| 67 | + fx.Provide(zap.NewDevelopment), |
| 68 | + fx.Provide(log.NewLogger), |
| 69 | + |
| 70 | + // Start the YARPC dispatcher |
| 71 | + fx.Invoke(func(lc fx.Lifecycle, dispatcher *yarpc.Dispatcher) { |
| 72 | + lc.Append(fx.StartStopHook(dispatcher.Start, dispatcher.Stop)) |
| 73 | + }), |
| 74 | + |
| 75 | + // Include the canary module |
| 76 | + canary.Module(fixedNamespace, ephemeralNamespace, shardDistributorServiceName), |
| 77 | + ) |
| 78 | +} |
| 79 | + |
| 80 | +func buildCLI() *cli.App { |
| 81 | + app := cli.NewApp() |
| 82 | + app.Name = "sharddistributor-canary" |
| 83 | + app.Usage = "Cadence shard distributor canary" |
| 84 | + app.Version = "0.0.1" |
| 85 | + |
| 86 | + app.Commands = []*cli.Command{ |
| 87 | + { |
| 88 | + Name: "start", |
| 89 | + Usage: "start shard distributor canary", |
| 90 | + Flags: []cli.Flag{ |
| 91 | + &cli.StringFlag{ |
| 92 | + Name: "endpoint", |
| 93 | + Aliases: []string{"e"}, |
| 94 | + Value: defaultShardDistributorEndpoint, |
| 95 | + Usage: "shard distributor endpoint address", |
| 96 | + }, |
| 97 | + &cli.StringFlag{ |
| 98 | + Name: "fixed-namespace", |
| 99 | + Value: defaultFixedNamespace, |
| 100 | + Usage: "namespace for fixed shard processing", |
| 101 | + }, |
| 102 | + &cli.StringFlag{ |
| 103 | + Name: "ephemeral-namespace", |
| 104 | + Value: defaultEphemeralNamespace, |
| 105 | + Usage: "namespace for ephemeral shard creation testing", |
| 106 | + }, |
| 107 | + }, |
| 108 | + Action: func(c *cli.Context) error { |
| 109 | + runApp(c) |
| 110 | + return nil |
| 111 | + }, |
| 112 | + }, |
| 113 | + } |
| 114 | + |
| 115 | + return app |
| 116 | +} |
| 117 | + |
| 118 | +func main() { |
| 119 | + app := buildCLI() |
| 120 | + commoncli.ExitHandler(app.Run(os.Args)) |
| 121 | +} |
0 commit comments