use of org.apache.rya.streams.api.queries.QueryChangeLog in project incubator-rya by apache.
the class AddQueryCommandIT method setup.
@Before
public void setup() {
// Make sure the topic that the change log uses exists.
final String changeLogTopic = KafkaTopics.queryChangeLogTopic("" + ryaInstance);
kafka.createTopic(changeLogTopic);
// Setup the QueryRepository used by the test.
final Producer<?, QueryChange> queryProducer = KafkaTestUtil.makeProducer(kafka, StringSerializer.class, QueryChangeSerializer.class);
final Consumer<?, QueryChange> queryConsumer = KafkaTestUtil.fromStartConsumer(kafka, StringDeserializer.class, QueryChangeDeserializer.class);
final QueryChangeLog changeLog = new KafkaQueryChangeLog(queryProducer, queryConsumer, changeLogTopic);
queryRepo = new InMemoryQueryRepository(changeLog, Scheduler.newFixedRateSchedule(0L, 5, TimeUnit.SECONDS));
}
use of org.apache.rya.streams.api.queries.QueryChangeLog in project incubator-rya by apache.
the class ListQueryCommandIT method setup.
@Before
public void setup() {
// Make sure the topic that the change log uses exists.
final String changeLogTopic = KafkaTopics.queryChangeLogTopic("" + ryaInstance);
kafka.createTopic(changeLogTopic);
// Setup the QueryRepository used by the test.
final Producer<?, QueryChange> queryProducer = KafkaTestUtil.makeProducer(kafka, StringSerializer.class, QueryChangeSerializer.class);
final Consumer<?, QueryChange> queryConsumer = KafkaTestUtil.fromStartConsumer(kafka, StringDeserializer.class, QueryChangeDeserializer.class);
final QueryChangeLog changeLog = new KafkaQueryChangeLog(queryProducer, queryConsumer, changeLogTopic);
queryRepo = new InMemoryQueryRepository(changeLog, Scheduler.newFixedRateSchedule(0L, 5, TimeUnit.SECONDS));
}
use of org.apache.rya.streams.api.queries.QueryChangeLog 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();
}
}
use of org.apache.rya.streams.api.queries.QueryChangeLog in project incubator-rya by apache.
the class KafkaQueryChangeLogSourceIT method discoverNewLogs.
@Test
public void discoverNewLogs() throws Exception {
// Create the source.
final QueryChangeLogSource source = new KafkaQueryChangeLogSource(kafka.getKafkaHostname(), Integer.parseInt(kafka.getKafkaPort()), Scheduler.newFixedRateSchedule(0, 100, TimeUnit.MILLISECONDS));
// Register a listener that counts down a latch if it sees the new topic.
final String ryaInstance = UUID.randomUUID().toString();
final CountDownLatch created = new CountDownLatch(1);
source.subscribe(new SourceListener() {
@Override
public void notifyCreate(final String ryaInstanceName, final QueryChangeLog log) {
assertEquals(ryaInstance, ryaInstanceName);
created.countDown();
}
@Override
public void notifyDelete(final String ryaInstanceName) {
}
});
try {
// Start the source.
source.startAndWait();
// Wait twice the polling duration to ensure it iterates at least once.
Thread.sleep(200);
// Create a valid Query Change Log topic.
final String topic = KafkaTopics.queryChangeLogTopic(ryaInstance);
kafka.createTopic(topic);
// If the latch isn't counted down, then fail the test.
assertTrue(created.await(5, TimeUnit.SECONDS));
} finally {
source.stopAndWait();
}
}
use of org.apache.rya.streams.api.queries.QueryChangeLog 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