Search in sources :

Example 1 with SchemaNotFoundException

use of com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException 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 SchemaNotFoundException

use of com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException 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 SchemaNotFoundException

use of com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException 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 SchemaNotFoundException

use of com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException 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 SchemaNotFoundException

use of com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException 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

SchemaNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)40 IncompatibleSchemaException (com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException)24 InvalidSchemaException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException)21 IOException (java.io.IOException)20 CatalogResponse (com.hortonworks.registries.common.catalog.CatalogResponse)18 SchemaLifecycleException (com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException)18 SchemaBranchNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException)17 Response (javax.ws.rs.core.Response)17 UnsupportedSchemaTypeException (com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException)16 SchemaBranchAlreadyExistsException (com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException)15 Timed (com.codahale.metrics.annotation.Timed)14 InvalidSchemaBranchDeletionException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchDeletionException)14 Path (javax.ws.rs.Path)14 UnitOfWork (com.hortonworks.registries.common.transaction.UnitOfWork)13 SchemaVersionInfo (com.hortonworks.registries.schemaregistry.SchemaVersionInfo)13 ApiOperation (io.swagger.annotations.ApiOperation)13 FileNotFoundException (java.io.FileNotFoundException)10 QueryParam (com.hortonworks.registries.common.QueryParam)7 SchemaVersionLifecycleContext (com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleContext)7 Collection (java.util.Collection)7