Search in sources :

Example 36 with SchemaNotFoundException

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

the class SchemaVersionLifecycleManager method executeState.

public void executeState(Long schemaVersionId, Byte targetState, byte[] transitionDetails) throws SchemaLifecycleException, SchemaNotFoundException {
    ImmutablePair<SchemaVersionLifecycleContext, SchemaVersionLifecycleState> schemaLifeCycleContextAndState = createSchemaVersionLifeCycleContextAndState(schemaVersionId);
    SchemaVersionLifecycleContext schemaVersionLifecycleContext = schemaLifeCycleContextAndState.getLeft();
    SchemaVersionLifecycleState currentState = schemaLifeCycleContextAndState.getRight();
    schemaVersionLifecycleContext.setState(currentState);
    schemaVersionLifecycleContext.setDetails(transitionDetails);
    SchemaVersionLifecycleStateTransition transition = new SchemaVersionLifecycleStateTransition(currentState.getId(), targetState);
    SchemaVersionLifecycleStateAction action = schemaVersionLifecycleContext.getSchemaLifeCycleStatesMachine().getTransitions().get(transition);
    try {
        List<SchemaVersionLifecycleStateTransitionListener> listeners = schemaVersionLifecycleContext.getSchemaLifeCycleStatesMachine().getListeners().getOrDefault(transition, DEFAULT_LISTENERS);
        listeners.stream().forEach(listener -> listener.preStateTransition(schemaVersionLifecycleContext));
        action.execute(schemaVersionLifecycleContext);
        listeners.stream().forEach(listener -> listener.postStateTransition(schemaVersionLifecycleContext));
    } catch (SchemaLifecycleException e) {
        Throwable cause = e.getCause();
        if (cause != null && cause instanceof SchemaNotFoundException) {
            throw (SchemaNotFoundException) cause;
        }
        throw e;
    }
}
Also used : InbuiltSchemaVersionLifecycleState(com.hortonworks.registries.schemaregistry.state.InbuiltSchemaVersionLifecycleState) SchemaVersionLifecycleState(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleState) SchemaVersionLifecycleStateTransitionListener(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateTransitionListener) SchemaVersionLifecycleStateAction(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateAction) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaLifecycleException(com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException) SchemaVersionLifecycleContext(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleContext) SchemaVersionLifecycleStateTransition(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateTransition)

Example 37 with SchemaNotFoundException

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

the class SchemaVersionLifecycleManager method fetchSchemaVersionInfo.

private SchemaVersionInfo fetchSchemaVersionInfo(String schemaName, Integer version) throws SchemaNotFoundException {
    LOG.info("##### fetching schema version for name: [{}] version: [{}]", schemaName, version);
    SchemaVersionInfo schemaVersionInfo = null;
    if (SchemaVersionKey.LATEST_VERSION.equals(version)) {
        schemaVersionInfo = getLatestSchemaVersionInfo(schemaName);
    } else {
        List<QueryParam> queryParams = Lists.newArrayList(new QueryParam(SchemaVersionStorable.NAME, schemaName), new QueryParam(SchemaVersionStorable.VERSION, version.toString()));
        Collection<SchemaVersionStorable> versionedSchemas = storageManager.find(SchemaVersionStorable.NAME_SPACE, queryParams);
        if (versionedSchemas != null && !versionedSchemas.isEmpty()) {
            if (versionedSchemas.size() > 1) {
                LOG.warn("More than one schema exists with name: [{}] and version [{}]", schemaName, version);
            }
            schemaVersionInfo = versionedSchemas.iterator().next().toSchemaVersionInfo();
        } else {
            throw new SchemaNotFoundException("No Schema version exists with name " + schemaName + " and version " + version);
        }
    }
    LOG.info("##### fetched schema version info [{}]", schemaVersionInfo);
    return schemaVersionInfo;
}
Also used : QueryParam(com.hortonworks.registries.common.QueryParam) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)

Example 38 with SchemaNotFoundException

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

the class ConfluentSchemaRegistryCompatibleResource method getSchemaVersion.

@GET
@Path("/subjects/{subject}/versions/{versionId}")
@ApiOperation(value = "Get the schema information for given subject and versionId", response = Integer.class, responseContainer = "Collection", tags = OPERATION_GROUP_CONFLUENT_SR)
@Timed
@UnitOfWork
public Response getSchemaVersion(@ApiParam(value = "subject", required = true) @PathParam("subject") String subject, @ApiParam(value = "versionId", required = true) @PathParam("versionId") String versionId) {
    Response response;
    try {
        SchemaVersionInfo schemaVersionInfo = null;
        if ("latest".equals(versionId)) {
            schemaVersionInfo = schemaRegistry.getLatestSchemaVersionInfo(subject);
        } else {
            SchemaMetadataInfo schemaMetadataInfo = schemaRegistry.getSchemaMetadataInfo(subject);
            if (schemaMetadataInfo == null) {
                throw new SchemaNotFoundException();
            }
            SchemaVersionInfo fetchedSchemaVersionInfo = null;
            try {
                Integer version = Integer.valueOf(versionId);
                if (version > 0 && version <= Integer.MAX_VALUE) {
                    fetchedSchemaVersionInfo = schemaRegistry.getSchemaVersionInfo(new SchemaVersionKey(subject, version));
                } else {
                    LOG.error("versionId is not in valid range [{}, {}] ", 1, Integer.MAX_VALUE);
                }
            } catch (NumberFormatException e) {
                LOG.error("Invalid version id string ", versionId, e);
            } catch (SchemaNotFoundException e) {
                LOG.error("Schema version not found with version id [{}]", versionId, e);
            }
            if (fetchedSchemaVersionInfo != null) {
                if (subject.equals(fetchedSchemaVersionInfo.getName())) {
                    schemaVersionInfo = fetchedSchemaVersionInfo;
                } else {
                    LOG.error("Received schema version for id [{}] belongs to subject [{}] which is different from requested subject [{}]", versionId, fetchedSchemaVersionInfo.getName(), subject);
                }
            }
        }
        if (schemaVersionInfo == null) {
            response = versionNotFoundError();
        } else {
            Schema schema = new Schema(schemaVersionInfo.getName(), schemaVersionInfo.getVersion(), schemaVersionInfo.getId(), schemaVersionInfo.getSchemaText());
            response = WSUtils.respondEntity(schema, 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) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey) 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) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 39 with SchemaNotFoundException

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

the class DefaultSchemaRegistry method deleteSchemaBranch.

@Override
public void deleteSchemaBranch(Long schemaBranchId) throws SchemaBranchNotFoundException, InvalidSchemaBranchDeletionException {
    Preconditions.checkNotNull(schemaBranchId, "Schema branch name can't be null");
    SchemaBranch schemaBranch = schemaBranchCache.get(SchemaBranchCache.Key.of(schemaBranchId));
    if (schemaBranch.getName().equals(SchemaBranch.MASTER_BRANCH))
        throw new InvalidSchemaBranchDeletionException(String.format("Can't delete '%s' branch", SchemaBranch.MASTER_BRANCH));
    SchemaBranchCache.Key keyOfSchemaBranchToDelete = SchemaBranchCache.Key.of(schemaBranchId);
    schemaBranchCache.invalidateSchemaBranch(keyOfSchemaBranchToDelete);
    List<QueryParam> schemaVersionMappingStorableQueryParams = new ArrayList<>();
    schemaVersionMappingStorableQueryParams.add(new QueryParam(SchemaBranchVersionMapping.SCHEMA_BRANCH_ID, schemaBranch.getId().toString()));
    List<OrderByField> schemaVersionMappingOrderbyFields = new ArrayList<>();
    schemaVersionMappingOrderbyFields.add(OrderByField.of(SchemaBranchVersionMapping.SCHEMA_VERSION_INFO_ID, false));
    Collection<SchemaBranchVersionMapping> schemaBranchVersionMappings = storageManager.find(SchemaBranchVersionMapping.NAMESPACE, schemaVersionMappingStorableQueryParams, schemaVersionMappingOrderbyFields);
    if (schemaBranchVersionMappings == null)
        throw new RuntimeException("Schema branch is invalid state, its not associated with any schema versions");
    // Ignore the first version as it used in the 'MASTER' branch
    Iterator<SchemaBranchVersionMapping> schemaBranchVersionMappingIterator = schemaBranchVersionMappings.iterator();
    SchemaBranchVersionMapping rootVersionMapping = schemaBranchVersionMappingIterator.next();
    storageManager.remove(rootVersionMapping.getStorableKey());
    // Validate if the schema versions in the branch to be deleted are the root versions for other branches
    Map<Integer, List<String>> schemaVersionTiedToOtherBranch = new HashMap<>();
    List<Long> schemaVersionsToBeDeleted = new ArrayList<>();
    while (schemaBranchVersionMappingIterator.hasNext()) {
        SchemaBranchVersionMapping schemaBranchVersionMapping = schemaBranchVersionMappingIterator.next();
        Long schemaVersionId = schemaBranchVersionMapping.getSchemaVersionInfoId();
        try {
            List<QueryParam> schemaVersionCountParam = new ArrayList<>();
            schemaVersionCountParam.add(new QueryParam(SchemaBranchVersionMapping.SCHEMA_VERSION_INFO_ID, schemaBranchVersionMapping.getSchemaVersionInfoId().toString()));
            Collection<SchemaBranchVersionMapping> mappingsForSchemaTiedToMutlipleBranch = storageManager.find(SchemaBranchVersionMapping.NAMESPACE, schemaVersionCountParam);
            if (mappingsForSchemaTiedToMutlipleBranch.size() > 1) {
                SchemaVersionInfo schemaVersionInfo = schemaVersionLifecycleManager.getSchemaVersionInfo(new SchemaIdVersion(schemaVersionId));
                List<String> forkedBranchName = mappingsForSchemaTiedToMutlipleBranch.stream().filter(mapping -> !mapping.getSchemaBranchId().equals(schemaBranchId)).map(mappping -> schemaBranchCache.get(SchemaBranchCache.Key.of(mappping.getSchemaBranchId())).getName()).collect(Collectors.toList());
                schemaVersionTiedToOtherBranch.put(schemaVersionInfo.getVersion(), forkedBranchName);
            } else {
                schemaVersionsToBeDeleted.add(schemaVersionId);
            }
        } catch (SchemaNotFoundException e) {
            throw new RuntimeException(String.format("Failed to delete schema version : '%s' of schema branch : '%s'", schemaVersionId.toString(), schemaBranchId), e);
        }
    }
    if (!schemaVersionTiedToOtherBranch.isEmpty()) {
        StringBuilder message = new StringBuilder();
        message.append("Failed to delete branch");
        schemaVersionTiedToOtherBranch.entrySet().stream().forEach(versionWithBranch -> {
            message.append(", schema version : '").append(versionWithBranch.getKey()).append("'");
            message.append(" is tied to branch : '").append(Arrays.toString(versionWithBranch.getValue().toArray())).append("'");
        });
        throw new InvalidSchemaBranchDeletionException(message.toString());
    } else {
        for (Long schemaVersionId : schemaVersionsToBeDeleted) {
            try {
                schemaVersionLifecycleManager.deleteSchemaVersion(schemaVersionId);
            } catch (SchemaLifecycleException e) {
                throw new InvalidSchemaBranchDeletionException("Failed to delete schema branch, all schema versions in the branch should be in one of 'INITIATED', 'ChangesRequired' or 'Archived' state ", e);
            } catch (SchemaNotFoundException e) {
                throw new RuntimeException(String.format("Failed to delete schema version : '%s' of schema branch : '%s'", schemaVersionId.toString(), schemaBranchId), e);
            }
        }
    }
    storageManager.remove(new SchemaBranchStorable(schemaBranchId).getStorableKey());
    invalidateSchemaBranchInAllHAServers(keyOfSchemaBranchToDelete);
}
Also used : ObjectMapperUtils(com.hortonworks.registries.schemaregistry.utils.ObjectMapperUtils) SchemaBranchCache(com.hortonworks.registries.schemaregistry.cache.SchemaBranchCache) SchemaVersionLifecycleStateMachineInfo(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateMachineInfo) 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) FileStorage(com.hortonworks.registries.common.util.FileStorage) LoggerFactory(org.slf4j.LoggerFactory) Storable(com.hortonworks.registries.storage.Storable) OrderByField(com.hortonworks.registries.storage.OrderByField) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) SchemaRegistryCacheType(com.hortonworks.registries.schemaregistry.cache.SchemaRegistryCacheType) Map(java.util.Map) OrderBy(com.hortonworks.registries.storage.search.OrderBy) WhereClause(com.hortonworks.registries.storage.search.WhereClause) IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) Collection(java.util.Collection) InvalidSchemaBranchDeletionException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchDeletionException) SchemaLifecycleException(com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException) IOException(java.io.IOException) UUID(java.util.UUID) SchemaVersionLifecycleContext(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleContext) Collectors(java.util.stream.Collectors) MergeInfo(com.hortonworks.registries.schemaregistry.state.details.MergeInfo) SerDesException(com.hortonworks.registries.schemaregistry.serde.SerDesException) SearchQuery(com.hortonworks.registries.storage.search.SearchQuery) List(java.util.List) SchemaVersionInfoCache(com.hortonworks.registries.schemaregistry.cache.SchemaVersionInfoCache) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) Preconditions(com.google.common.base.Preconditions) SchemaBranchAlreadyExistsException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException) StorableKey(com.hortonworks.registries.storage.StorableKey) StorageManager(com.hortonworks.registries.storage.StorageManager) Collections(java.util.Collections) InputStream(java.io.InputStream) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SchemaBranchCache(com.hortonworks.registries.schemaregistry.cache.SchemaBranchCache) OrderByField(com.hortonworks.registries.storage.OrderByField) ArrayList(java.util.ArrayList) List(java.util.List) InvalidSchemaBranchDeletionException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchDeletionException) QueryParam(com.hortonworks.registries.common.QueryParam) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaLifecycleException(com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException)

