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

Replace AtomicLong to long while ReentrantReadWriteLock is enough #3

Merged
merged 3 commits into from
Mar 6, 2019
Merged

Conversation

pifuant
Copy link
Contributor

@pifuant pifuant commented Mar 5, 2019

No description provided.

@pifuant
Copy link
Contributor Author

pifuant commented Mar 5, 2019

@killme2008 @fengjiachun PTAL

@fengjiachun
Copy link
Contributor

I think you are right, 'lastCommittedIndex' does not need to be AtomicLong, ReentrantReadWriteLock is enough, but I think, can you optimize it further?
replace ReentrantReadWriteLock with StampedLock, then getLastCommittedIndex() uses StampedLock#tryOptimisticRead() to read.

@killme2008 @pifuant PTAL

@fengjiachun fengjiachun added this to the 1.2.4 milestone Mar 5, 2019
@fengjiachun fengjiachun added this to In progress in v1.2.4 via automation Mar 5, 2019
@killme2008
Copy link
Contributor

Good catch! Yep, AtomicLong is not necessary here, it can be replaced by primitive long type.

I agree with @fengjiachun , use StampedLock is a better choice because getLastCommittedIndex is called so frequently that tryOptimisticRead will help.

@fengjiachun
Copy link
Contributor

@pifuant

public long getLastCommittedIndex() {
        long stamp = stampedLock.tryOptimisticRead();
        long optimisticVal = this.lastCommittedIndex;
        if (stampedLock.validate(stamp)) {
            return optimisticVal;
        }
        stamp = stampedLock.readLock();
        try {
            return this.lastCommittedIndex;
        } finally {
            stampedLock.unlockRead(stamp);
        }
    }

@pifuant
Copy link
Contributor Author

pifuant commented Mar 6, 2019

@killme2008 @fengjiachun PTAL

@fengjiachun
Copy link
Contributor

fengjiachun commented Mar 6, 2019

@pifuant

  1. long stamp -> final long stamp ? except on tryOptimisticRead()
  2. stampedLock.unlock(stamp) -> stampedLock.unlockWrite(stamp)

@pifuant
Copy link
Contributor Author

pifuant commented Mar 6, 2019

@fengjiachun PTAL

Copy link
Contributor

@killme2008 killme2008 left a comment

Choose a reason for hiding this comment

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

👍

@fengjiachun fengjiachun merged commit 147a5bf into sofastack:master Mar 6, 2019
v1.2.4 automation moved this from In progress to Done Mar 6, 2019
@fengjiachun
Copy link
Contributor

@pifuant tks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
v1.2.4
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants