Search in sources :

Example 6 with InMemoryQueryChangeLog

use of org.apache.rya.streams.api.queries.InMemoryQueryChangeLog 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();
    }
}
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 7 with InMemoryQueryChangeLog

use of org.apache.rya.streams.api.queries.InMemoryQueryChangeLog in project incubator-rya by apache.

the class LogEventWorkerTest method notify_logDeleted_exists.

@Test
public void notify_logDeleted_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();
    // 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);
        // Write a unit of work that indicates a log was deleted.
        logEventQueue.offer(LogEvent.delete("rya"));
        // Show that a single unit of work was created for deleting everything for "rya".
        assertEquals(QueryEvent.stopALL("rya"), queryEventQueue.poll(500, TimeUnit.MILLISECONDS));
        assertNull(queryEventQueue.poll(500, TimeUnit.MILLISECONDS));
    } finally {
        shutdownSignal.set(true);
        logEventWorker.join();
    }
}
Also used : InMemoryQueryChangeLog(org.apache.rya.streams.api.queries.InMemoryQueryChangeLog) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) LogEvent(org.apache.rya.streams.querymanager.QueryManager.LogEvent) 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) Test(org.junit.Test)

Aggregations

InMemoryQueryChangeLog (org.apache.rya.streams.api.queries.InMemoryQueryChangeLog)7 Test (org.junit.Test)7 StreamsQuery (org.apache.rya.streams.api.entity.StreamsQuery)6 QueryChangeLog (org.apache.rya.streams.api.queries.QueryChangeLog)6 UUID (java.util.UUID)3 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 SourceListener (org.apache.rya.streams.querymanager.QueryChangeLogSource.SourceListener)3 LogEvent (org.apache.rya.streams.querymanager.QueryManager.LogEvent)3 LogEventWorker (org.apache.rya.streams.querymanager.QueryManager.LogEventWorker)3 QueryEvent (org.apache.rya.streams.querymanager.QueryManager.QueryEvent)3 HashSet (java.util.HashSet)2 ArrayList (java.util.ArrayList)1 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)1 VisibilityStatement (org.apache.rya.api.model.VisibilityStatement)1 RyaStreamsException (org.apache.rya.streams.api.exception.RyaStreamsException)1 LoadStatements (org.apache.rya.streams.api.interactor.LoadStatements)1 RunQuery (org.apache.rya.streams.api.interactor.RunQuery)1 InMemoryQueryRepository (org.apache.rya.streams.api.queries.InMemoryQueryRepository)1