Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
unify caching across workers to maximize cache hits
  • Loading branch information
ankitk-me committed Apr 24, 2024
commit d6dffb631be88cd22ca9367ee1b995c17c6c4f71
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
*/
package io.aklivity.zilla.runtime.catalog.karapace.internal;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

import io.aklivity.zilla.runtime.catalog.karapace.internal.config.KarapaceOptionsConfig;
import io.aklivity.zilla.runtime.engine.EngineContext;
import io.aklivity.zilla.runtime.engine.catalog.CatalogContext;
Expand All @@ -23,17 +26,19 @@
public class KarapaceCatalogContext implements CatalogContext
{
private final EngineContext context;
private final ConcurrentHashMap<Integer, CompletableFuture<String>> cache;

public KarapaceCatalogContext(
EngineContext context)
{
this.context = context;
this.cache = new ConcurrentHashMap<>();
}

@Override
public CatalogHandler attach(
CatalogConfig catalog)
{
return new KarapaceCatalogHandler(KarapaceOptionsConfig.class.cast(catalog.options), context, catalog.id);
return new KarapaceCatalogHandler(KarapaceOptionsConfig.class.cast(catalog.options), context, catalog.id, cache);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

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.

Suggested change
private final ConcurrentHashMap<Integer, CompletableFuture<String>> cache;
private final ConcurrentMap<Integer, CompletableFuture<String>> cachedSchemas;


public KarapaceCatalogHandler(
KarapaceOptionsConfig config,
EngineContext context,
long catalogId)
long catalogId,
ConcurrentHashMap<Integer, CompletableFuture<String>> cache)

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.

Suggested change
ConcurrentHashMap<Integer, CompletableFuture<String>> cache)
ConcurrentMap<Integer, CompletableFuture<String>> cachedSchemas)

{
this.baseUrl = config.url;
this.client = HttpClient.newHttpClient();
Expand All @@ -69,24 +74,41 @@ public KarapaceCatalogHandler(
this.maxAgeMillis = config.maxAge.toMillis();
this.event = new KarapaceEventContext(context);
this.catalogId = catalogId;
this.cache = cache;

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.

Suggested change
this.cache = cache;
this.cachedSchemas = cachedSchemas;

}

@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

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.

There is still a race here, between cache.get(schemaId) and cache.put(schemaId, future).

To solve the race, we need to use one of the methods on ConcurrentMap that is not available on Map, such as:

CompletableFuture<> future = cache.get(schemaId);
if (future == null)
{
    CompletableFuture<> newFuture = new CompletableFuture<String>();
    future = cache.putIfAbsent(schemaId, newFuture);
    if (future == null)
    {
        // send request and complete newFuture
        future = newFuture;
    }
}
assert future != null;
schema = future.get();
if (schema != null)
{
    schemas.put(schemaId, schema);
}

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.

Note, we also need to prevent unnecessary concurrent requests to resolve the schemaIds, such as latest, same as for resolving the schemas themselves from schemaIds.

}
}
return schema;
Expand All @@ -113,6 +135,10 @@ public int resolve(
{
schemaIds.put(checkSum, new CachedSchemaId(System.currentTimeMillis(), schemaId));
}
else if (schemaIds.containsKey(checkSum))

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.

Suggested change
else if (schemaIds.containsKey(checkSum))
else if (schemaIds.containsKey(schemaKey))

{
schemaId = schemaIds.get(checkSum).id;
}
Comment on lines +139 to +270

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.

Is this needed to handle the case where remote access fails, but we can serve from (stale) cache instead?

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.

yes.

}
return schemaId;
}
Expand Down

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.

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.

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.

There should be no need to drive any direct calls to KarapaceCatalogHandler via code and mocks.

Instead, this should be driven by test binding in zilla.yaml for the test. It may be necessary to add support for expected schema in test binding options so that it can verify the retrieved schema matches expectations.

Note: this would be for test binding only, as other bindings have no need to validate expectations.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<>());

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.

Suggest adding a convenience constructor that calls this(config, context, catalogId, new ConcurrentHashMap<>()) instead of requiring the test to handle the complexity.


String schema = catalog.resolve(9);

Expand All @@ -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");

Expand All @@ -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);

Expand All @@ -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"));

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand Down