Skip to content

Commit

Permalink
Merge pull request #106 from ajohnstonTE/fix-cache-group-race-condition
Browse files Browse the repository at this point in the history
Fix cache group race condition
  • Loading branch information
msmith-techempower authored Aug 6, 2021
2 parents 30d1442 + d75fb61 commit 4c97015
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
27 changes: 21 additions & 6 deletions gemini/src/main/java/com/techempower/cache/CacheGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,23 +179,38 @@ public void setObjects(Collection<T> objects)
/**
* Resets this group, removing all the objects, and setting the initialized
* flag to false. The group will be rebuilt from the database on next use.
* Can lead to temporary stale data in certain edge cases. See
* <a href="https://github.com/TechEmpower/gemini/issues/105">this issue</a>
* for details.
*/
@Override
public void reset()
{
synchronized (this)
{
setInitialized(false);
// Instantiate a new object so that any threads currently working with
// an old reference outside of a synchronized block will not be
// affected.
this.objects = new ConcurrentHashMap<>();
this.objectsInOrder = new CopyOnWriteArrayList<>();
setErrorOnInitialize(false);
resetHighLowIdentities();
}
}

/**
* Synchronously resets and re-initializes this group, removing all the
* objects, and setting the initialized flag to false. The group will
* is then rebuilt from the database before the synchronization block
* ends. To avoids stale data in certain edge cases that {@link #reset()}
* can lead to.
*/
@Override
public void resetSynchronous()
{
synchronized (this)
{
reset();
initializeIfNecessary();
}
}

@Override
public T get(long id)
{
Expand Down Expand Up @@ -1073,4 +1088,4 @@ public Builder<T> constructorArgs(Object... arguments)

} // End Builder.

} // End CacheGroup.
} // End CacheGroup.
11 changes: 11 additions & 0 deletions gemini/src/main/java/com/techempower/data/EntityGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,17 @@ public void reset()
// Does nothing here.
}

/**
* Synchronously resets and re-initializes this group of entities. In
* the base class, this doesn't do anything, but subclasses such as
* CacheGroup act differently.
*/
public void resetSynchronous()
{
// Call reset() at least, for subclasses besides CacheGroup.
reset();
}

/**
* Returns the comparator to use when sorting entities of this type.
*/
Expand Down

0 comments on commit 4c97015

Please sign in to comment.