Search in sources :

Example 1 with SchemaVersionInfo

use of com.hortonworks.registries.schemaregistry.SchemaVersionInfo in project nifi by apache.

the class HortonworksSchemaRegistry method getLatestSchemaVersionInfo.

private SchemaVersionInfo getLatestSchemaVersionInfo(final SchemaRegistryClient client, final String schemaName, final String branchName) throws org.apache.nifi.schema.access.SchemaNotFoundException {
    try {
        // Try to fetch the SchemaVersionInfo from the cache.
        final Tuple<String, String> nameAndBranch = new Tuple<>(schemaName, branchName);
        final Tuple<SchemaVersionInfo, Long> timestampedVersionInfo = schemaVersionByNameCache.get(nameAndBranch);
        // Determine if the timestampedVersionInfo is expired
        boolean fetch = false;
        if (timestampedVersionInfo == null) {
            fetch = true;
        } else {
            final long minTimestamp = System.nanoTime() - versionInfoCacheNanos;
            fetch = timestampedVersionInfo.getValue() < minTimestamp;
        }
        // If not expired, use what we got from the cache
        if (!fetch) {
            return timestampedVersionInfo.getKey();
        }
        // schema version info was expired or not found in cache. Fetch from schema registry
        final SchemaVersionInfo versionInfo;
        if (StringUtils.isBlank(branchName)) {
            versionInfo = client.getLatestSchemaVersionInfo(schemaName);
        } else {
            versionInfo = client.getLatestSchemaVersionInfo(branchName, schemaName);
        }
        if (versionInfo == null) {
            throw new org.apache.nifi.schema.access.SchemaNotFoundException("Could not find schema with name '" + schemaName + "'");
        }
        // Store new version in cache.
        final Tuple<SchemaVersionInfo, Long> tuple = new Tuple<>(versionInfo, System.nanoTime());
        schemaVersionByNameCache.put(nameAndBranch, tuple);
        return versionInfo;
    } catch (final SchemaNotFoundException e) {
        throw new org.apache.nifi.schema.access.SchemaNotFoundException(e);
    }
}
Also used : SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) OptionalLong(java.util.OptionalLong) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) Tuple(org.apache.nifi.util.Tuple)

Example 2 with SchemaVersionInfo

use of com.hortonworks.registries.schemaregistry.SchemaVersionInfo in project nifi by apache.

the class HortonworksSchemaRegistry method getSchemaVersionInfo.

private SchemaVersionInfo getSchemaVersionInfo(final SchemaRegistryClient client, final SchemaVersionKey key) throws org.apache.nifi.schema.access.SchemaNotFoundException {
    try {
        // Try to fetch the SchemaVersionInfo from the cache.
        final Tuple<SchemaVersionInfo, Long> timestampedVersionInfo = schemaVersionByKeyCache.get(key);
        // Determine if the timestampedVersionInfo is expired
        boolean fetch = false;
        if (timestampedVersionInfo == null) {
            fetch = true;
        } else {
            final long minTimestamp = System.nanoTime() - versionInfoCacheNanos;
            fetch = timestampedVersionInfo.getValue() < minTimestamp;
        }
        // If not expired, use what we got from the cache
        if (!fetch) {
            return timestampedVersionInfo.getKey();
        }
        // schema version info was expired or not found in cache. Fetch from schema registry
        final SchemaVersionInfo versionInfo = client.getSchemaVersionInfo(key);
        if (versionInfo == null) {
            throw new org.apache.nifi.schema.access.SchemaNotFoundException("Could not find schema with name '" + key.getSchemaName() + "' and version " + key.getVersion());
        }
        // Store new version in cache.
        final Tuple<SchemaVersionInfo, Long> tuple = new Tuple<>(versionInfo, System.nanoTime());
        schemaVersionByKeyCache.put(key, tuple);
        return versionInfo;
    } catch (final SchemaNotFoundException e) {
        throw new org.apache.nifi.schema.access.SchemaNotFoundException(e);
    }
}
Also used : SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) OptionalLong(java.util.OptionalLong) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) Tuple(org.apache.nifi.util.Tuple)

Example 3 with SchemaVersionInfo

use of com.hortonworks.registries.schemaregistry.SchemaVersionInfo in project nifi by apache.

the class TestHortonworksSchemaRegistry method setup.

@Before
public void setup() throws SchemaNotFoundException {
    schemaVersionInfoMap.clear();
    schemaMetadataInfoMap.clear();
    client = mock(SchemaRegistryClient.class);
    doAnswer(new Answer<SchemaVersionInfo>() {

        @Override
        public SchemaVersionInfo answer(final InvocationOnMock invocation) throws Throwable {
            final String schemaName = invocation.getArgumentAt(0, String.class);
            final SchemaVersionInfo info = schemaVersionInfoMap.get(schemaName);
            if (info == null) {
                throw new SchemaNotFoundException();
            }
            return info;
        }
    }).when(client).getLatestSchemaVersionInfo(any(String.class));
    doAnswer(new Answer<SchemaMetadataInfo>() {

        @Override
        public SchemaMetadataInfo answer(InvocationOnMock invocation) throws Throwable {
            final String schemaName = invocation.getArgumentAt(0, String.class);
            final SchemaMetadataInfo info = schemaMetadataInfoMap.get(schemaName);
            if (info == null) {
                throw new SchemaNotFoundException();
            }
            return info;
        }
    }).when(client).getSchemaMetadataInfo(any(String.class));
    registry = new HortonworksSchemaRegistry() {

        @Override
        protected synchronized SchemaRegistryClient getClient() {
            return client;
        }
    };
}
Also used : SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaRegistryClient(com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo) Before(org.junit.Before)

