Search in sources :

Example 21 with SchemaMetadataInfo

use of com.hortonworks.registries.schemaregistry.SchemaMetadataInfo in project registry by hortonworks.

the class SchemaVersionLifecycleStatesTest method setup.

@Before
public void setup() {
    SchemaMetadata schemaMetadata = new SchemaMetadata.Builder("schema-1").type("avro").schemaGroup("kafka").build();
    SchemaMetadataInfo schemaMetadataInfo = new SchemaMetadataInfo(schemaMetadata, new Random().nextLong(), System.currentTimeMillis());
    SchemaVersionInfo schemaVersionInfo = new SchemaVersionInfo(new Random().nextLong(), schemaMetadata.getName(), 1, schemaMetadataInfo.getId(), "{\"type\":\"string\"}", System.currentTimeMillis(), "", SchemaVersionLifecycleStates.ENABLED.getId());
    SchemaVersionService schemaVersionServiceMock = new SchemaVersionService() {

        @Override
        public void updateSchemaVersionState(SchemaVersionLifecycleContext schemaLifeCycleContext) {
            LOG.info("Updating schema version: [{}]", schemaLifeCycleContext);
        }

        @Override
        public void deleteSchemaVersion(Long schemaVersionId) {
            LOG.info("Deleting schema version [{}]", schemaVersionId);
        }

        @Override
        public SchemaMetadataInfo getSchemaMetadata(long schemaVersionId) throws SchemaNotFoundException {
            return schemaMetadataInfo;
        }

        @Override
        public SchemaVersionInfo getSchemaVersionInfo(long schemaVersionId) throws SchemaNotFoundException {
            return schemaVersionInfo;
        }

        @Override
        public CompatibilityResult checkForCompatibility(SchemaMetadata schemaMetadata, String toSchemaText, String existingSchemaText) {
            return CompatibilityResult.SUCCESS;
        }

        @Override
        public Collection<SchemaVersionInfo> getAllSchemaVersions(String schemaBranchName, String schemaName) throws SchemaNotFoundException {
            return Collections.singletonList(schemaVersionInfo);
        }
    };
    SchemaVersionLifecycleStateMachine lifecycleStateMachine = SchemaVersionLifecycleStateMachine.newBuilder().build();
    context = new SchemaVersionLifecycleContext(schemaVersionInfo.getId(), 1, schemaVersionServiceMock, lifecycleStateMachine, new DefaultCustomSchemaStateExecutor());
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) Random(java.util.Random) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo) Before(org.junit.Before)

Example 22 with SchemaMetadataInfo

use of com.hortonworks.registries.schemaregistry.SchemaMetadataInfo in project registry by hortonworks.

the class SchemaVersionLifecycleStates method transitionToEnableState.

