use of com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException in project registry by hortonworks.
the class SchemaVersionLifecycleManager method executeState.
public void executeState(Long schemaVersionId, Byte targetState, byte[] transitionDetails) throws SchemaLifecycleException, SchemaNotFoundException {
ImmutablePair<SchemaVersionLifecycleContext, SchemaVersionLifecycleState> schemaLifeCycleContextAndState = createSchemaVersionLifeCycleContextAndState(schemaVersionId);
SchemaVersionLifecycleContext schemaVersionLifecycleContext = schemaLifeCycleContextAndState.getLeft();
SchemaVersionLifecycleState currentState = schemaLifeCycleContextAndState.getRight();
schemaVersionLifecycleContext.setState(currentState);
schemaVersionLifecycleContext.setDetails(transitionDetails);
SchemaVersionLifecycleStateTransition transition = new SchemaVersionLifecycleStateTransition(currentState.getId(), targetState);
SchemaVersionLifecycleStateAction action = schemaVersionLifecycleContext.getSchemaLifeCycleStatesMachine().getTransitions().get(transition);
try {
List<SchemaVersionLifecycleStateTransitionListener> listeners = schemaVersionLifecycleContext.getSchemaLifeCycleStatesMachine().getListeners().getOrDefault(transition, DEFAULT_LISTENERS);
listeners.stream().forEach(listener -> listener.preStateTransition(schemaVersionLifecycleContext));
action.execute(schemaVersionLifecycleContext);
listeners.stream().forEach(listener -> listener.postStateTransition(schemaVersionLifecycleContext));
} catch (SchemaLifecycleException e) {
Throwable cause = e.getCause();
if (cause != null && cause instanceof SchemaNotFoundException) {
throw (SchemaNotFoundException) cause;
}
throw e;
}
}
use of com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException 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