lab3: балансировка, идемпотентность, отказ реплики, трек 1
- X-Instance-Id, обязательный Idempotency-Key (PostgreSQL, одна транзакция) - HAProxy + 2..N реплик API, readiness/drain по SIGTERM, однократные миграции - скрипты stop-one/restore/restart/rolling-update/collect-evidence - трек 1: параметризованный стенд, cmd/bench, оркестратор и анализ - workflow реального прогона Docker-стенда на GitHub Actions Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+151
@@ -0,0 +1,151 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/charawein/mtusi2026lab3/internal/config"
|
||||
"github.com/charawein/mtusi2026lab3/internal/repository/postgres"
|
||||
redisrepo "github.com/charawein/mtusi2026lab3/internal/repository/redis"
|
||||
"github.com/charawein/mtusi2026lab3/internal/service"
|
||||
transporthttp "github.com/charawein/mtusi2026lab3/internal/transport/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cfg := config.Load()
|
||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: cfg.LogLevel}))
|
||||
slog.SetDefault(logger)
|
||||
|
||||
if cfg.DatabaseURL == "" {
|
||||
slog.Error("DATABASE_URL_FILE is not set or empty")
|
||||
os.Exit(1)
|
||||
}
|
||||
if cfg.CacheURL == "" {
|
||||
slog.Error("CACHE_URL_FILE is not set or empty")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
pool, err := postgres.NewPool(ctx, cfg.DatabaseURL, cfg.DBPoolMax)
|
||||
cancel()
|
||||
if err != nil {
|
||||
slog.Error("connect to postgres", "err", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
rctx, rcancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
redisClient, err := redisrepo.NewClient(rctx, cfg.CacheURL)
|
||||
rcancel()
|
||||
if err != nil {
|
||||
slog.Error("connect to redis", "err", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer redisClient.Close()
|
||||
|
||||
userRepo := postgres.NewUserRepository(pool)
|
||||
chatRepo := postgres.NewChatRepository(pool)
|
||||
msgRepo := postgres.NewMessageRepository(pool)
|
||||
sessRepo := redisrepo.NewSessionRepository(redisClient)
|
||||
|
||||
userSvc := service.NewUserService(userRepo)
|
||||
chatSvc := service.NewChatService(chatRepo, userRepo)
|
||||
|
||||
var msgCache *redisrepo.MessageCache
|
||||
if cfg.CacheEnabled {
|
||||
msgCache = redisrepo.NewMessageCache(redisClient, cfg.CacheInvalidation)
|
||||
}
|
||||
msgSvc := service.NewMessageService(msgRepo, chatRepo, msgCache, cfg.IdempotencyTTL)
|
||||
|
||||
authSvc := service.NewAuthService(userRepo, sessRepo, cfg.SessionTTL())
|
||||
|
||||
// draining выставляется по SIGTERM: /health/ready начинает отвечать 503,
|
||||
// LB исключает реплику, и только затем закрывается listener.
|
||||
var draining atomic.Bool
|
||||
router := transporthttp.NewRouter(userSvc, chatSvc, msgSvc, authSvc, pool, redisClient, cfg.InstanceID, &draining)
|
||||
|
||||
// Очистка просроченных ключей идемпотентности. Запрос идемпотентен,
|
||||
// поэтому параллельный запуск на всех репликах безопасен.
|
||||
cleanupCtx, cleanupStop := context.WithCancel(context.Background())
|
||||
defer cleanupStop()
|
||||
go func() {
|
||||
t := time.NewTicker(time.Hour)
|
||||
defer t.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-cleanupCtx.Done():
|
||||
return
|
||||
case <-t.C:
|
||||
ctx, cancel := context.WithTimeout(cleanupCtx, 30*time.Second)
|
||||
if n, err := msgRepo.DeleteExpiredKeys(ctx, cfg.IdempotencyTTL); err != nil {
|
||||
slog.Warn("idempotency cleanup failed", "err", err)
|
||||
} else if n > 0 {
|
||||
slog.Info("idempotency keys expired", "deleted", n)
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: cfg.Addr(),
|
||||
Handler: router,
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
ReadTimeout: 15 * time.Second,
|
||||
WriteTimeout: 15 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
}
|
||||
|
||||
// Внутренний endpoint метрик для экспериментов (трек 1). Порт не публикуется
|
||||
// и не проксируется LB: читается только через docker exec внутри контейнера.
|
||||
internalSrv := &http.Server{
|
||||
Addr: cfg.InternalAddr,
|
||||
Handler: transporthttp.NewInternalStatsHandler(pool, cfg),
|
||||
ReadHeaderTimeout: 2 * time.Second,
|
||||
}
|
||||
go func() {
|
||||
if err := internalSrv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
slog.Warn("internal stats server failed", "err", err)
|
||||
}
|
||||
}()
|
||||
defer internalSrv.Close()
|
||||
|
||||
errCh := make(chan error, 1)
|
||||
go func() {
|
||||
slog.Info("server starting", "addr", cfg.Addr(), "instance_id", cfg.InstanceID)
|
||||
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
errCh <- err
|
||||
}
|
||||
}()
|
||||
|
||||
sigCtx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
|
||||
defer stop()
|
||||
|
||||
select {
|
||||
case err := <-errCh:
|
||||
slog.Error("server failed", "err", err)
|
||||
os.Exit(1)
|
||||
case <-sigCtx.Done():
|
||||
slog.Info("shutdown signal received, draining", "delay", cfg.DrainDelay)
|
||||
}
|
||||
|
||||
// Перестаём быть ready и даём LB время заметить это (inter*fall у HAProxy),
|
||||
// продолжая обслуживать уже направленные к нам запросы.
|
||||
draining.Store(true)
|
||||
time.Sleep(cfg.DrainDelay)
|
||||
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), cfg.ShutdownTimeout)
|
||||
defer cancel()
|
||||
if err := srv.Shutdown(shutdownCtx); err != nil {
|
||||
slog.Error("graceful shutdown failed", "err", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
slog.Info("server stopped")
|
||||
}
|
||||
@@ -0,0 +1,328 @@
|
||||
// Command bench — генератор нагрузки для трека 1 (цена масштабирования).
|
||||
//
|
||||
// Профиль: смесь POST сообщения (с Idempotency-Key) и GET истории (limit=50)
|
||||
// по нескольким чатам; каждый запрос идёт через LB с Bearer-токеном.
|
||||
//
|
||||
// Режимы:
|
||||
// - closed loop (-concurrency N): N воркеров, каждый ждёт ответ перед следующим
|
||||
// запросом. Показывает пропускную способность при фиксированном параллелизме.
|
||||
// - open loop (-rate R): запросы назначаются по расписанию R/s независимо от
|
||||
// ответов; задержка считается от НАЗНАЧЕННОГО момента, поэтому ожидание в
|
||||
// очереди клиента входит в latency (поправка на coordinated omission).
|
||||
//
|
||||
// Повторов нет: ошибка/timeout учитывается как ошибка. Токены не печатаются.
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
mrand "math/rand/v2"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"sort"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
type actor struct {
|
||||
token string
|
||||
chat string
|
||||
}
|
||||
|
||||
type sample struct {
|
||||
write bool
|
||||
ok bool
|
||||
latency time.Duration
|
||||
class string
|
||||
}
|
||||
|
||||
type result struct {
|
||||
mu sync.Mutex
|
||||
samples []sample
|
||||
instances map[string]int
|
||||
dropped atomic.Int64
|
||||
}
|
||||
|
||||
func (r *result) add(s sample, instance string) {
|
||||
r.mu.Lock()
|
||||
r.samples = append(r.samples, s)
|
||||
if instance != "" {
|
||||
r.instances[instance]++
|
||||
}
|
||||
r.mu.Unlock()
|
||||
}
|
||||
|
||||
func randHex(n int) string {
|
||||
b := make([]byte, n)
|
||||
_, _ = rand.Read(b)
|
||||
return hex.EncodeToString(b)
|
||||
}
|
||||
|
||||
func main() {
|
||||
base := flag.String("base-url", "http://localhost:8080", "LB URL")
|
||||
duration := flag.Duration("duration", 30*time.Second, "measured duration")
|
||||
warmup := flag.Duration("warmup", 5*time.Second, "warmup (not measured)")
|
||||
concurrency := flag.Int("concurrency", 8, "closed-loop workers (ignored when -rate > 0)")
|
||||
rate := flag.Float64("rate", 0, "open-loop arrival rate, req/s (0 = closed loop)")
|
||||
maxInflight := flag.Int("max-inflight", 512, "open-loop: max concurrent requests; excess arrivals are dropped and counted")
|
||||
writeRatio := flag.Float64("write-ratio", 0.5, "fraction of POST message")
|
||||
chats := flag.Int("chats", 8, "number of chats/users")
|
||||
timeout := flag.Duration("timeout", 5*time.Second, "per-request timeout")
|
||||
label := flag.String("label", "", "free-form label copied to output")
|
||||
out := flag.String("output", "", "append JSON line to file")
|
||||
flag.Parse()
|
||||
|
||||
transport := &http.Transport{
|
||||
MaxIdleConns: 2048,
|
||||
MaxIdleConnsPerHost: 2048,
|
||||
IdleConnTimeout: 30 * time.Second,
|
||||
DialContext: (&net.Dialer{Timeout: 2 * time.Second}).DialContext,
|
||||
}
|
||||
client := &http.Client{Transport: transport, Timeout: *timeout}
|
||||
|
||||
actors, err := setup(client, *base, *chats)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "setup failed:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
res := &result{instances: map[string]int{}}
|
||||
var measuring atomic.Bool
|
||||
var seq atomic.Int64
|
||||
|
||||
do := func(scheduled time.Time) {
|
||||
n := seq.Add(1)
|
||||
a := actors[int(n)%len(actors)]
|
||||
// Независимо от выбора actor: иначе при чётном числе actor и 0.5 одни бы только писали.
|
||||
write := mrand.Float64() < *writeRatio
|
||||
var req *http.Request
|
||||
if write {
|
||||
body, _ := json.Marshal(map[string]string{"text": "bench " + randHex(8)})
|
||||
req, _ = http.NewRequest("POST", *base+"/api/v1/chats/"+a.chat+"/messages", bytes.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Idempotency-Key", randHex(16))
|
||||
} else {
|
||||
req, _ = http.NewRequest("GET", *base+"/api/v1/chats/"+a.chat+"/messages?limit=50", nil)
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+a.token)
|
||||
resp, err := client.Do(req)
|
||||
lat := time.Since(scheduled)
|
||||
s := sample{write: write, latency: lat}
|
||||
inst := ""
|
||||
switch {
|
||||
case err != nil:
|
||||
s.class = "transport"
|
||||
if e, ok := err.(net.Error); ok && e.Timeout() {
|
||||
s.class = "timeout"
|
||||
}
|
||||
default:
|
||||
_, _ = io.Copy(io.Discard, resp.Body)
|
||||
resp.Body.Close()
|
||||
inst = resp.Header.Get("X-Instance-Id")
|
||||
want := http.StatusOK
|
||||
if write {
|
||||
want = http.StatusCreated
|
||||
}
|
||||
s.ok = resp.StatusCode == want
|
||||
if !s.ok {
|
||||
s.class = fmt.Sprintf("http_%d", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
if measuring.Load() {
|
||||
res.add(s, inst)
|
||||
}
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
measureStart := start.Add(*warmup)
|
||||
end := measureStart.Add(*duration)
|
||||
go func() {
|
||||
time.Sleep(time.Until(measureStart))
|
||||
measuring.Store(true)
|
||||
time.Sleep(time.Until(end))
|
||||
measuring.Store(false)
|
||||
}()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
if *rate > 0 {
|
||||
sem := make(chan struct{}, *maxInflight)
|
||||
interval := time.Duration(float64(time.Second) / *rate)
|
||||
next := time.Now()
|
||||
for next.Before(end) {
|
||||
if d := time.Until(next); d > 0 {
|
||||
time.Sleep(d)
|
||||
}
|
||||
scheduled := next
|
||||
next = next.Add(interval)
|
||||
select {
|
||||
case sem <- struct{}{}:
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
defer func() { <-sem }()
|
||||
do(scheduled)
|
||||
}()
|
||||
default:
|
||||
if measuring.Load() {
|
||||
res.dropped.Add(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for i := 0; i < *concurrency; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for time.Now().Before(end) {
|
||||
do(time.Now())
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
report := summarize(res, *duration)
|
||||
report["label"] = *label
|
||||
report["mode"] = map[bool]string{true: "open", false: "closed"}[*rate > 0]
|
||||
report["concurrency"] = *concurrency
|
||||
report["rate"] = *rate
|
||||
report["write_ratio"] = *writeRatio
|
||||
report["chats"] = *chats
|
||||
report["duration_s"] = duration.Seconds()
|
||||
report["warmup_s"] = warmup.Seconds()
|
||||
report["timestamp"] = time.Now().UTC().Format(time.RFC3339)
|
||||
line, _ := json.Marshal(report)
|
||||
fmt.Println(string(line))
|
||||
if *out != "" {
|
||||
f, err := os.OpenFile(*out, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
|
||||
if err == nil {
|
||||
_, _ = f.Write(append(line, '\n'))
|
||||
f.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func percentiles(lat []time.Duration) map[string]float64 {
|
||||
if len(lat) == 0 {
|
||||
return map[string]float64{"p50": 0, "p95": 0, "p99": 0, "max": 0}
|
||||
}
|
||||
sort.Slice(lat, func(i, j int) bool { return lat[i] < lat[j] })
|
||||
p := func(q float64) float64 {
|
||||
i := int(math.Ceil(q*float64(len(lat)))) - 1
|
||||
if i < 0 {
|
||||
i = 0
|
||||
}
|
||||
return float64(lat[i].Microseconds()) / 1000
|
||||
}
|
||||
return map[string]float64{"p50": p(.50), "p95": p(.95), "p99": p(.99), "max": float64(lat[len(lat)-1].Microseconds()) / 1000}
|
||||
}
|
||||
|
||||
func summarize(r *result, d time.Duration) map[string]any {
|
||||
var all, w, rd []time.Duration
|
||||
errs := map[string]int{}
|
||||
ok, okW, okR := 0, 0, 0
|
||||
for _, s := range r.samples {
|
||||
if !s.ok {
|
||||
errs[s.class]++
|
||||
continue
|
||||
}
|
||||
ok++
|
||||
all = append(all, s.latency)
|
||||
if s.write {
|
||||
okW++
|
||||
w = append(w, s.latency)
|
||||
} else {
|
||||
okR++
|
||||
rd = append(rd, s.latency)
|
||||
}
|
||||
}
|
||||
total := len(r.samples)
|
||||
errN := total - ok
|
||||
return map[string]any{
|
||||
"requests": total,
|
||||
"ok": ok,
|
||||
"errors": errN,
|
||||
"error_ratio": ratio(errN, total),
|
||||
"errors_by_class": errs,
|
||||
"dropped": r.dropped.Load(),
|
||||
"ok_rps": float64(ok) / d.Seconds(),
|
||||
"ok_write_rps": float64(okW) / d.Seconds(),
|
||||
"ok_read_rps": float64(okR) / d.Seconds(),
|
||||
"latency_ms": percentiles(all),
|
||||
"write_ms": percentiles(w),
|
||||
"read_ms": percentiles(rd),
|
||||
"instances": r.instances,
|
||||
}
|
||||
}
|
||||
|
||||
func ratio(a, b int) float64 {
|
||||
if b == 0 {
|
||||
return 0
|
||||
}
|
||||
return float64(a) / float64(b)
|
||||
}
|
||||
|
||||
// setup создаёт пары пользователей, сессии и по одному чату на пару.
|
||||
func setup(c *http.Client, base string, n int) ([]actor, error) {
|
||||
post := func(path string, body any, hdr map[string]string) (map[string]any, error) {
|
||||
var rdr io.Reader
|
||||
if body != nil {
|
||||
raw, _ := json.Marshal(body)
|
||||
rdr = bytes.NewReader(raw)
|
||||
}
|
||||
req, _ := http.NewRequest("POST", base+path, rdr)
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
for k, v := range hdr {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
resp, err := c.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusCreated {
|
||||
return nil, fmt.Errorf("POST %s: status %d", path, resp.StatusCode)
|
||||
}
|
||||
var m map[string]any
|
||||
return m, json.NewDecoder(resp.Body).Decode(&m)
|
||||
}
|
||||
var actors []actor
|
||||
for i := 0; i < n; i++ {
|
||||
var ids [2]string
|
||||
var tokens [2]string
|
||||
for j := 0; j < 2; j++ {
|
||||
name := "bench_" + randHex(6)
|
||||
pw := "Bench-" + randHex(12)
|
||||
u, err := post("/api/v1/users", map[string]string{"username": name, "display_name": "Bench", "password": pw}, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ids[j] = u["id"].(string)
|
||||
cred := base64.StdEncoding.EncodeToString([]byte(name + ":" + pw))
|
||||
s, err := post("/api/v1/auth/sessions", nil, map[string]string{"Authorization": "Basic " + cred})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tokens[j] = s["token"].(string)
|
||||
}
|
||||
ch, err := post("/api/v1/chats", map[string]any{"title": "bench", "member_ids": []string{ids[0], ids[1]}},
|
||||
map[string]string{"Authorization": "Bearer " + tokens[0]})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
chat := ch["id"].(string)
|
||||
actors = append(actors, actor{token: tokens[0], chat: chat}, actor{token: tokens[1], chat: chat})
|
||||
}
|
||||
return actors, nil
|
||||
}
|
||||
Reference in New Issue
Block a user