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

Fix dataraces in memqdb #260

Merged
merged 11 commits into from
Aug 10, 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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pooler_run:
####################### TESTS #######################

unittest:
go test ./cmd/... ./pkg/... ./router/... ./qdb/... ./coordinator/...
go test -race ./cmd/... ./pkg/... ./router/... ./qdb/... ./coordinator/...

regress_local: proxy_2sh_run
./script/regress_local.sh
Expand Down
9 changes: 1 addition & 8 deletions pkg/coord/local/clocal.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,9 @@ func (qr *LocalCoordinator) Unite(ctx context.Context, req *kr.UniteKeyRange) er
}(qr.qdb, ctx, req.KeyRangeIDLeft)

// TODO: krRight seems to be empty.
if krright, err = qr.qdb.LockKeyRange(ctx, req.KeyRangeIDRight); err != nil {
if krright, err = qr.qdb.GetKeyRange(ctx, req.KeyRangeIDRight); err != nil {
return err
}
defer func(qdb qdb.QDB, ctx context.Context, keyRangeID string) {
err := qdb.UnlockKeyRange(ctx, keyRangeID)
if err != nil {
spqrlog.Zero.Error().Err(err).Msg("")
return
}
}(qr.qdb, ctx, req.KeyRangeIDRight)

if err = qr.qdb.DropKeyRange(ctx, krright.KeyRangeID); err != nil {
return err
Expand Down
74 changes: 54 additions & 20 deletions qdb/command.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package qdb

import (
"fmt"

"github.com/pg-sharding/spqr/pkg/spqrlog"
)

type Command interface {
Do()
Undo()
Do() error
Undo() error
}

func NewDeleteCommand[T any](m map[string]T, key string) *DeleteCommand[T] {
Expand All @@ -16,17 +22,19 @@ type DeleteCommand[T any] struct {
present bool
}

func (c *DeleteCommand[T]) Do() {
func (c *DeleteCommand[T]) Do() error {
c.value, c.present = c.m[c.key]
delete(c.m, c.key)
return nil
}

func (c *DeleteCommand[T]) Undo() {
func (c *DeleteCommand[T]) Undo() error {
if !c.present {
delete(c.m, c.key)
} else {
c.m[c.key] = c.value
}
return nil
}

func NewUpdateCommand[T any](m map[string]T, key string, value T) *UpdateCommand[T] {
Expand All @@ -41,17 +49,19 @@ type UpdateCommand[T any] struct {
present bool
}

func (c *UpdateCommand[T]) Do() {
func (c *UpdateCommand[T]) Do() error {
c.prevValue, c.present = c.m[c.key]
c.m[c.key] = c.value
return nil
}

func (c *UpdateCommand[T]) Undo() {
func (c *UpdateCommand[T]) Undo() error {
if !c.present {
delete(c.m, c.key)
} else {
c.m[c.key] = c.prevValue
}
return nil
}

func NewDropCommand[T any](m map[string]T) *DropCommand[T] {
Expand All @@ -63,47 +73,71 @@ type DropCommand[T any] struct {
copy map[string]T
}

func (c *DropCommand[T]) Do() {
func (c *DropCommand[T]) Do() error {
c.copy = make(map[string]T)
for k, v := range c.m {
c.copy[k] = v
}
for k := range c.m {
delete(c.m, k)
}
return nil
}

func (c *DropCommand[T]) Undo() {
func (c *DropCommand[T]) Undo() error {
for k, v := range c.copy {
c.m[k] = v
}
return nil
}

func NewCustomCommand(do func(), undo func()) *CustomCommand {
func NewCustomCommand(do func() error, undo func() error) *CustomCommand {
return &CustomCommand{do: do, undo: undo}
}

type CustomCommand struct {
do func()
undo func()
do func() error
undo func() error
}

func (c *CustomCommand) Do() {
c.do()
func (c *CustomCommand) Do() error {
return c.do()
}

func (c *CustomCommand) Undo() {
c.undo()
func (c *CustomCommand) Undo() error {
return c.undo()
}

func ExecuteCommands(saver func() error, commands ...Command) error {
func doCommands(commands ...Command) (int, error) {
for i, c := range commands {
err := c.Do()
if err != nil {
return i, err
}
}
return len(commands), nil
}

func undoCommands(commands ...Command) error {
spqrlog.Zero.Info().Msg("memqdb: undo commands")
for _, c := range commands {
c.Do()
err := c.Undo()
if err != nil {
return err
}
}
return nil
}

func ExecuteCommands(saver func() error, commands ...Command) error {
completed, err := doCommands(commands...)
if err == nil {
err = saver()
}
err := saver()
if err != nil {
for _, c := range commands {
c.Undo()
undoErr := undoCommands(commands[:completed]...)
if undoErr != nil {
return fmt.Errorf("failed to undo command %s while: %s", undoErr.Error(), err.Error())
}
return err
}
Expand Down
Loading
Loading