Repository navigation
fix(posting): set minTs for empty rollups, drop replayed commits belo… #9863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shiva-istari
wants to merge
1
commit into
main
Choose a base branch
from
shiva/minTs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+334
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| //go:build integration2 | ||
|
|
||
| /* | ||
| * SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/dgraph-io/dgo/v250/protos/api" | ||
| "github.com/dgraph-io/dgraph/v25/dgraphapi" | ||
| "github.com/dgraph-io/dgraph/v25/dgraphtest" | ||
| ) | ||
|
|
||
| // Once incremental rollup has rewritten a list that a delete emptied, the empty marker must not be | ||
| // served to transactions that started before the delete, and must carry its real version in an | ||
| // incremental backup. | ||
| // | ||
| // Rollup skips a key it rolled up less than 10s ago, so the tests wait emptyRollupCooldown between | ||
| // the setup mutation and the delete, then give the delete's rollup emptyRollupWait to land. | ||
|
|
||
| const ( | ||
| emptyRollupCooldown = 11 * time.Second | ||
| emptyRollupWait = 20 * time.Second | ||
| emptyRollupNodes = 10 | ||
| ) | ||
|
|
||
| func startEmptyRollupCluster(t *testing.T) (*dgraphtest.LocalCluster, *dgraphapi.GrpcClient, | ||
| *dgraphapi.HTTPClient) { | ||
| t.Helper() | ||
| conf := dgraphtest.NewClusterConfig().WithNumAlphas(1).WithNumZeros(1).WithReplicas(1) | ||
| c, err := dgraphtest.NewLocalCluster(conf) | ||
| require.NoError(t, err) | ||
| t.Cleanup(func() { c.Cleanup(t.Failed()) }) | ||
| require.NoError(t, c.Start()) | ||
|
|
||
| gc, cleanup, err := c.Client() | ||
| require.NoError(t, err) | ||
| t.Cleanup(cleanup) | ||
| hc, err := c.HTTPClient() | ||
| require.NoError(t, err) | ||
|
|
||
| require.NoError(t, gc.SetupSchema(` | ||
| friend: [uid] . | ||
| status: string .`)) | ||
| return c, gc, hc | ||
| } | ||
|
|
||
| // addNodesWithOneFriend creates n nodes with one friend edge and status "before" each. | ||
| func addNodesWithOneFriend(t *testing.T, gc *dgraphapi.GrpcClient, n int) []string { | ||
| t.Helper() | ||
| var set strings.Builder | ||
| for i := 0; i < n; i++ { | ||
| fmt.Fprintf(&set, "_:x%d <friend> _:y%d .\n_:x%d <status> \"before\" .\n", i, i, i) | ||
| } | ||
| resp, err := gc.Mutate(&api.Mutation{SetNquads: []byte(set.String()), CommitNow: true}) | ||
| require.NoError(t, err) | ||
| uids := make([]string, n) | ||
| for i := range uids { | ||
| uids[i] = resp.Uids[fmt.Sprintf("x%d", i)] | ||
| require.NotEmpty(t, uids[i]) | ||
| } | ||
| return uids | ||
| } | ||
|
|
||
| // deleteAllFriends empties every node's friend list and returns the commit ts. | ||
| func deleteAllFriends(t *testing.T, gc *dgraphapi.GrpcClient, uids []string) uint64 { | ||
| t.Helper() | ||
| var del, set strings.Builder | ||
| for _, uid := range uids { | ||
| fmt.Fprintf(&del, "<%s> <friend> * .\n", uid) | ||
| fmt.Fprintf(&set, "<%s> <status> \"after\" .\n", uid) | ||
| } | ||
| resp, err := gc.Mutate(&api.Mutation{ | ||
| DelNquads: []byte(del.String()), | ||
| SetNquads: []byte(set.String()), | ||
| CommitNow: true, | ||
| }) | ||
| require.NoError(t, err) | ||
| require.NotZero(t, resp.Txn.CommitTs) | ||
| return resp.Txn.CommitTs | ||
| } | ||
|
|
||
| func countNodesWithFriend(t *testing.T, gc *dgraphapi.GrpcClient) int { | ||
| t.Helper() | ||
| resp, err := gc.Query(`{ q(func: has(friend)) { count(uid) } }`) | ||
| require.NoError(t, err) | ||
| var out struct { | ||
| Q []struct{ Count int } `json:"q"` | ||
| } | ||
| require.NoError(t, json.Unmarshal(resp.Json, &out)) | ||
| require.Len(t, out.Q, 1) | ||
| return out.Q[0].Count | ||
| } | ||
|
|
||
| // A transaction that started before a delete must keep seeing the deleted edge at its own start | ||
| // ts, also after the alpha has rolled the emptied list up. | ||
| func TestReadAtEarlierStartTsAfterDeleteEmptiesList(t *testing.T) { | ||
| _, gc, _ := startEmptyRollupCluster(t) | ||
| uids := addNodesWithOneFriend(t, gc, emptyRollupNodes) | ||
| require.Equal(t, emptyRollupNodes, countNodesWithFriend(t, gc)) | ||
|
|
||
| time.Sleep(emptyRollupCooldown) | ||
|
|
||
| // Transaction A: its first query fixes its start ts. | ||
| ctx := context.Background() | ||
| txnA := gc.NewReadOnlyTxn() | ||
| defer func() { _ = txnA.Discard(ctx) }() | ||
| resp, err := txnA.Query(ctx, `{ q(func: uid(0x1)) { uid } }`) | ||
| require.NoError(t, err) | ||
| startTs := resp.Txn.StartTs | ||
| require.NotZero(t, startTs) | ||
|
|
||
| commitTs := deleteAllFriends(t, gc, uids) | ||
| require.Greater(t, commitTs, startTs) | ||
| require.Equal(t, 0, countNodesWithFriend(t, gc)) | ||
|
|
||
| // A must see every friend at its start ts, now and after the rollup rewrites the keys. | ||
| uidList := strings.Join(uids, ", ") | ||
| query := fmt.Sprintf(`{ | ||
| q(func: uid(%s)) { uid status cnt: count(friend) friend { uid } } | ||
| h(func: uid(%s)) @filter(has(friend)) { uid } | ||
| }`, uidList, uidList) | ||
| deadline := time.Now().Add(emptyRollupWait) | ||
| for round := 0; time.Now().Before(deadline); round++ { | ||
| resp, err := txnA.Query(ctx, query) | ||
| require.NoError(t, err) | ||
| require.Equal(t, startTs, resp.Txn.StartTs) | ||
|
|
||
| var out struct { | ||
| Q []struct { | ||
| Uid string `json:"uid"` | ||
| Status string `json:"status"` | ||
| Cnt int `json:"cnt"` | ||
| Friend []struct { | ||
| Uid string `json:"uid"` | ||
| } `json:"friend"` | ||
| } `json:"q"` | ||
| H []struct { | ||
| Uid string `json:"uid"` | ||
| } `json:"h"` | ||
| } | ||
| require.NoError(t, json.Unmarshal(resp.Json, &out)) | ||
| require.Len(t, out.Q, emptyRollupNodes) | ||
|
|
||
| missing := 0 | ||
| for _, n := range out.Q { | ||
| require.Equal(t, "before", n.Status, | ||
| "round %d: scalar read at start ts %d (delete committed at %d)", round, startTs, commitTs) | ||
| if len(n.Friend) != 1 || n.Cnt != 1 { | ||
| missing++ | ||
| } | ||
| } | ||
| require.Zero(t, missing, "round %d: %d of %d nodes lost their friend at start ts %d, "+ | ||
| "which precedes the delete committed at %d; response: %s", | ||
| round, missing, emptyRollupNodes, startTs, commitTs, resp.Json) | ||
| require.Len(t, out.H, emptyRollupNodes, | ||
| "round %d: has(friend) at start ts %d dropped nodes after the rollup", round, startTs) | ||
|
|
||
| time.Sleep(500 * time.Millisecond) | ||
| } | ||
| } | ||
|
|
||
| // A full backup taken while the edges exist plus an incremental backup taken after the delete and | ||
| // its rollup must restore to zero edges. | ||
| func TestRestoreAfterDeleteEmptiesListAndRollup(t *testing.T) { | ||
| c, gc, hc := startEmptyRollupCluster(t) | ||
| uids := addNodesWithOneFriend(t, gc, emptyRollupNodes) | ||
| require.Equal(t, emptyRollupNodes, countNodesWithFriend(t, gc)) | ||
|
|
||
| require.NoError(t, hc.Backup(c, true, dgraphtest.DefaultBackupDir)) | ||
|
|
||
| time.Sleep(emptyRollupCooldown) | ||
| deleteAllFriends(t, gc, uids) | ||
| require.Equal(t, 0, countNodesWithFriend(t, gc)) | ||
|
|
||
| // Give the rollup time to rewrite the emptied keys. | ||
| deadline := time.Now().Add(emptyRollupWait / 2) | ||
| for time.Now().Before(deadline) { | ||
| require.Equal(t, 0, countNodesWithFriend(t, gc)) | ||
| time.Sleep(500 * time.Millisecond) | ||
| } | ||
|
|
||
| require.NoError(t, hc.Backup(c, false, dgraphtest.DefaultBackupDir)) | ||
|
|
||
| // An edge in neither backup; a correct restore drops it. | ||
| _, err := gc.Mutate(&api.Mutation{SetNquads: []byte(`_:s <friend> _:t .`), CommitNow: true}) | ||
| require.NoError(t, err) | ||
| require.Equal(t, 1, countNodesWithFriend(t, gc)) | ||
|
|
||
| require.NoError(t, hc.Restore(c, dgraphtest.DefaultBackupDir, "", 0, 0)) | ||
| require.NoError(t, dgraphapi.WaitForRestore(c)) | ||
|
|
||
| require.Equal(t, 0, countNodesWithFriend(t, gc), | ||
| "friend edges deleted and rolled up before the incremental backup came back after restore") | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these can pass without the fix. Nothing checks that the rollup actually landed before the incremental backup, and if it hasn't, the backup carries the delete as a delta, which
ToBackupPostingListversions correctly anyway. Same withTestReadAtEarlierStartTsAfterDeleteEmptiesList: if the rollup is late, every round reads the deltas and passes.Did you confirm both go red against main? At ~30s of fixed waits per test I'd like to know they're earning it. I couldn't find a rollup metric to poll on either, so I get why the sleeps are here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @matthewmcneely , I confirmed both tests go red on main (built no-fix and fix binaries, ran both against each). But you're right it's not guaranteed every run. Incremental rollup is lossy and cooldown-gated — skipped keys get dropped from the batch and never retried — so the emptied keys get ~one rollup attempt and only a few land (I saw ~3–4 regardless of node count). A run where none roll up would pass on buggy main too; fewer keys makes that more likely, which is why I bumped the count. A longer sleep doesn't help — the dropped keys are never retried.
The real fix is a signal to wait on, like WaitForSnapshot. There's none for rollup today. it's local and unreported. incrRollupi already counts rollups (ir.count), just not exposed.
Separate PR: expose a rollups_total counter (one ostats.Record where ir.count increments in rollUpKey + a View in x/metrics.go) and a dgraphapi.WaitForRollup, so these tests wait on the counter instead of sleeping. Thoughts?