public static void transitionToEnableState(SchemaVersionLifecycleContext context) throws SchemaNotFoundException, IncompatibleSchemaException, SchemaLifecycleException, SchemaBranchNotFoundException {
    Long schemaVersionId = context.getSchemaVersionId();
    SchemaVersionService schemaVersionService = context.getSchemaVersionService();
    SchemaMetadataInfo schemaMetadataInfo = schemaVersionService.getSchemaMetadata(schemaVersionId);
    SchemaMetadata schemaMetadata = schemaMetadataInfo.getSchemaMetadata();
    String schemaName = schemaMetadata.getName();
    SchemaValidationLevel validationLevel = schemaMetadata.getValidationLevel();
    SchemaVersionInfo schemaVersionInfo = schemaVersionService.getSchemaVersionInfo(schemaVersionId);
    int schemaVersion = schemaVersionInfo.getVersion();
    String schemaText = schemaVersionInfo.getSchemaText();
    List<SchemaVersionInfo> allEnabledSchemaVersions = schemaVersionService.getAllSchemaVersions(SchemaBranch.MASTER_BRANCH, schemaName).stream().filter(x -> SchemaVersionLifecycleStates.ENABLED.getId().equals(x.getStateId())).collect(Collectors.toList());
    if (!allEnabledSchemaVersions.isEmpty()) {
        if (validationLevel.equals(SchemaValidationLevel.ALL)) {
            for (SchemaVersionInfo curSchemaVersionInfo : allEnabledSchemaVersions) {
                int curVersion = curSchemaVersionInfo.getVersion();
                if (curVersion < schemaVersion) {
                    checkCompatibility(schemaVersionService, schemaMetadata, schemaText, curSchemaVersionInfo.getSchemaText());
                } else {
                    checkCompatibility(schemaVersionService, schemaMetadata, curSchemaVersionInfo.getSchemaText(), schemaText);
                }
            }
        } else if (validationLevel.equals(SchemaValidationLevel.LATEST)) {
            List<SchemaVersionInfo> sortedSchemaVersionInfos = new ArrayList<>(allEnabledSchemaVersions);
            sortedSchemaVersionInfos.sort(Comparator.comparingInt(SchemaVersionInfo::getVersion));
            int i = 0;
            int size = sortedSchemaVersionInfos.size();
            for (; i < size && sortedSchemaVersionInfos.get(i).getVersion() < schemaVersion; i++) {
                String fromSchemaText = sortedSchemaVersionInfos.get(i).getSchemaText();
                checkCompatibility(schemaVersionService, schemaMetadata, schemaText, fromSchemaText);
            }
            for (; i < size && sortedSchemaVersionInfos.get(i).getVersion() > schemaVersion; i++) {
                String toSchemaText = sortedSchemaVersionInfos.get(i).getSchemaText();
                checkCompatibility(schemaVersionService, schemaMetadata, toSchemaText, schemaText);
            }
        }
    }
    context.setState(ENABLED);
    context.updateSchemaVersionState();
}
Also used : IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) SchemaBranchNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException) Collection(java.util.Collection) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) CompatibilityResult(com.hortonworks.registries.schemaregistry.CompatibilityResult) Collectors(java.util.stream.Collectors) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo) ArrayList(java.util.ArrayList) List(java.util.List) Lists(com.google.common.collect.Lists) SchemaBranch(com.hortonworks.registries.schemaregistry.SchemaBranch) Pair(org.apache.commons.lang3.tuple.Pair) SchemaValidationLevel(com.hortonworks.registries.schemaregistry.SchemaValidationLevel) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) Comparator(java.util.Comparator) Collections(java.util.Collections) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaValidationLevel(com.hortonworks.registries.schemaregistry.SchemaValidationLevel) ArrayList(java.util.ArrayList) List(java.util.List) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)

Aggregations

SchemaMetadataInfo (com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)22 SchemaMetadata (com.hortonworks.registries.schemaregistry.SchemaMetadata)14 SchemaVersionInfo (com.hortonworks.registries.schemaregistry.SchemaVersionInfo)13 SchemaNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)10 SchemaVersionKey (com.hortonworks.registries.schemaregistry.SchemaVersionKey)9 Test (org.junit.Test)8 SchemaVersion (com.hortonworks.registries.schemaregistry.SchemaVersion)7 IncompatibleSchemaException (com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException)6 IOException (java.io.IOException)6 CatalogResponse (com.hortonworks.registries.common.catalog.CatalogResponse)5 AggregatedSchemaMetadataInfo (com.hortonworks.registries.schemaregistry.AggregatedSchemaMetadataInfo)5 Response (javax.ws.rs.core.Response)5 Timed (com.codahale.metrics.annotation.Timed)4 UnitOfWork (com.hortonworks.registries.common.transaction.UnitOfWork)4 SchemaIdVersion (com.hortonworks.registries.schemaregistry.SchemaIdVersion)4 SchemaRegistryClient (com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient)4 InvalidSchemaException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException)4 UnsupportedSchemaTypeException (com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException)4 ApiOperation (io.swagger.annotations.ApiOperation)4 Path (javax.ws.rs.Path)4