Search in sources :

Example 6 with SourceListener

use of org.apache.rya.streams.querymanager.QueryChangeLogSource.SourceListener in project incubator-rya by apache.

the class KafkaQueryChangeLogSourceIT method newListenerReceivesAllKnownLogs.

@Test
public void newListenerReceivesAllKnownLogs() throws Exception {
    // Create a valid Query Change Log topic.
    final String ryaInstance = UUID.randomUUID().toString();
    final String topic = KafkaTopics.queryChangeLogTopic(ryaInstance);
    kafka.createTopic(topic);
    // Create the source.
    final QueryChangeLogSource source = new KafkaQueryChangeLogSource(kafka.getKafkaHostname(), Integer.parseInt(kafka.getKafkaPort()), Scheduler.newFixedRateSchedule(0, 1, TimeUnit.SECONDS));
    // Register a listener that counts down a latch if it sees the new topic.
    final CountDownLatch created = new CountDownLatch(1);
    source.subscribe(new SourceListener() {

        @Override
        public void notifyCreate(final String ryaInstanceName, final QueryChangeLog log) {
            assertEquals(ryaInstance, ryaInstanceName);
            created.countDown();
        }

        @Override
        public void notifyDelete(final String ryaInstanceName) {
        }
    });
    try {
        // Start the source
        source.startAndWait();
        // Wait for that first listener to indicate the topic was created. This means that one has been cached.
        assertTrue(created.await(5, TimeUnit.SECONDS));
        // Register a second listener that counts down when that same topic is encountered. This means the
        // newly subscribed listener was notified with the already known change log.
        final CountDownLatch newListenerCreated = new CountDownLatch(1);
        source.subscribe(new SourceListener() {

            @Override
            public void notifyCreate(final String ryaInstanceName, final QueryChangeLog log) {
                assertEquals(ryaInstance, ryaInstanceName);
                newListenerCreated.countDown();
            }

            @Override
            public void notifyDelete(final String ryaInstanceName) {
            }
        });
        assertTrue(newListenerCreated.await(5, TimeUnit.SECONDS));
    } finally {
        source.stopAndWait();
    }
}
Also used : SourceListener(org.apache.rya.streams.querymanager.QueryChangeLogSource.SourceListener) QueryChangeLogSource(org.apache.rya.streams.querymanager.QueryChangeLogSource) CountDownLatch(java.util.concurrent.CountDownLatch) QueryChangeLog(org.apache.rya.streams.api.queries.QueryChangeLog) Test(org.junit.Test)

Example 7 with SourceListener

use of org.apache.rya.streams.querymanager.QueryChangeLogSource.SourceListener in project incubator-rya by apache.

the class QueryManagerTest method testUpdateQuery.

/**
 * Tests when the query manager is notified to update an existing query, the
 * query is stopped.
 */
