use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.
the class QueryEventWorkGeneratorTest method notifyUpdate_isActive.
@Test
public void notifyUpdate_isActive() throws Exception {
// The signal that will kill the notifying thread.
final AtomicBoolean shutdownSignal = new AtomicBoolean(false);
// The queue generated work is offered to.
final BlockingQueue<QueryEvent> queue = new ArrayBlockingQueue<>(1);
// The listener that will perform the QueryEventWorkGenerator work.
final CountDownLatch latch = new CountDownLatch(1);
latch.countDown();
final QueryEventWorkGenerator generator = new QueryEventWorkGenerator("rya", latch, queue, 50, TimeUnit.MILLISECONDS, shutdownSignal);
// A thread that will attempt to notify the generator with an update query change.
final UUID queryId = UUID.randomUUID();
final StreamsQuery query = new StreamsQuery(queryId, "query", true, false);
final Thread notifyThread = new Thread(() -> {
final QueryChange change = QueryChange.update(queryId, true);
final ChangeLogEntry<QueryChange> entry = new ChangeLogEntry<>(0, change);
generator.notify(entry, Optional.of(query));
});
// Start the thread.
notifyThread.start();
try {
// Show work was added to the queue and the notifying thread died.
final QueryEvent event = queue.poll(500, TimeUnit.MILLISECONDS);
final QueryEvent expected = QueryEvent.executing("rya", new StreamsQuery(queryId, query.getSparql(), query.isActive(), query.isInsert()));
assertEquals(expected, event);
} finally {
shutdownSignal.set(true);
notifyThread.join();
}
}
use of org.apache.rya.streams.api.entity.StreamsQuery 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.entity.StreamsQuery 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.entity.StreamsQuery in project incubator-rya by apache.
the class LocalQueryExecutorTest method stopAll.
@Test
public void stopAll() throws Exception {
// Test values.
final String ryaInstance1 = "rya1";
final StreamsQuery query1 = new StreamsQuery(UUID.randomUUID(), "SELECT * WHERE { ?a ?b ?c. }", true, false);
final String ryaInstance2 = "rya2";
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);
when(jobFactory.make(eq(ryaInstance1), eq(query1))).thenReturn(queryJob1);
final KafkaStreams queryJob2 = mock(KafkaStreams.class);
when(jobFactory.make(eq(ryaInstance2), 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(ryaInstance1, query1);
executor.startQuery(ryaInstance2, query2);
// Verify both are running.
verify(queryJob1).start();
verify(queryJob2).start();
// Tell the executor to stop queries running under rya2.
executor.stopAll(ryaInstance2);
// Show the first query is still running, but the second isn't.
verify(queryJob1, never()).close();
verify(queryJob2).close();
} finally {
executor.stopAndWait();
}
}
use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.
the class LocalQueryExecutorTest method startQuery.
@Test
public void startQuery() 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 start 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);
// Show a job was started for that query's ID.
verify(queryJob).start();
} finally {
executor.stopAndWait();
}
}
Aggregations