|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os/signal" |
| 7 | + "sync" |
| 8 | + "syscall" |
| 9 | + "time" |
| 10 | + |
| 11 | + "golang.org/x/sync/errgroup" |
| 12 | + "golang.org/x/time/rate" |
| 13 | + |
| 14 | + "slo/internal/config" |
| 15 | + "slo/internal/generator" |
| 16 | + "slo/internal/workers" |
| 17 | +) |
| 18 | + |
| 19 | +func Main( |
| 20 | + label string, |
| 21 | + jobName string, |
| 22 | +) { |
| 23 | + ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT) |
| 24 | + defer cancel() |
| 25 | + |
| 26 | + cfg, err := config.New() |
| 27 | + if err != nil { |
| 28 | + panic(fmt.Errorf("create config failed: %w", err)) |
| 29 | + } |
| 30 | + |
| 31 | + fmt.Println("program started") |
| 32 | + defer fmt.Println("program finished") |
| 33 | + |
| 34 | + ctx, cancel = context.WithTimeout(ctx, time.Duration(cfg.Time)*time.Second) |
| 35 | + defer cancel() |
| 36 | + |
| 37 | + s, err := NewStorage(ctx, cfg, cfg.ReadRPS+cfg.WriteRPS, label) |
| 38 | + if err != nil { |
| 39 | + panic(fmt.Errorf("create storage failed: %w", err)) |
| 40 | + } |
| 41 | + defer func() { |
| 42 | + var ( |
| 43 | + shutdownCtx context.Context |
| 44 | + shutdownCancel context.CancelFunc |
| 45 | + ) |
| 46 | + if cfg.ShutdownTime > 0 { |
| 47 | + shutdownCtx, shutdownCancel = context.WithTimeout(context.Background(), |
| 48 | + time.Duration(cfg.ShutdownTime)*time.Second) |
| 49 | + } else { |
| 50 | + shutdownCtx, shutdownCancel = context.WithCancel(context.Background()) |
| 51 | + } |
| 52 | + defer shutdownCancel() |
| 53 | + |
| 54 | + _ = s.Close(shutdownCtx) |
| 55 | + }() |
| 56 | + |
| 57 | + fmt.Println("db init ok") |
| 58 | + |
| 59 | + switch cfg.Mode { |
| 60 | + case config.CreateMode: |
| 61 | + err = s.CreateTable(ctx) |
| 62 | + if err != nil { |
| 63 | + panic(fmt.Errorf("create table failed: %w", err)) |
| 64 | + } |
| 65 | + fmt.Println("create table ok") |
| 66 | + |
| 67 | + gen := generator.New(0) |
| 68 | + |
| 69 | + g := errgroup.Group{} |
| 70 | + |
| 71 | + for i := uint64(0); i < cfg.InitialDataCount; i++ { |
| 72 | + g.Go(func() (err error) { |
| 73 | + e, err := gen.Generate() |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + _, err = s.Write(ctx, e) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + |
| 83 | + return nil |
| 84 | + }) |
| 85 | + } |
| 86 | + |
| 87 | + err = g.Wait() |
| 88 | + if err != nil { |
| 89 | + panic(err) |
| 90 | + } |
| 91 | + |
| 92 | + fmt.Println("entries write ok") |
| 93 | + case config.CleanupMode: |
| 94 | + err = s.DropTable(ctx) |
| 95 | + if err != nil { |
| 96 | + panic(fmt.Errorf("create table failed: %w", err)) |
| 97 | + } |
| 98 | + |
| 99 | + fmt.Println("cleanup table ok") |
| 100 | + case config.RunMode: |
| 101 | + gen := generator.New(cfg.InitialDataCount) |
| 102 | + |
| 103 | + w, err := workers.New(cfg, s, label, jobName) |
| 104 | + if err != nil { |
| 105 | + panic(fmt.Errorf("create workers failed: %w", err)) |
| 106 | + } |
| 107 | + defer func() { |
| 108 | + err := w.Close() |
| 109 | + if err != nil { |
| 110 | + panic(fmt.Errorf("workers close failed: %w", err)) |
| 111 | + } |
| 112 | + fmt.Println("workers close ok") |
| 113 | + }() |
| 114 | + |
| 115 | + wg := sync.WaitGroup{} |
| 116 | + |
| 117 | + readRL := rate.NewLimiter(rate.Limit(cfg.ReadRPS), 1) |
| 118 | + wg.Add(cfg.ReadRPS) |
| 119 | + for i := 0; i < cfg.ReadRPS; i++ { |
| 120 | + go w.Read(ctx, &wg, readRL) |
| 121 | + } |
| 122 | + |
| 123 | + writeRL := rate.NewLimiter(rate.Limit(cfg.WriteRPS), 1) |
| 124 | + wg.Add(cfg.WriteRPS) |
| 125 | + for i := 0; i < cfg.WriteRPS; i++ { |
| 126 | + go w.Write(ctx, &wg, writeRL, gen) |
| 127 | + } |
| 128 | + |
| 129 | + metricsRL := rate.NewLimiter(rate.Every(time.Duration(cfg.ReportPeriod)*time.Millisecond), 1) |
| 130 | + wg.Add(1) |
| 131 | + go w.Metrics(ctx, &wg, metricsRL) |
| 132 | + |
| 133 | + wg.Wait() |
| 134 | + default: |
| 135 | + panic(fmt.Errorf("unknown mode: %v", cfg.Mode)) |
| 136 | + } |
| 137 | +} |
0 commit comments