test: make A the new holder while B is unreachable
Co-Authored-By: GPT-6 Astra <noreply@openai.com>
internal/host/region_ha_g2_loss_helpers_test.go+53 −0internal/host/region_ha_g2_loss_root_linux_test.go+55 −9
Intent
<task-notification> <task-id>b5q69tqlj</task-id> <tool-use-id>toolu_01K2PdTuD2a2rcaWG1QvjBNZ</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/5555438b-7fa2-408d-b2c4-9fa87ea89332/tasks/b5q69tqlj.output</output-file> <status>completed</status> <summary>Background command "Final F-B witness run 18" completed (exit code 0)</summary> </task-notification>
<task-notification> <task-id>b92fddbza</task-id> <tool-use-id>toolu_014sDHT1upEMinCNs9s5RHEr</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/5555438b-7fa2-408d-b2c4-9fa87ea89332/tasks/b92fddbza.output</output-file> <status>completed</status> <summary>Background command "F-A witness run 7 on merged tip" completed (exit code 0)</summary> </task-notification>
diff --git a/internal/host/region_ha_g2_loss_helpers_test.go b/internal/host/region_ha_g2_loss_helpers_test.go| index cba81eb6d..e0899b107 100644 |
| --- a/internal/host/region_ha_g2_loss_helpers_test.go |
| +++ b/internal/host/region_ha_g2_loss_helpers_test.go |
| @@ -4,7 +4,10 @@ package host |
| import ( |
| "context" |
| + "crypto/hmac" |
| + "crypto/sha256" |
| "database/sql" |
| + "encoding/hex" |
| "fmt" |
| "io" |
| "net" |
| @@ -13,11 +16,61 @@ import ( |
| "os" |
| "os/exec" |
| "path/filepath" |
| + "strconv" |
| "strings" |
| "testing" |
| "time" |
| ) |
| +func TestG2LossHopClientRefreshesSignature(t *testing.T) { |
| + mesh := []byte("fixture-mesh-key") |
| + now := time.Unix(1000, 0) |
| + calls := 0 |
| + client := &http.Client{Transport: g2RoundTripFunc(func(request *http.Request) (*http.Response, error) { |
| + calls++ |
| + timestamp, signature, ok := strings.Cut(request.Header.Get("X-Carlos-Hop-Auth"), ":") |
| + mac := hmac.New(sha256.New, mesh) |
| + mac.Write([]byte(g2Host + "\n" + timestamp)) |
| + if !ok || request.Header.Get("X-Carlos-Hop") != "1" || timestamp != strconv.FormatInt(now.Unix(), 10) || signature != hex.EncodeToString(mac.Sum(nil)) { |
| + t.Errorf("stale or invalid hop at %s: %v", now, request.Header) |
| + } |
| + return &http.Response{StatusCode: 200, Header: http.Header{}, Body: io.NopCloser(strings.NewReader("ok"))}, nil |
| + })} |
| + signed := g2LossHopClient(client, mesh, func() time.Time { return now }) |
| + request, err := http.NewRequestWithContext(t.Context(), "GET", "https://"+g2Host+"/", nil) |
| + g2Must(t, err) |
| + for _, seconds := range []int64{0, 31, 59} { |
| + now = time.Unix(1000+seconds, 0) |
| + response, err := signed.Do(request) |
| + g2Must(t, err) |
| + response.Body.Close() |
| + } |
| + if calls != 3 || request.Header.Get("X-Carlos-Hop-Auth") != "" { |
| + t.Fatalf("calls=%d original headers=%v", calls, request.Header) |
| + } |
| +} |
| + |
| +func TestG2LossNewHolderResponse(t *testing.T) { |
| + for _, tt := range []struct { |
| + name, ref string |
| + count int |
| + wantErr bool |
| + }{ |
| + {"once-on-A", "A", 1, false}, |
| + {"missing", "A", 0, true}, |
| + {"replayed", "A", 2, true}, |
| + {"wrong-holder", "B", 1, true}, |
| + } { |
| + t.Run(tt.name, func(t *testing.T) { |
| + response := g2Response{Status: http.StatusOK, Body: []byte(fmt.Sprintf( |
| + `{"ref":%q,"pid":7,"cgroup":"carlos-instance","deliveries":{%q:%d}}`, tt.ref, g2LossNewHolderID, tt.count))} |
| + if err := g2LossNewHolderResponse(response, "A"); (err != nil) != tt.wantErr { |
| + t.Fatalf("response=%s error=%v wantErr=%t", response.Body, err, tt.wantErr) |
| + } |
| + }) |
| + } |
| +} |
| + |
| func TestG2LossLBHealthRequest(t *testing.T) { |
| const ip = "10.0.0.2" |
| for _, status := range []int{http.StatusOK, http.StatusServiceUnavailable, http.StatusFound} { |
diff --git a/internal/host/region_ha_g2_loss_root_linux_test.go b/internal/host/region_ha_g2_loss_root_linux_test.go| index f0e69a837..e87c6d7e3 100644 |
| --- a/internal/host/region_ha_g2_loss_root_linux_test.go |
| +++ b/internal/host/region_ha_g2_loss_root_linux_test.go |
| @@ -557,6 +557,34 @@ func g2LossOwed(raw []string, host string) bool { |
| return false |
| } |
| +const g2LossNewHolderID = "new-holder-after-marker" |
| + |
| +func g2LossHopClient(client *http.Client, mesh []byte, now func() time.Time) *http.Client { |
| + signed := *client |
| + signed.Transport = g2RoundTripFunc(func(request *http.Request) (*http.Response, error) { |
| + request = request.Clone(request.Context()) |
| + // Each attempt needs a fresh proof: hop skew is 30s, readiness is 60s. |
| + timestamp := strconv.FormatInt(now().Unix(), 10) |
| + mac := hmac.New(sha256.New, mesh) |
| + mac.Write([]byte(request.URL.Hostname() + "\n" + timestamp)) |
| + request.Header.Set("X-Carlos-Hop", "1") |
| + request.Header.Set("X-Carlos-Hop-Auth", timestamp+":"+hex.EncodeToString(mac.Sum(nil))) |
| + return client.Transport.RoundTrip(request) |
| + }) |
| + return &signed |
| +} |
| + |
| +func g2LossNewHolderResponse(response g2Response, ref string) error { |
| + delivery, err := g2ServedDelivery(response, ref) |
| + if err != nil { |
| + return err |
| + } |
| + if delivery.Deliveries[g2LossNewHolderID] != 1 { |
| + return fmt.Errorf("new holder POST deliveries=%v", delivery.Deliveries) |
| + } |
| + return nil |
| +} |
| + |
| func g2LossNoDemand(t *testing.T, storage *g2Storage, audit release.Store, a *g2Box, b g2State, ingress, direct *http.Client, mesh []byte) { |
| t.Helper() |
| // Establish a second loss using the real pause detector, then let the real |
| @@ -572,17 +600,28 @@ func g2LossNoDemand(t *testing.T, storage *g2Storage, audit release.Store, a *g2 |
| return !released.Absent && released.Lock.Release != nil && released.Lock.Release.IID == b.IID |
| }) |
| g2Stopped(t, g2Peer(t, g2RPC{Action: "state"}), g2Host) |
| + if !released.Lock.Release.Clean || released.Lock.Release.Box != b.Ref || strings.Trim(released.ETag, "\"") == "" { |
| + t.Fatalf("second loss did not produce B's clean release marker: %+v", released) |
| + } |
| + // Keep the clean releaser unreachable until A owns the new generation. |
| + // The peer RPC remains available while the edge MainPID is frozen. |
| + g2Stopped(t, g2Peer(t, g2RPC{Action: "freeze"}), g2Host) |
| + frozen := true |
| + defer func() { |
| + if frozen { |
| + g2Peer(t, g2RPC{Action: "thaw"}) |
| + } |
| + }() |
| g2Must(t, a.pools("ha")) |
| g2Must(t, a.isolationReady("no-demand/new-holder")) |
| - timestamp := strconv.FormatInt(time.Now().Unix(), 10) |
| - mac := hmac.New(sha256.New, mesh) |
| - mac.Write([]byte(g2Host + "\n" + timestamp)) |
| - hop := timestamp + ":" + hex.EncodeToString(mac.Sum(nil)) |
| + signed := g2LossHopClient(ingress, mesh, time.Now) |
| self := a.state().Ref |
| - g2Outcome(t, ingress, "new-holder-after-marker", g2Host, "GET", "", hop, func(response g2Response) error { |
| - _, err := g2ServedDelivery(response, self) |
| - return err |
| + started := time.Now() |
| + g2Outcome(t, signed, g2LossNewHolderID, g2Host, "POST", g2LossNewHolderID, "", func(response g2Response) error { |
| + return g2LossNewHolderResponse(response, self) |
| }, func() { g2Stopped(t, g2Peer(t, g2RPC{Action: "state"}), g2Host) }) |
| + first200 := time.Since(started) |
| + g2Stopped(t, g2Peer(t, g2RPC{Action: "state"}), g2Host) |
| acquired := g2Lock(t, audit, g2Host) |
| owner, ok := hafresh.ParseOwner(acquired.Lock.Owner) |
| if !ok || owner.IID != a.iid || acquired.Lock.Generation <= released.Lock.Generation || acquired.Lock.Release != nil { |
| @@ -590,13 +629,16 @@ func g2LossNoDemand(t *testing.T, storage *g2Storage, audit release.Store, a *g2 |
| } |
| matched := false |
| for _, event := range storage.snapshot() { |
| - if event.Source == os.Getenv("G2_A_IP") && event.Path == "/"+g2Data+"/instances/"+g2Host+"/lock.json" && event.Method == "PUT" && event.Status == 200 && strings.Trim(event.IfMatch, "\"") == strings.Trim(released.ETag, "\"") { |
| + if !event.At.Before(started) && !event.Release && event.Owner == acquired.Lock.Owner && event.Source == os.Getenv("G2_A_IP") && event.Path == "/"+g2Data+"/instances/"+g2Host+"/lock.json" && event.Method == "PUT" && event.Status == 200 && strings.Trim(event.IfMatch, "\"") == strings.Trim(released.ETag, "\"") { |
| matched = true |
| } |
| } |
| if !matched { |
| t.Fatal("foreign acquisition did not condition on B's release marker") |
| } |
| + t.Logf("NEW_HOLDER ref=%s iid=%s generation=%d released_generation=%d marker_etag=%s first_200=%s deliveries=1", self, a.iid, acquired.Lock.Generation, released.Lock.Generation, released.ETag, first200) |
| + g2Stopped(t, g2Peer(t, g2RPC{Action: "thaw"}), g2Host) |
| + frozen = false |
| // One request provides demand. The latched B host discovers the real A |
| // lease and then must make no more per-host S3 calls without another request. |
| start := time.Now() |
| @@ -624,5 +666,9 @@ func g2LossNoDemand(t *testing.T, storage *g2Storage, audit release.Store, a *g2 |
| t.Fatalf("no-demand foreign holder: first_attempt_reads=%d later_s3_calls=%d", reads, attempts) |
| } |
| t.Logf("CASE no-demand-no-s3 PASS scope=completed-stop first_attempt_reads=%d later_s3_calls=%d window=65s", reads, attempts) |
| - g2Served(t, direct, "foreign-holder-http-recovery", g2Host, self, func() { g2Stopped(t, g2Peer(t, g2RPC{Action: "state"}), g2Host) }) |
| + delivery := g2Served(t, direct, "foreign-holder-http-recovery", g2Host, self, func() { g2Stopped(t, g2Peer(t, g2RPC{Action: "state"}), g2Host) }) |
| + g2Stopped(t, g2Peer(t, g2RPC{Action: "state"}), g2Host) |
| + if delivery.Deliveries[g2LossNewHolderID] != 1 { |
| + t.Fatalf("new holder POST replayed after B thawed: %v", delivery.Deliveries) |
| + } |
| } |