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

Modified base class for BrokerAppMetadata cache. Implemented as sing… #1638

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

shoatman
Copy link
Contributor

  • Added ReentrantReadWriteLock to base class
  • Added member variable to in-memory list to reduce the number of times reading from SharedPreferences and deserializing from JSON
  • Added readLock around read operations
  • Added writeLock around update/remove/clear operations
  • Converted getAll to use a copy constructor and return a copy of the in-memory list/set

@@ -78,7 +96,14 @@ public void remove(@NonNull final String clientId,
private void disposeOfDuplicateRecords(@NonNull final String clientId,
@NonNull final String environment,
final int uid) {
final List<BrokerApplicationMetadata> allMetadata = getAll();
Set<BrokerApplicationMetadata> allMetadata = null;
readLock.lock();
Copy link
Contributor

Choose a reason for hiding this comment

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

You'll need to hold the write lock for the entire duration of this activity. Read locks aren't exclusive.

Copy link
Contributor

@AdamBJohnsonx AdamBJohnsonx left a comment

Choose a reason for hiding this comment

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

You're going to need to be far more pessimistic about your locking here. Read locks aren't exclusive, so you're not actually preventing concurrent, overlapping modification unless you hold the write lock. You have to hold that write lock through the entire read-modify-write operation unless you want to implement an optimistic locking optimization... which is almost surely unjustified.

@shoatman
Copy link
Contributor Author

shoatman commented Dec 6, 2021

@tanmaymanolkar1 - Can you take a look at this PR as discussed and propose some tests for this work. Thanks

@tanmaymanolkar1
Copy link
Contributor

Will have look at it @shoatman, thanks for working on this fix :)

Comment on lines +54 to +56
if(INSTANCE == null){
INSTANCE = new SharedPreferencesBrokerApplicationMetadataCache(context);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: consider lazy-holder pattern instead.

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