-
Notifications
You must be signed in to change notification settings - Fork 0
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
CBG-3271 make sure expiration works on a closed bucket #17
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0db83cb
CBG-3271 make sure expiration works on a closed bucket
torcolvin ed11962
Make expirationManager work on canonical bucket
torcolvin 5539ee5
Switch to expiryManager in standardization of terminology
torcolvin 5090510
Skip slow test
torcolvin d5e2377
Address race conditions
torcolvin 515e69f
make sure there is no race in opening a bucket
torcolvin 30107c5
Move more bucket registration and error handling into getCachedBucket
torcolvin 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 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
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
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,107 @@ | ||
// Copyright 2023-Present Couchbase, Inc. | ||
// | ||
// Use of this software is governed by the Business Source License included | ||
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified | ||
// in that file, in accordance with the Business Source License, use of this | ||
// software will be governed by the Apache License, Version 2.0, included in | ||
// the file licenses/APL2.txt. | ||
|
||
package rosmar | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
// expiryManager handles expiration for a given bucket. It stores a timer which will call expirationFunc to delete documents. The value of when the timer | ||
type expiryManager struct { | ||
mutex *sync.Mutex // mutex for synchronized access to expiryManager | ||
timer *time.Timer // Schedules expiration of docs | ||
nextExp *uint32 // Timestamp when expTimer will run (0 if never) | ||
expirationFunc func() // Function to call when timer expires | ||
} | ||
|
||
func newExpirationManager(expiractionFunc func()) *expiryManager { | ||
var nextExp uint32 | ||
return &expiryManager{ | ||
mutex: &sync.Mutex{}, | ||
nextExp: &nextExp, | ||
expirationFunc: expiractionFunc, | ||
} | ||
} | ||
|
||
// stop stops existing timers and waits for any expiration processes to complete | ||
func (e *expiryManager) stop() { | ||
e.mutex.Lock() | ||
defer e.mutex.Unlock() | ||
if e.timer != nil { | ||
e.timer.Stop() | ||
} | ||
} | ||
|
||
// _getNext returns the next expiration time, 0 if there is no scheduled expiration. | ||
func (e *expiryManager) _getNext() uint32 { | ||
return *e.nextExp | ||
} | ||
|
||
// setNext sets the next expiration time and schedules an expiration to occur after that time. | ||
func (e *expiryManager) setNext(exp uint32) { | ||
e.mutex.Lock() | ||
defer e.mutex.Unlock() | ||
e._setNext(exp) | ||
} | ||
|
||
// _clearNext clears the next expiration time. | ||
func (e *expiryManager) _clearNext() { | ||
var exp uint32 | ||
e.nextExp = &exp | ||
} | ||
|
||
// setNext sets the next expiration time and schedules an expiration to occur after that time. Requires caller to have acquired mutex. | ||
func (e *expiryManager) _setNext(exp uint32) { | ||
info("_setNext ", exp) | ||
e.nextExp = &exp | ||
if exp == 0 { | ||
e.timer = nil | ||
return | ||
} | ||
dur := expDuration(exp) | ||
if dur < 0 { | ||
dur = 0 | ||
} | ||
debug("EXP: Scheduling in %s", dur) | ||
if e.timer == nil { | ||
e.timer = time.AfterFunc(dur, e.runExpiry) | ||
} else { | ||
e.timer.Reset(dur) | ||
} | ||
} | ||
|
||
// scheduleExpirationAtOrBefore schedules the next expiration of documents to occur, from the minimum expiration value in the bucket. | ||
func (e *expiryManager) scheduleExpirationAtOrBefore(exp uint32) { | ||
if exp == 0 { | ||
return | ||
} | ||
e.mutex.Lock() | ||
defer e.mutex.Unlock() | ||
e._scheduleExpirationAtOrBefore(exp) | ||
} | ||
|
||
// _scheduleExpirationAtOrBefore schedules the next expiration of documents to occur, from the minimum expiration value in the bucket. Requires the mutext to be held. | ||
func (e *expiryManager) _scheduleExpirationAtOrBefore(exp uint32) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't look like we actually need this version (that requires holding the mutex, based on my other comment) - can this just be moved inside scheduleExpirationAtOrBefore? |
||
if exp == 0 { | ||
return | ||
} | ||
currentNextExp := e._getNext() | ||
// if zero will unset the timer. | ||
if currentNextExp == 0 || exp < currentNextExp { | ||
e._setNext(exp) | ||
} | ||
} | ||
|
||
// runExpiry is called when the timer expires. It calls the expirationFunc and then reschedules the timer if necessary. | ||
func (e *expiryManager) runExpiry() { | ||
e.mutex.Lock() | ||
defer e.mutex.Unlock() | ||
e.expirationFunc() | ||
} |
Oops, something went wrong.
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.
What happens in the case of concurrent OpenBucket requests for a bucket that doesn't already exist in the registry? It doesn't look like we're doing any locking, so in the case that registerBucket finds an existing bucket in the registry at line 205 (i.e. this instance of OpenBucket lost the race), I think we should be returning a copy of that bucket, and not the newly created one.