Search in sources :

Example 1 with StorageException

use of com.hortonworks.registries.storage.exception.StorageException in project streamline by hortonworks.

the class StreamCatalogService method addTopologyComponentBundle.

public TopologyComponentBundle addTopologyComponentBundle(TopologyComponentBundle topologyComponentBundle, java.io.File bundleJar) throws ComponentConfigException, IOException {
    topologyComponentBundle.getTopologyComponentUISpecification().validate();
    loadTransformationClassForBundle(topologyComponentBundle, bundleJar);
    if (!topologyComponentBundle.getBuiltin()) {
        topologyComponentBundle.setBundleJar(getTopologyComponentBundleJarName(topologyComponentBundle));
        try (InputStream is = new FileInputStream(bundleJar)) {
            uploadFileToStorage(is, topologyComponentBundle.getBundleJar());
        }
    }
    try {
        if (topologyComponentBundle.getId() == null) {
            topologyComponentBundle.setId(this.dao.nextId(TopologyComponentBundle.NAME_SPACE));
        }
        if (topologyComponentBundle.getTimestamp() == null) {
            topologyComponentBundle.setTimestamp(System.currentTimeMillis());
        }
        this.dao.add(topologyComponentBundle);
    } catch (StorageException e) {
        if (!topologyComponentBundle.getBuiltin()) {
            LOG.debug("StorageException while adding the bundle. Deleting the bundle jar.");
            deleteFileFromStorage(topologyComponentBundle.getBundleJar());
        }
        throw e;
    }
    return topologyComponentBundle;
}
Also used : DigestInputStream(java.security.DigestInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StorageException(com.hortonworks.registries.storage.exception.StorageException) FileInputStream(java.io.FileInputStream)

Example 2 with StorageException

use of com.hortonworks.registries.storage.exception.StorageException in project registry by hortonworks.

the class JdbcStorageManager method find.

@Override
public <T extends Storable> Collection<T> find(String namespace, List<QueryParam> queryParams, List<OrderByField> orderByFields) throws StorageException {
    log.debug("Searching for entries in table [{}] that match queryParams [{}] and order by [{}]", namespace, queryParams, orderByFields);
    if (queryParams == null || queryParams.isEmpty()) {
        return list(namespace, orderByFields);
    }
    Collection<T> entries = Collections.emptyList();
    try {
        StorableKey storableKey = buildStorableKey(namespace, queryParams);
        if (storableKey != null) {
            entries = queryExecutor.select(storableKey, orderByFields);
        }
    } catch (Exception e) {
        throw new StorageException(e);
    }
    log.debug("Querying table = [{}]\n\t filter = [{}]\n\t returned [{}]", namespace, queryParams, entries);
    return entries;
}
Also used : StorableKey(com.hortonworks.registries.storage.StorableKey) StorageException(com.hortonworks.registries.storage.exception.StorageException) AlreadyExistsException(com.hortonworks.registries.storage.exception.AlreadyExistsException) StorageException(com.hortonworks.registries.storage.exception.StorageException) IllegalQueryParameterException(com.hortonworks.registries.storage.exception.IllegalQueryParameterException)

Example 3 with StorageException

use of com.hortonworks.registries.storage.exception.StorageException in project registry by hortonworks.

the class InMemoryStorageManager method find.

@Override
public <T extends Storable> Collection<T> find(final String namespace, final List<QueryParam> queryParams, final List<OrderByField> orderByFields) throws StorageException {
    List<T> storables = new ArrayList<>();
    if (queryParams == null) {
        Collection<T> collection = list(namespace);
        storables = Lists.newArrayList(collection);
    } else {
        Class<?> clazz = nameSpaceClassMap.get(namespace);
        if (clazz != null) {
            Map<PrimaryKey, Storable> storableMap = storageMap.get(namespace);
            if (storableMap != null) {
                for (Storable val : storableMap.values()) {
                    if (matches(val, queryParams, clazz)) {
                        storables.add((T) val);
                    }
                }
            }
        }
    }
    if (orderByFields != null && !orderByFields.isEmpty()) {
        storables.sort((storable1, storable2) -> {
            try {
                for (OrderByField orderByField : orderByFields) {
                    Comparable value1 = ReflectionHelper.invokeGetter(orderByField.getFieldName(), storable1);
                    Comparable value2 = ReflectionHelper.invokeGetter(orderByField.getFieldName(), storable2);
                    int compareTo;
                    // same values continue
                    if (value1 == value2) {
                        continue;
                    } else if (value1 == null) {
                        // value2 is non null
                        compareTo = -1;
                    } else if (value2 == null) {
                        // value1 is non null
                        compareTo = 1;
                    } else {
                        // both value and value2 non null
                        compareTo = value1.compareTo(value2);
                    }
                    if (compareTo == 0) {
                        continue;
                    }
                    return orderByField.isDescending() ? -compareTo : compareTo;
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            // all group by fields are matched means equal
            return 0;
        });
    }
    return storables;
}
Also used : ArrayList(java.util.ArrayList) PrimaryKey(com.hortonworks.registries.storage.PrimaryKey) Storable(com.hortonworks.registries.storage.Storable) AlreadyExistsException(com.hortonworks.registries.storage.exception.AlreadyExistsException) StorageException(com.hortonworks.registries.storage.exception.StorageException) InvocationTargetException(java.lang.reflect.InvocationTargetException) OrderByField(com.hortonworks.registries.storage.OrderByField)

Example 4 with StorageException

use of com.hortonworks.registries.storage.exception.StorageException in project registry by hortonworks.

the class AbstractStorable method getSchema.

/**
 * Default implementation that will generate schema by reading all the field names in the class and use its
 * define type to convert to the Schema type.
 *
 * @return the schema
 */
@JsonIgnore
public Schema getSchema() {
    Map<String, Class> fieldNamesToTypes = ReflectionHelper.getFieldNamesToTypes(this.getClass());
    List<Schema.Field> fields = new ArrayList<>();
    for (Map.Entry<String, Class> entry : fieldNamesToTypes.entrySet()) {
        try {
            getField(entry.getKey(), entry.getValue()).ifPresent(field -> {
                fields.add(field);
                LOG.trace("getSchema: Adding {}", field);
            });
        } catch (NoSuchFieldException | NoSuchMethodException | InvocationTargetException | IllegalAccessException | ParserException e) {
            throw new StorageException(e);
        }
    }
    return Schema.of(fields);
}
Also used : ParserException(com.hortonworks.registries.common.exception.ParserException) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) HashMap(java.util.HashMap) Map(java.util.Map) StorageException(com.hortonworks.registries.storage.exception.StorageException) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore)

Example 5 with StorageException

use of com.hortonworks.registries.storage.exception.StorageException 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)

Aggregations

StorageException (com.hortonworks.registries.storage.exception.StorageException)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 PrimaryKey (com.hortonworks.registries.storage.PrimaryKey)2 AlreadyExistsException (com.hortonworks.registries.storage.exception.AlreadyExistsException)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 DigestInputStream (java.security.DigestInputStream)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)1 ParserException (com.hortonworks.registries.common.exception.ParserException)1 IncompatibleSchemaException (com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException)1 InvalidSchemaBranchVersionMapping (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchVersionMapping)1 SchemaBranchNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException)1 UnsupportedSchemaTypeException (com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException)1 OrderByField (com.hortonworks.registries.storage.OrderByField)1 Storable (com.hortonworks.registries.storage.Storable)1 StorableKey (com.hortonworks.registries.storage.StorableKey)1 IllegalQueryParameterException (com.hortonworks.registries.storage.exception.IllegalQueryParameterException)1 TopologyComponentBundle (com.hortonworks.streamline.streams.catalog.topology.TopologyComponentBundle)1