Search in sources :

Example 31 with StreamsQuery

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

the class LocalQueryExecutorTest method stopAll_noneForThatRyaInstance.

@Test
public void stopAll_noneForThatRyaInstance() throws Exception {
    // Test values.
    final String ryaInstance = "rya";
    final StreamsQuery query1 = new StreamsQuery(UUID.randomUUID(), "SELECT * WHERE { ?a ?b ?c. }", true, false);
    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);
    final KafkaStreams queryJob2 = mock(KafkaStreams.class);
    when(jobFactory.make(eq(ryaInstance), eq(query1))).thenReturn(queryJob1);
    when(jobFactory.make(eq(ryaInstance), 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(ryaInstance, query1);
        executor.startQuery(ryaInstance, query2);
        // Verify both are running.
        verify(queryJob1).start();
        verify(queryJob2).start();
        // Tell the executor to stop queries running under rya2.
        executor.stopAll("someOtherRyaInstance");
        // Show none of the queries were stopped.
        verify(queryJob1, never()).close();
        verify(queryJob2, never()).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 32 with StreamsQuery

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

the class LocalQueryExecutorTest method startQuery_serviceNotStarted.

@Test(expected = IllegalStateException.class)
public void startQuery_serviceNotStarted() throws Exception {
    final QueryExecutor executor = new LocalQueryExecutor(mock(CreateKafkaTopic.class), mock(KafkaStreamsFactory.class));
    executor.startQuery("rya", new StreamsQuery(UUID.randomUUID(), "query", true, false));
}
Also used : CreateKafkaTopic(org.apache.rya.streams.kafka.interactor.CreateKafkaTopic) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) QueryExecutor(org.apache.rya.streams.querymanager.QueryExecutor) KafkaStreamsFactory(org.apache.rya.streams.kafka.KafkaStreamsFactory) Test(org.junit.Test)

Example 33 with StreamsQuery

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

the class LocalQueryExecutorTest method stopQuery.

@Test
public void stopQuery() 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 stop 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);
        // Tell the executor to stop the query.
        executor.stopQuery(query.getQueryId());
        // Show a job was stopped for that query's ID.
        verify(queryJob).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 34 with StreamsQuery

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

the class StreamsQueryFormatter method format.

/**
 * Pretty formats a collection {@link StreamsQuery}s.
 * They will be sorted based on their Query IDs.
 *
 * @param queries - The queries to format. (not null)
 * @return The pretty formatted string.
 * @throws Exception A problem was encountered while pretty formatting the SPARQL.
 */
public static String format(final Collection<StreamsQuery> queries) throws Exception {
    requireNonNull(queries);
    if (queries.size() == 1) {
        return format(queries.iterator().next());
    }
    // Sort the queries based on their IDs.
    final List<StreamsQuery> sorted = Lists.newArrayList(queries);
    sorted.sort((query1, query2) -> {
        final String id1 = query1.getQueryId().toString();
        final String id2 = query2.getQueryId().toString();
        return id1.compareTo(id2);
    });
    // Format a list of the queries.
    final StringBuilder builder = new StringBuilder();
    builder.append("-----------------------------------------------\n");
    for (final StreamsQuery query : sorted) {
        builder.append(format(query));
        builder.append("-----------------------------------------------\n");
    }
    return builder.toString();
}
Also used : StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery)

Example 35 with StreamsQuery

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

the class LogEventWorkerTest method nofity_logCreated_doesNotExist.

@Test
public void nofity_logCreated_doesNotExist() throws Exception {
    // The signal that will kill the working thread.
    final AtomicBoolean shutdownSignal = new AtomicBoolean(false);
    // The queue used to feed work.
    final BlockingQueue<LogEvent> logEventQueue = new ArrayBlockingQueue<>(10);
    // The queue work is written to.
    final BlockingQueue<QueryEvent> queryEventQueue = new ArrayBlockingQueue<>(10);
    // The Query Change Log that will be watched.
    final QueryChangeLog changeLog = new InMemoryQueryChangeLog();
    // Write a message that indicates a new query should be active.
    final UUID firstQueryId = UUID.randomUUID();
    changeLog.write(QueryChange.create(firstQueryId, "select * where { ?a ?b ?c . }", true, false));
    // Write a message that adds an active query, but then makes it inactive. Because both of these
    // events are written to the log before the worker subscribes to the repository for updates, they
    // must result in a single query stopped event.
    final UUID secondQueryId = UUID.randomUUID();
    changeLog.write(QueryChange.create(secondQueryId, "select * where { ?d ?e ?f . }", true, false));
    changeLog.write(QueryChange.update(secondQueryId, false));
    // Start the worker that will be tested.
    final Thread logEventWorker = new Thread(new LogEventWorker(logEventQueue, queryEventQueue, 50, TimeUnit.MILLISECONDS, shutdownSignal));
    logEventWorker.start();
    try {
        // Write a unit of work that indicates a log was created.
        final LogEvent createLogEvent = LogEvent.create("rya", changeLog);
        logEventQueue.offer(createLogEvent);
        // We must see the following Query Events added to the work queue.
        // Query 1, executing.
        // Query 2, stopped.
        Set<QueryEvent> expectedEvents = new HashSet<>();
        expectedEvents.add(QueryEvent.executing("rya", new StreamsQuery(firstQueryId, "select * where { ?a ?b ?c . }", true, false)));
        expectedEvents.add(QueryEvent.stopped("rya", secondQueryId));
        Set<QueryEvent> queryEvents = new HashSet<>();
        queryEvents.add(queryEventQueue.poll(500, TimeUnit.MILLISECONDS));
        queryEvents.add(queryEventQueue.poll(500, TimeUnit.MILLISECONDS));
        assertEquals(expectedEvents, queryEvents);
        // Write an event to the change log that stops the first query.
        changeLog.write(QueryChange.update(firstQueryId, false));
        // Show it was also reflected in the changes.
        // Query 1, stopped.
        expectedEvents = new HashSet<>();
        expectedEvents.add(QueryEvent.stopped("rya", firstQueryId));
        queryEvents = new HashSet<>();
        queryEvents.add(queryEventQueue.poll(500, TimeUnit.MILLISECONDS));
        assertEquals(expectedEvents, queryEvents);
    } finally {
        shutdownSignal.set(true);
        logEventWorker.join();
    }
}
Also used : InMemoryQueryChangeLog(org.apache.rya.streams.api.queries.InMemoryQueryChangeLog) LogEvent(org.apache.rya.streams.querymanager.QueryManager.LogEvent) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) QueryEvent(org.apache.rya.streams.querymanager.QueryManager.QueryEvent) InMemoryQueryChangeLog(org.apache.rya.streams.api.queries.InMemoryQueryChangeLog) QueryChangeLog(org.apache.rya.streams.api.queries.QueryChangeLog) LogEventWorker(org.apache.rya.streams.querymanager.QueryManager.LogEventWorker) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) UUID(java.util.UUID) HashSet(java.util.HashSet) 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