Search in sources :

Example 1 with KafkaQueryChangeLog

use of org.apache.rya.streams.kafka.queries.KafkaQueryChangeLog in project incubator-rya by apache.

the class KafkaQueryChangeLogSource method runOneIteration.

@Override
protected void runOneIteration() throws Exception {
    lock.lock();
    try {
        // Get the list of topics from the Kafka Server.
        final Set<String> changeLogTopics = new HashSet<>(listTopicsConsumer.listTopics().keySet());
        // Remove all topics that are not valid Rya Query Change Log topic names.
        changeLogTopics.removeIf(topic -> !KafkaTopics.getRyaInstanceFromQueryChangeLog(topic).isPresent());
        // Extract the Rya instance names from the change log topics.
        final Set<String> ryaInstances = changeLogTopics.stream().map(topic -> KafkaTopics.getRyaInstanceFromQueryChangeLog(topic).get()).collect(Collectors.toSet());
        // Any Rya instances that are in the old set of topics, but not the new one, have been deleted.
        final Set<String> deletedRyaInstances = new HashSet<>(Sets.difference(knownChangeLogs.keySet(), ryaInstances));
        // Any Rya instances that are in the new set of topics, but not the old set, have been created.
        final Set<String> createdRyaInstances = new HashSet<>(Sets.difference(ryaInstances, knownChangeLogs.keySet()));
        // Handle the deletes.
        for (final String deletedRyaInstance : deletedRyaInstances) {
            // Remove the change log from the set of known logs.
            knownChangeLogs.remove(deletedRyaInstance);
            // Notify the listeners of the update so that they may close the previously provided change log.
            for (final SourceListener listener : listeners) {
                listener.notifyDelete(deletedRyaInstance);
            }
        }
        // Handle the adds.
        for (final String createdRyaInstance : createdRyaInstances) {
            // Create and store the ChangeLog.
            final String changeLogTopic = KafkaTopics.queryChangeLogTopic(createdRyaInstance);
            knownChangeLogs.put(createdRyaInstance, changeLogTopic);
            // Notify the listeners of the update.
            for (final SourceListener listener : listeners) {
                final KafkaQueryChangeLog changeLog = KafkaQueryChangeLogFactory.make(kafkaBootstrapServer, changeLogTopic);
                listener.notifyCreate(createdRyaInstance, changeLog);
            }
        }
    } finally {
        lock.unlock();
    }
}
Also used : CommonClientConfigs(org.apache.kafka.clients.CommonClientConfigs) Properties(java.util.Properties) Logger(org.slf4j.Logger) ReentrantLock(java.util.concurrent.locks.ReentrantLock) KafkaTopics(org.apache.rya.streams.kafka.KafkaTopics) DefaultAnnotation(edu.umd.cs.findbugs.annotations.DefaultAnnotation) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) HashMap(java.util.HashMap) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) HashSet(java.util.HashSet) QueryChangeLogSource(org.apache.rya.streams.querymanager.QueryChangeLogSource) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) AbstractScheduledService(com.google.common.util.concurrent.AbstractScheduledService) Objects.requireNonNull(java.util.Objects.requireNonNull) NonNull(edu.umd.cs.findbugs.annotations.NonNull) Entry(java.util.Map.Entry) KafkaQueryChangeLog(org.apache.rya.streams.kafka.queries.KafkaQueryChangeLog) KafkaQueryChangeLogFactory(org.apache.rya.streams.kafka.queries.KafkaQueryChangeLogFactory) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) HashSet(java.util.HashSet) KafkaQueryChangeLog(org.apache.rya.streams.kafka.queries.KafkaQueryChangeLog)

Example 2 with KafkaQueryChangeLog

use of org.apache.rya.streams.kafka.queries.KafkaQueryChangeLog in project incubator-rya by apache.

the class KafkaQueryChangeLogSource method subscribe.

@Override
public void subscribe(final SourceListener listener) {
    requireNonNull(listener);
    lock.lock();
    try {
        // Add the listener to the list of known listeners.
        listeners.add(listener);
        // Notify it with everything that already exists.
        for (final Entry<String, String> entry : knownChangeLogs.entrySet()) {
            final String changeLogTopic = entry.getValue();
            final KafkaQueryChangeLog changeLog = KafkaQueryChangeLogFactory.make(kafkaBootstrapServer, changeLogTopic);
            listener.notifyCreate(entry.getKey(), changeLog);
        }
    } finally {
        lock.unlock();
    }
}
Also used : KafkaQueryChangeLog(org.apache.rya.streams.kafka.queries.KafkaQueryChangeLog)

Example 3 with KafkaQueryChangeLog

use of org.apache.rya.streams.kafka.queries.KafkaQueryChangeLog in project incubator-rya by apache.

the class RunQueryCommandIT method setup.

