Search in sources :

Example 26 with StreamsQuery

use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.

the class RyaStreamsCommandsTest method listQueries.

@Test
public void listQueries() throws Exception {
    // Mock the object that performs the rya streams operation.
    final RyaStreamsClient mockClient = mock(RyaStreamsClient.class);
    final ListQueries listQueries = mock(ListQueries.class);
    when(mockClient.getListQueries()).thenReturn(listQueries);
    final Set<StreamsQuery> queries = Sets.newHashSet(new StreamsQuery(UUID.fromString("33333333-3333-3333-3333-333333333333"), "SELECT * WHERE { ?person <urn:worksAt> ?business . }", true, false), new StreamsQuery(UUID.fromString("11111111-1111-1111-1111-111111111111"), "SELECT * WHERE { ?a ?b ?c . }", true, false), new StreamsQuery(UUID.fromString("22222222-2222-2222-2222-222222222222"), "SELECT * WHERE { ?d ?e ?f . }", false, false));
    when(listQueries.all()).thenReturn(queries);
    // Mock a shell state and connect it to a Rya instance.
    final SharedShellState state = new SharedShellState();
    state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mock(RyaClient.class));
    state.connectedToInstance("unitTest");
    state.connectedToRyaStreams(mockClient);
    // Execute the command.
    final RyaStreamsCommands commands = new RyaStreamsCommands(state, mock(SparqlPrompt.class), mock(ConsolePrinter.class));
    final String message = commands.listQueries();
    // Verify the correct report is returned.
    final String expected = "-----------------------------------------------\n" + " Query ID: 11111111-1111-1111-1111-111111111111\n" + "Is Active: true\n" + "Is Insert: false\n" + "   SPARQL: select ?a ?b ?c\n" + "           where {\n" + "             ?a ?b ?c.\n" + "           }\n" + "-----------------------------------------------\n" + " Query ID: 22222222-2222-2222-2222-222222222222\n" + "Is Active: false\n" + "Is Insert: false\n" + "   SPARQL: select ?d ?e ?f\n" + "           where {\n" + "             ?d ?e ?f.\n" + "           }\n" + "-----------------------------------------------\n" + " Query ID: 33333333-3333-3333-3333-333333333333\n" + "Is Active: true\n" + "Is Insert: false\n" + "   SPARQL: select ?person ?business\n" + "           where {\n" + "             ?person <urn:worksAt> ?business.\n" + "           }\n" + "-----------------------------------------------\n";
    assertEquals(expected, message);
}
Also used : RyaStreamsClient(org.apache.rya.streams.api.RyaStreamsClient) ConsolePrinter(org.apache.rya.shell.util.ConsolePrinter) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) AccumuloConnectionDetails(org.apache.rya.api.client.accumulo.AccumuloConnectionDetails) SparqlPrompt(org.apache.rya.shell.util.SparqlPrompt) ListQueries(org.apache.rya.streams.api.interactor.ListQueries) RyaClient(org.apache.rya.api.client.RyaClient) Test(org.junit.Test)

Example 27 with StreamsQuery

use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.

the class QueryEventWorkGeneratorTest method notifyUpdate_isNotActive.

@Test
public void notifyUpdate_isNotActive() 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", false, false);
    final Thread notifyThread = new Thread(() -> {
        final QueryChange change = QueryChange.update(queryId, false);
        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.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) 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 28 with StreamsQuery

use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.

the class QueryEventWorkGeneratorTest method notifyCreate.

@Test
public void notifyCreate() 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 created query.
    final UUID queryId = UUID.randomUUID();
    final StreamsQuery query = new StreamsQuery(queryId, "query", true, false);
    final Thread notifyThread = new Thread(() -> {
        final QueryChange change = QueryChange.create(queryId, query.getSparql(), query.isActive(), query.isInsert());
        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 29 with StreamsQuery

use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.

the class QueryEventWorkGeneratorTest method waitsForSubscriptionWork.

@Test
public void waitsForSubscriptionWork() 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);
    final QueryEventWorkGenerator generator = new QueryEventWorkGenerator("rya", latch, queue, 50, TimeUnit.MILLISECONDS, shutdownSignal);
    // A thread that will attempt to notify the generator with a created query.
    final UUID queryId = UUID.randomUUID();
    final StreamsQuery query = new StreamsQuery(queryId, "query", true, false);
    final Thread notifyThread = new Thread(() -> {
        final QueryChange change = QueryChange.create(queryId, query.getSparql(), query.isActive(), query.isInsert());
        final ChangeLogEntry<QueryChange> entry = new ChangeLogEntry<>(0, change);
        generator.notify(entry, Optional.of(query));
    });
    // Start the thread.
    notifyThread.start();
    try {
        // Wait longer than the blocking period and show the thread is still alive and nothing has been added
        // to the work queue.
        Thread.sleep(150);
        assertTrue(notifyThread.isAlive());
        // Count down the latch.
        latch.countDown();
        // 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 30 with StreamsQuery

use of org.apache.rya.streams.api.entity.StreamsQuery 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();
    }
}
Also used : InMemoryQueryChangeLog(org.apache.rya.streams.api.queries.InMemoryQueryChangeLog) SourceListener(org.apache.rya.streams.querymanager.QueryChangeLogSource.SourceListener) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) CountDownLatch(java.util.concurrent.CountDownLatch) InMemoryQueryChangeLog(org.apache.rya.streams.api.queries.InMemoryQueryChangeLog) QueryChangeLog(org.apache.rya.streams.api.queries.QueryChangeLog) Test(org.junit.Test)

Aggregations

StreamsQuery (org.apache.rya.streams.api.entity.StreamsQuery)51 Test (org.junit.Test)40 UUID (java.util.UUID)24 RyaStreamsClient (org.apache.rya.streams.api.RyaStreamsClient)14 CountDownLatch (java.util.concurrent.CountDownLatch)10 RyaClient (org.apache.rya.api.client.RyaClient)10 AccumuloConnectionDetails (org.apache.rya.api.client.accumulo.AccumuloConnectionDetails)10 ConsolePrinter (org.apache.rya.shell.util.ConsolePrinter)10 SparqlPrompt (org.apache.rya.shell.util.SparqlPrompt)10 QueryChangeLog (org.apache.rya.streams.api.queries.QueryChangeLog)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 RyaStreamsException (org.apache.rya.streams.api.exception.RyaStreamsException)8 KafkaStreamsFactory (org.apache.rya.streams.kafka.KafkaStreamsFactory)8 CreateKafkaTopic (org.apache.rya.streams.kafka.interactor.CreateKafkaTopic)8 QueryExecutor (org.apache.rya.streams.querymanager.QueryExecutor)8 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)7 KafkaStreams (org.apache.kafka.streams.KafkaStreams)7 QueryEvent (org.apache.rya.streams.querymanager.QueryManager.QueryEvent)7 InMemoryQueryChangeLog (org.apache.rya.streams.api.queries.InMemoryQueryChangeLog)6 HashSet (java.util.HashSet)5