-
Notifications
You must be signed in to change notification settings - Fork 159
parallelize node operations #302
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,62 @@ | ||
package gceGCEDriver | ||
|
||
import ( | ||
"github.com/golang/glog" | ||
"sync" | ||
) | ||
|
||
type lockWithWaiters struct { | ||
mux sync.Locker | ||
waiters uint32 | ||
} | ||
|
||
type LockManager struct { | ||
mux sync.Mutex | ||
newLocker func(...interface{}) sync.Locker | ||
locks map[string]*lockWithWaiters | ||
} | ||
|
||
func NewLockManager(f func(...interface{}) sync.Locker) *LockManager { | ||
return &LockManager{ | ||
newLocker: f, | ||
locks: make(map[string]*lockWithWaiters), | ||
} | ||
} | ||
|
||
func NewSyncMutex(lockerParams ...interface{}) sync.Locker { | ||
return &sync.Mutex{} | ||
} | ||
|
||
// Acquires the lock corresponding to a key, and allocates that lock if one does not exist. | ||
func (lm *LockManager) Acquire(key string, lockerParams ...interface{}) { | ||
lm.mux.Lock() | ||
lockForKey, ok := lm.locks[key] | ||
if !ok { | ||
lockForKey = &lockWithWaiters{ | ||
mux: lm.newLocker(lockerParams...), | ||
waiters: 0, | ||
} | ||
lm.locks[key] = lockForKey | ||
} | ||
lockForKey.waiters += 1 | ||
lm.mux.Unlock() | ||
lockForKey.mux.Lock() | ||
} | ||
|
||
// Releases the lock corresponding to a key, and deallocates that lock if no other thread | ||
// is waiting to acquire it. Logs an error and returns if the lock for a key does not exist. | ||
func (lm *LockManager) Release(key string) { | ||
lm.mux.Lock() | ||
lockForKey, ok := lm.locks[key] | ||
if !ok { | ||
// This should not happen, but if it does the only thing to do is to log the error | ||
glog.Errorf("the key being released does not correspond to an existing lock") | ||
return | ||
} | ||
lockForKey.waiters -= 1 | ||
lockForKey.mux.Unlock() | ||
if lockForKey.waiters == 0 { | ||
delete(lm.locks, key) | ||
} | ||
lm.mux.Unlock() | ||
} |
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,145 @@ | ||
package gceGCEDriver | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
"time" | ||
) | ||
|
||
// Checks that the lock manager has the expected number of locks allocated. | ||
// this function is implementation dependant! It acquires the lock and directly | ||
// checks the map of the lock manager. | ||
func checkAllocation(lm *LockManager, expectedNumAllocated int, t *testing.T) { | ||
lm.mux.Lock() | ||
defer lm.mux.Unlock() | ||
if len(lm.locks) != expectedNumAllocated { | ||
t.Fatalf("expected %d locks allocated, but found %d", expectedNumAllocated, len(lm.locks)) | ||
} | ||
} | ||
|
||
// coinOperatedMutex is a mutex that only acquires if a "coin" is provided. Otherwise | ||
// it sleeps until there is both a coin and the lock is free. This is used | ||
// so a parent thread can control the execution of children's lock. | ||
type coinOperatedMutex struct { | ||
mux *sync.Mutex | ||
cond *sync.Cond | ||
held bool | ||
coin chan coin | ||
t *testing.T | ||
} | ||
|
||
type coin struct{} | ||
|
||
func (m *coinOperatedMutex) Lock() { | ||
m.mux.Lock() | ||
defer m.mux.Unlock() | ||
|
||
for m.held || len(m.coin) == 0 { | ||
m.cond.Wait() | ||
} | ||
<-m.coin | ||
m.held = true | ||
} | ||
|
||
func (m *coinOperatedMutex) Unlock() { | ||
m.mux.Lock() | ||
defer m.mux.Unlock() | ||
|
||
m.held = false | ||
m.cond.Broadcast() | ||
} | ||
|
||
func (m *coinOperatedMutex) Deposit() { | ||
m.mux.Lock() | ||
defer m.mux.Unlock() | ||
|
||
m.coin <- coin{} | ||
m.cond.Broadcast() | ||
} | ||
|
||
func passCoinOperatedMutex(lockerParams ...interface{}) sync.Locker { | ||
return lockerParams[0].(*coinOperatedMutex) | ||
} | ||
|
||
func TestLockManagerSingle(t *testing.T) { | ||
lm := NewLockManager(NewSyncMutex) | ||
lm.Acquire("A") | ||
checkAllocation(lm, 1, t) | ||
lm.Acquire("B") | ||
checkAllocation(lm, 2, t) | ||
lm.Release("A") | ||
checkAllocation(lm, 1, t) | ||
lm.Release("B") | ||
checkAllocation(lm, 0, t) | ||
} | ||
|
||
func TestLockManagerMultiple(t *testing.T) { | ||
lm := NewLockManager(passCoinOperatedMutex) | ||
m := &sync.Mutex{} | ||
com := &coinOperatedMutex{ | ||
mux: m, | ||
cond: sync.NewCond(m), | ||
coin: make(chan coin, 1), | ||
held: false, | ||
t: t, | ||
} | ||
|
||
// start thread 1 | ||
t1OperationFinished := make(chan coin, 1) | ||
t1OkToRelease := make(chan coin, 1) | ||
go func() { | ||
lm.Acquire("A", com) | ||
t1OperationFinished <- coin{} | ||
<-t1OkToRelease | ||
lm.Release("A") | ||
t1OperationFinished <- coin{} | ||
}() | ||
|
||
// this allows the acquire by thread 1 to acquire | ||
com.Deposit() | ||
<-t1OperationFinished | ||
|
||
// thread 1 should have acquired the lock, putting allocation at 1 | ||
checkAllocation(lm, 1, t) | ||
|
||
// start thread 2 | ||
// this should allow thread 2 to start the acquire for A through the | ||
// lock manager, but block on the acquire Lock() of the lock for A. | ||
t2OperationFinished := make(chan coin, 1) | ||
t2OkToRelease := make(chan coin, 1) | ||
go func() { | ||
lm.Acquire("A") | ||
t2OperationFinished <- coin{} | ||
<-t2OkToRelease | ||
lm.Release("A") | ||
t2OperationFinished <- coin{} | ||
}() | ||
|
||
// because now thread 2 is the only thread that can run, we must wait | ||
// until it runs until it is blocked on acquire. for simplicity just wait | ||
// 5 seconds. | ||
time.Sleep(time.Second * 3) | ||
|
||
// this allows the release by thread 1 to complete | ||
// only the release can run because the acquire by thread 1 can only run if | ||
// there is both a coin and the lock is free | ||
t1OkToRelease <- coin{} | ||
<-t1OperationFinished | ||
|
||
// check that the lock has not been deallocated, since thread 2 is still waiting to acquire it | ||
checkAllocation(lm, 1, t) | ||
|
||
// this allows t2 to finish its acquire | ||
com.Deposit() | ||
<-t2OperationFinished | ||
|
||
// check that the lock has been deallocated, since thread 2 still holds it | ||
checkAllocation(lm, 1, t) | ||
|
||
// this allows the release by thread 2 to release | ||
t2OkToRelease <- coin{} | ||
<-t2OperationFinished | ||
|
||
// check that the lock has been deallocated | ||
checkAllocation(lm, 0, t) | ||
} |
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
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.
this is super cool. Definitely keep this code around and we can reference it for your reviews, though unfortunately sometimes the right thing to do is not the interesting/challenging thing :(