Search in sources :

Example 26 with QueryParam

use of com.hortonworks.registries.common.QueryParam in project registry by hortonworks.

the class SchemaVersionLifecycleManager method deleteSchemaVersionBranchMapping.

private void deleteSchemaVersionBranchMapping(Long schemaVersionId) throws SchemaNotFoundException, SchemaLifecycleException {
    List<QueryParam> schemaVersionMappingStorableQueryParams = Lists.newArrayList();
    schemaVersionMappingStorableQueryParams.add(new QueryParam(SchemaBranchVersionMapping.SCHEMA_VERSION_INFO_ID, schemaVersionId.toString()));
    List<OrderByField> orderByFields = new ArrayList<>();
    orderByFields.add(OrderByField.of(SchemaBranchVersionMapping.SCHEMA_VERSION_INFO_ID, false));
    Collection<SchemaBranchVersionMapping> storables = storageManager.find(SchemaBranchVersionMapping.NAMESPACE, schemaVersionMappingStorableQueryParams, orderByFields);
    if (storables == null || storables.isEmpty()) {
        LOG.debug("No need to delete schema version mapping as the database did a cascade delete");
        return;
    }
    if (storables.size() > 1) {
        List<String> branchNamesTiedToSchema = storables.stream().map(storable -> schemaBranchCache.get(SchemaBranchCache.Key.of(storable.getSchemaBranchId())).getName()).collect(Collectors.toList());
        throw new SchemaLifecycleException(String.format("Schema version with id : '%s' is tied with more than one branch : '%s' ", schemaVersionId.toString(), Arrays.toString(branchNamesTiedToSchema.toArray())));
    }
    storageManager.remove(new StorableKey(SchemaBranchVersionMapping.NAMESPACE, storables.iterator().next().getPrimaryKey()));
}
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) QueryParam(com.hortonworks.registries.common.QueryParam) OrderByField(com.hortonworks.registries.storage.OrderByField) StorableKey(com.hortonworks.registries.storage.StorableKey) ArrayList(java.util.ArrayList) InvalidSchemaBranchVersionMapping(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchVersionMapping) SchemaLifecycleException(com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException)

Example 27 with QueryParam

use of com.hortonworks.registries.common.QueryParam in project registry by hortonworks.

the class SchemaVersionLifecycleManager method getAllVersions.

public Collection<SchemaVersionInfo> getAllVersions(final String schemaName) throws SchemaNotFoundException {
    List<QueryParam> queryParams = Collections.singletonList(new QueryParam(SchemaVersionStorable.NAME, schemaName));
    SchemaMetadataInfo schemaMetadataInfo = getSchemaMetadataInfo(schemaName);
    if (schemaMetadataInfo == null) {
        throw new SchemaNotFoundException("Schema not found with name " + schemaName);
    }
    Collection<SchemaVersionStorable> storables = storageManager.find(SchemaVersionStorable.NAME_SPACE, queryParams, Collections.singletonList(OrderByField.of(SchemaVersionStorable.VERSION, true)));
    List<SchemaVersionInfo> schemaVersionInfos;
    if (storables != null && !storables.isEmpty()) {
        schemaVersionInfos = storables.stream().map(SchemaVersionStorable::toSchemaVersionInfo).collect(Collectors.toList());
    } else {
        schemaVersionInfos = Collections.emptyList();
    }
    return schemaVersionInfos;
}
Also used : QueryParam(com.hortonworks.registries.common.QueryParam) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)

Example 28 with QueryParam

use of com.hortonworks.registries.common.QueryParam in project registry by hortonworks.

the class SchemaVersionLifecycleManager method getSchemaBranches.

public Set<SchemaBranch> getSchemaBranches(Long schemaVersionId) throws SchemaBranchNotFoundException {
    List<QueryParam> schemaVersionMappingStorableQueryParams = new ArrayList<>();
    Set<SchemaBranch> schemaBranches = new HashSet<>();
    schemaVersionMappingStorableQueryParams.add(new QueryParam(SchemaBranchVersionMapping.SCHEMA_VERSION_INFO_ID, schemaVersionId.toString()));
    for (Storable storable : storageManager.find(SchemaBranchVersionMapping.NAMESPACE, schemaVersionMappingStorableQueryParams)) {
        schemaBranches.add(schemaBranchCache.get(SchemaBranchCache.Key.of(((SchemaBranchVersionMapping) storable).getSchemaBranchId())));
    }
    return schemaBranches;
}
Also used : QueryParam(com.hortonworks.registries.common.QueryParam) ArrayList(java.util.ArrayList) Storable(com.hortonworks.registries.storage.Storable) HashSet(java.util.HashSet)

Example 29 with QueryParam

use of com.hortonworks.registries.common.QueryParam 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 30 with QueryParam

use of com.hortonworks.registries.common.QueryParam in project registry by hortonworks.

the class DefaultSchemaRegistry method getSchemaMetadataInfo.

@Override
public SchemaMetadataInfo getSchemaMetadataInfo(Long schemaMetadataId) {
    SchemaMetadataStorable givenSchemaMetadataStorable = new SchemaMetadataStorable();
    givenSchemaMetadataStorable.setId(schemaMetadataId);
    List<QueryParam> params = Collections.singletonList(new QueryParam(SchemaMetadataStorable.ID, schemaMetadataId.toString()));
    Collection<SchemaMetadataStorable> schemaMetadataStorables = storageManager.find(SchemaMetadataStorable.NAME_SPACE, params);
    SchemaMetadataInfo schemaMetadataInfo = null;
    if (schemaMetadataStorables != null && !schemaMetadataStorables.isEmpty()) {
        schemaMetadataInfo = schemaMetadataStorables.iterator().next().toSchemaMetadataInfo();
        if (schemaMetadataStorables.size() > 1) {
            LOG.warn("No unique entry with schemaMetatadataId: [{}]", schemaMetadataId);
        }
        LOG.info("SchemaMetadata entries with id [{}] is [{}]", schemaMetadataStorables);
    }
    return schemaMetadataInfo;
}
Also used : QueryParam(com.hortonworks.registries.common.QueryParam)

Aggregations

QueryParam (com.hortonworks.registries.common.QueryParam)72 ArrayList (java.util.ArrayList)42 WSUtils.buildEdgesFromQueryParam (com.hortonworks.streamline.common.util.WSUtils.buildEdgesFromQueryParam)22 WSUtils.buildEdgesToQueryParam (com.hortonworks.streamline.common.util.WSUtils.buildEdgesToQueryParam)22 WSUtils.currentVersionQueryParam (com.hortonworks.streamline.common.util.WSUtils.currentVersionQueryParam)22 WSUtils.versionIdQueryParam (com.hortonworks.streamline.common.util.WSUtils.versionIdQueryParam)22 IOException (java.io.IOException)8 SchemaNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)7 StorableKey (com.hortonworks.registries.storage.StorableKey)7 OrderByField (com.hortonworks.registries.storage.OrderByField)6 HashSet (java.util.HashSet)6 SchemaVersionLifecycleContext (com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleContext)5 TopologyComponentBundle (com.hortonworks.streamline.streams.catalog.topology.TopologyComponentBundle)5 Timed (com.codahale.metrics.annotation.Timed)4 Preconditions (com.google.common.base.Preconditions)4 SchemaBranchNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException)4 StorageException (com.hortonworks.registries.storage.exception.StorageException)4 TopologyTestRunCaseSource (com.hortonworks.streamline.streams.catalog.TopologyTestRunCaseSource)4 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4