Example 4 with SchemaVersionInfo

use of com.hortonworks.registries.schemaregistry.SchemaVersionInfo in project streamline by hortonworks.

the class StreamlineEventSerializer method serialize.

@Override
public byte[] serialize(String topic, StreamlineEvent streamlineEvent) {
    SchemaMetadata schemaMetadata = getSchemaKey(topic, false);
    SchemaVersionInfo schemaVersionInfo;
    try {
        schemaMetadata = schemaRegistryClient.getSchemaMetadataInfo(schemaMetadata.getName()).getSchemaMetadata();
        if (writerSchemaVersion != null) {
            schemaVersionInfo = schemaRegistryClient.getSchemaVersionInfo(new SchemaVersionKey(schemaMetadata.getName(), writerSchemaVersion));
        } else {
            schemaVersionInfo = schemaRegistryClient.getLatestSchemaVersionInfo(schemaMetadata.getName());
        }
    } catch (SchemaNotFoundException e) {
        LOG.error("Exception occured while getting SchemaVersionInfo for " + schemaMetadata, e);
        throw new RuntimeException(e);
    }
    if (streamlineEvent == null || streamlineEvent.isEmpty()) {
        return null;
    } else {
        return avroSnapshotSerializer.serialize(getAvroRecord(streamlineEvent, new Schema.Parser().parse(schemaVersionInfo.getSchemaText())), schemaMetadata);
    }
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) Schema(org.apache.avro.Schema) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey)

Example 5 with SchemaVersionInfo

use of com.hortonworks.registries.schemaregistry.SchemaVersionInfo in project streamline by hortonworks.

the class SchemaResource method getSchemaForVersion.

@GET
@Path("/{schemaName}/versions/{version}")
@Timed
@Produces(MediaType.APPLICATION_JSON)
public Response getSchemaForVersion(@PathParam("schemaName") String schemaName, @PathParam("version") String version, @Context SecurityContext securityContext) {
    try {
        LOG.info("Get schema:version [{}:{}]", schemaName, version);
        SchemaVersionInfo schemaVersionInfo = schemaRegistryClient.getSchemaVersionInfo(new SchemaVersionKey(schemaName, Integer.parseInt(version)));
        if (schemaVersionInfo != null) {
            LOG.debug("Received schema version from schema registry: [{}]", schemaVersionInfo);
            String schema = schemaVersionInfo.getSchemaText();
            if (schema != null && !schema.isEmpty()) {
                schema = AvroStreamlineSchemaConverter.convertAvroSchemaToStreamlineSchema(schema);
            }
            LOG.debug("Converted schema: [{}]", schema);
            return WSUtils.respondEntity(schema, OK);
        } else {
            throw new SchemaNotFoundException("Version " + version + " of schema " + schemaName + " not found ");
        }
    } catch (SchemaNotFoundException e) {
        LOG.error("Schema not found: [{}]", schemaName, e);
        throw EntityNotFoundException.byName(schemaName);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey) EntityNotFoundException(com.hortonworks.streamline.common.exception.service.exception.request.EntityNotFoundException) BadRequestException(com.hortonworks.streamline.common.exception.service.exception.request.BadRequestException) IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET)

Aggregations

SchemaVersionInfo (com.hortonworks.registries.schemaregistry.SchemaVersionInfo)31 SchemaMetadata (com.hortonworks.registries.schemaregistry.SchemaMetadata)19 SchemaNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)17 SchemaIdVersion (com.hortonworks.registries.schemaregistry.SchemaIdVersion)14 SchemaMetadataInfo (com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)13 SchemaVersionKey (com.hortonworks.registries.schemaregistry.SchemaVersionKey)13 IncompatibleSchemaException (com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException)11 InvalidSchemaException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException)11 IOException (java.io.IOException)11 SchemaVersion (com.hortonworks.registries.schemaregistry.SchemaVersion)10 Test (org.junit.Test)10 Timed (com.codahale.metrics.annotation.Timed)7 CatalogResponse (com.hortonworks.registries.common.catalog.CatalogResponse)7 SchemaBranchNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException)7 GET (javax.ws.rs.GET)7 Path (javax.ws.rs.Path)7 UnitOfWork (com.hortonworks.registries.common.transaction.UnitOfWork)6 InvalidSchemaBranchDeletionException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchDeletionException)6 SchemaBranchAlreadyExistsException (com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException)6 UnsupportedSchemaTypeException (com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException)6