use of org.apache.rya.streams.api.queries.InMemoryQueryChangeLog 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();
}
}
use of org.apache.rya.streams.api.queries.InMemoryQueryChangeLog 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();
}
}
use of org.apache.rya.streams.api.queries.InMemoryQueryChangeLog 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.queries.InMemoryQueryChangeLog 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.queries.InMemoryQueryChangeLog in project incubator-rya by apache.
the class QueryManagerTest method testUpdateQuery.
/**
* Tests when the query manager is notified to update an existing query, the
* query is stopped.
*/
@Test
public void testUpdateQuery() 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.update(query.getQueryId(), false));
return null;
}).when(source).subscribe(any(SourceListener.class));
final QueryManager qm = new QueryManager(qe, source, 50, TimeUnit.MILLISECONDS);
try {
qm.startAndWait();
queryDeleted.await(10, TimeUnit.SECONDS);
verify(qe).stopQuery(query.getQueryId());
} finally {
qm.stopAndWait();
}
}
Aggregations