Skip to content

#170 Cache server loose track of client known keys on concurrent fetch and invalidate - #173

Merged
diegosalvi merged 5 commits into
diennea:masterfrom
dmercuriali:bug/fetch-and-load-concurrent
Feb 21, 2022
Merged

diegosalvi merged 5 commits into
diennea:masterfrom
dmercuriali:bug/fetch-and-load-concurrent

Conversation

@dmercuriali

@dmercuriali dmercuriali commented Feb 10, 2022 •

Copy link
Copy Markdown
Contributor

This PR aims to fix the bug #170 caused by concurrent fetches and invalidations.

Server-side we already have locks in place so an invalidate request for a given key can't be run concurrently to a fetch for the same key. The problem arises client-side, where the fetch code checks if fetch is still valid before actually storing the value.

The solution proposed here is to take a "lock" on the key whenever the fetch/invalidate actually tries to finish its cycle by consuming/removing the fetch from the runningFetches list and storing/removing the key from the local cache.

  • This way if the invalidate message comes first, it will cancel the running fetch and invalidate the local store. When the fetch tries to write, it will find the fetch-request as invalid and stop.
  • If instead the fetch come first, it will consume the running fetch and store the data. Then the invalidate will remove the data and send the ack message to the server.

This patch picks up from @diegosalvi branch were he added a reproducer test

To make clear that you license your contribution under
the Apache License Version 2.0, January 2004
you have to acknowledge this by using the following check-box.

@dmercuriali

Copy link
Copy Markdown
Contributor Author

@diegosalvi @eolivelli thoughts on this? Using an actual lock would be impractical since we would need a lock for every key, or some kind of lock pool.
Otherwise we could move the fetch consuming after the store call (keeping only the "validate fetch" part before the store) and, invalidation side, waiting until there are no running fetches for the key

private final CacheClientStatisticsMXBean statisticsMXBean;
private final CacheClientStatusMXBean statusMXBean;
private EntrySerializer entrySerializer = new JDKEntrySerializer();
private Set<RawString> lockedKeys = new HashSet<>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

HashSet ?

}

private void locallyLockKey(RawString key) {
synchronized (lockedKeys) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

aren't we serialising all the operations of the client ? we are not locking "per-key" here

IIRC in BlazingCache we already have good utilities for per-key locking, at least in the server

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It it serializing the whole server but just for the access at the lockedKeys set. Waiting threads won't block other to enter the serialization block and they will block only if can't add an element in lockedKeys

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We can use the server KeyedLockManager, but it will need some refactor (methods are package protected and ask for a clientId param which is never used)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Idk if it's worth it. Even with that we will still serialize the lock creation/retrieval since all operations are guarded by a general lock

}

private void locallyLockKey(RawString key) {
synchronized (lockedKeys) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It it serializing the whole server but just for the access at the lockedKeys set. Waiting threads won't block other to enter the serialization block and they will block only if can't add an element in lockedKeys

@dmercuriali
dmercuriali force-pushed the bug/fetch-and-load-concurrent branch from 8182bec to 9ad377f Compare February 12, 2022 15:05
@dmercuriali
dmercuriali marked this pull request as ready for review February 12, 2022 15:07
@dmercuriali

Copy link
Copy Markdown
Contributor Author

@eolivelli PTAL when you can

@diegosalvi
diegosalvi merged commit 414a385 into diennea:master Feb 21, 2022

@eolivelli eolivelli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Lgtm

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