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

[executor] multi-key conflict bug #837

Merged
merged 7 commits into from
Apr 17, 2024
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
25 changes: 16 additions & 9 deletions executor/executor.go
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should align shared/readers...they no longer sound like opposite ends of a single relationship.

Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func (e *Executor) work() {
type task struct {
id int
f func() error
// shared are the tasks that this task is using non-exclusively.
shared []*task
// reading are the tasks that this task is using non-exclusively.
reading map[int]*task
Copy link
Contributor Author

@wlawt wlawt Apr 17, 2024

Choose a reason for hiding this comment

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

maybe we can have a bool flag like accessed to see if any keys in a task have used a key non-exclusively.

this would let us keep the array approach, and not adding ourselves multiple times, and is better than the map approach since we aren't doing no-ops on that action


l sync.Mutex
executed bool
Expand All @@ -95,12 +95,12 @@ func (e *Executor) runTask(t *task) {
// to ensure we can exit.
defer func() {
// Notify other tasks that we are done reading them
for _, rt := range t.shared {
for _, rt := range t.reading {
rt.l.Lock()
delete(rt.readers, t.id)
rt.l.Unlock()
}
t.shared = nil
t.reading = nil

// Nodify blocked tasks that they can execute
t.l.Lock()
Expand Down Expand Up @@ -145,9 +145,9 @@ func (e *Executor) Run(keys state.Keys, f func() error) {
id := e.tasks
e.tasks++
t := &task{
id: id,
f: f,
shared: []*task{},
id: id,
f: f,
reading: make(map[int]*task),

blocked: make(map[int]*task),
readers: make(map[int]*task),
Expand All @@ -170,16 +170,23 @@ func (e *Executor) Run(keys state.Keys, f func() error) {
if v == state.Read {
// If we don't need exclusive access to a key, just mark
// that we are reading it and that we are a reader of it.
t.shared = append(t.shared, lt)
//
// We use a map for [reading] because a single task can have
// different keys that are all just readers of another task.
t.reading[lt.id] = lt
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should note clearly the scenario for why we use a map here (i.e. that a single task can have different keys that are all just readers of another task)

lt.readers[id] = t
} else {
// If we do need exclusive access to a key, we need to
// mark ourselves blocked on all readers ahead of us.
//
// If a task is a reader, that means it is not executed yet
// and can't mark itself as executed until all [shared] are
// and can't mark itself as executed until all [reading] are
// cleared (which can't be done while we hold the lock for [lt]).
for _, rt := range lt.readers {
// Don't block on ourself if we already marked ourself as a reader
if rt.id == id {
continue
}
rt.l.Lock()
rt.blocked[id] = t
rt.l.Unlock()
Expand Down
19 changes: 5 additions & 14 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,13 +502,8 @@ func TestTwoConflictKeys(t *testing.T) {
for k := 0; k < i+1; k++ {
s.Add(ids.GenerateTestID().String(), state.Write)
}
if i == 0 || i == 1 {
s.Add(conflictKey1, state.Write)
s.Add(conflictKey2, state.Write)
} else {
s.Add(conflictKey1, state.Read)
s.Add(conflictKey2, state.Read)
}
s.Add(conflictKey1, state.Read)
s.Add(conflictKey2, state.Write)
ti := i
e.Run(s, func() error {
if ti == 10 {
Expand Down Expand Up @@ -879,13 +874,10 @@ func TestLargeRandomReadsAndWrites(t *testing.T) {
randomConflictingKeys.Add(rand.Intn(conflictSize)) //nolint:gosec
}

// randomly pick if these conflict keys are
// going to be Read/Write
conflictMode := rand.Intn(2) //nolint:gosec

// add the random keys to tx
for k := range randomConflictingKeys {
switch conflictMode {
// randomly pick if conflict key is Read/Write
switch rand.Intn(2) { //nolint:gosec
case 0:
s.Add(conflictKeys[k], state.Read)
case 1:
Expand All @@ -897,8 +889,7 @@ func TestLargeRandomReadsAndWrites(t *testing.T) {
remaining := numKeys - setSize
for j := 0; j < remaining; j++ {
// randomly pick the permission for unique keys
uniqueMode := rand.Intn(2) //nolint:gosec
switch uniqueMode {
switch rand.Intn(2) { //nolint:gosec
case 0:
s.Add(ids.GenerateTestID().String(), state.Read)
case 1:
Expand Down
Loading