use of com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException 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.IncompatibleSchemaException in project registry by hortonworks.
the class SchemaRegistryResource method executeState.
@POST
@Path("/schemas/versions/{id}/state/{stateId}")
@ApiOperation(value = "Runs the state execution for schema version identified by the given version id and executes action associated with target state id", response = Boolean.class, tags = OPERATION_GROUP_SCHEMA)
@Timed
@UnitOfWork
public Response executeState(@ApiParam(value = "version identifier of the schema", required = true) @PathParam("id") Long versionId, @ApiParam(value = "", required = true) @PathParam("stateId") Byte stateId, byte[] transitionDetails) {
Response response;
try {
schemaRegistry.transitionState(versionId, stateId, transitionDetails);
response = WSUtils.respondEntity(true, Response.Status.OK);
} catch (SchemaNotFoundException e) {
LOG.info("No schema version is found with schema version id : [{}]", versionId);
response = WSUtils.respond(Response.Status.NOT_FOUND, CatalogResponse.ResponseMessage.ENTITY_NOT_FOUND, versionId.toString());
} catch (SchemaLifecycleException e) {
LOG.error("Encountered error while disabling schema version with id [{}]", versionId, e);
CatalogResponse.ResponseMessage badRequestResponse = e.getCause() != null && e.getCause() instanceof IncompatibleSchemaException ? CatalogResponse.ResponseMessage.INCOMPATIBLE_SCHEMA : CatalogResponse.ResponseMessage.BAD_REQUEST;
response = WSUtils.respond(Response.Status.BAD_REQUEST, badRequestResponse, e.getMessage());
} catch (Exception ex) {
LOG.error("Encountered error while getting schema version with id [{}]", versionId, ex);
response = WSUtils.respond(Response.Status.INTERNAL_SERVER_ERROR, CatalogResponse.ResponseMessage.EXCEPTION, ex.getMessage());
}
return response;
}
use of com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException 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);
}
use of com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException in project streamline by hortonworks.
the class SchemaResource method postStreamsSchema.
@POST
@Timed
@Produces(MediaType.APPLICATION_JSON)
public Response postStreamsSchema(StreamsSchemaInfo streamsSchemaInfo, @Context SecurityContext securityContext) throws IOException {
Preconditions.checkNotNull(streamsSchemaInfo, "streamsSchemaInfo can not be null");
SchemaIdVersion schemaIdVersion = null;
SchemaMetadata schemaMetadata = streamsSchemaInfo.getSchemaMetadata();
String schemaName = schemaMetadata.getName();
Long schemaMetadataId = schemaRegistryClient.registerSchemaMetadata(schemaMetadata);
LOG.info("Registered schemaMetadataId [{}] for schema with name:[{}]", schemaMetadataId, schemaName);
String streamsSchemaText = streamsSchemaInfo.getSchemaVersion().getSchemaText();
try {
// convert streams schema to avro schema.
String avroSchemaText = AvroStreamlineSchemaConverter.convertStreamlineSchemaToAvroSchema(streamsSchemaText);
SchemaVersion avroSchemaVersion = new SchemaVersion(avroSchemaText, streamsSchemaInfo.getSchemaVersion().getDescription());
schemaIdVersion = schemaRegistryClient.addSchemaVersion(schemaName, avroSchemaVersion);
} catch (InvalidSchemaException e) {
String errMsg = String.format("Invalid schema received for schema with name [%s] : [%s]", schemaName, streamsSchemaText);
LOG.error(errMsg, e);
throw BadRequestException.message(errMsg, e);
} catch (SchemaNotFoundException e) {
String errMsg = String.format("Schema not found for topic: [%s]", schemaName);
LOG.error(errMsg, e);
throw EntityNotFoundException.byId(schemaName);
} catch (IncompatibleSchemaException e) {
String errMsg = String.format("Incompatible schema received for schema with name [%s] : [%s]", schemaName, streamsSchemaText);
LOG.error(errMsg, e);
throw BadRequestException.message(errMsg, e);
} catch (Exception e) {
throw new RuntimeException(e);
}
return WSUtils.respondEntity(schemaIdVersion, OK);
}
use of com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException in project registry by hortonworks.
the class SchemaRegistryClient method mergeSchemaVersion.
@Override
public SchemaVersionMergeResult mergeSchemaVersion(Long schemaVersionId) throws SchemaNotFoundException, IncompatibleSchemaException {
WebTarget target = currentSchemaRegistryTargets().schemasTarget.path(schemaVersionId + "/merge");
Response response = Subject.doAs(subject, new PrivilegedAction<Response>() {
@Override
public Response run() {
return target.request().post(null);
}
});
int status = response.getStatus();
if (status == Response.Status.OK.getStatusCode()) {
String msg = response.readEntity(String.class);
return readEntity(msg, SchemaVersionMergeResult.class);
} else if (status == Response.Status.NOT_FOUND.getStatusCode()) {
throw new SchemaNotFoundException(response.readEntity(String.class));
} else if (status == Response.Status.BAD_REQUEST.getStatusCode()) {
throw new IncompatibleSchemaException(response.readEntity(String.class));
} else {
throw new RuntimeException(response.readEntity(String.class));
}
}
Aggregations