-
Notifications
You must be signed in to change notification settings - Fork 56
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
Model specific cache detect schema change update #767
Merged
jfallows
merged 3 commits into
aklivity:feature/schema-registry
from
ankitk-me:modelCacheTTL
Jan 31, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,12 +16,14 @@ | |
|
||
import java.io.StringReader; | ||
import java.util.function.LongFunction; | ||
import java.util.zip.CRC32C; | ||
|
||
import jakarta.json.spi.JsonProvider; | ||
import jakarta.json.stream.JsonParser; | ||
import jakarta.json.stream.JsonParserFactory; | ||
|
||
import org.agrona.DirectBuffer; | ||
import org.agrona.collections.Int2IntHashMap; | ||
import org.agrona.collections.Int2ObjectCache; | ||
import org.agrona.io.DirectBufferInputStream; | ||
import org.leadpony.justify.api.JsonSchema; | ||
|
@@ -35,7 +37,7 @@ | |
import io.aklivity.zilla.runtime.engine.config.SchemaConfig; | ||
import io.aklivity.zilla.runtime.model.json.config.JsonModelConfig; | ||
|
||
public abstract class JsonConverterHandler | ||
public abstract class JsonModelHandler | ||
{ | ||
protected final SchemaConfig catalog; | ||
protected final CatalogHandler handler; | ||
|
@@ -46,9 +48,11 @@ public abstract class JsonConverterHandler | |
private final JsonProvider schemaProvider; | ||
private final JsonValidationService service; | ||
private final JsonParserFactory factory; | ||
private final CRC32C crc32c; | ||
private final Int2IntHashMap crcCache; | ||
private DirectBufferInputStream in; | ||
|
||
public JsonConverterHandler( | ||
public JsonModelHandler( | ||
JsonModelConfig config, | ||
LongFunction<CatalogHandler> supplyCatalog) | ||
{ | ||
|
@@ -64,6 +68,8 @@ public JsonConverterHandler( | |
this.schemas = new Int2ObjectCache<>(1, 1024, i -> {}); | ||
this.providers = new Int2ObjectCache<>(1, 1024, i -> {}); | ||
this.in = new DirectBufferInputStream(); | ||
this.crc32c = new CRC32C(); | ||
this.crcCache = new Int2IntHashMap(0); | ||
} | ||
|
||
protected final boolean validate( | ||
|
@@ -75,6 +81,7 @@ protected final boolean validate( | |
boolean status = false; | ||
try | ||
{ | ||
invalidateCacheOnSchemaUpdate(schemaId); | ||
JsonProvider provider = supplyProvider(schemaId); | ||
in.wrap(buffer, index, length); | ||
provider.createReader(in).readValue(); | ||
|
@@ -87,18 +94,34 @@ protected final boolean validate( | |
return status; | ||
} | ||
|
||
private JsonSchema supplySchema( | ||
protected void invalidateCacheOnSchemaUpdate( | ||
int schemaId) | ||
{ | ||
return schemas.computeIfAbsent(schemaId, this::resolveSchema); | ||
if (crcCache.containsKey(schemaId)) | ||
{ | ||
String schemaText = handler.resolve(schemaId); | ||
int checkSum = generateCRC32C(schemaText); | ||
if (schemaText != null && crcCache.get(schemaId) != checkSum) | ||
{ | ||
crcCache.remove(schemaId); | ||
schemas.remove(schemaId); | ||
providers.remove(schemaId); | ||
} | ||
} | ||
} | ||
|
||
private JsonProvider supplyProvider( | ||
protected JsonProvider supplyProvider( | ||
int schemaId) | ||
{ | ||
return providers.computeIfAbsent(schemaId, this::createProvider); | ||
} | ||
|
||
private JsonSchema supplySchema( | ||
int schemaId) | ||
{ | ||
return schemas.computeIfAbsent(schemaId, this::resolveSchema); | ||
} | ||
|
||
private JsonSchema resolveSchema( | ||
int schemaId) | ||
{ | ||
|
@@ -109,6 +132,7 @@ private JsonSchema resolveSchema( | |
JsonParser schemaParser = factory.createParser(new StringReader(schemaText)); | ||
JsonSchemaReader reader = service.createSchemaReader(schemaParser); | ||
schema = reader.read(); | ||
crcCache.put(schemaId, generateCRC32C(schemaText)); | ||
} | ||
|
||
return schema; | ||
|
@@ -125,4 +149,13 @@ private JsonProvider createProvider( | |
} | ||
return provider; | ||
} | ||
|
||
private int generateCRC32C( | ||
String schemaText) | ||
{ | ||
byte[] bytes = schemaText.getBytes(); | ||
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 feedback here as for |
||
crc32c.reset(); | ||
crc32c.update(bytes, 0, bytes.length); | ||
return (int) crc32c.getValue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This line is going to allocate a new byte[] on every call to validate.
Let's instead store the actual schemaText rather than a crc32c hash to compare.