public SchemaVersionLifecycleStatePojo(SchemaVersionLifecycleState schemaVersionLifecycleState) { this.id = schemaVersionLifecycleState.getId(); this.name = schemaVersionLifecycleState.getName(); this.description = schemaVersionLifecycleState.getDescription(); }
private void checkForInbuiltStateIds(SchemaVersionLifecycleState state) { if (!(state instanceof InbuiltSchemaVersionLifecycleState)) { if (state.getId() <= INBUILT_STATE_ID_MAX) { throw new IllegalArgumentException("Given custom state id should be more than 32"); } } }
private void checkForInbuiltStateIds(SchemaVersionLifecycleState state) { if (!(state instanceof InbuiltSchemaVersionLifecycleState)) { if (state.getId() <= INBUILT_STATE_ID_MAX) { throw new IllegalArgumentException("Given custom state id should be more than 32"); } } }
public SchemaVersionLifecycleStatePojo(SchemaVersionLifecycleState schemaVersionLifecycleState) { this.id = schemaVersionLifecycleState.getId(); this.name = schemaVersionLifecycleState.getName(); this.description = schemaVersionLifecycleState.getDescription(); }
/** * Registers the given state with REGISTRY. * * @param state state to be registered. * * @throws IllegalArgumentException if the given state is already registered. */ public void register(SchemaVersionLifecycleState state) { checkForInbuiltStateIds(state); SchemaVersionLifecycleState prevState = states.putIfAbsent(state.getId(), state); if (prevState != null) { throw new IllegalArgumentException("Given state is already registered as " + prevState); } }
SchemaVersionLifecycleStateMachineInfo(Collection<SchemaVersionLifecycleState> states, Collection<SchemaVersionLifecycleStateTransition> transitions) { this.states = new ArrayList<>(states.size()); this.transitions = Lists.newArrayList(transitions); for (SchemaVersionLifecycleState state : states) { this.states.add(new BaseSchemaVersionLifecycleState(state.getName(), state.getId(), state.getDescription())); } }
/** * Registers the given state with REGISTRY. * * @param state state to be registered. * * @throws IllegalArgumentException if the given state is already registered. */ public void register(SchemaVersionLifecycleState state) { checkForInbuiltStateIds(state); SchemaVersionLifecycleState prevState = states.putIfAbsent(state.getId(), state); if (prevState != null) { throw new IllegalArgumentException("Given state is already registered as " + prevState); } }
SchemaVersionLifecycleStateMachineInfo(Collection<SchemaVersionLifecycleState> states, Collection<SchemaVersionLifecycleStateTransition> transitions) { this.states = new ArrayList<>(states.size()); this.transitions = Lists.newArrayList(transitions); for (SchemaVersionLifecycleState state : states) { this.states.add(new BaseSchemaVersionLifecycleState(state.getName(), state.getId(), state.getDescription())); } }
private InReviewState(final SchemaVersionLifecycleState targetState) { super("InReview", (byte) 32, "Finish the schema version." ); SchemaVersionLifecycleStateTransition stateTransition = new SchemaVersionLifecycleStateTransition(getId(), targetState.getId(), "FinishReview", "Finish schema review process"); SchemaVersionLifecycleStateAction stateAction = context -> { context.setState(targetState); try { context.updateSchemaVersionState(); } catch (SchemaNotFoundException e) { throw new SchemaLifecycleException(e); } }; transitionActionPair = Collections.singletonList(Pair.of(stateTransition, stateAction)); }
private InReviewState(final SchemaVersionLifecycleState targetState) { super("InReview", (byte) 32, "Finish the schema version." ); SchemaVersionLifecycleStateTransition stateTransition = new SchemaVersionLifecycleStateTransition(getId(), targetState.getId(), "FinishReview", "Finish schema review process"); SchemaVersionLifecycleStateAction stateAction = context -> { context.setState(targetState); try { context.updateSchemaVersionState(); } catch (SchemaNotFoundException e) { throw new SchemaLifecycleException(e); } }; transitionActionPair = Collections.singletonList(Pair.of(stateTransition, stateAction)); }
public SchemaVersionLifecycleContext createSchemaVersionLifeCycleContext(Long schemaVersionId, SchemaVersionLifecycleState schemaVersionLifecycleState) 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. List<QueryParam> queryParams = new ArrayList<>(); queryParams.add(new QueryParam(SchemaVersionStateStorable.SCHEMA_VERSION_ID, schemaVersionId.toString())); queryParams.add(new QueryParam(SchemaVersionStateStorable.STATE, schemaVersionLifecycleState.getId() .toString())); Collection<SchemaVersionStateStorable> schemaVersionStates = storageManager.find(SchemaVersionStateStorable.NAME_SPACE, queryParams, 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(); SchemaVersionService schemaVersionService = createSchemaVersionService(); SchemaVersionLifecycleContext context = new SchemaVersionLifecycleContext(stateStorable.getSchemaVersionId(), stateStorable.getSequence(), schemaVersionService, schemaVersionLifecycleStateMachine, customSchemaStateExecutor); context.setDetails(stateStorable.getDetails()); return context; }
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; } }
private void storeSchemaVersionState(SchemaVersionLifecycleContext schemaVersionLifecycleContext) throws SchemaNotFoundException { // store versions state, sequence SchemaVersionStateStorable stateStorable = new SchemaVersionStateStorable(); Long schemaVersionId = schemaVersionLifecycleContext.getSchemaVersionId(); byte stateId = schemaVersionLifecycleContext.getState().getId(); stateStorable.setSchemaVersionId(schemaVersionId); stateStorable.setSequence(schemaVersionLifecycleContext.getSequence() + 1); stateStorable.setStateId(stateId); stateStorable.setTimestamp(System.currentTimeMillis()); stateStorable.setDetails(schemaVersionLifecycleContext.getDetails()); stateStorable.setId(storageManager.nextId(SchemaVersionStateStorable.NAME_SPACE)); storageManager.add(stateStorable); // store latest state in versions entity StorableKey storableKey = new StorableKey(SchemaVersionStorable.NAME_SPACE, SchemaVersionStorable.getPrimaryKey(schemaVersionId)); SchemaVersionStorable versionedSchema = storageManager.get(storableKey); if (versionedSchema == null) { throw new SchemaNotFoundException("No Schema version exists with id " + schemaVersionId); } versionedSchema.setState(stateId); storageManager.update(versionedSchema); // invalidate schema version from cache SchemaVersionInfoCache.Key schemaVersionCacheKey = SchemaVersionInfoCache.Key.of(new SchemaIdVersion(schemaVersionId)); invalidateSchemaInAllHAServer(schemaVersionCacheKey); }