Search in sources :

Example 1 with InvalidSchemaBranchDeletionException

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

the class SchemaRegistryClient method deleteSchemaBranch.

@Override
public void deleteSchemaBranch(Long schemaBranchId) throws SchemaBranchNotFoundException, InvalidSchemaBranchDeletionException {
    WebTarget target = currentSchemaRegistryTargets().schemasTarget.path("branch/" + schemaBranchId);
    Response response = Subject.doAs(subject, new PrivilegedAction<Response>() {

        @Override
        public Response run() {
            return target.request().delete();
        }
    });
    int status = response.getStatus();
    if (status == Response.Status.NOT_FOUND.getStatusCode()) {
        throw new SchemaBranchNotFoundException(response.readEntity(String.class));
    } else if (status == Response.Status.BAD_REQUEST.getStatusCode()) {
        throw new InvalidSchemaBranchDeletionException(response.readEntity(String.class));
    } else if (status != Response.Status.OK.getStatusCode()) {
        throw new RuntimeException(response.readEntity(String.class));
    }
}
Also used : Response(javax.ws.rs.core.Response) CatalogResponse(com.hortonworks.registries.common.catalog.CatalogResponse) InvalidSchemaBranchDeletionException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchDeletionException) SchemaBranchNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException) WebTarget(javax.ws.rs.client.WebTarget)

Example 2 with InvalidSchemaBranchDeletionException

use of com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchDeletionException 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)

Aggregations

InvalidSchemaBranchDeletionException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchDeletionException)2 SchemaBranchNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException)2 Preconditions (com.google.common.base.Preconditions)1 QueryParam (com.hortonworks.registries.common.QueryParam)1 CatalogResponse (com.hortonworks.registries.common.catalog.CatalogResponse)1 FileStorage (com.hortonworks.registries.common.util.FileStorage)1 SchemaBranchCache (com.hortonworks.registries.schemaregistry.cache.SchemaBranchCache)1 SchemaRegistryCacheType (com.hortonworks.registries.schemaregistry.cache.SchemaRegistryCacheType)1 SchemaVersionInfoCache (com.hortonworks.registries.schemaregistry.cache.SchemaVersionInfoCache)1 IncompatibleSchemaException (com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException)1 InvalidSchemaException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException)1 SchemaBranchAlreadyExistsException (com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException)1 SchemaNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)1 UnsupportedSchemaTypeException (com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException)1 SerDesException (com.hortonworks.registries.schemaregistry.serde.SerDesException)1 SchemaLifecycleException (com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException)1 SchemaVersionLifecycleContext (com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleContext)1 SchemaVersionLifecycleStateMachineInfo (com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateMachineInfo)1 SchemaVersionLifecycleStates (com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStates)1 InitializedStateDetails (com.hortonworks.registries.schemaregistry.state.details.InitializedStateDetails)1