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;
}
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;
}
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;
}
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);
}
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();
}
Aggregations