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();
}
}
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));
}
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();
}
}
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();
}
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();
}
}
Aggregations