forked from mtusi2026/mtusi2026lab3
This commit is contained in:
+168
-3
@@ -10,6 +10,7 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -19,6 +20,7 @@ type apiClient struct {
|
||||
baseURL string
|
||||
client *http.Client
|
||||
token string
|
||||
userID string
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -26,7 +28,7 @@ func main() {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println("PASS: PostgreSQL and Valkey outage recovery")
|
||||
fmt.Println("PASS: replica idempotency and dependency recovery")
|
||||
}
|
||||
|
||||
func run() error {
|
||||
@@ -39,6 +41,9 @@ func run() error {
|
||||
if err := client.expect(http.MethodGet, "/api/v1/users/me", http.StatusOK, true); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := client.verifyConcurrentIdempotency(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := verifyOutage(client, "valkey"); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -62,9 +67,21 @@ func (c *apiClient) registerAndLogin() error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("register: %w", err)
|
||||
}
|
||||
if err := closeResponse(response, http.StatusCreated); err != nil {
|
||||
return fmt.Errorf("register: %w", err)
|
||||
registrationBody := response.Body
|
||||
defer func() { _ = registrationBody.Close() }()
|
||||
if response.StatusCode != http.StatusCreated {
|
||||
return fmt.Errorf("register status = %d", response.StatusCode)
|
||||
}
|
||||
var user struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
if err := json.NewDecoder(io.LimitReader(response.Body, 1<<20)).Decode(&user); err != nil {
|
||||
return fmt.Errorf("decode user: %w", err)
|
||||
}
|
||||
if user.ID == "" {
|
||||
return fmt.Errorf("register returned an empty user id")
|
||||
}
|
||||
c.userID = user.ID
|
||||
|
||||
request, err = http.NewRequest(http.MethodPost, c.baseURL+"/api/v1/auth/sessions", nil)
|
||||
if err != nil {
|
||||
@@ -92,6 +109,154 @@ func (c *apiClient) registerAndLogin() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type messageResult struct {
|
||||
messageID string
|
||||
instanceID string
|
||||
err error
|
||||
}
|
||||
|
||||
func (c *apiClient) verifyConcurrentIdempotency() error {
|
||||
chatID, err := c.createChat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
const workers = 16
|
||||
var (
|
||||
key = uuid.NewString()
|
||||
start = make(chan struct{})
|
||||
results = make(chan messageResult, workers)
|
||||
group sync.WaitGroup
|
||||
instances = make(map[string]struct{})
|
||||
messageID string
|
||||
)
|
||||
transport := &http.Transport{DisableKeepAlives: true, MaxConnsPerHost: workers}
|
||||
client := &http.Client{Transport: transport, Timeout: 10 * time.Second}
|
||||
defer transport.CloseIdleConnections()
|
||||
|
||||
for range workers {
|
||||
group.Add(1)
|
||||
go func() {
|
||||
defer group.Done()
|
||||
<-start
|
||||
id, instance, requestErr := c.createMessage(client, chatID, key, "concurrent message")
|
||||
results <- messageResult{messageID: id, instanceID: instance, err: requestErr}
|
||||
}()
|
||||
}
|
||||
close(start)
|
||||
group.Wait()
|
||||
close(results)
|
||||
|
||||
for result := range results {
|
||||
if result.err != nil {
|
||||
return result.err
|
||||
}
|
||||
if result.instanceID == "" {
|
||||
return fmt.Errorf("message response has an empty X-Instance-Id")
|
||||
}
|
||||
instances[result.instanceID] = struct{}{}
|
||||
if messageID == "" {
|
||||
messageID = result.messageID
|
||||
}
|
||||
if result.messageID != messageID {
|
||||
return fmt.Errorf("idempotent responses returned different message ids")
|
||||
}
|
||||
}
|
||||
if len(instances) < 2 {
|
||||
return fmt.Errorf("requests reached %d API instance, want at least 2", len(instances))
|
||||
}
|
||||
return c.verifyMessageHistory(chatID, messageID)
|
||||
}
|
||||
|
||||
func (c *apiClient) createChat() (string, error) {
|
||||
payload, err := json.Marshal(map[string]any{
|
||||
"title": "Replica check", "member_ids": []string{c.userID},
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
request, err := http.NewRequest(http.MethodPost, c.baseURL+"/api/v1/chats", bytes.NewReader(payload))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
request.Header.Set("Authorization", "Bearer "+c.token)
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
response, err := c.client.Do(request)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("create chat: %w", err)
|
||||
}
|
||||
defer func() { _ = response.Body.Close() }()
|
||||
if response.StatusCode != http.StatusCreated {
|
||||
return "", fmt.Errorf("create chat status = %d", response.StatusCode)
|
||||
}
|
||||
var chat struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
if err := json.NewDecoder(io.LimitReader(response.Body, 1<<20)).Decode(&chat); err != nil {
|
||||
return "", fmt.Errorf("decode chat: %w", err)
|
||||
}
|
||||
return chat.ID, nil
|
||||
}
|
||||
|
||||
func (c *apiClient) createMessage(client *http.Client, chatID, key, text string) (string, string, error) {
|
||||
payload, err := json.Marshal(map[string]string{"text": text})
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
request, err := http.NewRequest(
|
||||
http.MethodPost, c.baseURL+"/api/v1/chats/"+chatID+"/messages", bytes.NewReader(payload),
|
||||
)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
request.Header.Set("Authorization", "Bearer "+c.token)
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
request.Header.Set("Idempotency-Key", key)
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("create message: %w", err)
|
||||
}
|
||||
defer func() { _ = response.Body.Close() }()
|
||||
if response.StatusCode != http.StatusCreated {
|
||||
return "", response.Header.Get("X-Instance-Id"), fmt.Errorf("create message status = %d", response.StatusCode)
|
||||
}
|
||||
var message struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
if err := json.NewDecoder(io.LimitReader(response.Body, 1<<20)).Decode(&message); err != nil {
|
||||
return "", response.Header.Get("X-Instance-Id"), fmt.Errorf("decode message: %w", err)
|
||||
}
|
||||
return message.ID, response.Header.Get("X-Instance-Id"), nil
|
||||
}
|
||||
|
||||
func (c *apiClient) verifyMessageHistory(chatID, messageID string) error {
|
||||
request, err := http.NewRequest(http.MethodGet, c.baseURL+"/api/v1/chats/"+chatID+"/messages?limit=100", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
request.Header.Set("Authorization", "Bearer "+c.token)
|
||||
response, err := c.client.Do(request)
|
||||
if err != nil {
|
||||
return fmt.Errorf("list messages: %w", err)
|
||||
}
|
||||
defer func() { _ = response.Body.Close() }()
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("list messages status = %d", response.StatusCode)
|
||||
}
|
||||
var page struct {
|
||||
Items []struct {
|
||||
ID string `json:"id"`
|
||||
} `json:"items"`
|
||||
}
|
||||
if err := json.NewDecoder(io.LimitReader(response.Body, 1<<20)).Decode(&page); err != nil {
|
||||
return fmt.Errorf("decode messages: %w", err)
|
||||
}
|
||||
if len(page.Items) != 1 || page.Items[0].ID != messageID {
|
||||
return fmt.Errorf("message history contains %d items with an unexpected id", len(page.Items))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func verifyOutage(client *apiClient, service string) (returnErr error) {
|
||||
if err := compose("stop", service); err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user