use of org.apache.pulsar.broker.service.schema.exceptions.IncompatibleSchemaException in project pulsar by apache.
the class AbstractTopic method addSchema.
@Override
public CompletableFuture<SchemaVersion> addSchema(SchemaData schema) {
if (schema == null) {
return CompletableFuture.completedFuture(SchemaVersion.Empty);
}
String base = TopicName.get(getName()).getPartitionedTopicName();
String id = TopicName.get(base).getSchemaName();
SchemaRegistryService schemaRegistryService = brokerService.pulsar().getSchemaRegistryService();
if (allowAutoUpdateSchema()) {
return schemaRegistryService.putSchemaIfAbsent(id, schema, getSchemaCompatibilityStrategy());
} else {
return schemaRegistryService.trimDeletedSchemaAndGetList(id).thenCompose(schemaAndMetadataList -> schemaRegistryService.getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
if (schemaVersion == null) {
return FutureUtil.failedFuture(new IncompatibleSchemaException("Schema not found and schema auto updating is disabled."));
} else {
return CompletableFuture.completedFuture(schemaVersion);
}
}));
}
}
use of org.apache.pulsar.broker.service.schema.exceptions.IncompatibleSchemaException in project pulsar by apache.
the class AvroSchemaBasedCompatibilityCheck method checkCompatible.
@Override
public void checkCompatible(Iterable<SchemaData> from, SchemaData to, SchemaCompatibilityStrategy strategy) throws IncompatibleSchemaException {
LinkedList<Schema> fromList = new LinkedList<>();
checkArgument(from != null, "check compatibility list is null");
try {
for (SchemaData schemaData : from) {
Schema.Parser parser = new Schema.Parser();
parser.setValidateDefaults(false);
fromList.addFirst(parser.parse(new String(schemaData.getData(), UTF_8)));
}
Schema.Parser parser = new Schema.Parser();
parser.setValidateDefaults(false);
Schema toSchema = parser.parse(new String(to.getData(), UTF_8));
SchemaValidator schemaValidator = createSchemaValidator(strategy);
schemaValidator.validate(toSchema, fromList);
} catch (SchemaParseException e) {
log.warn("Error during schema parsing: {}", e.getMessage());
throw new IncompatibleSchemaException(e);
} catch (SchemaValidationException e) {
log.warn("Error during schema compatibility check: {}", e.getMessage());
throw new IncompatibleSchemaException(e);
}
}
use of org.apache.pulsar.broker.service.schema.exceptions.IncompatibleSchemaException in project pulsar by apache.
the class SchemaRegistryServiceImpl method checkCompatibilityWithAll.
private CompletableFuture<Void> checkCompatibilityWithAll(SchemaData schema, SchemaCompatibilityStrategy strategy, List<SchemaAndMetadata> schemaAndMetadataList) {
CompletableFuture<Void> result = new CompletableFuture<>();
if (strategy == SchemaCompatibilityStrategy.ALWAYS_COMPATIBLE) {
result.complete(null);
} else {
SchemaAndMetadata breakSchema = null;
for (SchemaAndMetadata schemaAndMetadata : schemaAndMetadataList) {
if (schemaAndMetadata.schema.getType() != schema.getType()) {
breakSchema = schemaAndMetadata;
break;
}
}
if (breakSchema == null) {
try {
compatibilityChecks.getOrDefault(schema.getType(), SchemaCompatibilityCheck.DEFAULT).checkCompatible(schemaAndMetadataList.stream().map(schemaAndMetadata -> schemaAndMetadata.schema).collect(Collectors.toList()), schema, strategy);
result.complete(null);
} catch (Exception e) {
if (e instanceof IncompatibleSchemaException) {
result.completeExceptionally(e);
} else {
result.completeExceptionally(new IncompatibleSchemaException(e));
}
}
} else {
result.completeExceptionally(new IncompatibleSchemaException(String.format("Incompatible schema: exists schema type %s, new schema type %s", breakSchema.schema.getType(), schema.getType())));
}
}
return result;
}
use of org.apache.pulsar.broker.service.schema.exceptions.IncompatibleSchemaException in project pulsar by apache.
the class JsonSchemaCompatibilityCheck method isCompatibleJsonSchema.
private void isCompatibleJsonSchema(SchemaData from, SchemaData to) throws IncompatibleSchemaException {
try {
ObjectMapper objectMapper = getObjectMapper();
JsonSchema fromSchema = objectMapper.readValue(from.getData(), JsonSchema.class);
JsonSchema toSchema = objectMapper.readValue(to.getData(), JsonSchema.class);
if (!fromSchema.getId().equals(toSchema.getId())) {
throw new IncompatibleSchemaException(String.format("Incompatible Schema from %s + to %s", new String(from.getData(), UTF_8), new String(to.getData(), UTF_8)));
}
} catch (IOException e) {
throw new IncompatibleSchemaException(e);
}
}
use of org.apache.pulsar.broker.service.schema.exceptions.IncompatibleSchemaException in project pulsar by apache.
the class ProtobufNativeSchemaCompatibilityCheck method checkCompatible.
@Override
public void checkCompatible(SchemaData from, SchemaData to, SchemaCompatibilityStrategy strategy) throws IncompatibleSchemaException {
Descriptor fromDescriptor = ProtobufNativeSchemaUtils.deserialize(from.getData());
Descriptor toDescriptor = ProtobufNativeSchemaUtils.deserialize(to.getData());
switch(strategy) {
case BACKWARD_TRANSITIVE:
case BACKWARD:
case FORWARD_TRANSITIVE:
case FORWARD:
case FULL_TRANSITIVE:
case FULL:
checkRootMessageChange(fromDescriptor, toDescriptor, strategy);
return;
case ALWAYS_COMPATIBLE:
return;
default:
throw new IncompatibleSchemaException("Unknown SchemaCompatibilityStrategy.");
}
}
Aggregations