Search in sources :

Example 1 with UnsupportedSchemaTypeException

use of com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException in project registry by hortonworks.

the class SchemaVersionLifecycleManager method createSchemaVersion.

private SchemaVersionInfo createSchemaVersion(String schemaBranchName, SchemaMetadata schemaMetadata, Long schemaMetadataId, SchemaVersion schemaVersion) throws IncompatibleSchemaException, InvalidSchemaException, SchemaNotFoundException, SchemaBranchNotFoundException {
    Preconditions.checkNotNull(schemaBranchName, "schemaBranchName must not be null");
    Preconditions.checkNotNull(schemaMetadataId, "schemaMetadataId must not be null");
    String type = schemaMetadata.getType();
    if (getSchemaProvider(type) == null) {
        throw new UnsupportedSchemaTypeException("Given schema type " + type + " not supported");
    }
    SchemaBranch schemaBranch = null;
    try {
        schemaBranch = schemaBranchCache.get(SchemaBranchCache.Key.of(new SchemaBranchKey(schemaBranchName, schemaMetadata.getName())));
    } catch (SchemaBranchNotFoundException e) {
    // Ignore this error
    }
    if (schemaBranch == null) {
        if (getAllVersions(schemaBranchName, schemaMetadata.getName()).size() != 0)
            throw new RuntimeException(String.format("Schema name : '%s' and branch name : '%s' has schema version, yet failed to obtain schema branch instance", schemaMetadata.getName(), schemaBranchName));
    }
    // generate fingerprint, it parses the schema and checks for semantic validation.
    // throws InvalidSchemaException for invalid schemas.
    final String fingerprint = getFingerprint(type, schemaVersion.getSchemaText());
    final String schemaName = schemaMetadata.getName();
    SchemaVersionStorable schemaVersionStorable = new SchemaVersionStorable();
    final Long schemaVersionStorableId = storageManager.nextId(schemaVersionStorable.getNameSpace());
    schemaVersionStorable.setId(schemaVersionStorableId);
    schemaVersionStorable.setSchemaMetadataId(schemaMetadataId);
    schemaVersionStorable.setFingerprint(fingerprint);
    schemaVersionStorable.setName(schemaName);
    schemaVersionStorable.setSchemaText(schemaVersion.getSchemaText());
    schemaVersionStorable.setDescription(schemaVersion.getDescription());
    schemaVersionStorable.setTimestamp(System.currentTimeMillis());
    schemaVersionStorable.setState(DEFAULT_VERSION_STATE.getId());
    if (!schemaBranchName.equals(SchemaBranch.MASTER_BRANCH)) {
        schemaVersion.setState(SchemaVersionLifecycleStates.INITIATED.getId());
        schemaVersion.setStateDetails(null);
    }
    // take a lock for a schema with same name.
    int retryCt = 0;
    while (true) {
        try {
            Integer version = 0;
            Byte initialState = schemaVersion.getInitialState();
            if (schemaMetadata.isEvolve()) {
                // if the given version is added with enabled or initiated state then only check for compatibility
                if (SchemaVersionLifecycleStates.ENABLED.getId().equals(initialState) || SchemaVersionLifecycleStates.INITIATED.getId().equals(initialState)) {
                    CompatibilityResult compatibilityResult = checkCompatibility(schemaBranchName, schemaName, schemaVersion.getSchemaText());
                    if (!compatibilityResult.isCompatible()) {
                        String errMsg = String.format("Given schema is not compatible with latest schema versions. \n" + "Error location: [%s] \n" + "Error encountered is: [%s]", compatibilityResult.getErrorLocation(), compatibilityResult.getErrorMessage());
                        LOG.error(errMsg);
                        throw new IncompatibleSchemaException(errMsg);
                    }
                }
                SchemaVersionInfo latestSchemaVersionInfo = getLatestSchemaVersionInfo(schemaName);
                if (latestSchemaVersionInfo != null) {
                    version = latestSchemaVersionInfo.getVersion();
                }
            }
            schemaVersionStorable.setVersion(version + 1);
            storageManager.add(schemaVersionStorable);
            updateSchemaVersionState(schemaVersionStorable.getId(), 1, initialState, schemaVersion.getStateDetails());
            break;
        } catch (StorageException e) {
            // optimistic to try the next try would be successful. When retry attempts are exhausted, throw error back to invoker.
            if (++retryCt == DEFAULT_RETRY_CT) {
                LOG.error("Giving up after retry attempts [{}] while trying to add new version of schema with metadata [{}]", retryCt, schemaMetadata, e);
                throw e;
            }
            LOG.debug("Encountered storage exception while trying to add a new version, attempting again : [{}] with error: [{}]", retryCt, e);
        }
    }
    // fetching this as the ID may have been set by storage manager.
    Long schemaInstanceId = schemaVersionStorable.getId();
    SchemaBranchVersionMapping schemaBranchVersionMapping = new SchemaBranchVersionMapping(schemaBranch.getId(), schemaInstanceId);
    storageManager.add(schemaBranchVersionMapping);
    String storableNamespace = new SchemaFieldInfoStorable().getNameSpace();
    List<SchemaFieldInfo> schemaFieldInfos = getSchemaProvider(type).generateFields(schemaVersionStorable.getSchemaText());
    for (SchemaFieldInfo schemaFieldInfo : schemaFieldInfos) {
        final Long fieldInstanceId = storageManager.nextId(storableNamespace);
        SchemaFieldInfoStorable schemaFieldInfoStorable = SchemaFieldInfoStorable.fromSchemaFieldInfo(schemaFieldInfo, fieldInstanceId);
        schemaFieldInfoStorable.setSchemaInstanceId(schemaInstanceId);
        schemaFieldInfoStorable.setTimestamp(System.currentTimeMillis());
        storageManager.add(schemaFieldInfoStorable);
    }
    return schemaVersionStorable.toSchemaVersionInfo();
}
Also used : SchemaBranchNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException) IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) UnsupportedSchemaTypeException(com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException) InvalidSchemaBranchVersionMapping(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchVersionMapping) StorageException(com.hortonworks.registries.storage.exception.StorageException)

Example 2 with UnsupportedSchemaTypeException

use of com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException 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;
    });
}
Also used : CatalogResponse(com.hortonworks.registries.common.catalog.CatalogResponse) Response(javax.ws.rs.core.Response) 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) UnsupportedSchemaTypeException(com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) UnsupportedSchemaTypeException(com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException) IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) IOException(java.io.IOException) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo) 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 3 with UnsupportedSchemaTypeException

use of com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException 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 4 with UnsupportedSchemaTypeException

use of com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException in project registry by hortonworks.

the class DefaultSchemaRegistry method addSchemaMetadata.

public Long addSchemaMetadata(SchemaMetadata schemaMetadata, boolean throwErrorIfExists) throws UnsupportedSchemaTypeException {
    SchemaMetadataStorable givenSchemaMetadataStorable = SchemaMetadataStorable.fromSchemaMetadataInfo(new SchemaMetadataInfo(schemaMetadata));
    String type = schemaMetadata.getType();
    if (schemaTypeWithProviders.get(type) == null) {
        throw new UnsupportedSchemaTypeException("Given schema type " + type + " not supported");
    }
    if (!throwErrorIfExists) {
        Storable schemaMetadataStorable = storageManager.get(givenSchemaMetadataStorable.getStorableKey());
        if (schemaMetadataStorable != null) {
            return schemaMetadataStorable.getId();
        }
    }
    final Long nextId = storageManager.nextId(givenSchemaMetadataStorable.getNameSpace());
    givenSchemaMetadataStorable.setId(nextId);
    givenSchemaMetadataStorable.setTimestamp(System.currentTimeMillis());
    storageManager.addOrUpdate(givenSchemaMetadataStorable);
    // Add a schema branch for this metadata
    SchemaBranchStorable schemaBranchStorable = new SchemaBranchStorable(SchemaBranch.MASTER_BRANCH, schemaMetadata.getName(), String.format(SchemaBranch.MASTER_BRANCH_DESC, schemaMetadata.getName()), System.currentTimeMillis());
    schemaBranchStorable.setId(storageManager.nextId(SchemaBranchStorable.NAME_SPACE));
    storageManager.add(schemaBranchStorable);
    return givenSchemaMetadataStorable.getId();
}
Also used : UnsupportedSchemaTypeException(com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException) Storable(com.hortonworks.registries.storage.Storable)

Example 5 with UnsupportedSchemaTypeException

use of com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException in project registry by hortonworks.

the class SchemaRegistryResource method addSchemaInfo.

@POST
@Path("/schemas")
@ApiOperation(value = "Create a schema if it does not already exist", notes = "Creates a schema with the given schema information if it does not already exist." + " A unique schema identifier is returned.", response = Long.class, tags = OPERATION_GROUP_SCHEMA)
@Timed
@UnitOfWork
public Response addSchemaInfo(@ApiParam(value = "Schema to be added to the registry", required = true) SchemaMetadata schemaMetadata, @Context UriInfo uriInfo, @Context HttpHeaders httpHeaders) {
    return handleLeaderAction(uriInfo, () -> {
        Response response;
        try {
            schemaMetadata.trim();
            checkValueAsNullOrEmpty("Schema name", schemaMetadata.getName());
            checkValueAsNullOrEmpty("Schema type", schemaMetadata.getType());
            checkValidNames(schemaMetadata.getName());
            boolean throwErrorIfExists = isThrowErrorIfExists(httpHeaders);
            Long schemaId = schemaRegistry.addSchemaMetadata(schemaMetadata, throwErrorIfExists);
            response = WSUtils.respondEntity(schemaId, Response.Status.CREATED);
        } catch (IllegalArgumentException ex) {
            LOG.error("Expected parameter is invalid", schemaMetadata, ex);
            response = WSUtils.respond(Response.Status.BAD_REQUEST, CatalogResponse.ResponseMessage.BAD_REQUEST_PARAM_MISSING, ex.getMessage());
        } catch (UnsupportedSchemaTypeException ex) {
            LOG.error("Unsupported schema type encountered while adding schema metadata [{}]", schemaMetadata, ex);
            response = WSUtils.respond(Response.Status.BAD_REQUEST, CatalogResponse.ResponseMessage.UNSUPPORTED_SCHEMA_TYPE, ex.getMessage());
        } catch (Exception ex) {
            LOG.error("Error encountered while adding schema info [{}] ", schemaMetadata, ex);
            response = WSUtils.respond(Response.Status.INTERNAL_SERVER_ERROR, CatalogResponse.ResponseMessage.EXCEPTION, String.format("Storing the given SchemaMetadata [%s] is failed", schemaMetadata.toString()));
        }
        return response;
    });
}
Also used : Response(javax.ws.rs.core.Response) CatalogResponse(com.hortonworks.registries.common.catalog.CatalogResponse) UnsupportedSchemaTypeException(com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException) 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)

Aggregations

UnsupportedSchemaTypeException (com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException)5 IncompatibleSchemaException (com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException)4 Timed (com.codahale.metrics.annotation.Timed)3 CatalogResponse (com.hortonworks.registries.common.catalog.CatalogResponse)3 UnitOfWork (com.hortonworks.registries.common.transaction.UnitOfWork)3 InvalidSchemaException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException)3 SchemaBranchNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException)3 SchemaNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)3 ApiOperation (io.swagger.annotations.ApiOperation)3 IOException (java.io.IOException)3 POST (javax.ws.rs.POST)3 Path (javax.ws.rs.Path)3 Response (javax.ws.rs.core.Response)3 SchemaIdVersion (com.hortonworks.registries.schemaregistry.SchemaIdVersion)2 InvalidSchemaBranchDeletionException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchDeletionException)2 SchemaBranchAlreadyExistsException (com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException)2 SchemaLifecycleException (com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException)2 FileNotFoundException (java.io.FileNotFoundException)2 SchemaMetadata (com.hortonworks.registries.schemaregistry.SchemaMetadata)1 SchemaMetadataInfo (com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)1