use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.
the class LocalQueryExecutorTest method getRunningQueryIds_noneStopped.
@Test
public void getRunningQueryIds_noneStopped() 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);
final StreamsQuery query3 = new StreamsQuery(UUID.randomUUID(), "SELECT * WHERE { ?a ?b ?c. }", true, false);
// Mock the streams factory so that we can figure out what is started.
final KafkaStreamsFactory jobFactory = mock(KafkaStreamsFactory.class);
when(jobFactory.make(eq(ryaInstance), eq(query1))).thenReturn(mock(KafkaStreams.class));
when(jobFactory.make(eq(ryaInstance), eq(query2))).thenReturn(mock(KafkaStreams.class));
when(jobFactory.make(eq(ryaInstance), eq(query3))).thenReturn(mock(KafkaStreams.class));
// Start the executor that will be tested.
final QueryExecutor executor = new LocalQueryExecutor(mock(CreateKafkaTopic.class), jobFactory);
executor.startAndWait();
try {
// Start the queries.
executor.startQuery(ryaInstance, query1);
executor.startQuery(ryaInstance, query2);
executor.startQuery(ryaInstance, query3);
// All of those query IDs should be reported as running.
final Set<UUID> expected = Sets.newHashSet(query1.getQueryId(), query2.getQueryId(), query3.getQueryId());
assertEquals(expected, executor.getRunningQueryIds());
} finally {
executor.stopAndWait();
}
}
use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.
the class LocalQueryExecutorTest method getRunningQueryIds_stoppedNoLongerListed.
@Test
public void getRunningQueryIds_stoppedNoLongerListed() 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);
final StreamsQuery query3 = new StreamsQuery(UUID.randomUUID(), "SELECT * WHERE { ?a ?b ?c. }", true, false);
// Mock the streams factory so that we can figure out what is started.
final KafkaStreamsFactory jobFactory = mock(KafkaStreamsFactory.class);
when(jobFactory.make(eq(ryaInstance), eq(query1))).thenReturn(mock(KafkaStreams.class));
when(jobFactory.make(eq(ryaInstance), eq(query2))).thenReturn(mock(KafkaStreams.class));
when(jobFactory.make(eq(ryaInstance), eq(query3))).thenReturn(mock(KafkaStreams.class));
// Start the executor that will be tested.
final QueryExecutor executor = new LocalQueryExecutor(mock(CreateKafkaTopic.class), jobFactory);
executor.startAndWait();
try {
// Start the queries.
executor.startQuery(ryaInstance, query1);
executor.startQuery(ryaInstance, query2);
executor.startQuery(ryaInstance, query3);
// Stop the second query.
executor.stopQuery(query2.getQueryId());
// Only the first and third queries are running.
final Set<UUID> expected = Sets.newHashSet(query1.getQueryId(), query3.getQueryId());
assertEquals(expected, executor.getRunningQueryIds());
} finally {
executor.stopAndWait();
}
}
use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.
the class LogEventWorkerTest method nofity_logCreated_exists.
@Test
public void nofity_logCreated_exists() 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));
// 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);
// Say the same log was created a second time.
logEventQueue.offer(createLogEvent);
// Show that only a single unit of work was added for the log. This indicates the
// second message was effectively skipped as it would have add its work added twice otherwise.
final Set<QueryEvent> expectedEvents = new HashSet<>();
expectedEvents.add(QueryEvent.executing("rya", new StreamsQuery(firstQueryId, "select * where { ?a ?b ?c . }", true, false)));
final Set<QueryEvent> queryEvents = new HashSet<>();
queryEvents.add(queryEventQueue.poll(500, TimeUnit.MILLISECONDS));
assertNull(queryEventQueue.poll(500, TimeUnit.MILLISECONDS));
assertEquals(expectedEvents, queryEvents);
} finally {
shutdownSignal.set(true);
logEventWorker.join();
}
}
use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.
the class KafkaRunQueryIT method runQuery.
@Test
public void runQuery() throws Exception {
// Setup some constant that will be used through the test.
final String ryaInstance = UUID.randomUUID().toString();
final String statementsTopic = KafkaTopics.statementsTopic(ryaInstance);
// This query is completely in memory, so it doesn't need to be closed.
final QueryRepository queries = new InMemoryQueryRepository(new InMemoryQueryChangeLog(), Scheduler.newFixedRateSchedule(0L, 5, TimeUnit.SECONDS));
// Add the query to the query repository.
final StreamsQuery sQuery = queries.add("SELECT * WHERE { ?person <urn:worksAt> ?business . }", true, false);
final UUID queryId = sQuery.getQueryId();
final String resultsTopic = KafkaTopics.queryResultsTopic(ryaInstance, queryId);
// The thread that will run the tested interactor.
final Thread testThread = new Thread() {
@Override
public void run() {
final RunQuery runQuery = new KafkaRunQuery(kafka.getKafkaHostname(), kafka.getKafkaPort(), statementsTopic, resultsTopic, queries, new TopologyFactory());
try {
runQuery.run(queryId);
} catch (final RyaStreamsException e) {
// Do nothing. Test will still fail because the expected results will be missing.
}
}
};
// Create the topics.
kafka.createTopic(statementsTopic);
kafka.createTopic(resultsTopic);
// Create the statements that will be loaded.
final ValueFactory vf = new ValueFactoryImpl();
final List<VisibilityStatement> statements = new ArrayList<>();
statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Alice"), vf.createURI("urn:worksAt"), vf.createURI("urn:BurgerJoint")), "a"));
statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Bob"), vf.createURI("urn:worksAt"), vf.createURI("urn:TacoShop")), "a"));
statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Charlie"), vf.createURI("urn:worksAt"), vf.createURI("urn:TacoShop")), "a"));
// Create the expected results.
final List<VisibilityBindingSet> expected = new ArrayList<>();
MapBindingSet bs = new MapBindingSet();
bs.addBinding("person", vf.createURI("urn:Alice"));
bs.addBinding("business", vf.createURI("urn:BurgerJoint"));
expected.add(new VisibilityBindingSet(bs, "a"));
bs = new MapBindingSet();
bs.addBinding("person", vf.createURI("urn:Bob"));
bs.addBinding("business", vf.createURI("urn:TacoShop"));
expected.add(new VisibilityBindingSet(bs, "a"));
bs = new MapBindingSet();
bs.addBinding("person", vf.createURI("urn:Charlie"));
bs.addBinding("business", vf.createURI("urn:TacoShop"));
expected.add(new VisibilityBindingSet(bs, "a"));
// Execute the test. This will result in a set of results that were read from the results topic.
final List<VisibilityBindingSet> results;
try {
// Wait for the program to start.
testThread.start();
Thread.sleep(2000);
// Write some statements to the program.
final LoadStatements loadStatements = new KafkaLoadStatements(statementsTopic, producer);
loadStatements.fromCollection(statements);
// Read the output of the streams program.
consumer.subscribe(Lists.newArrayList(resultsTopic));
results = KafkaTestUtil.pollForResults(500, 6, 3, consumer);
} finally {
// Tear down the test.
testThread.interrupt();
testThread.join(3000);
}
// Show the read results matched the expected ones.
assertEquals(expected, results);
}
use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.
the class RyaStreamsCommandsTest method startQuery_alreadyRunning.
@Test
public void startQuery_alreadyRunning() throws Exception {
// Mock the object that performs the rya streams operation.
final RyaStreamsClient mockClient = mock(RyaStreamsClient.class);
final StartQuery startQuery = mock(StartQuery.class);
when(mockClient.getStartQuery()).thenReturn(startQuery);
final GetQuery getQuery = mock(GetQuery.class);
when(mockClient.getGetQuery()).thenReturn(getQuery);
// Mock a shell state and connect it to a Rya instance.
final SharedShellState state = new SharedShellState();
state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mock(RyaClient.class));
state.connectedToInstance("unitTest");
state.connectedToRyaStreams(mockClient);
// Report the query as running.
final UUID queryId = UUID.randomUUID();
when(getQuery.getQuery(eq(queryId))).thenReturn(java.util.Optional.of(new StreamsQuery(queryId, "sparql", true, false)));
// Execute the command.
final RyaStreamsCommands commands = new RyaStreamsCommands(state, mock(SparqlPrompt.class), mock(ConsolePrinter.class));
final String message = commands.startQuery(queryId.toString());
// Verify the interactor was not invoked.
verify(startQuery, never()).start(queryId);
// Verify a message is printed to the user.
final String expected = "That query is already running.";
assertEquals(expected, message);
}
Aggregations