Search in sources :

Example 36 with SchemaIdentifier

use of org.apache.nifi.serialization.record.SchemaIdentifier in project nifi by apache.

the class TestHortonworksSchemaRegistry method testCacheUsed.

@Test
public void testCacheUsed() throws Exception {
    final String text = new String(Files.readAllBytes(Paths.get("src/test/resources/empty-schema.avsc")));
    final SchemaVersionInfo info = new SchemaVersionInfo(1L, "unit-test", 2, text, System.currentTimeMillis(), "description");
    schemaVersionInfoMap.put("unit-test", info);
    final SchemaMetadata metadata = new SchemaMetadata.Builder("unit-test").compatibility(SchemaCompatibility.NONE).evolve(true).schemaGroup("group").type("AVRO").build();
    final Constructor<SchemaMetadataInfo> ctr = SchemaMetadataInfo.class.getDeclaredConstructor(SchemaMetadata.class, Long.class, Long.class);
    ctr.setAccessible(true);
    final SchemaMetadataInfo schemaMetadataInfo = ctr.newInstance(metadata, 1L, System.currentTimeMillis());
    schemaMetadataInfoMap.put("unit-test", schemaMetadataInfo);
    final Map<PropertyDescriptor, String> properties = new HashMap<>();
    properties.put(HortonworksSchemaRegistry.URL, "http://localhost:44444");
    properties.put(HortonworksSchemaRegistry.CACHE_EXPIRATION, "5 mins");
    properties.put(HortonworksSchemaRegistry.CACHE_SIZE, "1000");
    final ConfigurationContext configurationContext = new MockConfigurationContext(properties, null);
    registry.enable(configurationContext);
    for (int i = 0; i < 10000; i++) {
        final SchemaIdentifier schemaIdentifier = SchemaIdentifier.builder().name("unit-test").build();
        final RecordSchema schema = registry.retrieveSchema(schemaIdentifier);
        assertNotNull(schema);
    }
    Mockito.verify(client, Mockito.times(1)).getLatestSchemaVersionInfo(any(String.class));
}
Also used : ConfigurationContext(org.apache.nifi.controller.ConfigurationContext) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo) Test(org.junit.Test)

Example 37 with SchemaIdentifier

use of org.apache.nifi.serialization.record.SchemaIdentifier in project nifi by apache.

the class TestHortonworksSchemaRegistry method testCacheExpires.

@Test
@Ignore("This can be useful for manual testing/debugging, but will keep ignored for now because we don't want automated builds to run this, since it depends on timing")
public void testCacheExpires() throws Exception {
    final String text = new String(Files.readAllBytes(Paths.get("src/test/resources/empty-schema.avsc")));
    final SchemaVersionInfo info = new SchemaVersionInfo(1L, "unit-test", 2, text, System.currentTimeMillis(), "description");
    schemaVersionInfoMap.put("unit-test", info);
    final SchemaMetadata metadata = new SchemaMetadata.Builder("unit-test").compatibility(SchemaCompatibility.NONE).evolve(true).schemaGroup("group").type("AVRO").build();
    final Constructor<SchemaMetadataInfo> ctr = SchemaMetadataInfo.class.getDeclaredConstructor(SchemaMetadata.class, Long.class, Long.class);
    ctr.setAccessible(true);
    final SchemaMetadataInfo schemaMetadataInfo = ctr.newInstance(metadata, 1L, System.currentTimeMillis());
    schemaMetadataInfoMap.put("unit-test", schemaMetadataInfo);
    final Map<PropertyDescriptor, String> properties = new HashMap<>();
    properties.put(HortonworksSchemaRegistry.URL, "http://localhost:44444");
    properties.put(HortonworksSchemaRegistry.CACHE_EXPIRATION, "1 sec");
    properties.put(HortonworksSchemaRegistry.CACHE_SIZE, "1000");
    final ConfigurationContext configurationContext = new MockConfigurationContext(properties, null);
    registry.enable(configurationContext);
    for (int i = 0; i < 2; i++) {
        final SchemaIdentifier schemaIdentifier = SchemaIdentifier.builder().name("unit-test").build();
        final RecordSchema schema = registry.retrieveSchema(schemaIdentifier);
        assertNotNull(schema);
    }
    Mockito.verify(client, Mockito.times(1)).getLatestSchemaVersionInfo(any(String.class));
    Thread.sleep(2000L);
    for (int i = 0; i < 2; i++) {
        final SchemaIdentifier schemaIdentifier = SchemaIdentifier.builder().name("unit-test").build();
        final RecordSchema schema = registry.retrieveSchema(schemaIdentifier);
        assertNotNull(schema);
    }
    Mockito.verify(client, Mockito.times(2)).getLatestSchemaVersionInfo(any(String.class));
}
Also used : ConfigurationContext(org.apache.nifi.controller.ConfigurationContext) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

SchemaIdentifier (org.apache.nifi.serialization.record.SchemaIdentifier)37 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)28 Test (org.junit.Test)22 SimpleRecordSchema (org.apache.nifi.serialization.SimpleRecordSchema)12 OptionalLong (java.util.OptionalLong)6 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 OptionalInt (java.util.OptionalInt)5 Schema (org.apache.avro.Schema)5 PropertyValue (org.apache.nifi.components.PropertyValue)5 MockPropertyValue (org.apache.nifi.util.MockPropertyValue)5 SchemaMetadataInfo (com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)4 SchemaVersionInfo (com.hortonworks.registries.schemaregistry.SchemaVersionInfo)4 ByteBuffer (java.nio.ByteBuffer)4 SchemaMetadata (com.hortonworks.registries.schemaregistry.SchemaMetadata)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)3 InitializationException (org.apache.nifi.reporting.InitializationException)3 SchemaNotFoundException (org.apache.nifi.schema.access.SchemaNotFoundException)3