-
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 2 commits
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
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,10 @@ | |
| import java.net.http.HttpResponse; | ||
| import java.nio.ByteOrder; | ||
| 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; | ||
|
|
||
| import org.agrona.BitUtil; | ||
|
|
@@ -54,11 +58,23 @@ public class KarapaceCatalogHandler implements CatalogHandler | |
| private final long maxAgeMillis; | ||
| private final KarapaceEventContext event; | ||
| private final long catalogId; | ||
| 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, | ||
| ConcurrentMap<Integer, CompletableFuture<String>> cachedSchemas, | ||
| ConcurrentMap<Integer, CompletableFuture<CachedSchemaId>> cachedSchemaIds) | ||
| { | ||
| this.baseUrl = config.url; | ||
| this.client = HttpClient.newHttpClient(); | ||
|
|
@@ -69,24 +85,48 @@ public KarapaceCatalogHandler( | |
| this.maxAgeMillis = config.maxAge.toMillis(); | ||
| this.event = new KarapaceEventContext(context); | ||
| this.catalogId = catalogId; | ||
| this.cachedSchemas = cachedSchemas; | ||
| this.cachedSchemaIds = cachedSchemaIds; | ||
| } | ||
|
|
||
| @Override | ||
| public String resolve( | ||
| int schemaId) | ||
| { | ||
| String schema; | ||
| String schema = null; | ||
| if (schemas.containsKey(schemaId)) | ||
| { | ||
| schema = schemas.get(schemaId); | ||
| } | ||
| else | ||
| { | ||
| String response = sendHttpRequest(MessageFormat.format(SCHEMA_PATH, schemaId)); | ||
| schema = response != null ? request.resolveSchemaResponse(response) : null; | ||
| if (schema != null) | ||
| CompletableFuture<String> future = cachedSchemas.get(schemaId); | ||
| if (future == null) | ||
| { | ||
| CompletableFuture<String> newFuture = new CompletableFuture<String>(); | ||
| future = cachedSchemas.putIfAbsent(schemaId, newFuture); | ||
| if (future == 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) | ||
| { | ||
| schemas.put(schemaId, schema); | ||
| } | ||
| } | ||
| catch (ExecutionException | InterruptedException e) | ||
| { | ||
| schemas.put(schemaId, schema); | ||
| // TODO: log an event | ||
| } | ||
| } | ||
| return schema; | ||
|
|
@@ -97,22 +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? |
||
| } | ||
| } | ||
| assert future != null; | ||
| try | ||
| { | ||
| 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; | ||
| } | ||
|
|
||
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?