mirror of
https://github.com/floci-io/floci.git
synced 2026-11-28 16:11:48 -05:00
fix(glue): ignore Avro custom attributes in schema identity (#4692)
* fix(glue): ignore Avro custom attributes in schema identity * fix(glue): preserve lookup across legacy duplicate schema versions * fix(glue): synchronize schema definition lookups with deletion * perf(glue): narrow definition lookup lock * fix(glue): revalidate schema format during definition lookup * fix(glue): scope Avro schema properties by type * fix(glue): use modeled error for schema lookup retries
This commit is contained in:
1 parent
0c8d184854
commit
c60da16c54
5 files changed
+543
-14
No files matched your search
+38
-13
@@ -43,6 +43,7 @@ public class GlueSchemaRegistryService {
|
||||
private static final int DEFAULT_MAX_RESULTS = 100;
|
||||
private static final int MAX_MAX_RESULTS = 100;
|
||||
private static final int MAX_DELETE_SCHEMA_VERSIONS = 25;
|
||||
private static final int MAX_LOOKUP_FORMAT_ATTEMPTS = 4;
|
||||
private static final int MAX_SCHEMA_DEFINITION_LENGTH = 170_000;
|
||||
|
||||
private static final Set<String> DATA_FORMATS = Set.of("AVRO", "JSON", "PROTOBUF");
|
||||
@@ -424,8 +425,15 @@ public class GlueSchemaRegistryService {
|
||||
byNumber.remove(v);
|
||||
if (hash != null) {
|
||||
Map<String, String> hashIndex = versionByDefinitionHash.get(key);
|
||||
if (hashIndex != null) {
|
||||
hashIndex.remove(hash);
|
||||
if (hashIndex != null && hashIndex.remove(hash, id)) {
|
||||
for (String remainingId : byNumber.values()) {
|
||||
SchemaVersion remaining = versionStore.get(remainingId).orElse(null);
|
||||
if (remaining != null && hash.equals(canonicalHash(
|
||||
remaining.getSchemaDefinition(), remaining.getDataFormat()))) {
|
||||
hashIndex.putIfAbsent(hash, remainingId);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (v.equals(latestRemaining)) {
|
||||
@@ -639,16 +647,31 @@ public class GlueSchemaRegistryService {
|
||||
validateDefinitionRequired(definition);
|
||||
Schema schema = resolveSchema(schemaId, region);
|
||||
String schemaKey = schemaKey(schema.getRegistryName(), schema.getSchemaName());
|
||||
String hash = canonicalHash(definition, schema.getDataFormat());
|
||||
Map<String, String> hashIndex = versionByDefinitionHash.get(schemaKey);
|
||||
String id = hashIndex != null ? hashIndex.get(hash) : null;
|
||||
if (id == null) {
|
||||
throw new AwsException("EntityNotFoundException",
|
||||
"Schema version is not found. Definition not found in " + schemaKey, 400);
|
||||
String dataFormat = schema.getDataFormat();
|
||||
for (int attempt = 0; attempt < MAX_LOOKUP_FORMAT_ATTEMPTS; attempt++) {
|
||||
String hash = canonicalHash(definition, dataFormat);
|
||||
synchronized (this) {
|
||||
String currentFormat = schemaStore.get(schemaKey)
|
||||
.orElseThrow(() -> new AwsException("EntityNotFoundException",
|
||||
"Schema is not found. " + schema.getRegistryName() + "/" + schema.getSchemaName(), 400))
|
||||
.getDataFormat();
|
||||
if (!dataFormat.equals(currentFormat)) {
|
||||
dataFormat = currentFormat;
|
||||
continue;
|
||||
}
|
||||
Map<String, String> hashIndex = versionByDefinitionHash.get(schemaKey);
|
||||
String id = hashIndex != null ? hashIndex.get(hash) : null;
|
||||
if (id == null) {
|
||||
throw new AwsException("EntityNotFoundException",
|
||||
"Schema version is not found. Definition not found in " + schemaKey, 400);
|
||||
}
|
||||
return versionStore.get(id)
|
||||
.orElseThrow(() -> new AwsException("EntityNotFoundException",
|
||||
"Schema version vanished: " + id, 400));
|
||||
}
|
||||
}
|
||||
return versionStore.get(id)
|
||||
.orElseThrow(() -> new AwsException("EntityNotFoundException",
|
||||
"Schema version vanished: " + id, 400));
|
||||
throw new AwsException("InternalServiceException",
|
||||
"Schema format changed repeatedly during definition lookup: " + schemaKey, 500);
|
||||
}
|
||||
|
||||
// ---- Helpers ---------------------------------------------------------
|
||||
@@ -933,7 +956,7 @@ public class GlueSchemaRegistryService {
|
||||
String hash = canonicalHash(version.getSchemaDefinition(), version.getDataFormat());
|
||||
versionByDefinitionHash
|
||||
.computeIfAbsent(schemaKey, k -> new ConcurrentHashMap<>())
|
||||
.put(hash, version.getSchemaVersionId());
|
||||
.putIfAbsent(hash, version.getSchemaVersionId());
|
||||
}
|
||||
|
||||
private void rebuildVersionIndexes() {
|
||||
@@ -943,7 +966,9 @@ public class GlueSchemaRegistryService {
|
||||
for (Schema s : schemaStore.scan(k -> true)) {
|
||||
arnToSchemaKey.put(s.getSchemaArn(), schemaKey(s.getRegistryName(), s.getSchemaName()));
|
||||
}
|
||||
for (SchemaVersion v : versionStore.scan(k -> true)) {
|
||||
List<SchemaVersion> versions = new ArrayList<>(versionStore.scan(k -> true));
|
||||
versions.sort(Comparator.comparing(SchemaVersion::getVersionNumber));
|
||||
for (SchemaVersion v : versions) {
|
||||
String schemaKey = arnToSchemaKey.get(v.getSchemaArn());
|
||||
if (schemaKey == null) {
|
||||
continue;
|
||||
|
||||
+62
-1
@@ -1,5 +1,10 @@
|
||||
package io.github.hectorvent.floci.services.glue.schemaregistry;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.apicurio.registry.content.ContentHandle;
|
||||
import io.apicurio.registry.content.canon.AvroContentCanonicalizer;
|
||||
import io.apicurio.registry.content.canon.ContentCanonicalizer;
|
||||
@@ -20,6 +25,7 @@ import io.apicurio.registry.rules.validity.ValidityLevel;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -28,6 +34,22 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
public final class SchemaCompatibilityChecker {
|
||||
|
||||
private static final ObjectMapper JSON = new ObjectMapper();
|
||||
private static final Set<String> AVRO_RECORD_PROPERTIES = Set.of(
|
||||
"type", "name", "namespace", "doc", "aliases", "fields", "logicalType", "precision", "scale");
|
||||
private static final Set<String> AVRO_ENUM_PROPERTIES = Set.of(
|
||||
"type", "name", "namespace", "doc", "aliases", "symbols", "default",
|
||||
"logicalType", "precision", "scale");
|
||||
private static final Set<String> AVRO_FIXED_PROPERTIES = Set.of(
|
||||
"type", "name", "namespace", "doc", "aliases", "size", "logicalType", "precision", "scale");
|
||||
private static final Set<String> AVRO_ARRAY_PROPERTIES = Set.of(
|
||||
"type", "items", "default", "logicalType", "precision", "scale");
|
||||
private static final Set<String> AVRO_MAP_PROPERTIES = Set.of(
|
||||
"type", "values", "default", "logicalType", "precision", "scale");
|
||||
private static final Set<String> AVRO_PRIMITIVE_PROPERTIES = Set.of("type", "logicalType", "precision", "scale");
|
||||
private static final Set<String> AVRO_FIELD_PROPERTIES = Set.of(
|
||||
"name", "type", "doc", "default", "order", "aliases");
|
||||
|
||||
public record Result(boolean compatible, String reason) {
|
||||
public static Result ok() {
|
||||
return new Result(true, null);
|
||||
@@ -70,7 +92,46 @@ public final class SchemaCompatibilityChecker {
|
||||
public static String canonicalize(String definition, String dataFormat) {
|
||||
ContentCanonicalizer canon = canonicalizerFor(dataFormat);
|
||||
ContentHandle handle = ContentHandle.create(definition);
|
||||
return canon.canonicalize(handle, Map.of()).content();
|
||||
String canonical = canon.canonicalize(handle, Map.of()).content();
|
||||
if (!"AVRO".equals(dataFormat)) {
|
||||
return canonical;
|
||||
}
|
||||
try {
|
||||
JsonNode schema = JSON.readTree(canonical);
|
||||
stripAvroCustomAttributes(schema);
|
||||
return schema.toString();
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new IllegalArgumentException("Unable to canonicalize AVRO schema", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void stripAvroCustomAttributes(JsonNode schema) {
|
||||
if (schema instanceof ArrayNode union) {
|
||||
for (JsonNode branch : union) {
|
||||
stripAvroCustomAttributes(branch);
|
||||
}
|
||||
} else if (schema instanceof ObjectNode object) {
|
||||
Set<String> properties = switch (object.path("type").asText()) {
|
||||
case "record", "error" -> AVRO_RECORD_PROPERTIES;
|
||||
case "enum" -> AVRO_ENUM_PROPERTIES;
|
||||
case "fixed" -> AVRO_FIXED_PROPERTIES;
|
||||
case "array" -> AVRO_ARRAY_PROPERTIES;
|
||||
case "map" -> AVRO_MAP_PROPERTIES;
|
||||
default -> AVRO_PRIMITIVE_PROPERTIES;
|
||||
};
|
||||
object.retain(properties);
|
||||
stripAvroCustomAttributes(object.get("type"));
|
||||
stripAvroCustomAttributes(object.get("items"));
|
||||
stripAvroCustomAttributes(object.get("values"));
|
||||
if (object.get("fields") instanceof ArrayNode fields) {
|
||||
for (JsonNode field : fields) {
|
||||
if (field instanceof ObjectNode fieldObject) {
|
||||
fieldObject.retain(AVRO_FIELD_PROPERTIES);
|
||||
stripAvroCustomAttributes(fieldObject.get("type"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user