use of com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException in project registry by hortonworks.
the class SchemaRegistryClient method handleSchemaIdVersionResponse.
private SchemaIdVersion handleSchemaIdVersionResponse(SchemaMetadataInfo schemaMetadataInfo, Response response) throws IncompatibleSchemaException, InvalidSchemaException {
int status = response.getStatus();
String msg = response.readEntity(String.class);
if (status == Response.Status.BAD_REQUEST.getStatusCode() || status == Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()) {
CatalogResponse catalogResponse = readCatalogResponse(msg);
if (CatalogResponse.ResponseMessage.INCOMPATIBLE_SCHEMA.getCode() == catalogResponse.getResponseCode()) {
throw new IncompatibleSchemaException(catalogResponse.getResponseMessage());
} else if (CatalogResponse.ResponseMessage.INVALID_SCHEMA.getCode() == catalogResponse.getResponseCode()) {
throw new InvalidSchemaException(catalogResponse.getResponseMessage());
} else {
throw new RuntimeException(catalogResponse.getResponseMessage());
}
}
Integer version = readEntity(msg, Integer.class);
SchemaVersionInfo schemaVersionInfo = doGetSchemaVersionInfo(new SchemaVersionKey(schemaMetadataInfo.getSchemaMetadata().getName(), version));
return new SchemaIdVersion(schemaMetadataInfo.getId(), version, schemaVersionInfo.getId());
}
use of com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException in project registry by hortonworks.
the class SchemaVersionLifecycleManager method mergeSchemaVersion.
public SchemaVersionMergeResult mergeSchemaVersion(Long schemaVersionId, SchemaVersionMergeStrategy schemaVersionMergeStrategy) throws SchemaNotFoundException, IncompatibleSchemaException {
try {
SchemaVersionInfo schemaVersionInfo = getSchemaVersionInfo(new SchemaIdVersion(schemaVersionId));
SchemaMetadataInfo schemaMetadataInfo = getSchemaMetadataInfo(schemaVersionInfo.getName());
Set<SchemaBranch> schemaBranches = getSchemaBranches(schemaVersionId).stream().filter(schemaBranch -> {
try {
return !getRootVersion(schemaBranch).getId().equals(schemaVersionId);
} catch (SchemaNotFoundException e) {
throw new RuntimeException(e);
}
}).collect(Collectors.toSet());
if (schemaBranches.size() > 1) {
throw new SchemaVersionMergeException(String.format("Can't determine a unique schema branch for schema version id : '%s'", schemaVersionId));
} else if (schemaBranches.size() == 0) {
throw new SchemaVersionMergeException(String.format("Schema version id : '%s' is not associated with any branch", schemaVersionId));
}
Long schemaBranchId = schemaBranches.iterator().next().getId();
SchemaBranch schemaBranch = schemaBranchCache.get(SchemaBranchCache.Key.of(schemaBranchId));
if (schemaVersionMergeStrategy.equals(SchemaVersionMergeStrategy.PESSIMISTIC)) {
SchemaVersionInfo latestSchemaVersion = getLatestEnabledSchemaVersionInfo(SchemaBranch.MASTER_BRANCH, schemaMetadataInfo.getSchemaMetadata().getName());
SchemaVersionInfo rootSchemaVersion = getRootVersion(schemaBranch);
if (!latestSchemaVersion.getId().equals(rootSchemaVersion.getId())) {
throw new SchemaVersionMergeException(String.format("The latest version of '%s' is different from the root version of the branch : '%s'", SchemaBranch.MASTER_BRANCH, schemaMetadataInfo.getSchemaMetadata().getName()));
}
}
byte[] initializedStateDetails;
try {
initializedStateDetails = ObjectMapperUtils.serialize(new InitializedStateDetails(schemaBranch.getName(), schemaVersionInfo.getId()));
} catch (JsonProcessingException e) {
throw new RuntimeException(String.format("Failed to serialize initializedState for %s and %s", schemaBranch.getName(), schemaVersionInfo.getId()));
}
SchemaVersionInfo createdSchemaVersionInfo;
try {
SchemaVersionInfo existingSchemaVersionInfo = findSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadataInfo.getSchemaMetadata().getType(), schemaVersionInfo.getSchemaText(), schemaMetadataInfo.getSchemaMetadata().getName());
if (existingSchemaVersionInfo != null) {
String mergeMessage = String.format("Given version %d is already merged to master with version %d", schemaVersionId, existingSchemaVersionInfo.getVersion());
LOG.info(mergeMessage);
return new SchemaVersionMergeResult(new SchemaIdVersion(schemaMetadataInfo.getId(), existingSchemaVersionInfo.getVersion(), existingSchemaVersionInfo.getId()), mergeMessage);
}
createdSchemaVersionInfo = createSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadataInfo.getSchemaMetadata(), schemaMetadataInfo.getId(), new SchemaVersion(schemaVersionInfo.getSchemaText(), schemaVersionInfo.getDescription(), SchemaVersionLifecycleStates.INITIATED.getId(), initializedStateDetails));
} catch (InvalidSchemaException e) {
throw new SchemaVersionMergeException(String.format("Failed to merge schema version : '%s'", schemaVersionId.toString()), e);
}
Collection<SchemaVersionStateStorable> schemaVersionStates = storageManager.find(SchemaVersionStateStorable.NAME_SPACE, Collections.singletonList(new QueryParam(SchemaVersionStateStorable.SCHEMA_VERSION_ID, schemaVersionId.toString())), Collections.singletonList(OrderByField.of(SchemaVersionStateStorable.SEQUENCE, true)));
if (schemaVersionStates == null || schemaVersionStates.isEmpty()) {
throw new RuntimeException(String.format("The database doesn't have any state transition recorded for the schema version id : '%s'", schemaVersionId));
}
updateSchemaVersionState(createdSchemaVersionInfo.getId(), schemaVersionStates.iterator().next().getSequence(), SchemaVersionLifecycleStates.ENABLED.getId(), null);
String mergeMessage = String.format("Given version %d is merged successfully to master with version %d", schemaVersionId, createdSchemaVersionInfo.getVersion());
LOG.info(mergeMessage);
return new SchemaVersionMergeResult(new SchemaIdVersion(schemaMetadataInfo.getId(), createdSchemaVersionInfo.getVersion(), createdSchemaVersionInfo.getId()), mergeMessage);
} catch (SchemaBranchNotFoundException e) {
throw new SchemaVersionMergeException(String.format("Failed to merge schema version : '%s'", schemaVersionId.toString()), e);
}
}
use of com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException in project registry by hortonworks.
the class ConfluentSchemaRegistryCompatibleResource method registerSchemaVersion.
@POST
@Path("/subjects/{subject}/versions")
@ApiOperation(value = "Register a new version of the schema", notes = "Registers the given schema version to schema with subject if the given schemaText is not registered as a version for this schema, " + "and returns respective unique id." + "In case of incompatible schema errors, it throws error message like 'Unable to read schema: <> using schema <>' ", response = Id.class, tags = OPERATION_GROUP_CONFLUENT_SR)
@Timed
@UnitOfWork
public Response registerSchemaVersion(@ApiParam(value = "subject", required = true) @PathParam("subject") String subject, @ApiParam(value = "Details about the schema", required = true) String schema, @Context UriInfo uriInfo) {
LOG.info("registerSchema for [{}] is [{}]", subject);
return handleLeaderAction(uriInfo, () -> {
Response response;
try {
LOG.info("registerSchema for [{}] is [{}]", subject);
SchemaMetadataInfo schemaMetadataInfo = schemaRegistry.getSchemaMetadataInfo(subject);
if (schemaMetadataInfo == null) {
SchemaMetadata schemaMetadata = new SchemaMetadata.Builder(subject).type(AvroSchemaProvider.TYPE).schemaGroup("Kafka").build();
schemaRegistry.addSchemaMetadata(schemaMetadata);
schemaMetadataInfo = schemaRegistry.getSchemaMetadataInfo(subject);
}
SchemaIdVersion schemaVersionInfo = schemaRegistry.addSchemaVersion(schemaMetadataInfo.getSchemaMetadata(), new SchemaVersion(schemaStringFromJson(schema).getSchema(), null));
Id id = new Id();
id.setId(schemaVersionInfo.getSchemaVersionId());
response = WSUtils.respondEntity(id, Response.Status.OK);
} catch (InvalidSchemaException ex) {
LOG.error("Invalid schema error encountered while adding subject [{}]", subject, ex);
response = invalidSchemaError();
} catch (IncompatibleSchemaException ex) {
LOG.error("Incompatible schema error encountered while adding subject [{}]", subject, ex);
response = incompatibleSchemaError();
} catch (UnsupportedSchemaTypeException ex) {
LOG.error("Unsupported schema type encountered while adding subject [{}]", subject, ex);
response = incompatibleSchemaError();
} catch (Exception ex) {
LOG.error("Encountered error while adding subject [{}]", subject, ex);
response = serverError();
}
return response;
});
}
use of com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException in project registry by hortonworks.
the class SchemaRegistryResource method addSchemaVersion.
@POST
@Path("/schemas/{name}/versions")
@ApiOperation(value = "Register a new version of the schema", notes = "Registers the given schema version to schema with name if the given schemaText is not registered as a version for this schema, " + "and returns respective version number." + "In case of incompatible schema errors, it throws error message like 'Unable to read schema: <> using schema <>' ", response = Integer.class, tags = OPERATION_GROUP_SCHEMA)
@Timed
@UnitOfWork
public Response addSchemaVersion(@QueryParam("branch") @DefaultValue(MASTER_BRANCH) String schemaBranchName, @ApiParam(value = "Schema name", required = true) @PathParam("name") String schemaName, @ApiParam(value = "Details about the schema", required = true) SchemaVersion schemaVersion, @Context UriInfo uriInfo) {
return handleLeaderAction(uriInfo, () -> {
Response response;
try {
LOG.info("adding schema version for name [{}] with [{}]", schemaName, schemaVersion);
SchemaIdVersion version = schemaRegistry.addSchemaVersion(schemaBranchName, schemaName, schemaVersion);
response = WSUtils.respondEntity(version.getVersion(), Response.Status.CREATED);
} catch (InvalidSchemaException ex) {
LOG.error("Invalid schema error encountered while adding schema [{}] with key [{}]", schemaVersion, schemaName, ex);
response = WSUtils.respond(Response.Status.BAD_REQUEST, CatalogResponse.ResponseMessage.INVALID_SCHEMA, ex.getMessage());
} catch (IncompatibleSchemaException ex) {
LOG.error("Incompatible schema error encountered while adding schema [{}] with key [{}]", schemaVersion, schemaName, ex);
response = WSUtils.respond(Response.Status.BAD_REQUEST, CatalogResponse.ResponseMessage.INCOMPATIBLE_SCHEMA, ex.getMessage());
} catch (UnsupportedSchemaTypeException ex) {
LOG.error("Unsupported schema type encountered while adding schema [{}] with key [{}]", schemaVersion, schemaName, ex);
response = WSUtils.respond(Response.Status.BAD_REQUEST, CatalogResponse.ResponseMessage.UNSUPPORTED_SCHEMA_TYPE, ex.getMessage());
} catch (SchemaBranchNotFoundException e) {
return WSUtils.respond(Response.Status.NOT_FOUND, CatalogResponse.ResponseMessage.ENTITY_NOT_FOUND, e.getMessage());
} catch (Exception ex) {
LOG.error("Encountered error while adding schema [{}] with key [{}]", schemaVersion, schemaName, ex, ex);
response = WSUtils.respond(Response.Status.INTERNAL_SERVER_ERROR, CatalogResponse.ResponseMessage.EXCEPTION, ex.getMessage());
}
return response;
});
}
use of com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException in project registry by hortonworks.
the class SchemaBranchLifeCycleTest method addSchemaVersionToBranch.
@Test
public void addSchemaVersionToBranch() throws IOException, SchemaBranchNotFoundException, InvalidSchemaException, SchemaNotFoundException, IncompatibleSchemaException, SchemaBranchAlreadyExistsException {
SchemaMetadata schemaMetadata = addSchemaMetadata(testNameRule.getMethodName(), SchemaCompatibility.NONE);
SchemaIdVersion masterSchemaIdVersion1 = addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata, "/device.avsc");
SchemaBranch schemaBranch1 = addSchemaBranch("BRANCH1", schemaMetadata, masterSchemaIdVersion1.getSchemaVersionId());
addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-incompat.avsc");
addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-compat.avsc");
Collection<SchemaVersionInfo> schemaBranch1VersionInfos = schemaRegistryClient.getAllVersions(schemaBranch1.getName(), schemaMetadata.getName());
Collection<SchemaVersionInfo> masterSchemaVersionInfos = schemaRegistryClient.getAllVersions(schemaMetadata.getName());
Assert.assertTrue(masterSchemaVersionInfos.size() == 1);
Assert.assertTrue(schemaBranch1VersionInfos.size() == 3);
Long versionsInInitiatedState = schemaBranch1VersionInfos.stream().filter(schemaVersionInfo -> schemaVersionInfo.getStateId().equals(SchemaVersionLifecycleStates.INITIATED.getId())).count();
Long versionsInEnabledState = schemaBranch1VersionInfos.stream().filter(schemaVersionInfo -> schemaVersionInfo.getStateId().equals(SchemaVersionLifecycleStates.ENABLED.getId())).count();
Assert.assertTrue(versionsInInitiatedState == 2);
Assert.assertTrue(versionsInEnabledState == 1);
}
Aggregations