Skip to content

Conversation

@suraj44
Copy link
Contributor

@suraj44 suraj44 commented Dec 21, 2020

When a lock server is created, a lease duration has to be set. All locks that are granted are only valid for this specified duration.

A client can acquire a lock only if it has expired or if it has been released.

A lock can be released only by the session that acquired it and only if its lease is still valid.

@SUMUKHA-PK

@suraj44 suraj44 requested a review from SUMUKHA-PK December 21, 2020 06:11
Copy link
Contributor

@SUMUKHA-PK SUMUKHA-PK left a comment

Choose a reason for hiding this comment

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

Preliminary clean up review, will do a functional review after this!
Good job on getting the PR done!

time.Sleep(200 * time.Millisecond)
// changed to 10s just to test lock expiry
// TODO: Make the desired length of session expiry user configurable
time.Sleep(10000 * time.Millisecond)
Copy link
Contributor

Choose a reason for hiding this comment

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

And? Making it a configurable, injectable parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed this


got = sc.Release(d, session)
want = ErrSessionNonExistent
want = lockservice.ErrCantReleaseFile
Copy link
Contributor

Choose a reason for hiding this comment

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

Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So i changed the test that checks if session expiry works to instead check for lock expiry.

This test highlighted the redundancy for either lock expiry / session expiry. I feel like only one of them is needed. Having both is a bit redundant imo. What do you think @SUMUKHA-PK ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, ok


// If the lock is not present in the LockMap or
// the lock has expired, then allow the acquisition
if _, ok := ls.lockMap.LockMap[sd.ID()]; !ok || (ok && (compareDuration(ls.lockMap.LockMap[sd.ID()].timestamp, ls.lockMap.LeaseDuration))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

So do this instead of this messy thing:

conditioName := ok && (compareDuration(ls.lockMap.LockMap[sd.ID()].timestamp, ls.lockMap.LeaseDuration)) (name condition1 appropriately)

Then combine them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is it better now?

Str("owner", ls.lockMap.LockMap[sd.ID()].owner).
Time("timestamp", ls.lockMap.LockMap[sd.ID()].timestamp).
Msg("locked")
return nil
Copy link
Contributor

Choose a reason for hiding this comment

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

Please explain.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the if condition has changed. The new if condition is essential NOT of the previous one. If you see the additions at like #188, it contains what has been removed here.

} else if _, ok := ls.lockMap.LockMap[sd.ID()]; !ok {
return ErrCantReleaseFile

} else if compareDuration(ls.lockMap.LockMap[sd.ID()].timestamp, ls.lockMap.LeaseDuration) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a less messy way of doing this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is this really messy? Isn't it just a function call with current timestamp and lease duration as parameters?

// NewSimpleClient returns a new SimpleClient of the given parameters.
// This client works with or without the existance of a cache.
func NewSimpleClient(config *lockservice.SimpleConfig, log zerolog.Logger, cache *cache.LRUCache) *SimpleClient {
func NewSimpleClient(config *lockservice.SimpleConfig, log zerolog.Logger, cache *cache.LRUCache, sessionDuration time.Duration) *SimpleClient {
Copy link
Contributor

Choose a reason for hiding this comment

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

This line is too long, try cascading them to lines

Suggested change
func NewSimpleClient(config *lockservice.SimpleConfig, log zerolog.Logger, cache *cache.LRUCache, sessionDuration time.Duration) *SimpleClient {
func NewSimpleClient(config *lockservice.SimpleConfig,
log zerolog.Logger,
cache *cache.LRUCache,
sessionDuration time.Duration
) *SimpleClient {

Something like this with lint ^

sc.mu.Unlock()
// Sessions last for 200ms.
time.Sleep(200 * time.Millisecond)
// Sessions last for user configured duration
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Sessions last for user configured duration
// Sessions last for user configured duration.


got = sc.Release(d, session)
want = ErrSessionNonExistent
want = lockservice.ErrCantReleaseFile
Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, ok

func NewSimpleLockService(log zerolog.Logger, leaseDuration time.Duration) *SimpleLockService {
safeLockMap := &SafeLockMap{
LockMap: make(map[string]string),
LockMap: make(map[string]*LockMapEntry),
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this have to be a pointer?

func hasLeaseExpired(timestamp time.Time, lease time.Duration) bool {
intDuration := int64(time.Now().Sub(timestamp))
intLease := int64(lease)
fmt.Printf("%d %d \n", intDuration, intLease)
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove.

}

// hasLeaseExpired returns true if the lease for a lock has expired and
// returns false if the lease is still valid
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// returns false if the lease is still valid
// false if the lease is still valid.


// If the lock is not present in the LockMap or
// the lock has expired, then allow the acquisition
if _, ok := ls.lockMap.LockMap[sd.ID()]; !ok || hasLeaseExpired(ls.lockMap.LockMap[sd.ID()].timestamp, ls.lockMap.LeaseDuration) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be made less messy?
Say have a timestamp := ls.lockMap.LockMap[sd.ID()].timestamp and this use it while returning too?

type SafeLockMap struct {
LockMap map[string]string
Mutex sync.Mutex
LockMap map[string]*LockMapEntry
Copy link
Contributor

Choose a reason for hiding this comment

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

SafeLockMap.LockMap seems weird.
Make the variable as map or something that doesnt repeat weirdly. (Kinda a Go standard)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants