Search in sources :

Example 11 with SchemaNotFoundException

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

the class SchemaVersionLifecycleManager method fetchSchemaVersionInfo.

private SchemaVersionInfo fetchSchemaVersionInfo(Long id) throws SchemaNotFoundException {
    StorableKey storableKey = new StorableKey(SchemaVersionStorable.NAME_SPACE, SchemaVersionStorable.getPrimaryKey(id));
    SchemaVersionStorable versionedSchema = storageManager.get(storableKey);
    if (versionedSchema == null) {
        throw new SchemaNotFoundException("No Schema version exists with id " + id);
    }
    return versionedSchema.toSchemaVersionInfo();
}
Also used : StorableKey(com.hortonworks.registries.storage.StorableKey) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)

Example 12 with SchemaNotFoundException

use of com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException 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);
    }
}
Also used : ObjectMapperUtils(com.hortonworks.registries.schemaregistry.utils.ObjectMapperUtils) SchemaBranchCache(com.hortonworks.registries.schemaregistry.cache.SchemaBranchCache) Arrays(java.util.Arrays) SchemaBranchNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException) UnsupportedSchemaTypeException(com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException) InitializedStateDetails(com.hortonworks.registries.schemaregistry.state.details.InitializedStateDetails) SchemaVersionLifecycleStates(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStates) QueryParam(com.hortonworks.registries.common.QueryParam) InbuiltSchemaVersionLifecycleState(com.hortonworks.registries.schemaregistry.state.InbuiltSchemaVersionLifecycleState) LoggerFactory(org.slf4j.LoggerFactory) Storable(com.hortonworks.registries.storage.Storable) OrderByField(com.hortonworks.registries.storage.OrderByField) Hex(org.apache.commons.codec.binary.Hex) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) SchemaVersionLifecycleStateTransitionListener(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateTransitionListener) Map(java.util.Map) SchemaVersionLifecycleStateTransition(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateTransition) InvalidSchemaBranchVersionMapping(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchVersionMapping) IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) Logger(org.slf4j.Logger) SchemaVersionLifecycleStateAction(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateAction) SchemaVersionService(com.hortonworks.registries.schemaregistry.state.SchemaVersionService) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) Collection(java.util.Collection) SchemaVersionMergeException(com.hortonworks.registries.schemaregistry.errors.SchemaVersionMergeException) SchemaLifecycleException(com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException) Set(java.util.Set) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) CustomSchemaStateExecutor(com.hortonworks.registries.schemaregistry.state.CustomSchemaStateExecutor) SchemaVersionLifecycleContext(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleContext) Collectors(java.util.stream.Collectors) StorageException(com.hortonworks.registries.storage.exception.StorageException) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) List(java.util.List) SchemaVersionInfoCache(com.hortonworks.registries.schemaregistry.cache.SchemaVersionInfoCache) SchemaVersionLifecycleStateMachine(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateMachine) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) Preconditions(com.google.common.base.Preconditions) SchemaVersionLifecycleState(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleState) StorableKey(com.hortonworks.registries.storage.StorableKey) StorageManager(com.hortonworks.registries.storage.StorageManager) Collections(java.util.Collections) SchemaBranchNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException) InitializedStateDetails(com.hortonworks.registries.schemaregistry.state.details.InitializedStateDetails) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) QueryParam(com.hortonworks.registries.common.QueryParam) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaVersionMergeException(com.hortonworks.registries.schemaregistry.errors.SchemaVersionMergeException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 13 with SchemaNotFoundException

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

the class ConfluentSchemaRegistryCompatibleResource method getSchemaById.

@GET
@Path("/schemas/ids/{id}")
@ApiOperation(value = "Get schema version by id", response = Schema.class, tags = OPERATION_GROUP_CONFLUENT_SR)
@Timed
@UnitOfWork
public Response getSchemaById(@ApiParam(value = "schema version id", required = true) @PathParam("id") Long id) {
    Response response;
    try {
        SchemaVersionInfo schemaVersionInfo = schemaRegistry.getSchemaVersionInfo(new SchemaIdVersion(id));
        SchemaString schema = new SchemaString();
        schema.setSchema(schemaVersionInfo.getSchemaText());
        response = WSUtils.respondEntity(schema, Response.Status.OK);
    } catch (SchemaNotFoundException ex) {
        LOG.error("No schema version found with id [{}]", id, ex);
        response = schemaNotFoundError();
    } catch (Exception ex) {
        LOG.error("Encountered error while retrieving Schema with id: [{}]", id, ex);
        response = serverError();
    }
    return response;
}
Also used : CatalogResponse(com.hortonworks.registries.common.catalog.CatalogResponse) Response(javax.ws.rs.core.Response) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) 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) Path(javax.ws.rs.Path) UnitOfWork(com.hortonworks.registries.common.transaction.UnitOfWork) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 14 with SchemaNotFoundException

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

