Search in sources :

Example 1 with QueryChangeLog

use of org.apache.rya.streams.api.queries.QueryChangeLog in project incubator-rya by apache.

the class QueryManagerTest method testCreateQuery.

/**
 * Tests when the query manager is notified to create a new query, the query
 * is created and started.
 */
@Test
public void testCreateQuery() throws Exception {
    // The new QueryChangeLog
    final QueryChangeLog newChangeLog = new InMemoryQueryChangeLog();
    final String ryaInstance = "ryaTestInstance";
    final StreamsQuery query = new StreamsQuery(UUID.randomUUID(), "some query", true, false);
    // 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);
    doAnswer(invocation -> {
        queryStarted.countDown();
        return null;
    }).when(qe).startQuery(eq(ryaInstance), eq(query));
    final QueryChangeLogSource source = mock(QueryChangeLogSource.class);
    // When the QueryChangeLogSource is subscribed to in the QueryManager, mock notify of a new QueryChangeLog
    doAnswer(invocation -> {
        // The listener created by the Query Manager
        final SourceListener listener = (SourceListener) invocation.getArguments()[0];
        listener.notifyCreate(ryaInstance, newChangeLog);
        newChangeLog.write(QueryChange.create(query.getQueryId(), query.getSparql(), query.isActive(), query.isInsert()));
        return null;
    }).when(source).subscribe(any(SourceListener.class));
    final QueryManager qm = new QueryManager(qe, source, 50, TimeUnit.MILLISECONDS);
    try {
        qm.startAndWait();
        queryStarted.await(5, TimeUnit.SECONDS);
        verify(qe).startQuery(ryaInstance, query);
    } 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 2 with QueryChangeLog

use of org.apache.rya.streams.api.queries.QueryChangeLog in project incubator-rya by apache.

the class QueryManagerTest method testDeleteQuery.

/**
 * Tests when the query manager is notified to delete a new query, the query
 * is stopped and deleted.
 */
@Test
public void testDeleteQuery() 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.delete(query.getQueryId()));
        return null;
    }).when(source).subscribe(any(SourceListener.class));
    final QueryManager qm = new QueryManager(qe, source, 50, TimeUnit.MILLISECONDS);
    try {
        qm.startAndWait();
        queryDeleted.await(5, 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 3 with QueryChangeLog

use of org.apache.rya.streams.api.queries.QueryChangeLog in project incubator-rya by apache.

the class KafkaQueryChangeLogSourceIT method unsubscribedDoesNotReceiveNotifications.

@Test
public void unsubscribedDoesNotReceiveNotifications() throws Exception {
    // Create the source.
    final QueryChangeLogSource source = new KafkaQueryChangeLogSource(kafka.getKafkaHostname(), Integer.parseInt(kafka.getKafkaPort()), Scheduler.newFixedRateSchedule(0, 100, TimeUnit.MILLISECONDS));
    try {
        // Start the source.
        source.startAndWait();
        // Create a listener that flips a boolean to true when it is notified.
        final AtomicBoolean notified = new AtomicBoolean(false);
        final SourceListener listener = new SourceListener() {

            @Override
            public void notifyCreate(final String ryaInstanceName, final QueryChangeLog log) {
                notified.set(true);
            }

            @Override
            public void notifyDelete(final String ryaInstanceName) {
                notified.set(true);
            }
        };
        // Register and then unregister it.
        source.subscribe(listener);
        source.unsubscribe(listener);
        // Create a topic.
        final String ryaInstance = UUID.randomUUID().toString();
        final String topic = KafkaTopics.queryChangeLogTopic(ryaInstance);
        kafka.createTopic(topic);
        // Wait longer than the polling time for the listener to be notified.
        Thread.sleep(300);
        // Show the boolean was never flipped to true.
        assertFalse(notified.get());
    } finally {
        source.stopAndWait();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SourceListener(org.apache.rya.streams.querymanager.QueryChangeLogSource.SourceListener) QueryChangeLogSource(org.apache.rya.streams.querymanager.QueryChangeLogSource) QueryChangeLog(org.apache.rya.streams.api.queries.QueryChangeLog) Test(org.junit.Test)

Example 4 with QueryChangeLog

use of org.apache.rya.streams.api.queries.QueryChangeLog in project incubator-rya by apache.

the class KafkaQueryChangeLogSourceIT method discoverExistingLogs.

@Test
public void discoverExistingLogs() 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();
        // 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)

Example 5 with QueryChangeLog

use of org.apache.rya.streams.api.queries.QueryChangeLog in project incubator-rya by apache.

the class KafkaQueryChangeLogSourceIT method discoverLogDeletions.

@Test
public void discoverLogDeletions() 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 uses latches to indicate when the topic is created and deleted.
    final CountDownLatch created = new CountDownLatch(1);
    final CountDownLatch deleted = 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) {
            assertEquals(ryaInstance, ryaInstanceName);
            deleted.countDown();
        }
    });
    try {
        // Start the source
        source.startAndWait();
        // Wait for it to indicate the topic was created.
        assertTrue(created.await(5, TimeUnit.SECONDS));
        // Delete the topic.
        kafka.deleteTopic(topic);
        // If the latch isn't counted down, then fail the test.
        assertTrue(deleted.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)21 Test (org.junit.Test)11 InMemoryQueryRepository (org.apache.rya.streams.api.queries.InMemoryQueryRepository)10 StreamsQuery (org.apache.rya.streams.api.entity.StreamsQuery)9 SourceListener (org.apache.rya.streams.querymanager.QueryChangeLogSource.SourceListener)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 InMemoryQueryChangeLog (org.apache.rya.streams.api.queries.InMemoryQueryChangeLog)6 QueryRepository (org.apache.rya.streams.api.queries.QueryRepository)6 JCommander (com.beust.jcommander.JCommander)5 ParameterException (com.beust.jcommander.ParameterException)5 Scheduler (com.google.common.util.concurrent.AbstractScheduledService.Scheduler)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 QueryChange (org.apache.rya.streams.api.queries.QueryChange)5 KafkaQueryChangeLog (org.apache.rya.streams.kafka.queries.KafkaQueryChangeLog)5 QueryChangeLogSource (org.apache.rya.streams.querymanager.QueryChangeLogSource)5 UUID (java.util.UUID)4 Before (org.junit.Before)4 HashSet (java.util.HashSet)3 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)3 RyaStreamsException (org.apache.rya.streams.api.exception.RyaStreamsException)3