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

Support atomic writes in the docstore #3500

Merged
merged 3 commits into from
Jan 23, 2025
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
29 changes: 18 additions & 11 deletions docstore/awsdynamodb/dynamo.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,15 @@

func (c *collection) RunActions(ctx context.Context, actions []*driver.Action, opts *driver.RunActionsOptions) driver.ActionListError {
errs := make([]error, len(actions))
beforeGets, gets, writes, afterGets := driver.GroupActions(actions)
beforeGets, gets, writes, writesTx, afterGets := driver.GroupActions(actions)
c.runGets(ctx, beforeGets, errs, opts)
ch := make(chan struct{})
ch2 := make(chan struct{})
go func() { defer close(ch); c.runWrites(ctx, writes, errs, opts) }()
go func() { defer close(ch2); c.transactWrite(ctx, writesTx, errs, opts) }()
c.runGets(ctx, gets, errs, opts)
<-ch
jba marked this conversation as resolved.
Show resolved Hide resolved
<-ch2
c.runGets(ctx, afterGets, errs, opts)
return driver.NewActionListError(errs)
}
Expand Down Expand Up @@ -613,25 +616,29 @@
return &cb, nil
}

// TODO(jba): use this if/when we support atomic writes.
func (c *collection) transactWrite(ctx context.Context, actions []*driver.Action, errs []error, opts *driver.RunActionsOptions, start, end int) {
// transactWrite executes the write actions atomically: either they all succeed or they all fail together.
func (c *collection) transactWrite(ctx context.Context, actions []*driver.Action, errs []error, opts *driver.RunActionsOptions) {
jba marked this conversation as resolved.
Show resolved Hide resolved
if len(actions) == 0 {
return
}
// all actions will fail atomically even if a single action fails
setErr := func(err error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea here is that if anything in this function fails, everything fails (since it's atomic). Document that here.

for i := start; i <= end; i++ {
errs[actions[i].Index] = err
for _, a := range actions {
errs[a.Index] = err

Check warning on line 627 in docstore/awsdynamodb/dynamo.go

View check run for this annotation

Codecov / codecov/patch

docstore/awsdynamodb/dynamo.go#L626-L627

Added lines #L626 - L627 were not covered by tests
}
}

tws := make([]*dyn.TransactWriteItem, 0, len(actions))

Check warning on line 631 in docstore/awsdynamodb/dynamo.go

View check run for this annotation

Codecov / codecov/patch

docstore/awsdynamodb/dynamo.go#L631

Added line #L631 was not covered by tests
var ops []*writeOp
tws := make([]*dyn.TransactWriteItem, 0, end-start+1)
for i := start; i <= end; i++ {
a := actions[i]
op, err := c.newWriteOp(a, opts)
for _, w := range actions {
op, err := c.newWriteOp(w, opts)

Check warning on line 634 in docstore/awsdynamodb/dynamo.go

View check run for this annotation

Codecov / codecov/patch

docstore/awsdynamodb/dynamo.go#L633-L634

Added lines #L633 - L634 were not covered by tests
if err != nil {
setErr(err)
return
} else {
ops = append(ops, op)
tws = append(tws, op.writeItem)

Check warning on line 640 in docstore/awsdynamodb/dynamo.go

View check run for this annotation

Codecov / codecov/patch

docstore/awsdynamodb/dynamo.go#L638-L640

Added lines #L638 - L640 were not covered by tests
}
ops = append(ops, op)
tws = append(tws, op.writeItem)
}

in := &dyn.TransactWriteItemsInput{
Expand Down
Loading
Loading