use of com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleState 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.schemaregistry.state.SchemaVersionLifecycleState 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;
}
}
Aggregations