Example 40 with SchemaNotFoundException

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

the class SchemaVersionLifecycleStates method transitionToEnableState.

public static void transitionToEnableState(SchemaVersionLifecycleContext context) throws SchemaNotFoundException, IncompatibleSchemaException, SchemaLifecycleException, SchemaBranchNotFoundException {
    Long schemaVersionId = context.getSchemaVersionId();
    SchemaVersionService schemaVersionService = context.getSchemaVersionService();
    SchemaMetadataInfo schemaMetadataInfo = schemaVersionService.getSchemaMetadata(schemaVersionId);
    SchemaMetadata schemaMetadata = schemaMetadataInfo.getSchemaMetadata();
    String schemaName = schemaMetadata.getName();
    SchemaValidationLevel validationLevel = schemaMetadata.getValidationLevel();
    SchemaVersionInfo schemaVersionInfo = schemaVersionService.getSchemaVersionInfo(schemaVersionId);
    int schemaVersion = schemaVersionInfo.getVersion();
    String schemaText = schemaVersionInfo.getSchemaText();
    List<SchemaVersionInfo> allEnabledSchemaVersions = schemaVersionService.getAllSchemaVersions(SchemaBranch.MASTER_BRANCH, schemaName).stream().filter(x -> SchemaVersionLifecycleStates.ENABLED.getId().equals(x.getStateId())).collect(Collectors.toList());
    if (!allEnabledSchemaVersions.isEmpty()) {
        if (validationLevel.equals(SchemaValidationLevel.ALL)) {
            for (SchemaVersionInfo curSchemaVersionInfo : allEnabledSchemaVersions) {
                int curVersion = curSchemaVersionInfo.getVersion();
                if (curVersion < schemaVersion) {
                    checkCompatibility(schemaVersionService, schemaMetadata, schemaText, curSchemaVersionInfo.getSchemaText());
                } else {
                    checkCompatibility(schemaVersionService, schemaMetadata, curSchemaVersionInfo.getSchemaText(), schemaText);
                }
            }
        } else if (validationLevel.equals(SchemaValidationLevel.LATEST)) {
            List<SchemaVersionInfo> sortedSchemaVersionInfos = new ArrayList<>(allEnabledSchemaVersions);
            sortedSchemaVersionInfos.sort(Comparator.comparingInt(SchemaVersionInfo::getVersion));
            int i = 0;
            int size = sortedSchemaVersionInfos.size();
            for (; i < size && sortedSchemaVersionInfos.get(i).getVersion() < schemaVersion; i++) {
                String fromSchemaText = sortedSchemaVersionInfos.get(i).getSchemaText();
                checkCompatibility(schemaVersionService, schemaMetadata, schemaText, fromSchemaText);
            }
            for (; i < size && sortedSchemaVersionInfos.get(i).getVersion() > schemaVersion; i++) {
                String toSchemaText = sortedSchemaVersionInfos.get(i).getSchemaText();
                checkCompatibility(schemaVersionService, schemaMetadata, toSchemaText, schemaText);
            }
        }
    }
    context.setState(ENABLED);
    context.updateSchemaVersionState();
}
Also used : IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) SchemaBranchNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException) Collection(java.util.Collection) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) CompatibilityResult(com.hortonworks.registries.schemaregistry.CompatibilityResult) Collectors(java.util.stream.Collectors) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo) ArrayList(java.util.ArrayList) List(java.util.List) Lists(com.google.common.collect.Lists) SchemaBranch(com.hortonworks.registries.schemaregistry.SchemaBranch) Pair(org.apache.commons.lang3.tuple.Pair) SchemaValidationLevel(com.hortonworks.registries.schemaregistry.SchemaValidationLevel) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) Comparator(java.util.Comparator) Collections(java.util.Collections) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaValidationLevel(com.hortonworks.registries.schemaregistry.SchemaValidationLevel) ArrayList(java.util.ArrayList) List(java.util.List) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)

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