|
| 1 | +//go:build integration2 |
| 2 | + |
| 3 | +/* |
| 4 | + * SPDX-FileCopyrightText: © 2017-2025 Istari Digital, Inc. |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + */ |
| 7 | + |
| 8 | +package main |
| 9 | + |
| 10 | +import ( |
| 11 | + "context" |
| 12 | + "strings" |
| 13 | + "testing" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | + "google.golang.org/grpc" |
| 18 | + "google.golang.org/grpc/codes" |
| 19 | + "google.golang.org/grpc/credentials/insecure" |
| 20 | + "google.golang.org/grpc/status" |
| 21 | + |
| 22 | + "github.com/dgraph-io/dgo/v250/protos/api" |
| 23 | + "github.com/dgraph-io/dgraph/v25/dgraphtest" |
| 24 | +) |
| 25 | + |
| 26 | +// TestStaleStartTsAbortReason proves the "stale-startts" abort category end-to-end against a |
| 27 | +// real cluster. A transaction's start timestamp becomes "stale" when it predates the current |
| 28 | +// Zero leader's lease — i.e. after a leader change. We reproduce that deterministically by |
| 29 | +// opening a transaction, then restarting the (single) Zero: on restart Zero renews its lease and |
| 30 | +// advances startTxnTs past every previously-leased start ts, so committing the now-old txn aborts |
| 31 | +// with the stale-startts reason rather than a plain write-write conflict. |
| 32 | +// |
| 33 | +// As in the alpha-level reason tests, the commit goes through the raw CommitOrAbort stub so we |
| 34 | +// observe the unflattened codes.Aborted status (dgo's Txn.Commit would replace it with the |
| 35 | +// reasonless ErrAborted). |
| 36 | +func TestStaleStartTsAbortReason(t *testing.T) { |
| 37 | + conf := dgraphtest.NewClusterConfig().WithNumAlphas(1).WithNumZeros(1).WithReplicas(1) |
| 38 | + c, err := dgraphtest.NewLocalCluster(conf) |
| 39 | + require.NoError(t, err) |
| 40 | + t.Cleanup(func() { c.Cleanup(t.Failed()) }) |
| 41 | + require.NoError(t, c.Start()) |
| 42 | + |
| 43 | + gc, cleanup, err := c.Client() |
| 44 | + require.NoError(t, err) |
| 45 | + defer cleanup() |
| 46 | + |
| 47 | + ctx := context.Background() |
| 48 | + require.NoError(t, gc.Alter(ctx, &api.Operation{DropAll: true})) |
| 49 | + |
| 50 | + // Open a transaction and mutate so it gets a real (soon-to-be-stale) start ts and keys. |
| 51 | + txn := gc.NewTxn() |
| 52 | + resp, err := txn.Mutate(ctx, &api.Mutation{SetJson: []byte(`{"name": "Manish"}`)}) |
| 53 | + require.NoError(t, err) |
| 54 | + tc := resp.GetTxn() |
| 55 | + require.NotZero(t, tc.GetStartTs(), "mutation must yield a start ts") |
| 56 | + |
| 57 | + // Restart Zero. On coming back up it renews its lease and sets startTxnTs to MaxTxnTs+1, |
| 58 | + // which is strictly greater than the start ts leased above — making our open txn stale. |
| 59 | + require.NoError(t, c.StopZero(0)) |
| 60 | + require.NoError(t, c.StartZero(0)) |
| 61 | + require.NoError(t, c.HealthCheck(false)) |
| 62 | + |
| 63 | + // Wait until a Zero leader is established again (lease renewal, hence startTxnTs bump, runs |
| 64 | + // when a Zero becomes leader). Avoids racing the commit against the leaderless window. |
| 65 | + require.Eventually(t, func() bool { |
| 66 | + _, err := c.GetZeroLeader(0) |
| 67 | + return err == nil |
| 68 | + }, 60*time.Second, time.Second, "zero leader did not re-establish after restart") |
| 69 | + |
| 70 | + // Commit the stale txn via the raw stub to observe the categorized status. |
| 71 | + port, err := c.GetAlphaGrpcPublicPort(0) |
| 72 | + require.NoError(t, err) |
| 73 | + conn, err := grpc.NewClient("0.0.0.0:"+port, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 74 | + require.NoError(t, err) |
| 75 | + defer func() { _ = conn.Close() }() |
| 76 | + raw := api.NewDgraphClient(conn) |
| 77 | + |
| 78 | + _, err = raw.CommitOrAbort(ctx, tc) |
| 79 | + require.Error(t, err, "committing a txn whose start ts predates the new leader must abort") |
| 80 | + |
| 81 | + st := status.Convert(err) |
| 82 | + require.Equal(t, codes.Aborted, st.Code(), |
| 83 | + "abort must keep codes.Aborted so existing clients still retry") |
| 84 | + require.True(t, strings.HasPrefix(st.Message(), "stale-startts: "), |
| 85 | + "abort reason should be categorized as stale-startts; got %q", st.Message()) |
| 86 | +} |
0 commit comments