|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "log/slog" |
| 8 | + "net" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "os/signal" |
| 12 | + "syscall" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/bootjp/elastickv/proxy" |
| 16 | + "github.com/prometheus/client_golang/prometheus" |
| 17 | + "github.com/prometheus/client_golang/prometheus/promhttp" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + sentryFlushTimeout = 2 * time.Second |
| 22 | + metricsShutdownTimeout = 5 * time.Second |
| 23 | +) |
| 24 | + |
| 25 | +func main() { |
| 26 | + if err := run(); err != nil { |
| 27 | + fmt.Fprintf(os.Stderr, "error: %v\n", err) |
| 28 | + os.Exit(1) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func run() error { |
| 33 | + cfg := proxy.DefaultConfig() |
| 34 | + var modeStr string |
| 35 | + |
| 36 | + flag.StringVar(&cfg.ListenAddr, "listen", cfg.ListenAddr, "Proxy listen address") |
| 37 | + flag.StringVar(&cfg.PrimaryAddr, "primary", cfg.PrimaryAddr, "Primary (Redis) address") |
| 38 | + flag.IntVar(&cfg.PrimaryDB, "primary-db", cfg.PrimaryDB, "Primary Redis DB number") |
| 39 | + flag.StringVar(&cfg.PrimaryPassword, "primary-password", cfg.PrimaryPassword, "Primary Redis password") |
| 40 | + flag.StringVar(&cfg.SecondaryAddr, "secondary", cfg.SecondaryAddr, "Secondary (ElasticKV) address") |
| 41 | + flag.IntVar(&cfg.SecondaryDB, "secondary-db", cfg.SecondaryDB, "Secondary Redis DB number") |
| 42 | + flag.StringVar(&cfg.SecondaryPassword, "secondary-password", cfg.SecondaryPassword, "Secondary Redis password") |
| 43 | + flag.StringVar(&modeStr, "mode", "dual-write", "Proxy mode: redis-only, dual-write, dual-write-shadow, elastickv-primary, elastickv-only") |
| 44 | + flag.DurationVar(&cfg.SecondaryTimeout, "secondary-timeout", cfg.SecondaryTimeout, "Secondary write timeout") |
| 45 | + flag.DurationVar(&cfg.ShadowTimeout, "shadow-timeout", cfg.ShadowTimeout, "Shadow read timeout") |
| 46 | + flag.StringVar(&cfg.SentryDSN, "sentry-dsn", cfg.SentryDSN, "Sentry DSN (empty = disabled)") |
| 47 | + flag.StringVar(&cfg.SentryEnv, "sentry-env", cfg.SentryEnv, "Sentry environment") |
| 48 | + flag.Float64Var(&cfg.SentrySampleRate, "sentry-sample", cfg.SentrySampleRate, "Sentry sample rate") |
| 49 | + flag.StringVar(&cfg.MetricsAddr, "metrics", cfg.MetricsAddr, "Prometheus metrics address") |
| 50 | + flag.Parse() |
| 51 | + |
| 52 | + mode, ok := proxy.ParseProxyMode(modeStr) |
| 53 | + if !ok { |
| 54 | + return fmt.Errorf("unknown mode: %s", modeStr) |
| 55 | + } |
| 56 | + cfg.Mode = mode |
| 57 | + |
| 58 | + logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})) |
| 59 | + |
| 60 | + // Sentry |
| 61 | + sentryReporter := proxy.NewSentryReporter(cfg.SentryDSN, cfg.SentryEnv, cfg.SentrySampleRate, logger) |
| 62 | + defer sentryReporter.Flush(sentryFlushTimeout) |
| 63 | + |
| 64 | + // Prometheus |
| 65 | + reg := prometheus.NewRegistry() |
| 66 | + metrics := proxy.NewProxyMetrics(reg) |
| 67 | + |
| 68 | + // Backends |
| 69 | + primaryOpts := proxy.DefaultBackendOptions() |
| 70 | + primaryOpts.DB = cfg.PrimaryDB |
| 71 | + primaryOpts.Password = cfg.PrimaryPassword |
| 72 | + secondaryOpts := proxy.DefaultBackendOptions() |
| 73 | + secondaryOpts.DB = cfg.SecondaryDB |
| 74 | + secondaryOpts.Password = cfg.SecondaryPassword |
| 75 | + |
| 76 | + var primary, secondary proxy.Backend |
| 77 | + switch cfg.Mode { |
| 78 | + case proxy.ModeElasticKVPrimary, proxy.ModeElasticKVOnly: |
| 79 | + primary = proxy.NewRedisBackendWithOptions(cfg.SecondaryAddr, "elastickv", secondaryOpts) |
| 80 | + secondary = proxy.NewRedisBackendWithOptions(cfg.PrimaryAddr, "redis", primaryOpts) |
| 81 | + case proxy.ModeRedisOnly, proxy.ModeDualWrite, proxy.ModeDualWriteShadow: |
| 82 | + primary = proxy.NewRedisBackendWithOptions(cfg.PrimaryAddr, "redis", primaryOpts) |
| 83 | + secondary = proxy.NewRedisBackendWithOptions(cfg.SecondaryAddr, "elastickv", secondaryOpts) |
| 84 | + } |
| 85 | + defer primary.Close() |
| 86 | + defer secondary.Close() |
| 87 | + |
| 88 | + dual := proxy.NewDualWriter(primary, secondary, cfg, metrics, sentryReporter, logger) |
| 89 | + defer dual.Close() // wait for in-flight async goroutines |
| 90 | + srv := proxy.NewProxyServer(cfg, dual, metrics, sentryReporter, logger) |
| 91 | + |
| 92 | + // Context for graceful shutdown |
| 93 | + ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) |
| 94 | + defer cancel() |
| 95 | + |
| 96 | + // Start metrics server |
| 97 | + go func() { |
| 98 | + mux := http.NewServeMux() |
| 99 | + mux.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{})) |
| 100 | + var lc net.ListenConfig |
| 101 | + ln, err := lc.Listen(ctx, "tcp", cfg.MetricsAddr) |
| 102 | + if err != nil { |
| 103 | + logger.Error("metrics listen failed", "addr", cfg.MetricsAddr, "err", err) |
| 104 | + return |
| 105 | + } |
| 106 | + metricsSrv := &http.Server{Handler: mux, ReadHeaderTimeout: time.Second} |
| 107 | + go func() { |
| 108 | + <-ctx.Done() |
| 109 | + shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), metricsShutdownTimeout) |
| 110 | + defer shutdownCancel() |
| 111 | + if err := metricsSrv.Shutdown(shutdownCtx); err != nil { |
| 112 | + logger.Warn("metrics server shutdown error", "err", err) |
| 113 | + } |
| 114 | + }() |
| 115 | + logger.Info("metrics server starting", "addr", cfg.MetricsAddr) |
| 116 | + if err := metricsSrv.Serve(ln); err != nil && err != http.ErrServerClosed { |
| 117 | + logger.Error("metrics server error", "err", err) |
| 118 | + } |
| 119 | + }() |
| 120 | + |
| 121 | + // Start proxy |
| 122 | + if err := srv.ListenAndServe(ctx); err != nil { |
| 123 | + return fmt.Errorf("proxy server: %w", err) |
| 124 | + } |
| 125 | + return nil |
| 126 | +} |
0 commit comments