@Test
public void testUpdateQuery() throws Exception {
    // The new QueryChangeLog
    final QueryChangeLog newChangeLog = new InMemoryQueryChangeLog();
    final StreamsQuery query = new StreamsQuery(UUID.randomUUID(), "some query", true, false);
    final String ryaInstance = "ryaTestInstance";
    // when the query executor is told to start the test query on the test
    // rya instance, count down on the countdown latch
    final QueryExecutor qe = mock(QueryExecutor.class);
    when(qe.isRunning()).thenReturn(true);
    final CountDownLatch queryStarted = new CountDownLatch(1);
    final CountDownLatch queryDeleted = new CountDownLatch(1);
    doAnswer(invocation -> {
        queryDeleted.countDown();
        return null;
    }).when(qe).stopQuery(query.getQueryId());
    final QueryChangeLogSource source = mock(QueryChangeLogSource.class);
    // when the query executor is told to start the test query on the test
    // rya instance, count down on the countdown latch
    doAnswer(invocation -> {
        queryStarted.countDown();
        return null;
    }).when(qe).startQuery(eq(ryaInstance), eq(query));
    // When the QueryChangeLogSource is subscribed to in the QueryManager,
    // mock notify of a new QueryChangeLog
    // add the query, so it can be removed
    doAnswer(invocation -> {
        // The listener created by the Query Manager
        final SourceListener listener = (SourceListener) invocation.getArguments()[0];
        listener.notifyCreate(ryaInstance, newChangeLog);
        Thread.sleep(1000);
        newChangeLog.write(QueryChange.create(query.getQueryId(), query.getSparql(), query.isActive(), query.isInsert()));
        queryStarted.await(5, TimeUnit.SECONDS);
        newChangeLog.write(QueryChange.update(query.getQueryId(), false));
        return null;
    }).when(source).subscribe(any(SourceListener.class));
    final QueryManager qm = new QueryManager(qe, source, 50, TimeUnit.MILLISECONDS);
    try {
        qm.startAndWait();
        queryDeleted.await(10, TimeUnit.SECONDS);
        verify(qe).stopQuery(query.getQueryId());
    } finally {
        qm.stopAndWait();
    }
}
Also used : InMemoryQueryChangeLog(org.apache.rya.streams.api.queries.InMemoryQueryChangeLog) SourceListener(org.apache.rya.streams.querymanager.QueryChangeLogSource.SourceListener) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) CountDownLatch(java.util.concurrent.CountDownLatch) InMemoryQueryChangeLog(org.apache.rya.streams.api.queries.InMemoryQueryChangeLog) QueryChangeLog(org.apache.rya.streams.api.queries.QueryChangeLog) Test(org.junit.Test)

Example 8 with SourceListener

use of org.apache.rya.streams.querymanager.QueryChangeLogSource.SourceListener in project incubator-rya by apache.

the class KafkaQueryChangeLogSourceIT method discoverNewLogs.

@Test
public void discoverNewLogs() throws Exception {
    // Create the source.
    final QueryChangeLogSource source = new KafkaQueryChangeLogSource(kafka.getKafkaHostname(), Integer.parseInt(kafka.getKafkaPort()), Scheduler.newFixedRateSchedule(0, 100, TimeUnit.MILLISECONDS));
    // Register a listener that counts down a latch if it sees the new topic.
    final String ryaInstance = UUID.randomUUID().toString();
    final CountDownLatch created = new CountDownLatch(1);
    source.subscribe(new SourceListener() {

        @Override
        public void notifyCreate(final String ryaInstanceName, final QueryChangeLog log) {
            assertEquals(ryaInstance, ryaInstanceName);
            created.countDown();
        }

        @Override
        public void notifyDelete(final String ryaInstanceName) {
        }
    });
    try {
        // Start the source.
        source.startAndWait();
        // Wait twice the polling duration to ensure it iterates at least once.
        Thread.sleep(200);
        // Create a valid Query Change Log topic.
        final String topic = KafkaTopics.queryChangeLogTopic(ryaInstance);
        kafka.createTopic(topic);
        // If the latch isn't counted down, then fail the test.
        assertTrue(created.await(5, TimeUnit.SECONDS));
    } finally {
        source.stopAndWait();
    }
}
Also used : SourceListener(org.apache.rya.streams.querymanager.QueryChangeLogSource.SourceListener) QueryChangeLogSource(org.apache.rya.streams.querymanager.QueryChangeLogSource) CountDownLatch(java.util.concurrent.CountDownLatch) QueryChangeLog(org.apache.rya.streams.api.queries.QueryChangeLog) Test(org.junit.Test)

Aggregations

QueryChangeLog (org.apache.rya.streams.api.queries.QueryChangeLog)8 SourceListener (org.apache.rya.streams.querymanager.QueryChangeLogSource.SourceListener)8 Test (org.junit.Test)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 QueryChangeLogSource (org.apache.rya.streams.querymanager.QueryChangeLogSource)5 StreamsQuery (org.apache.rya.streams.api.entity.StreamsQuery)3 InMemoryQueryChangeLog (org.apache.rya.streams.api.queries.InMemoryQueryChangeLog)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1