Search in sources :

Example 1 with SchemaVersionLifecycleState

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);
}
Also used : InbuiltSchemaVersionLifecycleState(com.hortonworks.registries.schemaregistry.state.InbuiltSchemaVersionLifecycleState) SchemaVersionLifecycleState(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleState) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) QueryParam(com.hortonworks.registries.common.QueryParam) SchemaVersionService(com.hortonworks.registries.schemaregistry.state.SchemaVersionService) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaVersionLifecycleContext(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleContext)

Example 2 with 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;
    }
}
Also used : InbuiltSchemaVersionLifecycleState(com.hortonworks.registries.schemaregistry.state.InbuiltSchemaVersionLifecycleState) SchemaVersionLifecycleState(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleState) SchemaVersionLifecycleStateTransitionListener(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateTransitionListener) SchemaVersionLifecycleStateAction(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateAction) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaLifecycleException(com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException) SchemaVersionLifecycleContext(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleContext) SchemaVersionLifecycleStateTransition(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateTransition)

Aggregations

SchemaNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)2 InbuiltSchemaVersionLifecycleState (com.hortonworks.registries.schemaregistry.state.InbuiltSchemaVersionLifecycleState)2 SchemaVersionLifecycleContext (com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleContext)2 SchemaVersionLifecycleState (com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleState)2 QueryParam (com.hortonworks.registries.common.QueryParam)1 SchemaLifecycleException (com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException)1 SchemaVersionLifecycleStateAction (com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateAction)1 SchemaVersionLifecycleStateTransition (com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateTransition)1 SchemaVersionLifecycleStateTransitionListener (com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateTransitionListener)1 SchemaVersionService (com.hortonworks.registries.schemaregistry.state.SchemaVersionService)1 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)1