@Override public void init(Properties props, String topicName) { super.init(props, topicName); try { SchemaRegistry<Schema> registry = (SchemaRegistry<Schema>) Class.forName( props.getProperty(KafkaAvroMessageEncoder.KAFKA_MESSAGE_CODER_SCHEMA_REGISTRY_CLASS)).newInstance(); log.info("Prop " + KafkaAvroMessageEncoder.KAFKA_MESSAGE_CODER_SCHEMA_REGISTRY_CLASS + " is: " + props.getProperty(KafkaAvroMessageEncoder.KAFKA_MESSAGE_CODER_SCHEMA_REGISTRY_CLASS)); log.info("Underlying schema registry for topic: " + topicName + " is: " + registry); registry.init(props); this.registry = new CachedSchemaRegistry<Schema>(registry, props); this.latestSchema = registry.getLatestSchemaByTopic(topicName).getSchema(); } catch (Exception e) { throw new MessageDecoderException(e); } decoderFactory = DecoderFactory.get(); }
private synchronized S fetchFromSchemaRegistry(String topic, String id) { try { S schema = registry.getSchemaByID(topic, id); return schema; } catch (SchemaNotFoundException e) { addFetchToFailureHistory(id); throw e; } }
public S getSchemaByID(String topic, String id) { CachedSchemaTuple cacheKey = new CachedSchemaTuple(topic, id); S schema = cachedById.get(cacheKey); if (schema == null) { synchronized (this) { if (shouldFetchFromSchemaRegistry(id)) { schema = fetchFromSchemaRegistry(topic, id); cachedById.putIfAbsent(new CachedSchemaTuple(topic, id), schema); } else { throw new SchemaNotFoundException(); } } } return schema; }
@Test public void testMaxRetries() { EasyMock.expect(registry.getSchemaByID(EasyMock.anyString(), EasyMock.anyString())).andThrow( new SchemaNotFoundException()); EasyMock.expectLastCall().times(20); EasyMock.replay(registry); props.setProperty(GET_SCHEMA_BY_ID_MAX_RETIRES, "10"); props.setProperty(GET_SCHEMA_BY_ID_MIN_INTERVAL_SECONDS, "0"); cachedRegistry = new CachedSchemaRegistry<Schema>(registry, props); for (int i = 0; i < 100; i++) { try { cachedRegistry.getSchemaByID("dummyTopic", "dummyID"); } catch (SchemaNotFoundException e) { } try { cachedRegistry.getSchemaByID("dummyTopic", "dummyID2"); } catch (SchemaNotFoundException e) { } } EasyMock.verify(registry); }
public CachedSchemaRegistry(SchemaRegistry<S> registry, Properties props) { this.registry = registry; this.cachedById = new ConcurrentHashMap<CachedSchemaTuple, S>(); this.cachedLatest = new ConcurrentHashMap<String, SchemaDetails<S>>(); this.failedFetchHistories = new HashMap<String, FailedFetchHistory>(); this.init(props); }
@Test public void testMinInterval() throws InterruptedException { EasyMock.expect(registry.getSchemaByID(EasyMock.anyString(), EasyMock.anyString())).andThrow( new SchemaNotFoundException()); EasyMock.expectLastCall().times(4); EasyMock.replay(registry); props.setProperty(GET_SCHEMA_BY_ID_MAX_RETIRES, String.valueOf(Integer.MAX_VALUE)); props.setProperty(GET_SCHEMA_BY_ID_MIN_INTERVAL_SECONDS, "2"); cachedRegistry = new CachedSchemaRegistry<Schema>(registry, props); for (int i = 0; i < 2; i++) { for (int j = 0; j < 5; j++) { try { cachedRegistry.getSchemaByID("dummyTopic", "dummyID"); } catch (SchemaNotFoundException e) { } try { cachedRegistry.getSchemaByID("dummyTopic", "dummyID2"); } catch (SchemaNotFoundException e) { } } Thread.sleep(2500); } EasyMock.verify(registry); } }
public CachedSchemaRegistry(SchemaRegistry<S> registry, Properties props) { this.registry = registry; this.cachedById = new ConcurrentHashMap<CachedSchemaTuple, S>(); this.cachedLatest = new ConcurrentHashMap<String, SchemaDetails<S>>(); this.failedFetchHistories = new HashMap<String, FailedFetchHistory>(); this.init(props); }
public S getSchemaByID(String topic, String id) { CachedSchemaTuple cacheKey = new CachedSchemaTuple(topic, id); S schema = cachedById.get(cacheKey); if (schema == null) { synchronized (this) { if (shouldFetchFromSchemaRegistry(id)) { schema = fetchFromSchemaRegistry(topic, id); cachedById.putIfAbsent(new CachedSchemaTuple(topic, id), schema); } else { throw new SchemaNotFoundException(); } } } return schema; }
public void init(Properties props, String topicName) { super.init(props, topicName); this.props = props; this.topicName = topicName; this.schemaIDField = props.getProperty(CAMUS_SCHEMA_ID_FIELD, DEFAULT_SCHEMA_ID_FIELD); try { SchemaRegistry<Schema> registry = (SchemaRegistry<Schema>) Class.forName( props.getProperty(KafkaAvroMessageEncoder.KAFKA_MESSAGE_CODER_SCHEMA_REGISTRY_CLASS)).newInstance(); log.info("Prop " + KafkaAvroMessageEncoder.KAFKA_MESSAGE_CODER_SCHEMA_REGISTRY_CLASS + " is: " + props.getProperty(KafkaAvroMessageEncoder.KAFKA_MESSAGE_CODER_SCHEMA_REGISTRY_CLASS)); log.info("Underlying schema registry for topic: " + topicName + " is: " + registry); registry.init(props); this.registry = new CachedSchemaRegistry<Schema>(registry, props); this.latestSchema = ((Schema) registry.getLatestSchemaByTopic(topicName).getSchema()); } catch (Exception e) { throw new MessageDecoderException(e); } this.decoderFactory = DecoderFactory.get(); }
private synchronized S fetchFromSchemaRegistry(String topic, String id) { try { S schema = registry.getSchemaByID(topic, id); return schema; } catch (SchemaNotFoundException e) { addFetchToFailureHistory(id); throw e; } }