Skip to content
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

Fixes duplicate idempotent key issue during recovery and non-recovery #406

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions internal/sessions/committable/committable.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,14 @@ func (a *SavepointCommittable) Commit(ctx context.Context, idempotencyKey []byte
a.writable = false

if a.skip {
a.skip = false
return a.db.Get(ctx, ApphashKey, true)
}

if a.savepoint == nil {
return nil, fmt.Errorf("no session exists")
}

a.skip = false

var appHash []byte
if a.idFn != nil {
var err error
Expand Down
5 changes: 0 additions & 5 deletions internal/sessions/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ func setCurrentKey(kv KV, idempotencyKey []byte) error {
return kv.Set(currentKeyKey, idempotencyKey)
}

// deleteCurrentKey deletes the current key in the given connection.
func deleteCurrentKey(kv KV) error {
return kv.Delete(currentKeyKey)
}

// getCurrentKey gets the current key in the given connection.
// it can return nil if there is no current key.
func getCurrentKey(kv KV) ([]byte, error) {
Expand Down
9 changes: 3 additions & 6 deletions internal/sessions/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,9 @@ func (m *MultiCommitter) Begin(ctx context.Context, idempotencyKey []byte) (err
return err
}

// if lastKey is not nil, we are recovering from a crash
if lastKey != nil {
if !bytes.Equal(lastKey, idempotencyKey) {
return fmt.Errorf("%w on recovery: expected %s, got %s", ErrIdempotencyKeyMismatch, lastKey, idempotencyKey)
}
// if the last key is the same as the current key, we are recovering
// from a crash, therefore begin recovery mode.
if bytes.Equal(lastKey, idempotencyKey) {
for _, committable := range m.committables {
err = committable.BeginRecovery(ctx, idempotencyKey)
if err != nil {
Expand Down Expand Up @@ -143,7 +141,6 @@ func (m *MultiCommitter) Commit(ctx context.Context, idempotencyKey []byte) (id
id = append(id, newId...)
}

err = deleteCurrentKey(m.kv)
charithabandi marked this conversation as resolved.
Show resolved Hide resolved
return id, err
}

Expand Down
3 changes: 2 additions & 1 deletion internal/sessions/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func Test_Sessions(t *testing.T) {
_, err = mc.Commit(ctx, key2)
assert.NoError(t, err)

assert.Equal(t, len(kv.vals), 0)
// IdempotentKey is never deleted, just updated with every block, so it should always be 1
assert.Equal(t, len(kv.vals), 1)
},
},
{
Expand Down