forked from hypermodeinc/modusGraph
-
Notifications
You must be signed in to change notification settings - Fork 1
feat: aborted-transaction retry policy and client integration #18
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
mlwelles
wants to merge
1
commit into
matthewmcneely:main
Choose a base branch
from
mlwelles:feature/retry-record
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.
+364
−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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package modusgraph | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "math/rand/v2" | ||
| "time" | ||
|
|
||
| "github.com/dgraph-io/dgo/v250" | ||
| ) | ||
|
|
||
| // RetryPolicy controls how WithRetry handles aborted transactions. | ||
| // Modeled after dgraph4j's RetryPolicy: exponential backoff with jitter. | ||
| type RetryPolicy struct { | ||
| // MaxRetries is the maximum number of retry attempts after the initial try. | ||
| MaxRetries int | ||
|
|
||
| // BaseDelay is the initial delay before the first retry. | ||
| // Subsequent delays grow exponentially: BaseDelay * 2^attempt. | ||
| BaseDelay time.Duration | ||
|
|
||
| // MaxDelay caps the backoff duration. No single delay exceeds this. | ||
| MaxDelay time.Duration | ||
|
|
||
| // Jitter adds randomness to each delay to prevent thundering herd. | ||
| // Expressed as a fraction of the computed delay (e.g. 0.1 = 10%). | ||
| Jitter float64 | ||
| } | ||
|
|
||
| // DefaultRetryPolicy mirrors dgraph4j's defaults: | ||
| // 5 retries, 100ms base delay, 5s max delay, 10% jitter. | ||
| var DefaultRetryPolicy = RetryPolicy{ | ||
| MaxRetries: 10, | ||
| BaseDelay: 100 * time.Millisecond, | ||
| MaxDelay: 5 * time.Second, | ||
| Jitter: 0.1, | ||
| } | ||
|
|
||
| // delay computes the backoff duration for a given attempt (0-indexed). | ||
| // Formula: min(BaseDelay * 2^attempt, MaxDelay) + random(0, delay * Jitter) | ||
| func (p RetryPolicy) delay(attempt int) time.Duration { | ||
| d := p.BaseDelay * time.Duration(1<<uint(attempt)) | ||
| if d > p.MaxDelay { | ||
| d = p.MaxDelay | ||
| } | ||
| if p.Jitter > 0 { | ||
| d += time.Duration(float64(d) * p.Jitter * rand.Float64()) | ||
| } | ||
| return d | ||
| } | ||
|
|
||
| // WithRetry executes fn, retrying on aborted transactions according to policy. | ||
| // | ||
| // This is an opt-in mechanism modeled after dgraph4j's client.withRetry(). | ||
| // The caller wraps their mutation logic in fn; WithRetry handles creating | ||
| // fresh attempts with exponential backoff when Dgraph returns a transaction | ||
| // abort due to concurrent conflicts. | ||
| // | ||
| // fn is called at least once. On each aborted-transaction error, WithRetry | ||
| // waits according to the policy's backoff schedule and calls fn again, up to | ||
| // policy.MaxRetries additional times. Non-abort errors are returned immediately. | ||
| // | ||
| // The context is checked between retries; if cancelled during a backoff sleep, | ||
| // the context error is returned. | ||
| // | ||
| // Usage: | ||
| // | ||
| // err := client.WithRetry(ctx, modusgraph.DefaultRetryPolicy, func() error { | ||
| // return client.Insert(ctx, &entity) | ||
| // }) | ||
| func (c client) WithRetry(ctx context.Context, policy RetryPolicy, fn func() error) error { | ||
| for attempt := range policy.MaxRetries + 1 { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Negative Prompt for AI agents |
||
| err := fn() | ||
| if err == nil { | ||
| return nil | ||
| } | ||
| if !errors.Is(err, dgo.ErrAborted) || attempt >= policy.MaxRetries { | ||
| return err | ||
| } | ||
| d := policy.delay(attempt) | ||
| c.logger.V(1).Info("Transaction aborted, retrying", | ||
| "attempt", attempt+1, "maxRetries", policy.MaxRetries, "delay", d) | ||
| select { | ||
| case <-time.After(d): | ||
| case <-ctx.Done(): | ||
| return ctx.Err() | ||
| } | ||
| } | ||
| // Unreachable: the loop runs MaxRetries+1 times and returns on every path. | ||
| panic("unreachable") | ||
| } | ||
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,68 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package modusgraph | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestRetryPolicyDelayExponentialGrowth(t *testing.T) { | ||
| p := RetryPolicy{ | ||
| BaseDelay: 100 * time.Millisecond, | ||
| MaxDelay: 10 * time.Second, | ||
| Jitter: 0, | ||
| } | ||
|
|
||
| assert.Equal(t, 100*time.Millisecond, p.delay(0)) | ||
| assert.Equal(t, 200*time.Millisecond, p.delay(1)) | ||
| assert.Equal(t, 400*time.Millisecond, p.delay(2)) | ||
| assert.Equal(t, 800*time.Millisecond, p.delay(3)) | ||
| assert.Equal(t, 1600*time.Millisecond, p.delay(4)) | ||
| } | ||
|
|
||
| func TestRetryPolicyDelayMaxCap(t *testing.T) { | ||
| p := RetryPolicy{ | ||
| BaseDelay: 1 * time.Second, | ||
| MaxDelay: 3 * time.Second, | ||
| Jitter: 0, | ||
| } | ||
|
|
||
| assert.Equal(t, 1*time.Second, p.delay(0)) | ||
| assert.Equal(t, 2*time.Second, p.delay(1)) | ||
| assert.Equal(t, 3*time.Second, p.delay(2)) | ||
| assert.Equal(t, 3*time.Second, p.delay(3)) | ||
| assert.Equal(t, 3*time.Second, p.delay(10)) | ||
| } | ||
|
|
||
| func TestRetryPolicyDelayWithJitter(t *testing.T) { | ||
| p := RetryPolicy{ | ||
| BaseDelay: 100 * time.Millisecond, | ||
| MaxDelay: 10 * time.Second, | ||
| Jitter: 0.5, | ||
| } | ||
|
|
||
| for range 100 { | ||
| d := p.delay(0) | ||
| assert.GreaterOrEqual(t, d, 100*time.Millisecond, "delay should be at least base") | ||
| assert.LessOrEqual(t, d, 150*time.Millisecond, "delay should not exceed base + 50% jitter") | ||
| } | ||
| } | ||
|
|
||
| func TestRetryPolicyDelayZeroJitter(t *testing.T) { | ||
| p := RetryPolicy{ | ||
| BaseDelay: 100 * time.Millisecond, | ||
| MaxDelay: 10 * time.Second, | ||
| Jitter: 0, | ||
| } | ||
|
|
||
| for range 10 { | ||
| assert.Equal(t, 100*time.Millisecond, p.delay(0)) | ||
| assert.Equal(t, 200*time.Millisecond, p.delay(1)) | ||
| } | ||
| } |
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,197 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package modusgraph_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "sync" | ||
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/matthewmcneely/modusgraph" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // RetryEntity is a test struct with a unique index to provoke transaction conflicts. | ||
| type RetryEntity struct { | ||
| UID string `json:"uid,omitempty"` | ||
| DType []string `json:"dgraph.type,omitempty"` | ||
| Name string `json:"name,omitempty" dgraph:"index=term,exact upsert"` | ||
| Value int `json:"value,omitempty"` | ||
| } | ||
|
|
||
| // TestConcurrentInsertsWithRetry verifies that WithRetry handles aborted | ||
| // transactions from concurrent inserts. Without WithRetry, concurrent inserts | ||
| // on the same predicate index would fail with dgo.ErrAborted. | ||
| func TestConcurrentInsertsWithRetry(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| uri string | ||
| skip bool | ||
| }{ | ||
| { | ||
| name: "FileURI", | ||
| uri: "file://" + GetTempDir(t), | ||
| }, | ||
| { | ||
| name: "DgraphURI", | ||
| uri: "dgraph://" + os.Getenv("MODUSGRAPH_TEST_ADDR"), | ||
| skip: os.Getenv("MODUSGRAPH_TEST_ADDR") == "", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| if tc.skip { | ||
| t.Skipf("Skipping %s: MODUSGRAPH_TEST_ADDR not set", tc.name) | ||
| return | ||
| } | ||
|
|
||
| client, cleanup := CreateTestClient(t, tc.uri) | ||
| defer cleanup() | ||
|
|
||
| ctx := context.Background() | ||
| const numWorkers = 8 | ||
| const entitiesPerWorker = 10 | ||
|
|
||
| var succeeded atomic.Int64 | ||
| var wg sync.WaitGroup | ||
|
|
||
| for w := range numWorkers { | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| for i := range entitiesPerWorker { | ||
| entity := &RetryEntity{ | ||
| Name: fmt.Sprintf("entity-%d-%d", w, i), | ||
| Value: w*entitiesPerWorker + i, | ||
| } | ||
| err := client.WithRetry(ctx, modusgraph.DefaultRetryPolicy, func() error { | ||
| return client.Insert(ctx, entity) | ||
| }) | ||
| if err != nil { | ||
| t.Errorf("worker %d entity %d: %v", w, i, err) | ||
| return | ||
| } | ||
| succeeded.Add(1) | ||
| } | ||
| }() | ||
| } | ||
| wg.Wait() | ||
|
|
||
| total := int64(numWorkers * entitiesPerWorker) | ||
| require.Equal(t, total, succeeded.Load(), | ||
| "all concurrent inserts should succeed with retry") | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestWithRetryContextCancellation verifies that WithRetry respects context | ||
| // cancellation during backoff sleeps. | ||
| func TestWithRetryContextCancellation(t *testing.T) { | ||
| uri := "file://" + GetTempDir(t) | ||
| client, cleanup := CreateTestClient(t, uri) | ||
| defer cleanup() | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) | ||
| defer cancel() | ||
|
|
||
| // Use a policy with a long delay so the context expires during backoff. | ||
| slowPolicy := modusgraph.RetryPolicy{ | ||
| MaxRetries: 10, | ||
| BaseDelay: 1 * time.Second, | ||
| MaxDelay: 5 * time.Second, | ||
| Jitter: 0, | ||
| } | ||
|
|
||
| callCount := 0 | ||
| err := client.WithRetry(ctx, slowPolicy, func() error { | ||
| callCount++ | ||
| // Always return an error that looks like an abort to trigger retry. | ||
| // We simulate this by inserting a duplicate to get a UniqueError, | ||
| // but that won't be retried. Instead, use a real insert to a fresh | ||
| // entity so the first call succeeds. | ||
| // Actually, to test the cancellation path we need the fn to always | ||
| // fail with an aborted error. Since we can't easily manufacture | ||
| // dgo.ErrAborted, test that context cancellation returns ctx.Err() | ||
| // by having fn block until context is done. | ||
| <-ctx.Done() | ||
| return ctx.Err() | ||
| }) | ||
|
|
||
| assert.ErrorIs(t, err, context.DeadlineExceeded) | ||
| assert.Equal(t, 1, callCount, "fn should be called once before context expires") | ||
| } | ||
|
|
||
| // TestRetryPolicyDelay verifies the exponential backoff calculation. | ||
| func TestRetryPolicyDelay(t *testing.T) { | ||
| // Use the public struct fields to verify delay behavior indirectly | ||
| // by checking that DefaultRetryPolicy has the expected values. | ||
| p := modusgraph.DefaultRetryPolicy | ||
| assert.Equal(t, 10, p.MaxRetries) | ||
| assert.Equal(t, 100*time.Millisecond, p.BaseDelay) | ||
| assert.Equal(t, 5*time.Second, p.MaxDelay) | ||
| assert.InDelta(t, 0.1, p.Jitter, 0.001) | ||
| } | ||
|
|
||
| // TestWithRetryNonAbortError verifies that non-abort errors are returned | ||
| // immediately without any retry. | ||
| func TestWithRetryNonAbortError(t *testing.T) { | ||
| uri := "file://" + GetTempDir(t) | ||
| client, cleanup := CreateTestClient(t, uri) | ||
| defer cleanup() | ||
|
|
||
| callCount := 0 | ||
| expectedErr := fmt.Errorf("not an abort error") | ||
|
|
||
| err := client.WithRetry(context.Background(), modusgraph.DefaultRetryPolicy, func() error { | ||
| callCount++ | ||
| return expectedErr | ||
| }) | ||
|
|
||
| assert.ErrorIs(t, err, expectedErr) | ||
| assert.Equal(t, 1, callCount, "non-abort errors should not trigger retry") | ||
| } | ||
|
|
||
| // TestWithRetrySucceedsFirstTry verifies that WithRetry returns nil | ||
| // when fn succeeds on the first call. | ||
| func TestWithRetrySucceedsFirstTry(t *testing.T) { | ||
| uri := "file://" + GetTempDir(t) | ||
| client, cleanup := CreateTestClient(t, uri) | ||
| defer cleanup() | ||
|
|
||
| callCount := 0 | ||
| err := client.WithRetry(context.Background(), modusgraph.DefaultRetryPolicy, func() error { | ||
| callCount++ | ||
| return nil | ||
| }) | ||
|
|
||
| assert.NoError(t, err) | ||
| assert.Equal(t, 1, callCount) | ||
| } | ||
|
|
||
| // TestWithRetryMaxRetriesZero verifies that MaxRetries=0 calls fn exactly once | ||
| // and returns any error without retrying. | ||
| func TestWithRetryMaxRetriesZero(t *testing.T) { | ||
| uri := "file://" + GetTempDir(t) | ||
| client, cleanup := CreateTestClient(t, uri) | ||
| defer cleanup() | ||
|
|
||
| policy := modusgraph.RetryPolicy{MaxRetries: 0} | ||
| callCount := 0 | ||
|
|
||
| err := client.WithRetry(context.Background(), policy, func() error { | ||
| callCount++ | ||
| return fmt.Errorf("always fails") | ||
| }) | ||
|
|
||
| assert.Error(t, err) | ||
| assert.Equal(t, 1, callCount, "MaxRetries=0 should call fn exactly once") | ||
| } |
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.
P2: MaxDelay cap is applied before jitter, so actual backoff can exceed the configured maximum. The
MaxDelayfield is documented as capping the backoff so "No single delay exceeds this," but thedelay()method applies the cap before adding positive jitter, allowing the final delay to exceedMaxDelay.Prompt for AI agents