-
Notifications
You must be signed in to change notification settings - Fork 70
unify caching across workers to maximize cache hits #977
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
Changes from 1 commit
d6dffb6
d778b9e
6844810
04ee960
da28dd7
4602867
e6d7cf5
d92bc16
0027f5f
3485813
dc244a6
0ce3f55
62c8220
4fba34f
0fd0060
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| import java.text.MessageFormat; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ConcurrentMap; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.zip.CRC32C; | ||
|
|
||
|
|
@@ -57,13 +58,23 @@ public class KarapaceCatalogHandler implements CatalogHandler | |
| private final long maxAgeMillis; | ||
| private final KarapaceEventContext event; | ||
| private final long catalogId; | ||
| private final ConcurrentHashMap<Integer, CompletableFuture<String>> cache; | ||
| private final ConcurrentMap<Integer, CompletableFuture<String>> cachedSchemas; | ||
| private final ConcurrentMap<Integer, CompletableFuture<CachedSchemaId>> cachedSchemaIds; | ||
|
|
||
| public KarapaceCatalogHandler( | ||
| KarapaceOptionsConfig config, | ||
| EngineContext context, | ||
| long catalogId) | ||
| { | ||
| this(config, context, catalogId, new ConcurrentHashMap<>(), new ConcurrentHashMap<>()); | ||
| } | ||
|
|
||
| public KarapaceCatalogHandler( | ||
| KarapaceOptionsConfig config, | ||
| EngineContext context, | ||
| long catalogId, | ||
| ConcurrentHashMap<Integer, CompletableFuture<String>> cache) | ||
| ConcurrentMap<Integer, CompletableFuture<String>> cachedSchemas, | ||
| ConcurrentMap<Integer, CompletableFuture<CachedSchemaId>> cachedSchemaIds) | ||
| { | ||
| this.baseUrl = config.url; | ||
| this.client = HttpClient.newHttpClient(); | ||
|
|
@@ -74,7 +85,8 @@ public KarapaceCatalogHandler( | |
| this.maxAgeMillis = config.maxAge.toMillis(); | ||
| this.event = new KarapaceEventContext(context); | ||
| this.catalogId = catalogId; | ||
| this.cache = cache; | ||
| this.cachedSchemas = cachedSchemas; | ||
| this.cachedSchemaIds = cachedSchemaIds; | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -88,21 +100,27 @@ public String resolve( | |
| } | ||
| else | ||
| { | ||
| CompletableFuture<String> future = cache.get(schemaId); | ||
| CompletableFuture<String> future = cachedSchemas.get(schemaId); | ||
| if (future == null) | ||
| { | ||
| future = CompletableFuture.supplyAsync(() -> | ||
| CompletableFuture<String> newFuture = new CompletableFuture<String>(); | ||
| future = cachedSchemas.putIfAbsent(schemaId, newFuture); | ||
| if (future == null) | ||
| { | ||
| String response = sendHttpRequest(MessageFormat.format(SCHEMA_PATH, schemaId)); | ||
| return response != null ? request.resolveSchemaResponse(response) : null; | ||
| }); | ||
| newFuture = CompletableFuture.supplyAsync(() -> | ||
| { | ||
| String response = sendHttpRequest(MessageFormat.format(SCHEMA_PATH, schemaId)); | ||
| return response != null ? request.resolveSchemaResponse(response) : null; | ||
| }); | ||
| future = newFuture; | ||
| } | ||
| } | ||
| assert future != null; | ||
| try | ||
| { | ||
| schema = future.get(); | ||
| if (schema != null) | ||
| { | ||
| cache.put(schemaId, future); | ||
| schemas.put(schemaId, schema); | ||
| } | ||
| } | ||
|
|
@@ -119,26 +137,80 @@ public int resolve( | |
| String subject, | ||
| String version) | ||
| { | ||
| int schemaId; | ||
| int schemaId = NO_SCHEMA_ID; | ||
|
|
||
| int checkSum = generateCRC32C(subject, version); | ||
| if (schemaIds.containsKey(checkSum) && | ||
| (System.currentTimeMillis() - schemaIds.get(checkSum).timestamp) < maxAgeMillis) | ||
| int schemaKey = generateCRC32C(subject, version); | ||
| if (schemaIds.containsKey(schemaKey) && | ||
| (System.currentTimeMillis() - schemaIds.get(schemaKey).timestamp) < maxAgeMillis) | ||
| { | ||
| schemaId = schemaIds.get(checkSum).id; | ||
| schemaId = schemaIds.get(schemaKey).id; | ||
| } | ||
| else | ||
| { | ||
| String response = sendHttpRequest(MessageFormat.format(SUBJECT_VERSION_PATH, subject, version)); | ||
| schemaId = response != null ? request.resolveResponse(response) : NO_SCHEMA_ID; | ||
| if (schemaId != NO_SCHEMA_ID) | ||
| CompletableFuture<CachedSchemaId> future = cachedSchemaIds.get(schemaKey); | ||
| if (future == null) | ||
| { | ||
| schemaIds.put(checkSum, new CachedSchemaId(System.currentTimeMillis(), schemaId)); | ||
| CompletableFuture<CachedSchemaId> newFuture = new CompletableFuture<>(); | ||
| future = cachedSchemaIds.putIfAbsent(schemaKey, newFuture); | ||
| if (future == null) | ||
| { | ||
| newFuture = CompletableFuture.supplyAsync(() -> | ||
| { | ||
| String response = sendHttpRequest(MessageFormat.format(SUBJECT_VERSION_PATH, subject, version)); | ||
| return new CachedSchemaId(System.currentTimeMillis(), | ||
| response != null ? request.resolveResponse(response) : NO_SCHEMA_ID); | ||
| }); | ||
| future = newFuture; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same issue here as above, agree? |
||
| } | ||
| } | ||
| else if (schemaIds.containsKey(checkSum)) | ||
| assert future != null; | ||
| try | ||
| { | ||
| schemaId = schemaIds.get(checkSum).id; | ||
| CachedSchemaId cachedSchemaId = future.get(); | ||
| schemaId = cachedSchemaId.id; | ||
| if (schemaId != NO_SCHEMA_ID && (System.currentTimeMillis() - cachedSchemaId.timestamp) < maxAgeMillis) | ||
| { | ||
| schemaIds.put(schemaKey, cachedSchemaId); | ||
| } | ||
| else | ||
| { | ||
| CompletableFuture<CachedSchemaId> newFuture = | ||
| cachedSchemaIds.computeIfPresent(schemaKey, (key, existingFuture) -> | ||
| { | ||
| if (existingFuture.isDone() || existingFuture.isCompletedExceptionally()) | ||
| { | ||
| CompletableFuture<CachedSchemaId> id = CompletableFuture.supplyAsync(() -> | ||
| { | ||
| String response = sendHttpRequest(MessageFormat.format(SUBJECT_VERSION_PATH, | ||
| subject, version)); | ||
| return new CachedSchemaId(System.currentTimeMillis(), | ||
| response != null ? request.resolveResponse(response) : NO_SCHEMA_ID); | ||
| }); | ||
| return id; | ||
| } | ||
| else | ||
| { | ||
| return existingFuture; | ||
| } | ||
| }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can perhaps simplify the logic by creating the candidate newFuture and calling: |
||
| cachedSchemaId = newFuture.get(); | ||
| schemaId = cachedSchemaId.id; | ||
| if (schemaId != NO_SCHEMA_ID) | ||
| { | ||
| schemaIds.put(schemaKey, cachedSchemaId); | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+139
to
+270
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this needed to handle the case where remote access fails, but we can serve from (stale) cache instead?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes. |
||
| catch (ExecutionException | InterruptedException e) | ||
| { | ||
| // TODO: log an event | ||
| } | ||
| } | ||
|
|
||
| if (schemaId == NO_SCHEMA_ID && schemaIds.containsKey(schemaKey)) | ||
| { | ||
| schemaId = schemaIds.get(schemaKey).id; | ||
| // TODO: log an event to notify, that stale schemaId was returned | ||
| } | ||
| return schemaId; | ||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer this IT done via scripts not mocks. Note: if using mocks then it is a unit test, not an integration test - unit tests with mocks are not stable after a refactor of the code as they also need to be changed, whereas integration tests remain stable, providing better confidence after changing implementation.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be no need to drive any direct calls to Instead, this should be driven by Note: this would be for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we can reassign
newFuturehere, as the value being lost is the one that already made it into the concurrent map, so if we reassign it then the one in the map would not be completed, agree?