Search in sources :

Example 1 with StreamsQuery

use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.

the class QueryEventWorkGeneratorTest method notifyUpdate_isActive.

@Test
public void notifyUpdate_isActive() throws Exception {
    // The signal that will kill the notifying thread.
    final AtomicBoolean shutdownSignal = new AtomicBoolean(false);
    // The queue generated work is offered to.
    final BlockingQueue<QueryEvent> queue = new ArrayBlockingQueue<>(1);
    // The listener that will perform the QueryEventWorkGenerator work.
    final CountDownLatch latch = new CountDownLatch(1);
    latch.countDown();
    final QueryEventWorkGenerator generator = new QueryEventWorkGenerator("rya", latch, queue, 50, TimeUnit.MILLISECONDS, shutdownSignal);
    // A thread that will attempt to notify the generator with an update query change.
    final UUID queryId = UUID.randomUUID();
    final StreamsQuery query = new StreamsQuery(queryId, "query", true, false);
    final Thread notifyThread = new Thread(() -> {
        final QueryChange change = QueryChange.update(queryId, true);
        final ChangeLogEntry<QueryChange> entry = new ChangeLogEntry<>(0, change);
        generator.notify(entry, Optional.of(query));
    });
    // Start the thread.
    notifyThread.start();
    try {
        // Show work was added to the queue and the notifying thread died.
        final QueryEvent event = queue.poll(500, TimeUnit.MILLISECONDS);
        final QueryEvent expected = QueryEvent.executing("rya", new StreamsQuery(queryId, query.getSparql(), query.isActive(), query.isInsert()));
        assertEquals(expected, event);
    } finally {
        shutdownSignal.set(true);
        notifyThread.join();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) QueryEventWorkGenerator(org.apache.rya.streams.querymanager.QueryManager.QueryEventWorkGenerator) QueryChange(org.apache.rya.streams.api.queries.QueryChange) ChangeLogEntry(org.apache.rya.streams.api.queries.ChangeLogEntry) QueryEvent(org.apache.rya.streams.querymanager.QueryManager.QueryEvent) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID) Test(org.junit.Test)

Example 2 with StreamsQuery

use of org.apache.rya.streams.api.entity.StreamsQuery 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 3 with StreamsQuery

use of org.apache.rya.streams.api.entity.StreamsQuery 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 4 with StreamsQuery

use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.

the class LocalQueryExecutorTest method stopAll.

@Test
public void stopAll() throws Exception {
    // Test values.
    final String ryaInstance1 = "rya1";
    final StreamsQuery query1 = new StreamsQuery(UUID.randomUUID(), "SELECT * WHERE { ?a ?b ?c. }", true, false);
    final String ryaInstance2 = "rya2";
    final StreamsQuery query2 = new StreamsQuery(UUID.randomUUID(), "SELECT * WHERE { ?a ?b ?c. }", true, false);
    // Mock the streams factory so that we can tell if the stop function is invoked by the executor.
    final KafkaStreamsFactory jobFactory = mock(KafkaStreamsFactory.class);
    final KafkaStreams queryJob1 = mock(KafkaStreams.class);
    when(jobFactory.make(eq(ryaInstance1), eq(query1))).thenReturn(queryJob1);
    final KafkaStreams queryJob2 = mock(KafkaStreams.class);
    when(jobFactory.make(eq(ryaInstance2), eq(query2))).thenReturn(queryJob2);
    // Start the executor that will be tested.
    final QueryExecutor executor = new LocalQueryExecutor(mock(CreateKafkaTopic.class), jobFactory);
    executor.startAndWait();
    try {
        // Tell the executor to start the queries.
        executor.startQuery(ryaInstance1, query1);
        executor.startQuery(ryaInstance2, query2);
        // Verify both are running.
        verify(queryJob1).start();
        verify(queryJob2).start();
        // Tell the executor to stop queries running under rya2.
        executor.stopAll(ryaInstance2);
        // Show the first query is still running, but the second isn't.
        verify(queryJob1, never()).close();
        verify(queryJob2).close();
    } finally {
        executor.stopAndWait();
    }
}
Also used : KafkaStreams(org.apache.kafka.streams.KafkaStreams) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) CreateKafkaTopic(org.apache.rya.streams.kafka.interactor.CreateKafkaTopic) QueryExecutor(org.apache.rya.streams.querymanager.QueryExecutor) KafkaStreamsFactory(org.apache.rya.streams.kafka.KafkaStreamsFactory) Test(org.junit.Test)

Example 5 with StreamsQuery

use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.

the class LocalQueryExecutorTest method startQuery.

@Test
public void startQuery() throws Exception {
    // Test values.
    final String ryaInstance = "rya";
    final StreamsQuery query = new StreamsQuery(UUID.randomUUID(), "SELECT * WHERE { ?a ?b ?c. }", true, false);
    // Mock the streams factory so that we can tell if the start function is invoked by the executor.
    final KafkaStreamsFactory jobFactory = mock(KafkaStreamsFactory.class);
    final KafkaStreams queryJob = mock(KafkaStreams.class);
    when(jobFactory.make(eq(ryaInstance), eq(query))).thenReturn(queryJob);
    // Start the executor that will be tested.
    final QueryExecutor executor = new LocalQueryExecutor(mock(CreateKafkaTopic.class), jobFactory);
    executor.startAndWait();
    try {
        // Tell the executor to start the query.
        executor.startQuery(ryaInstance, query);
        // Show a job was started for that query's ID.
        verify(queryJob).start();
    } finally {
        executor.stopAndWait();
    }
}
Also used : KafkaStreams(org.apache.kafka.streams.KafkaStreams) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) CreateKafkaTopic(org.apache.rya.streams.kafka.interactor.CreateKafkaTopic) QueryExecutor(org.apache.rya.streams.querymanager.QueryExecutor) KafkaStreamsFactory(org.apache.rya.streams.kafka.KafkaStreamsFactory) Test(org.junit.Test)

Aggregations

StreamsQuery (org.apache.rya.streams.api.entity.StreamsQuery)51 Test (org.junit.Test)40 UUID (java.util.UUID)24 RyaStreamsClient (org.apache.rya.streams.api.RyaStreamsClient)14 CountDownLatch (java.util.concurrent.CountDownLatch)10 RyaClient (org.apache.rya.api.client.RyaClient)10 AccumuloConnectionDetails (org.apache.rya.api.client.accumulo.AccumuloConnectionDetails)10 ConsolePrinter (org.apache.rya.shell.util.ConsolePrinter)10 SparqlPrompt (org.apache.rya.shell.util.SparqlPrompt)10 QueryChangeLog (org.apache.rya.streams.api.queries.QueryChangeLog)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 RyaStreamsException (org.apache.rya.streams.api.exception.RyaStreamsException)8 KafkaStreamsFactory (org.apache.rya.streams.kafka.KafkaStreamsFactory)8 CreateKafkaTopic (org.apache.rya.streams.kafka.interactor.CreateKafkaTopic)8 QueryExecutor (org.apache.rya.streams.querymanager.QueryExecutor)8 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)7 KafkaStreams (org.apache.kafka.streams.KafkaStreams)7 QueryEvent (org.apache.rya.streams.querymanager.QueryManager.QueryEvent)7 InMemoryQueryChangeLog (org.apache.rya.streams.api.queries.InMemoryQueryChangeLog)6 HashSet (java.util.HashSet)5