-
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -20,6 +20,9 @@ | |||||
| 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.ExecutionException; | ||||||
| import java.util.zip.CRC32C; | ||||||
|
|
||||||
| import org.agrona.BitUtil; | ||||||
|
|
@@ -54,11 +57,13 @@ public class KarapaceCatalogHandler implements CatalogHandler | |||||
| private final long maxAgeMillis; | ||||||
| private final KarapaceEventContext event; | ||||||
| private final long catalogId; | ||||||
| private final ConcurrentHashMap<Integer, CompletableFuture<String>> cache; | ||||||
|
|
||||||
| public KarapaceCatalogHandler( | ||||||
| KarapaceOptionsConfig config, | ||||||
| EngineContext context, | ||||||
| long catalogId) | ||||||
| long catalogId, | ||||||
| ConcurrentHashMap<Integer, CompletableFuture<String>> cache) | ||||||
|
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.
Suggested change
|
||||||
| { | ||||||
| this.baseUrl = config.url; | ||||||
| this.client = HttpClient.newHttpClient(); | ||||||
|
|
@@ -69,24 +74,41 @@ public KarapaceCatalogHandler( | |||||
| this.maxAgeMillis = config.maxAge.toMillis(); | ||||||
| this.event = new KarapaceEventContext(context); | ||||||
| this.catalogId = catalogId; | ||||||
| this.cache = cache; | ||||||
|
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.
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| @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 = cache.get(schemaId); | ||||||
| if (future == null) | ||||||
| { | ||||||
| schemas.put(schemaId, schema); | ||||||
| future = CompletableFuture.supplyAsync(() -> | ||||||
| { | ||||||
| String response = sendHttpRequest(MessageFormat.format(SCHEMA_PATH, schemaId)); | ||||||
| return response != null ? request.resolveSchemaResponse(response) : null; | ||||||
| }); | ||||||
| } | ||||||
| try | ||||||
| { | ||||||
| schema = future.get(); | ||||||
| if (schema != null) | ||||||
| { | ||||||
| cache.put(schemaId, future); | ||||||
| schemas.put(schemaId, schema); | ||||||
| } | ||||||
| } | ||||||
| catch (ExecutionException | InterruptedException e) | ||||||
| { | ||||||
| // TODO: log an event | ||||||
|
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 is still a race here, between To solve the race, we need to use one of the methods on
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. Note, we also need to prevent unnecessary concurrent requests to resolve the schemaIds, such as |
||||||
| } | ||||||
| } | ||||||
| return schema; | ||||||
|
|
@@ -113,6 +135,10 @@ public int resolve( | |||||
| { | ||||||
| schemaIds.put(checkSum, new CachedSchemaId(System.currentTimeMillis(), schemaId)); | ||||||
| } | ||||||
| else if (schemaIds.containsKey(checkSum)) | ||||||
|
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.
Suggested change
|
||||||
| { | ||||||
| schemaId = schemaIds.get(checkSum).id; | ||||||
| } | ||||||
|
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. |
||||||
| } | ||||||
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| import static org.mockito.Mockito.mock; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| import org.agrona.DirectBuffer; | ||
| import org.agrona.concurrent.UnsafeBuffer; | ||
|
|
@@ -72,7 +73,7 @@ public void shouldResolveSchemaViaSchemaId() throws Exception | |
| "{\"name\":\"status\",\"type\":\"string\"}]," + | ||
| "\"name\":\"Event\",\"namespace\":\"io.aklivity.example\",\"type\":\"record\"}"; | ||
|
|
||
| KarapaceCatalogHandler catalog = new KarapaceCatalogHandler(config, context, 0L); | ||
| KarapaceCatalogHandler catalog = new KarapaceCatalogHandler(config, context, 0L, new ConcurrentHashMap<>()); | ||
|
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. Suggest adding a convenience constructor that calls |
||
|
|
||
| String schema = catalog.resolve(9); | ||
|
|
||
|
|
@@ -91,7 +92,7 @@ public void shouldResolveSchemaViaSubjectVersion() throws Exception | |
| "{\"name\":\"status\",\"type\":\"string\"}]," + | ||
| "\"name\":\"Event\",\"namespace\":\"io.aklivity.example\",\"type\":\"record\"}"; | ||
|
|
||
| KarapaceCatalogHandler catalog = new KarapaceCatalogHandler(config, context, 0L); | ||
| KarapaceCatalogHandler catalog = new KarapaceCatalogHandler(config, context, 0L, new ConcurrentHashMap<>()); | ||
|
|
||
| int schemaId = catalog.resolve("items-snapshots-value", "latest"); | ||
|
|
||
|
|
@@ -113,7 +114,7 @@ public void shouldResolveSchemaViaSchemaIdFromCache() throws Exception | |
| "{\"name\":\"status\",\"type\":\"string\"}]," + | ||
| "\"name\":\"Event\",\"namespace\":\"io.aklivity.example\",\"type\":\"record\"}"; | ||
|
|
||
| KarapaceCatalogHandler catalog = new KarapaceCatalogHandler(config, context, 0L); | ||
| KarapaceCatalogHandler catalog = new KarapaceCatalogHandler(config, context, 0L, new ConcurrentHashMap<>()); | ||
|
|
||
| catalog.resolve(9); | ||
|
|
||
|
|
@@ -134,7 +135,7 @@ public void shouldResolveSchemaViaSubjectVersionFromCache() throws Exception | |
| "{\"name\":\"status\",\"type\":\"string\"}]," + | ||
| "\"name\":\"Event\",\"namespace\":\"io.aklivity.example\",\"type\":\"record\"}"; | ||
|
|
||
| KarapaceCatalogHandler catalog = new KarapaceCatalogHandler(config, context, 0L); | ||
| KarapaceCatalogHandler catalog = new KarapaceCatalogHandler(config, context, 0L, new ConcurrentHashMap<>()); | ||
|
|
||
| catalog.resolve(catalog.resolve("items-snapshots-value", "latest")); | ||
|
|
||
|
|
@@ -152,15 +153,15 @@ public void shouldResolveSchemaViaSubjectVersionFromCache() throws Exception | |
| @Test | ||
| public void shouldVerifyMaxPadding() | ||
| { | ||
| KarapaceCatalogHandler catalog = new KarapaceCatalogHandler(config, context, 0L); | ||
| KarapaceCatalogHandler catalog = new KarapaceCatalogHandler(config, context, 0L, new ConcurrentHashMap<>()); | ||
|
|
||
| assertEquals(5, catalog.encodePadding()); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldVerifyEncodedData() | ||
| { | ||
| KarapaceCatalogHandler catalog = new KarapaceCatalogHandler(config, context, 0L); | ||
| KarapaceCatalogHandler catalog = new KarapaceCatalogHandler(config, context, 0L, new ConcurrentHashMap<>()); | ||
|
|
||
| DirectBuffer data = new UnsafeBuffer(); | ||
|
|
||
|
|
@@ -176,7 +177,7 @@ public void shouldVerifyEncodedData() | |
| public void shouldResolveSchemaIdAndProcessData() | ||
| { | ||
|
|
||
| KarapaceCatalogHandler catalog = new KarapaceCatalogHandler(config, context, 0L); | ||
| KarapaceCatalogHandler catalog = new KarapaceCatalogHandler(config, context, 0L, new ConcurrentHashMap<>()); | ||
|
|
||
| DirectBuffer data = new UnsafeBuffer(); | ||
|
|
||
|
|
@@ -192,7 +193,7 @@ public void shouldResolveSchemaIdAndProcessData() | |
| @Test | ||
| public void shouldResolveSchemaIdFromData() | ||
| { | ||
| KarapaceCatalogHandler catalog = new KarapaceCatalogHandler(config, context, 0L); | ||
| KarapaceCatalogHandler catalog = new KarapaceCatalogHandler(config, context, 0L, new ConcurrentHashMap<>()); | ||
|
|
||
| DirectBuffer data = new UnsafeBuffer(); | ||
|
|
||
|
|
||
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.