Search in sources :

Example 1 with QueryEvent

use of org.apache.rya.streams.querymanager.QueryManager.QueryEvent 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();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) QueryEventWorkGenerator(org.apache.rya.streams.querymanager.QueryManager.QueryEventWorkGenerator) QueryChange(org.apache.rya.streams.api.queries.QueryChange) ChangeLogEntry(org.apache.rya.streams.api.queries.ChangeLogEntry) QueryEvent(org.apache.rya.streams.querymanager.QueryManager.QueryEvent) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID) Test(org.junit.Test)

Example 2 with QueryEvent

use of org.apache.rya.streams.querymanager.QueryManager.QueryEvent in project incubator-rya by apache.

the class QueryEventWorkGeneratorTest method notifyDelete.

@Test
public void notifyDelete() 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 a deleted query.
    final UUID queryId = UUID.randomUUID();
    final Thread notifyThread = new Thread(() -> {
        final QueryChange change = QueryChange.delete(queryId);
        final ChangeLogEntry<QueryChange> entry = new ChangeLogEntry<>(0, change);
        generator.notify(entry, Optional.empty());
    });
    // 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.stopped("rya", queryId);
        assertEquals(expected, event);
    } finally {
        shutdownSignal.set(true);
        notifyThread.join();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) QueryEventWorkGenerator(org.apache.rya.streams.querymanager.QueryManager.QueryEventWorkGenerator) QueryChange(org.apache.rya.streams.api.queries.QueryChange) ChangeLogEntry(org.apache.rya.streams.api.queries.ChangeLogEntry) QueryEvent(org.apache.rya.streams.querymanager.QueryManager.QueryEvent) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID) Test(org.junit.Test)

Example 3 with QueryEvent

use of org.apache.rya.streams.querymanager.QueryManager.QueryEvent 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();
    }
}
Also used : InMemoryQueryChangeLog(org.apache.rya.streams.api.queries.InMemoryQueryChangeLog) LogEvent(org.apache.rya.streams.querymanager.QueryManager.LogEvent) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) QueryEvent(org.apache.rya.streams.querymanager.QueryManager.QueryEvent) InMemoryQueryChangeLog(org.apache.rya.streams.api.queries.InMemoryQueryChangeLog) QueryChangeLog(org.apache.rya.streams.api.queries.QueryChangeLog) LogEventWorker(org.apache.rya.streams.querymanager.QueryManager.LogEventWorker) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) UUID(java.util.UUID) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with QueryEvent

use of org.apache.rya.streams.querymanager.QueryManager.QueryEvent in project incubator-rya by apache.

the class QueryEventWorkerTest method stopAllWork.

@Test
public void stopAllWork() throws Exception {
    // The signal that will kill the working thread.
    final AtomicBoolean shutdownSignal = new AtomicBoolean(false);
    // The queue used to send the execute work to the thread.
    final BlockingQueue<QueryEvent> queue = new ArrayBlockingQueue<>(1);
    // The message that indicates all queries for a rya instance need to be stopped.
    final String ryaInstance = "rya";
    final QueryEvent stopAllEvent = QueryEvent.stopALL(ryaInstance);
    // Release a latch if the stopQuery method on the queryExecutor is invoked with the correct values.
    final CountDownLatch testMethodInvoked = new CountDownLatch(1);
    final QueryExecutor queryExecutor = mock(QueryExecutor.class);
    doAnswer(invocation -> {
        testMethodInvoked.countDown();
        return null;
    }).when(queryExecutor).stopAll(ryaInstance);
    final Thread queryEventWorker = new Thread(new QueryEventWorker(queue, queryExecutor, 50, TimeUnit.MILLISECONDS, shutdownSignal));
    try {
        // The thread that will perform the QueryEventWorker task.
        queryEventWorker.start();
        // Provide a message indicating a query needs to be executing.
        queue.put(stopAllEvent);
        // Verify the Query Executor was told to stop all the queries.
        assertTrue(testMethodInvoked.await(150, TimeUnit.MILLISECONDS));
    } finally {
        shutdownSignal.set(true);
        queryEventWorker.join();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) QueryEventWorker(org.apache.rya.streams.querymanager.QueryManager.QueryEventWorker) QueryEvent(org.apache.rya.streams.querymanager.QueryManager.QueryEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 5 with QueryEvent

use of org.apache.rya.streams.querymanager.QueryManager.QueryEvent in project incubator-rya by apache.

the class QueryEventWorkGeneratorTest method shutdownSignalKillsThread.

@Test
public void shutdownSignalKillsThread() 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 QueryEventWorkGenerator generator = new QueryEventWorkGenerator("rya", new CountDownLatch(1), queue, 50, TimeUnit.MILLISECONDS, shutdownSignal);
    // A thread that will attempt to notify the generator with a created query.
    final Thread notifyThread = new Thread(() -> {
        generator.notify(mock(ChangeLogEntry.class), Optional.empty());
    });
    // Fill the queue so that nothing may be offered to it.
    queue.offer(QueryEvent.stopALL("rya"));
    // Start the thread and show that it is still alive after the offer period.
    notifyThread.start();
    assertTrue(ThreadUtil.stillAlive(notifyThread, 200));
    // Set the shutdown signal to true and join the thread. If we were able to join, then it shut down.
    shutdownSignal.set(true);
    assertFalse(ThreadUtil.stillAlive(notifyThread, 1000));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) QueryEventWorkGenerator(org.apache.rya.streams.querymanager.QueryManager.QueryEventWorkGenerator) ChangeLogEntry(org.apache.rya.streams.api.queries.ChangeLogEntry) QueryEvent(org.apache.rya.streams.querymanager.QueryManager.QueryEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 QueryEvent (org.apache.rya.streams.querymanager.QueryManager.QueryEvent)13 Test (org.junit.Test)13 CountDownLatch (java.util.concurrent.CountDownLatch)9 UUID (java.util.UUID)8 StreamsQuery (org.apache.rya.streams.api.entity.StreamsQuery)7 ChangeLogEntry (org.apache.rya.streams.api.queries.ChangeLogEntry)6 QueryEventWorkGenerator (org.apache.rya.streams.querymanager.QueryManager.QueryEventWorkGenerator)6 QueryChange (org.apache.rya.streams.api.queries.QueryChange)5 LogEvent (org.apache.rya.streams.querymanager.QueryManager.LogEvent)4 LogEventWorker (org.apache.rya.streams.querymanager.QueryManager.LogEventWorker)4 InMemoryQueryChangeLog (org.apache.rya.streams.api.queries.InMemoryQueryChangeLog)3 QueryChangeLog (org.apache.rya.streams.api.queries.QueryChangeLog)3 QueryEventWorker (org.apache.rya.streams.querymanager.QueryManager.QueryEventWorker)3 HashSet (java.util.HashSet)2