@Before
public void setup() {
    // Make sure the topic that the change log uses exists.
    final String changeLogTopic = KafkaTopics.queryChangeLogTopic("" + ryaInstance);
    kafka.createTopic(changeLogTopic);
    // Setup the QueryRepository used by the test.
    final Producer<?, QueryChange> queryProducer = KafkaTestUtil.makeProducer(kafka, StringSerializer.class, QueryChangeSerializer.class);
    final Consumer<?, QueryChange> queryConsumer = KafkaTestUtil.fromStartConsumer(kafka, StringDeserializer.class, QueryChangeDeserializer.class);
    final QueryChangeLog changeLog = new KafkaQueryChangeLog(queryProducer, queryConsumer, changeLogTopic);
    queryRepo = new InMemoryQueryRepository(changeLog, Scheduler.newFixedRateSchedule(0L, 5, TimeUnit.SECONDS));
    // Initialize the Statements Producer and the Results Consumer.
    stmtProducer = KafkaTestUtil.makeProducer(kafka, StringSerializer.class, VisibilityStatementSerializer.class);
    resultConsumer = KafkaTestUtil.fromStartConsumer(kafka, StringDeserializer.class, VisibilityBindingSetDeserializer.class);
}
Also used : StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) VisibilityStatementSerializer(org.apache.rya.streams.kafka.serialization.VisibilityStatementSerializer) VisibilityBindingSetDeserializer(org.apache.rya.streams.kafka.serialization.VisibilityBindingSetDeserializer) QueryChange(org.apache.rya.streams.api.queries.QueryChange) InMemoryQueryRepository(org.apache.rya.streams.api.queries.InMemoryQueryRepository) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) QueryChangeLog(org.apache.rya.streams.api.queries.QueryChangeLog) KafkaQueryChangeLog(org.apache.rya.streams.kafka.queries.KafkaQueryChangeLog) KafkaQueryChangeLog(org.apache.rya.streams.kafka.queries.KafkaQueryChangeLog) Before(org.junit.Before)

Example 4 with KafkaQueryChangeLog

use of org.apache.rya.streams.kafka.queries.KafkaQueryChangeLog in project incubator-rya by apache.

the class AddQueryCommandIT method setup.

@Before
public void setup() {
    // Make sure the topic that the change log uses exists.
    final String changeLogTopic = KafkaTopics.queryChangeLogTopic("" + ryaInstance);
    kafka.createTopic(changeLogTopic);
    // Setup the QueryRepository used by the test.
    final Producer<?, QueryChange> queryProducer = KafkaTestUtil.makeProducer(kafka, StringSerializer.class, QueryChangeSerializer.class);
    final Consumer<?, QueryChange> queryConsumer = KafkaTestUtil.fromStartConsumer(kafka, StringDeserializer.class, QueryChangeDeserializer.class);
    final QueryChangeLog changeLog = new KafkaQueryChangeLog(queryProducer, queryConsumer, changeLogTopic);
    queryRepo = new InMemoryQueryRepository(changeLog, Scheduler.newFixedRateSchedule(0L, 5, TimeUnit.SECONDS));
}
Also used : QueryChange(org.apache.rya.streams.api.queries.QueryChange) InMemoryQueryRepository(org.apache.rya.streams.api.queries.InMemoryQueryRepository) QueryChangeLog(org.apache.rya.streams.api.queries.QueryChangeLog) KafkaQueryChangeLog(org.apache.rya.streams.kafka.queries.KafkaQueryChangeLog) KafkaQueryChangeLog(org.apache.rya.streams.kafka.queries.KafkaQueryChangeLog) Before(org.junit.Before)

Example 5 with KafkaQueryChangeLog

use of org.apache.rya.streams.kafka.queries.KafkaQueryChangeLog in project incubator-rya by apache.

the class ListQueryCommandIT method setup.

@Before
public void setup() {
    // Make sure the topic that the change log uses exists.
    final String changeLogTopic = KafkaTopics.queryChangeLogTopic("" + ryaInstance);
    kafka.createTopic(changeLogTopic);
    // Setup the QueryRepository used by the test.
    final Producer<?, QueryChange> queryProducer = KafkaTestUtil.makeProducer(kafka, StringSerializer.class, QueryChangeSerializer.class);
    final Consumer<?, QueryChange> queryConsumer = KafkaTestUtil.fromStartConsumer(kafka, StringDeserializer.class, QueryChangeDeserializer.class);
    final QueryChangeLog changeLog = new KafkaQueryChangeLog(queryProducer, queryConsumer, changeLogTopic);
    queryRepo = new InMemoryQueryRepository(changeLog, Scheduler.newFixedRateSchedule(0L, 5, TimeUnit.SECONDS));
}
Also used : QueryChange(org.apache.rya.streams.api.queries.QueryChange) InMemoryQueryRepository(org.apache.rya.streams.api.queries.InMemoryQueryRepository) QueryChangeLog(org.apache.rya.streams.api.queries.QueryChangeLog) KafkaQueryChangeLog(org.apache.rya.streams.kafka.queries.KafkaQueryChangeLog) KafkaQueryChangeLog(org.apache.rya.streams.kafka.queries.KafkaQueryChangeLog) Before(org.junit.Before)

Aggregations

KafkaQueryChangeLog (org.apache.rya.streams.kafka.queries.KafkaQueryChangeLog)7 InMemoryQueryRepository (org.apache.rya.streams.api.queries.InMemoryQueryRepository)5 QueryChange (org.apache.rya.streams.api.queries.QueryChange)5 QueryChangeLog (org.apache.rya.streams.api.queries.QueryChangeLog)5 Before (org.junit.Before)4 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)2 VisibilityBindingSetDeserializer (org.apache.rya.streams.kafka.serialization.VisibilityBindingSetDeserializer)2 Sets (com.google.common.collect.Sets)1 AbstractScheduledService (com.google.common.util.concurrent.AbstractScheduledService)1 DefaultAnnotation (edu.umd.cs.findbugs.annotations.DefaultAnnotation)1 NonNull (edu.umd.cs.findbugs.annotations.NonNull)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Entry (java.util.Map.Entry)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Properties (java.util.Properties)1 Set (java.util.Set)1 UUID (java.util.UUID)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 Collectors (java.util.stream.Collectors)1