use of com.hortonworks.registries.common.QueryParam in project registry by hortonworks.
the class SchemaVersionLifecycleManager method createSchemaVersionLifeCycleContextAndState.
private ImmutablePair<SchemaVersionLifecycleContext, SchemaVersionLifecycleState> createSchemaVersionLifeCycleContextAndState(Long schemaVersionId) throws SchemaNotFoundException {
// get the current state from storage for the given versionID
// we can use a query to get max value for the column for a given schema-version-id but StorageManager does not
// have API to take custom queries.
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.isEmpty()) {
throw new SchemaNotFoundException("No schema versions found with id " + schemaVersionId);
}
SchemaVersionStateStorable stateStorable = schemaVersionStates.iterator().next();
SchemaVersionLifecycleState schemaVersionLifecycleState = schemaVersionLifecycleStateMachine.getStates().get(stateStorable.getStateId());
SchemaVersionService schemaVersionService = createSchemaVersionService();
SchemaVersionLifecycleContext context = new SchemaVersionLifecycleContext(stateStorable.getSchemaVersionId(), stateStorable.getSequence(), schemaVersionService, schemaVersionLifecycleStateMachine, customSchemaStateExecutor);
return new ImmutablePair<>(context, schemaVersionLifecycleState);
}
use of com.hortonworks.registries.common.QueryParam in project registry by hortonworks.
the class SchemaVersionLifecycleManager method getSortedSchemaVersions.
private List<SchemaVersionInfo> getSortedSchemaVersions(Long schemaBranchId) throws SchemaNotFoundException, SchemaBranchNotFoundException {
List<QueryParam> schemaVersionMappingStorableQueryParams = Lists.newArrayList();
schemaVersionMappingStorableQueryParams.add(new QueryParam(SchemaBranchVersionMapping.SCHEMA_BRANCH_ID, schemaBranchId.toString()));
List<OrderByField> orderByFields = new ArrayList<>();
orderByFields.add(OrderByField.of(SchemaBranchVersionMapping.SCHEMA_VERSION_INFO_ID, false));
List<SchemaVersionInfo> schemaVersionInfos = new ArrayList<>();
Collection<SchemaBranchVersionMapping> storables = storageManager.find(SchemaBranchVersionMapping.NAMESPACE, schemaVersionMappingStorableQueryParams, orderByFields);
if (storables == null || storables.size() == 0) {
if (schemaBranchCache.get(SchemaBranchCache.Key.of(schemaBranchId)).getName().equals(SchemaBranch.MASTER_BRANCH))
return Collections.emptyList();
else
throw new InvalidSchemaBranchVersionMapping(String.format("No schema versions are attached to the schema branch id : '%s'", schemaBranchId));
}
for (SchemaBranchVersionMapping storable : storables) {
SchemaIdVersion schemaIdVersion = new SchemaIdVersion(storable.getSchemaVersionInfoId());
schemaVersionInfos.add(schemaVersionInfoCache.getSchema(SchemaVersionInfoCache.Key.of(schemaIdVersion)));
}
return schemaVersionInfos;
}
use of com.hortonworks.registries.common.QueryParam in project registry by hortonworks.
the class SchemaVersionLifecycleManager method findSchemaVersion.
private SchemaVersionInfo findSchemaVersion(String schemaBranchName, String type, String schemaText, String schemaMetadataName) throws InvalidSchemaException, SchemaNotFoundException, SchemaBranchNotFoundException {
Preconditions.checkNotNull(schemaBranchName, "Schema branch name can't be null");
String fingerPrint = getFingerprint(type, schemaText);
LOG.debug("Fingerprint of the given schema [{}] is [{}]", schemaText, fingerPrint);
List<QueryParam> queryParams = Lists.newArrayList(new QueryParam(SchemaVersionStorable.NAME, schemaMetadataName), new QueryParam(SchemaVersionStorable.FINGERPRINT, fingerPrint));
Collection<SchemaVersionStorable> versionedSchemas = storageManager.find(SchemaVersionStorable.NAME_SPACE, queryParams);
Set<Long> matchedSchemaVersionIds = null;
if (versionedSchemas != null && !versionedSchemas.isEmpty()) {
if (versionedSchemas.size() > 1) {
LOG.warn("Exists more than one schema with schemaMetadataName: [{}] and schemaText [{}]", schemaMetadataName, schemaText);
}
matchedSchemaVersionIds = versionedSchemas.stream().map(schemaVersionStorable -> schemaVersionStorable.getId()).collect(Collectors.toSet());
}
if (matchedSchemaVersionIds == null) {
return null;
} else {
SchemaBranch schemaBranch = schemaBranchCache.get(SchemaBranchCache.Key.of(new SchemaBranchKey(schemaBranchName, schemaMetadataName)));
for (SchemaVersionInfo schemaVersionInfo : getSortedSchemaVersions(schemaBranch)) {
if (matchedSchemaVersionIds.contains(schemaVersionInfo.getId()))
return schemaVersionInfo;
}
return null;
}
}
use of com.hortonworks.registries.common.QueryParam 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;
}
use of com.hortonworks.registries.common.QueryParam 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);
}
Aggregations