Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
*/
package io.aklivity.zilla.runtime.catalog.karapace.internal;

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

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 +27,22 @@
public class KarapaceCatalogContext implements CatalogContext
{
private final EngineContext context;
private final ConcurrentMap<Integer, CompletableFuture<String>> cachedSchemas;
private final ConcurrentMap<Integer, CompletableFuture<CachedSchemaId>> cachedSchemaIds;

public KarapaceCatalogContext(
EngineContext context)
{
this.context = context;
this.cachedSchemas = new ConcurrentHashMap<>();
this.cachedSchemaIds = 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,
cachedSchemas, cachedSchemaIds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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;

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.

I don't think we can reassign newFuture here, 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?

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

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.

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;
}
});

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.

I think we can perhaps simplify the logic by creating the candidate newFuture and calling:

CompletableFuture<> newFuture = new CompletableFuture<CachedSchemaId>();
CompletableFuture<> future = cachedSchemaIds.merge(schemaKey, newFuture, (v1, v2) -> v1 != null && !v1.isDone() ? v1 : v2);
if (future == newFuture)
{
    // send request
    // complete future
}
cachedSchemaId  = future.get();

cachedSchemaId = newFuture.get();
schemaId = cachedSchemaId.id;
if (schemaId != NO_SCHEMA_ID)
{
schemaIds.put(schemaKey, cachedSchemaId);
}
}
}
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.

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;
}
Expand Down