Search in sources :

Example 6 with IncompatibleSchemaException

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;
    });
}
Also used : Response(javax.ws.rs.core.Response) CatalogResponse(com.hortonworks.registries.common.catalog.CatalogResponse) IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) UnsupportedSchemaTypeException(com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException) SchemaBranchNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) SchemaBranchNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException) InvalidSchemaBranchDeletionException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchDeletionException) FileNotFoundException(java.io.FileNotFoundException) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaBranchAlreadyExistsException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException) UnsupportedSchemaTypeException(com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException) IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) SchemaLifecycleException(com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException) IOException(java.io.IOException) Path(javax.ws.rs.Path) UnitOfWork(com.hortonworks.registries.common.transaction.UnitOfWork) POST(javax.ws.rs.POST) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation)

Example 7 with IncompatibleSchemaException

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;
}
Also used : Response(javax.ws.rs.core.Response) CatalogResponse(com.hortonworks.registries.common.catalog.CatalogResponse) IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaLifecycleException(com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException) SchemaBranchNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException) InvalidSchemaBranchDeletionException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchDeletionException) FileNotFoundException(java.io.FileNotFoundException) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaBranchAlreadyExistsException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException) UnsupportedSchemaTypeException(com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException) IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) SchemaLifecycleException(com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException) IOException(java.io.IOException) Path(javax.ws.rs.Path) UnitOfWork(com.hortonworks.registries.common.transaction.UnitOfWork) POST(javax.ws.rs.POST) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation)

Example 8 with IncompatibleSchemaException

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);
}
Also used : Arrays(java.util.Arrays) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) SchemaBranchNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException) SchemaRegistryTestProfileType(com.hortonworks.registries.schemaregistry.avro.conf.SchemaRegistryTestProfileType) SchemaVersionLifecycleStates(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStates) SchemaRegistryTestServerClientWrapper(com.hortonworks.registries.schemaregistry.avro.helper.SchemaRegistryTestServerClientWrapper) RunWith(org.junit.runner.RunWith) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) AvroSchemaRegistryClientUtil(com.hortonworks.registries.schemaregistry.avro.util.AvroSchemaRegistryClientUtil) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) TestName(org.junit.rules.TestName) After(org.junit.After) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) Before(org.junit.Before) IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) SchemaCompatibility(com.hortonworks.registries.schemaregistry.SchemaCompatibility) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) SetUtils(org.apache.commons.collections.SetUtils) Collection(java.util.Collection) InvalidSchemaBranchDeletionException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchDeletionException) SchemaLifecycleException(com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException) Set(java.util.Set) SchemaRegistryClient(com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient) Test(org.junit.Test) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) SchemaBranch(com.hortonworks.registries.schemaregistry.SchemaBranch) Rule(org.junit.Rule) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) CustomParameterizedRunner(com.hortonworks.registries.schemaregistry.avro.util.CustomParameterizedRunner) SchemaBranchAlreadyExistsException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException) Assert(org.junit.Assert) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) SchemaBranch(com.hortonworks.registries.schemaregistry.SchemaBranch) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) Test(org.junit.Test)

Example 9 with IncompatibleSchemaException

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);
}
Also used : IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) EntityNotFoundException(com.hortonworks.streamline.common.exception.service.exception.request.EntityNotFoundException) BadRequestException(com.hortonworks.streamline.common.exception.service.exception.request.BadRequestException) IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed)

Example 10 with IncompatibleSchemaException

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));
    }
}
Also used : Response(javax.ws.rs.core.Response) CatalogResponse(com.hortonworks.registries.common.catalog.CatalogResponse) IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) WebTarget(javax.ws.rs.client.WebTarget) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)

Aggregations

IncompatibleSchemaException (com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException)15 SchemaNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)12 InvalidSchemaException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException)10 SchemaBranchNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException)9 SchemaLifecycleException (com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException)8 IOException (java.io.IOException)8 CatalogResponse (com.hortonworks.registries.common.catalog.CatalogResponse)7 SchemaIdVersion (com.hortonworks.registries.schemaregistry.SchemaIdVersion)7 SchemaMetadata (com.hortonworks.registries.schemaregistry.SchemaMetadata)7 SchemaVersion (com.hortonworks.registries.schemaregistry.SchemaVersion)6 InvalidSchemaBranchDeletionException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchDeletionException)6 SchemaBranchAlreadyExistsException (com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException)6 Timed (com.codahale.metrics.annotation.Timed)5 Lists (com.google.common.collect.Lists)5 SchemaVersionInfo (com.hortonworks.registries.schemaregistry.SchemaVersionInfo)5 UnsupportedSchemaTypeException (com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException)5 Collection (java.util.Collection)5 Collectors (java.util.stream.Collectors)5 POST (javax.ws.rs.POST)5 Response (javax.ws.rs.core.Response)5