the class ConfluentSchemaRegistryCompatibleResource method getAllVersions.

@GET
@Path("/subjects/{subject}/versions")
@ApiOperation(value = "Get all schema versions of given subject", response = Integer.class, responseContainer = "Collection", tags = OPERATION_GROUP_CONFLUENT_SR)
@Timed
@UnitOfWork
public Response getAllVersions(@ApiParam(value = "subject", required = true) @PathParam("subject") String subject) {
    Response response;
    try {
        List<Integer> registeredSubjects = schemaRegistry.getAllVersions(subject).stream().map(SchemaVersionInfo::getVersion).collect(Collectors.toList());
        response = WSUtils.respondEntity(registeredSubjects, Response.Status.OK);
    } catch (SchemaNotFoundException ex) {
        LOG.error("No schema found with subject [{}]", subject, ex);
        response = subjectNotFoundError();
    } catch (Exception ex) {
        LOG.error("Encountered error while retrieving all subjects", ex);
        response = serverError();
    }
    return response;
}
Also used : CatalogResponse(com.hortonworks.registries.common.catalog.CatalogResponse) Response(javax.ws.rs.core.Response) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) 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) Path(javax.ws.rs.Path) UnitOfWork(com.hortonworks.registries.common.transaction.UnitOfWork) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 15 with SchemaNotFoundException

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

the class SchemaRegistryResource method getSchemaVersion.

@GET
@Path("/schemas/{name}/versions/{version}")
@ApiOperation(value = "Get a version of the schema identified by the schema name", response = SchemaVersionInfo.class, tags = OPERATION_GROUP_SCHEMA)
@Timed
@UnitOfWork
public Response getSchemaVersion(@ApiParam(value = "Schema name", required = true) @PathParam("name") String schemaMetadata, @ApiParam(value = "version of the schema", required = true) @PathParam("version") Integer versionNumber) {
    SchemaVersionKey schemaVersionKey = new SchemaVersionKey(schemaMetadata, versionNumber);
    Response response;
    try {
        SchemaVersionInfo schemaVersionInfo = schemaRegistry.getSchemaVersionInfo(schemaVersionKey);
        response = WSUtils.respondEntity(schemaVersionInfo, Response.Status.OK);
    } catch (SchemaNotFoundException e) {
        LOG.info("No schemas found with schemaVersionKey: [{}]", schemaVersionKey);
        response = WSUtils.respond(Response.Status.NOT_FOUND, CatalogResponse.ResponseMessage.ENTITY_NOT_FOUND, schemaVersionKey.toString());
    } catch (Exception ex) {
        LOG.error("Encountered error while getting all schema versions for schemakey [{}]", schemaMetadata, 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) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey) 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) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

SchemaNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)40 IncompatibleSchemaException (com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException)24 InvalidSchemaException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException)21 IOException (java.io.IOException)20 CatalogResponse (com.hortonworks.registries.common.catalog.CatalogResponse)18 SchemaLifecycleException (com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException)18 SchemaBranchNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException)17 Response (javax.ws.rs.core.Response)17 UnsupportedSchemaTypeException (com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException)16 SchemaBranchAlreadyExistsException (com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException)15 Timed (com.codahale.metrics.annotation.Timed)14 InvalidSchemaBranchDeletionException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchDeletionException)14 Path (javax.ws.rs.Path)14 UnitOfWork (com.hortonworks.registries.common.transaction.UnitOfWork)13 SchemaVersionInfo (com.hortonworks.registries.schemaregistry.SchemaVersionInfo)13 ApiOperation (io.swagger.annotations.ApiOperation)13 FileNotFoundException (java.io.FileNotFoundException)10 QueryParam (com.hortonworks.registries.common.QueryParam)7 SchemaVersionLifecycleContext (com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleContext)7 Collection (java.util.Collection)7