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

fix: Make cached GVR thread-safe #980

Merged
merged 5 commits into from
Mar 15, 2024

Conversation

usmansaleem
Copy link
Contributor

PR Description

Make cached GVR calculation thread-safe. During Web3Signer first boot, if multiple requests for signing comes in, it can result in multiple threads attempting to insert gvr in a fresh database. This would show an constraint violation in database logs.

Fixed Issue(s)

Fixes #978

Documentation

  • I thought about documentation and added the doc-change-required label to this PR if updates are required.

Changelog

  • I thought about adding a changelog entry, and added one if I deemed necessary.

Testing

  • I thought about testing these changes in a realistic/non-local environment.

var cachedGVR = cachedGVRRef.get();
if (cachedGVR == null) {
LOG.debug("No cached GVR found, fetching from database");
synchronized (lockForCachedGVR) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can one of the AtomicReference atomic operations be used to update the value to avoid the synchronized block?

@@ -36,7 +37,8 @@ public class GenesisValidatorRootValidator {
private final MetadataDao metadataDao;
private final FailsafeExecutor<Object> failsafeExecutor;

private Bytes32 cachedGenesisValidatorRoot;
private final AtomicReference<Bytes32> cachedGVRRef = new AtomicReference<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like we are using our cache, think it would be better to use the Guava cache rather than writing our thread safe implementation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, correct. Guava Cache would achieve the same result.

Copy link
Contributor

Choose a reason for hiding this comment

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

or ConcurrentHashMap?

latch.await(); // Wait for all threads to finish
executorService.shutdown(); // Shutdown the executor service

assertThat(allTrue.get()).isTrue();
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't think this check or the latch is doing anything.

When this fails, it's due to the number of times findGenesisValidatorsRoot is called

Copy link
Contributor Author

Choose a reason for hiding this comment

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

latch.await(); to make sure that all threads have joined. Agreed with allTrue. I added findByGVR assertions later on :). Will be removing allTrue assertion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

decided to rename allTrue and still kept it.

@@ -36,7 +37,8 @@ public class GenesisValidatorRootValidator {
private final MetadataDao metadataDao;
private final FailsafeExecutor<Object> failsafeExecutor;

private Bytes32 cachedGenesisValidatorRoot;
private final AtomicReference<Bytes32> cachedGVRRef = new AtomicReference<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

or ConcurrentHashMap?

Copy link
Contributor

@siladu siladu left a comment

Choose a reason for hiding this comment

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

LGTM

@usmansaleem usmansaleem enabled auto-merge (squash) March 15, 2024 03:24
@usmansaleem usmansaleem merged commit 16d301c into Consensys:master Mar 15, 2024
7 checks passed
@usmansaleem usmansaleem deleted the fix_gvr_racecondition branch March 18, 2024 01:13
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.

ERROR: duplicate key value violates unique constraint "metadata_pkey"
3 participants