Перенесите реализацию ЛР 4 на стенд ЛР 5
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/chat"
|
||||
)
|
||||
|
||||
// client is a minimal typed HTTP client for the messenger API.
|
||||
type client struct {
|
||||
base string
|
||||
http *http.Client
|
||||
}
|
||||
|
||||
func newClient(baseURL string) *client {
|
||||
return &client{base: baseURL, http: &http.Client{Timeout: 10 * time.Second}}
|
||||
}
|
||||
|
||||
func (c *client) post(path, token string, body, out any) error {
|
||||
raw, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequest(http.MethodPost, c.base+path, bytes.NewReader(raw))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
return c.do(req, token, http.StatusCreated, out)
|
||||
}
|
||||
|
||||
// postMessage sends one message under a fresh Idempotency-Key.
|
||||
func (c *client) postMessage(chatID, token, text string, out any) error {
|
||||
raw, err := json.Marshal(map[string]string{"text": text})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequest(http.MethodPost, c.base+"/api/v1/chats/"+chatID+"/messages", bytes.NewReader(raw))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Idempotency-Key", uuid.NewString())
|
||||
return c.do(req, token, http.StatusCreated, out)
|
||||
}
|
||||
|
||||
// sendWithKey sends one attempt on a connection of its own, so that the
|
||||
// attempts of one key reach different replicas.
|
||||
func (c *client) sendWithKey(chatID, token, key, text string) (int, chat.MessageResponse, string, error) {
|
||||
var msg chat.MessageResponse
|
||||
raw, err := json.Marshal(map[string]string{"text": text})
|
||||
if err != nil {
|
||||
return 0, msg, "", err
|
||||
}
|
||||
req, err := http.NewRequest(http.MethodPost, c.base+"/api/v1/chats/"+chatID+"/messages", bytes.NewReader(raw))
|
||||
if err != nil {
|
||||
return 0, msg, "", err
|
||||
}
|
||||
req.Close = true
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
req.Header.Set("Idempotency-Key", key)
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return 0, msg, "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
payload, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
||||
if err != nil {
|
||||
return 0, msg, "", err
|
||||
}
|
||||
if resp.StatusCode == http.StatusCreated {
|
||||
if err := json.Unmarshal(payload, &msg); err != nil {
|
||||
return 0, msg, "", err
|
||||
}
|
||||
}
|
||||
return resp.StatusCode, msg, resp.Header.Get("X-Instance-Id"), nil
|
||||
}
|
||||
|
||||
// login opens a session with Basic credentials and returns its bearer token.
|
||||
func (c *client) login(username, password string) (string, error) {
|
||||
req, err := http.NewRequest(http.MethodPost, c.base+"/api/v1/auth/sessions", nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.SetBasicAuth(username, password)
|
||||
var session struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
if err := c.do(req, "", http.StatusCreated, &session); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return session.Token, nil
|
||||
}
|
||||
|
||||
// get requests the largest page the contract allows, continuing from cursor
|
||||
// when it is set.
|
||||
func (c *client) get(path, token, cursor string, out any) error {
|
||||
query := url.Values{"limit": {"100"}}
|
||||
if cursor != "" {
|
||||
query.Set("cursor", cursor)
|
||||
}
|
||||
req, err := http.NewRequest(http.MethodGet, c.base+path+"?"+query.Encode(), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.do(req, token, http.StatusOK, out)
|
||||
}
|
||||
|
||||
func (c *client) do(req *http.Request, token string, want int, out any) error {
|
||||
if token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
payload, err := io.ReadAll(io.LimitReader(resp.Body, 4<<20))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.StatusCode != want {
|
||||
return fmt.Errorf("%s %s: status %d, want %d: %s", req.Method, req.URL.Path, resp.StatusCode, want, payload)
|
||||
}
|
||||
return json.Unmarshal(payload, out)
|
||||
}
|
||||
@@ -0,0 +1,322 @@
|
||||
// Command concurrency is the black-box race check from the grading rubric.
|
||||
//
|
||||
// Phase 1: N clients send M messages each into one chat at the same time,
|
||||
// then the history is paged and must contain exactly N*M unique messages in
|
||||
// id order.
|
||||
//
|
||||
// Phase 2: in each of K rounds one key is sent P times in parallel through the
|
||||
// balancer; every attempt must get 201 with one message, at least two replicas
|
||||
// must answer, the history must hold K messages, and another text must get 409.
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/platform/pagination"
|
||||
"messenger/internal/user"
|
||||
)
|
||||
|
||||
// Report is the machine readable outcome saved as evidence.
|
||||
type Report struct {
|
||||
BaseURL string `json:"base_url"`
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
Clients int `json:"clients"`
|
||||
PerClient int `json:"per_client"`
|
||||
Expected int `json:"expected_messages"`
|
||||
Created int `json:"created_201"`
|
||||
Failed int `json:"failed_posts"`
|
||||
Listed int `json:"listed_messages"`
|
||||
Unique int `json:"unique_ids"`
|
||||
Missing int `json:"created_but_not_listed"`
|
||||
Ordered bool `json:"history_ordered_by_id"`
|
||||
Pages int `json:"pages_read"`
|
||||
ElapsedSeconds float64 `json:"elapsed_seconds"`
|
||||
// Idempotency is phase 2.
|
||||
Idempotency IdempotencyReport `json:"idempotency"`
|
||||
Passed bool `json:"passed"`
|
||||
}
|
||||
|
||||
type IdempotencyReport struct {
|
||||
Rounds int `json:"rounds"`
|
||||
Parallel int `json:"parallel"`
|
||||
// SameMessageRounds counts rounds whose attempts all answered 201 with
|
||||
// one and the same message.
|
||||
SameMessageRounds int `json:"same_message_rounds"`
|
||||
// InstancesSeen is the number of distinct X-Instance-Id values among all
|
||||
// attempts.
|
||||
InstancesSeen int `json:"instances_seen"`
|
||||
RequireReplicas bool `json:"require_two_replicas"`
|
||||
Conflict409 bool `json:"different_body_409"`
|
||||
History int `json:"history_messages"`
|
||||
Passed bool `json:"passed"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
baseURL := flag.String("base-url", "http://localhost:8080", "API base URL")
|
||||
clients := flag.Int("clients", 10, "number of concurrent clients")
|
||||
perClient := flag.Int("per-client", 10, "messages sent by each client")
|
||||
rounds := flag.Int("rounds", 20, "idempotency rounds, one key each")
|
||||
parallel := flag.Int("parallel", 8, "parallel attempts with the same key per round")
|
||||
requireReplicas := flag.Bool("require-replicas", true, "fail unless the attempts reached at least two replicas")
|
||||
output := flag.String("output", "", "optional path for the JSON report")
|
||||
flag.Parse()
|
||||
|
||||
report, err := run(*baseURL, *clients, *perClient, *rounds, *parallel, *requireReplicas)
|
||||
if err != nil {
|
||||
fail(err)
|
||||
}
|
||||
text, _ := json.MarshalIndent(report, "", " ")
|
||||
fmt.Println(string(text))
|
||||
if *output != "" {
|
||||
if err := writeReport(*output, text); err != nil {
|
||||
fail(err)
|
||||
}
|
||||
}
|
||||
if !report.Passed {
|
||||
fail(fmt.Errorf("concurrency invariants violated"))
|
||||
}
|
||||
fmt.Printf("PASS: %d clients x %d messages produced exactly %d unique ordered messages\n",
|
||||
report.Clients, report.PerClient, report.Unique)
|
||||
fmt.Printf("PASS: %d rounds x %d parallel attempts with one key produced %d messages across %d instances\n",
|
||||
report.Idempotency.Rounds, report.Idempotency.Parallel, report.Idempotency.History, report.Idempotency.InstancesSeen)
|
||||
}
|
||||
|
||||
func fail(err error) {
|
||||
fmt.Fprintln(os.Stderr, "FAIL:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func writeReport(path string, text []byte) error {
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(path, append(text, '\n'), 0o644)
|
||||
}
|
||||
|
||||
func run(baseURL string, clients, perClient, rounds, parallel int, requireReplicas bool) (Report, error) {
|
||||
if clients < 2 || perClient < 1 || rounds < 1 || parallel < 1 {
|
||||
return Report{}, fmt.Errorf("clients must be at least 2; per-client, rounds and parallel positive")
|
||||
}
|
||||
report := Report{
|
||||
BaseURL: baseURL,
|
||||
StartedAt: time.Now().UTC(),
|
||||
Clients: clients,
|
||||
PerClient: perClient,
|
||||
Expected: clients * perClient,
|
||||
}
|
||||
api := newClient(baseURL)
|
||||
runID := uuid.NewString()[:8]
|
||||
|
||||
members, tokens, err := registerClients(api, runID, clients)
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
room, err := createChat(api, runID, members, tokens[0])
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
|
||||
started := time.Now()
|
||||
created, failed := sendConcurrently(baseURL, room.ID, tokens, perClient)
|
||||
report.ElapsedSeconds = time.Since(started).Seconds()
|
||||
report.Created = len(created)
|
||||
report.Failed = failed
|
||||
|
||||
listed, pages, err := readHistory(api, room.ID, tokens[0])
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
report.Pages = pages
|
||||
report.Listed = len(listed)
|
||||
report.Unique, report.Missing = compare(created, listed)
|
||||
report.Ordered = sort.StringsAreSorted(listed)
|
||||
report.Idempotency, err = checkIdempotency(api, baseURL, runID, members[:2], tokens[0], rounds, parallel, requireReplicas)
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
report.Passed = report.Failed == 0 &&
|
||||
report.Created == report.Expected &&
|
||||
report.Listed == report.Expected &&
|
||||
report.Unique == report.Expected &&
|
||||
report.Missing == 0 &&
|
||||
report.Ordered &&
|
||||
report.Idempotency.Passed
|
||||
return report, nil
|
||||
}
|
||||
|
||||
// checkIdempotency runs phase 2 in a chat of its own.
|
||||
func checkIdempotency(api *client, baseURL, runID string, members []string, token string, rounds, parallel int, requireReplicas bool) (IdempotencyReport, error) {
|
||||
r := IdempotencyReport{Rounds: rounds, Parallel: parallel, RequireReplicas: requireReplicas}
|
||||
room, err := createChat(api, runID+"-idem", members, token)
|
||||
if err != nil {
|
||||
return r, err
|
||||
}
|
||||
instances := map[string]bool{}
|
||||
var key, text string
|
||||
for round := range rounds {
|
||||
key, text = uuid.NewString(), fmt.Sprintf("idempotency round %d", round)
|
||||
type attempt struct {
|
||||
status int
|
||||
id string
|
||||
instance string
|
||||
}
|
||||
attempts := make([]attempt, parallel)
|
||||
var wg sync.WaitGroup
|
||||
start := make(chan struct{})
|
||||
for i := range parallel {
|
||||
wg.Go(func() {
|
||||
own := newClient(baseURL)
|
||||
<-start
|
||||
status, msg, instance, err := own.sendWithKey(room.ID, token, key, text)
|
||||
if err == nil {
|
||||
attempts[i] = attempt{status, msg.ID, instance}
|
||||
}
|
||||
})
|
||||
}
|
||||
close(start)
|
||||
wg.Wait()
|
||||
same := true
|
||||
for _, a := range attempts {
|
||||
if a.instance != "" {
|
||||
instances[a.instance] = true
|
||||
}
|
||||
if a.status != http.StatusCreated || a.id == "" || a.id != attempts[0].id {
|
||||
same = false
|
||||
}
|
||||
}
|
||||
if same {
|
||||
r.SameMessageRounds++
|
||||
}
|
||||
}
|
||||
r.InstancesSeen = len(instances)
|
||||
status, _, _, err := newClient(baseURL).sendWithKey(room.ID, token, key, text+" changed")
|
||||
if err != nil {
|
||||
return r, err
|
||||
}
|
||||
r.Conflict409 = status == http.StatusConflict
|
||||
listed, _, err := readHistory(api, room.ID, token)
|
||||
if err != nil {
|
||||
return r, err
|
||||
}
|
||||
r.History = len(listed)
|
||||
r.Passed = r.SameMessageRounds == rounds &&
|
||||
r.History == rounds &&
|
||||
r.Conflict409 &&
|
||||
(!requireReplicas || r.InstancesSeen >= 2)
|
||||
return r, nil
|
||||
}
|
||||
|
||||
// clientPassword is shared by the synthetic clients of one run.
|
||||
const clientPassword = "concurrency-check-password"
|
||||
|
||||
// registerClients registers and logs in n clients, returning their ids and
|
||||
// bearer tokens in the same order.
|
||||
func registerClients(api *client, runID string, n int) (ids, tokens []string, err error) {
|
||||
for i := range n {
|
||||
var created user.Response
|
||||
username := fmt.Sprintf("load_%s_%d", runID, i)
|
||||
body := map[string]string{
|
||||
"username": username,
|
||||
"display_name": fmt.Sprintf("Load client %d", i),
|
||||
"password": clientPassword,
|
||||
}
|
||||
if err := api.post("/api/v1/users", "", body, &created); err != nil {
|
||||
return nil, nil, fmt.Errorf("register client %d: %w", i, err)
|
||||
}
|
||||
token, err := api.login(username, clientPassword)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("log in client %d: %w", i, err)
|
||||
}
|
||||
ids = append(ids, created.ID)
|
||||
tokens = append(tokens, token)
|
||||
}
|
||||
return ids, tokens, nil
|
||||
}
|
||||
|
||||
func createChat(api *client, runID string, members []string, creatorToken string) (chat.ChatResponse, error) {
|
||||
var created chat.ChatResponse
|
||||
body := map[string]any{"title": "Concurrency check " + runID, "member_ids": members}
|
||||
if err := api.post("/api/v1/chats", creatorToken, body, &created); err != nil {
|
||||
return chat.ChatResponse{}, fmt.Errorf("create chat: %w", err)
|
||||
}
|
||||
return created, nil
|
||||
}
|
||||
|
||||
// sendConcurrently gives every member its own HTTP client and releases them
|
||||
// together, so the server sees genuinely parallel writes to one chat.
|
||||
func sendConcurrently(baseURL, chatID string, tokens []string, perClient int) (map[string]struct{}, int) {
|
||||
var mu sync.Mutex
|
||||
created := make(map[string]struct{}, len(tokens)*perClient)
|
||||
failed := 0
|
||||
|
||||
var wg sync.WaitGroup
|
||||
start := make(chan struct{})
|
||||
for i, token := range tokens {
|
||||
wg.Add(1)
|
||||
go func(n int, token string) {
|
||||
defer wg.Done()
|
||||
own := newClient(baseURL)
|
||||
<-start
|
||||
for m := range perClient {
|
||||
var msg chat.MessageResponse
|
||||
err := own.postMessage(chatID, token, fmt.Sprintf("client %d message %d", n, m), &msg)
|
||||
mu.Lock()
|
||||
if err != nil {
|
||||
failed++
|
||||
} else {
|
||||
created[msg.ID] = struct{}{}
|
||||
}
|
||||
mu.Unlock()
|
||||
}
|
||||
}(i, token)
|
||||
}
|
||||
close(start)
|
||||
wg.Wait()
|
||||
return created, failed
|
||||
}
|
||||
|
||||
func readHistory(api *client, chatID, token string) ([]string, int, error) {
|
||||
var ids []string
|
||||
pages := 0
|
||||
cursor := ""
|
||||
for {
|
||||
var page pagination.PageResponse[chat.MessageResponse]
|
||||
if err := api.get("/api/v1/chats/"+chatID+"/messages", token, cursor, &page); err != nil {
|
||||
return nil, pages, fmt.Errorf("read history: %w", err)
|
||||
}
|
||||
pages++
|
||||
for _, m := range page.Items {
|
||||
ids = append(ids, m.ID)
|
||||
}
|
||||
if page.NextCursor == nil {
|
||||
return ids, pages, nil
|
||||
}
|
||||
cursor = *page.NextCursor
|
||||
}
|
||||
}
|
||||
|
||||
// compare returns how many distinct ids the history holds and how many
|
||||
// acknowledged ids never showed up in it.
|
||||
func compare(created map[string]struct{}, listed []string) (unique, missing int) {
|
||||
seen := make(map[string]struct{}, len(listed))
|
||||
for _, id := range listed {
|
||||
seen[id] = struct{}{}
|
||||
}
|
||||
for id := range created {
|
||||
if _, ok := seen[id]; !ok {
|
||||
missing++
|
||||
}
|
||||
}
|
||||
return len(seen), missing
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/internal/app"
|
||||
"messenger/internal/testenv"
|
||||
)
|
||||
|
||||
var (
|
||||
db *testenv.DB
|
||||
kv *testenv.KV
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
ctx := context.Background()
|
||||
var err error
|
||||
if db, err = testenv.StartPostgres(ctx); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if kv, err = testenv.StartValkey(ctx); err != nil {
|
||||
db.Close()
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
code := m.Run()
|
||||
kv.Close()
|
||||
db.Close()
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
func TestRunAgainstLiveServer(t *testing.T) {
|
||||
log := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
a := app.New(app.Deps{Log: log, DB: db.Pool, Sessions: kv.Client, SessionTTL: time.Hour, MaxBodyBytes: 64 * 1024, InstanceID: "concurrency-test"})
|
||||
srv := httptest.NewServer(a.Engine)
|
||||
defer srv.Close()
|
||||
|
||||
report, err := run(srv.URL, 10, 10, 5, 4, false)
|
||||
if err != nil {
|
||||
t.Fatalf("run error: %v", err)
|
||||
}
|
||||
if report.Clients != 10 || report.PerClient != 10 || report.Expected != 100 {
|
||||
t.Fatalf("report = %+v", report)
|
||||
}
|
||||
if report.Created != 100 || report.Listed != 100 || report.Unique != 100 || report.Missing != 0 {
|
||||
t.Fatalf("report = %+v", report)
|
||||
}
|
||||
if !report.Ordered || !report.Passed {
|
||||
t.Fatalf("report = %+v", report)
|
||||
}
|
||||
idem := report.Idempotency
|
||||
if idem.Rounds != 5 || idem.Parallel != 4 || idem.SameMessageRounds != 5 || idem.History != 5 || !idem.Conflict409 || !idem.Passed {
|
||||
t.Fatalf("idempotency = %+v", idem)
|
||||
}
|
||||
if idem.InstancesSeen != 1 {
|
||||
t.Fatalf("one in-process server must be seen as one instance: %+v", idem)
|
||||
}
|
||||
}
|
||||
|
||||
// Through the balancer the same key must reach at least two replicas; one
|
||||
// replica alone does not prove the idempotency is shared.
|
||||
func TestIdempotencyRequiresTwoReplicasByDefault(t *testing.T) {
|
||||
log := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
a := app.New(app.Deps{Log: log, DB: db.Pool, Sessions: kv.Client, SessionTTL: time.Hour, MaxBodyBytes: 64 * 1024, InstanceID: "concurrency-test"})
|
||||
srv := httptest.NewServer(a.Engine)
|
||||
defer srv.Close()
|
||||
|
||||
report, err := run(srv.URL, 2, 1, 2, 2, true)
|
||||
if err != nil {
|
||||
t.Fatalf("run error: %v", err)
|
||||
}
|
||||
if report.Idempotency.Passed || report.Passed {
|
||||
t.Fatalf("a single instance passed the shared idempotency check: %+v", report.Idempotency)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunFailsOnUnreachableServer(t *testing.T) {
|
||||
if _, err := run("http://127.0.0.1:1", 2, 2, 1, 1, false); err == nil {
|
||||
t.Fatal("expected an error for an unreachable server")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user