-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(controller): Fix race of csi controller calls on the same volume (#…
…588) * Fix race of csi controller calls When a CreateVolume requests takes longer then 10 seconds the request runs into a client-side timeout and gets retied by kubelet. While processing the new request, the old request is still being canceled on the server-side and the created ZFSVolume CR gets deleted. Signed-off-by: Luca Berneking <[email protected]> * Add LockVolumeWithSnapshot function to prevent future deadlocks Signed-off-by: Luca Berneking <[email protected]> * Use single mutex for volume locks to prevent memory leak Signed-off-by: Luca Berneking <[email protected]> --------- Signed-off-by: Luca Berneking <[email protected]>
- Loading branch information
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,49 @@ | ||
package driver | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
type volumeLock struct { | ||
cond sync.Cond | ||
locked map[string]struct{} | ||
} | ||
|
||
func newVolumeLock() *volumeLock { | ||
return &volumeLock{ | ||
cond: *sync.NewCond(&sync.Mutex{}), | ||
locked: map[string]struct{}{}, | ||
} | ||
} | ||
|
||
func (l *volumeLock) LockVolume(volume string) func() { | ||
l.cond.L.Lock() | ||
defer l.cond.L.Unlock() | ||
|
||
for { | ||
if _, locked := l.locked[volume]; !locked { | ||
break | ||
} | ||
|
||
l.cond.Wait() | ||
} | ||
|
||
l.locked[volume] = struct{}{} | ||
|
||
return func() { | ||
l.cond.L.Lock() | ||
defer l.cond.L.Unlock() | ||
|
||
delete(l.locked, volume) | ||
l.cond.Broadcast() | ||
} | ||
} | ||
|
||
func (l *volumeLock) LockVolumeWithSnapshot(volume string, snapshot string) func() { | ||
unlockVol := l.LockVolume(volume) | ||
unlockSnap := l.LockVolume(snapshot) | ||
return func() { | ||
unlockVol() | ||
unlockSnap() | ||
} | ||
} |