Search in sources :

Example 6 with StreamsQuery

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

the class LocalQueryExecutorTest method getRunningQueryIds_noneStopped.

@Test
public void getRunningQueryIds_noneStopped() throws Exception {
    // Test values.
    final String ryaInstance = "rya";
    final StreamsQuery query1 = new StreamsQuery(UUID.randomUUID(), "SELECT * WHERE { ?a ?b ?c. }", true, false);
    final StreamsQuery query2 = new StreamsQuery(UUID.randomUUID(), "SELECT * WHERE { ?a ?b ?c. }", true, false);
    final StreamsQuery query3 = new StreamsQuery(UUID.randomUUID(), "SELECT * WHERE { ?a ?b ?c. }", true, false);
    // Mock the streams factory so that we can figure out what is started.
    final KafkaStreamsFactory jobFactory = mock(KafkaStreamsFactory.class);
    when(jobFactory.make(eq(ryaInstance), eq(query1))).thenReturn(mock(KafkaStreams.class));
    when(jobFactory.make(eq(ryaInstance), eq(query2))).thenReturn(mock(KafkaStreams.class));
    when(jobFactory.make(eq(ryaInstance), eq(query3))).thenReturn(mock(KafkaStreams.class));
    // Start the executor that will be tested.
    final QueryExecutor executor = new LocalQueryExecutor(mock(CreateKafkaTopic.class), jobFactory);
    executor.startAndWait();
    try {
        // Start the queries.
        executor.startQuery(ryaInstance, query1);
        executor.startQuery(ryaInstance, query2);
        executor.startQuery(ryaInstance, query3);
        // All of those query IDs should be reported as running.
        final Set<UUID> expected = Sets.newHashSet(query1.getQueryId(), query2.getQueryId(), query3.getQueryId());
        assertEquals(expected, executor.getRunningQueryIds());
    } finally {
        executor.stopAndWait();
    }
}
Also used : KafkaStreams(org.apache.kafka.streams.KafkaStreams) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) CreateKafkaTopic(org.apache.rya.streams.kafka.interactor.CreateKafkaTopic) QueryExecutor(org.apache.rya.streams.querymanager.QueryExecutor) KafkaStreamsFactory(org.apache.rya.streams.kafka.KafkaStreamsFactory) UUID(java.util.UUID) Test(org.junit.Test)

Example 7 with StreamsQuery

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

the class LocalQueryExecutorTest method getRunningQueryIds_stoppedNoLongerListed.

@Test
public void getRunningQueryIds_stoppedNoLongerListed() throws Exception {
    // Test values.
    final String ryaInstance = "rya";
    final StreamsQuery query1 = new StreamsQuery(UUID.randomUUID(), "SELECT * WHERE { ?a ?b ?c. }", true, false);
    final StreamsQuery query2 = new StreamsQuery(UUID.randomUUID(), "SELECT * WHERE { ?a ?b ?c. }", true, false);
    final StreamsQuery query3 = new StreamsQuery(UUID.randomUUID(), "SELECT * WHERE { ?a ?b ?c. }", true, false);
    // Mock the streams factory so that we can figure out what is started.
    final KafkaStreamsFactory jobFactory = mock(KafkaStreamsFactory.class);
    when(jobFactory.make(eq(ryaInstance), eq(query1))).thenReturn(mock(KafkaStreams.class));
    when(jobFactory.make(eq(ryaInstance), eq(query2))).thenReturn(mock(KafkaStreams.class));
    when(jobFactory.make(eq(ryaInstance), eq(query3))).thenReturn(mock(KafkaStreams.class));
    // Start the executor that will be tested.
    final QueryExecutor executor = new LocalQueryExecutor(mock(CreateKafkaTopic.class), jobFactory);
    executor.startAndWait();
    try {
        // Start the queries.
        executor.startQuery(ryaInstance, query1);
        executor.startQuery(ryaInstance, query2);
        executor.startQuery(ryaInstance, query3);
        // Stop the second query.
        executor.stopQuery(query2.getQueryId());
        // Only the first and third queries are running.
        final Set<UUID> expected = Sets.newHashSet(query1.getQueryId(), query3.getQueryId());
        assertEquals(expected, executor.getRunningQueryIds());
    } finally {
        executor.stopAndWait();
    }
}
Also used : KafkaStreams(org.apache.kafka.streams.KafkaStreams) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) CreateKafkaTopic(org.apache.rya.streams.kafka.interactor.CreateKafkaTopic) QueryExecutor(org.apache.rya.streams.querymanager.QueryExecutor) KafkaStreamsFactory(org.apache.rya.streams.kafka.KafkaStreamsFactory) UUID(java.util.UUID) Test(org.junit.Test)

Example 8 with StreamsQuery

use of org.apache.rya.streams.api.entity.StreamsQuery 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 9 with StreamsQuery

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

the class KafkaRunQueryIT method runQuery.

@Test
public void runQuery() throws Exception {
    // Setup some constant that will be used through the test.
    final String ryaInstance = UUID.randomUUID().toString();
    final String statementsTopic = KafkaTopics.statementsTopic(ryaInstance);
    // This query is completely in memory, so it doesn't need to be closed.
    final QueryRepository queries = new InMemoryQueryRepository(new InMemoryQueryChangeLog(), Scheduler.newFixedRateSchedule(0L, 5, TimeUnit.SECONDS));
    // Add the query to the query repository.
    final StreamsQuery sQuery = queries.add("SELECT * WHERE { ?person <urn:worksAt> ?business . }", true, false);
    final UUID queryId = sQuery.getQueryId();
    final String resultsTopic = KafkaTopics.queryResultsTopic(ryaInstance, queryId);
    // The thread that will run the tested interactor.
    final Thread testThread = new Thread() {

        @Override
        public void run() {
            final RunQuery runQuery = new KafkaRunQuery(kafka.getKafkaHostname(), kafka.getKafkaPort(), statementsTopic, resultsTopic, queries, new TopologyFactory());
            try {
                runQuery.run(queryId);
            } catch (final RyaStreamsException e) {
            // Do nothing. Test will still fail because the expected results will be missing.
            }
        }
    };
    // Create the topics.
    kafka.createTopic(statementsTopic);
    kafka.createTopic(resultsTopic);
    // Create the statements that will be loaded.
    final ValueFactory vf = new ValueFactoryImpl();
    final List<VisibilityStatement> statements = new ArrayList<>();
    statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Alice"), vf.createURI("urn:worksAt"), vf.createURI("urn:BurgerJoint")), "a"));
    statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Bob"), vf.createURI("urn:worksAt"), vf.createURI("urn:TacoShop")), "a"));
    statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Charlie"), vf.createURI("urn:worksAt"), vf.createURI("urn:TacoShop")), "a"));
    // Create the expected results.
    final List<VisibilityBindingSet> expected = new ArrayList<>();
    MapBindingSet bs = new MapBindingSet();
    bs.addBinding("person", vf.createURI("urn:Alice"));
    bs.addBinding("business", vf.createURI("urn:BurgerJoint"));
    expected.add(new VisibilityBindingSet(bs, "a"));
    bs = new MapBindingSet();
    bs.addBinding("person", vf.createURI("urn:Bob"));
    bs.addBinding("business", vf.createURI("urn:TacoShop"));
    expected.add(new VisibilityBindingSet(bs, "a"));
    bs = new MapBindingSet();
    bs.addBinding("person", vf.createURI("urn:Charlie"));
    bs.addBinding("business", vf.createURI("urn:TacoShop"));
    expected.add(new VisibilityBindingSet(bs, "a"));
    // Execute the test. This will result in a set of results that were read from the results topic.
    final List<VisibilityBindingSet> results;
    try {
        // Wait for the program to start.
        testThread.start();
        Thread.sleep(2000);
        // Write some statements to the program.
        final LoadStatements loadStatements = new KafkaLoadStatements(statementsTopic, producer);
        loadStatements.fromCollection(statements);
        // Read the output of the streams program.
        consumer.subscribe(Lists.newArrayList(resultsTopic));
        results = KafkaTestUtil.pollForResults(500, 6, 3, consumer);
    } finally {
        // Tear down the test.
        testThread.interrupt();
        testThread.join(3000);
    }
    // Show the read results matched the expected ones.
    assertEquals(expected, results);
}
Also used : InMemoryQueryChangeLog(org.apache.rya.streams.api.queries.InMemoryQueryChangeLog) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) RunQuery(org.apache.rya.streams.api.interactor.RunQuery) LoadStatements(org.apache.rya.streams.api.interactor.LoadStatements) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ArrayList(java.util.ArrayList) TopologyFactory(org.apache.rya.streams.kafka.topology.TopologyFactory) ValueFactory(org.openrdf.model.ValueFactory) InMemoryQueryRepository(org.apache.rya.streams.api.queries.InMemoryQueryRepository) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) RyaStreamsException(org.apache.rya.streams.api.exception.RyaStreamsException) InMemoryQueryRepository(org.apache.rya.streams.api.queries.InMemoryQueryRepository) QueryRepository(org.apache.rya.streams.api.queries.QueryRepository) MapBindingSet(org.openrdf.query.impl.MapBindingSet) UUID(java.util.UUID) Test(org.junit.Test)

Example 10 with StreamsQuery

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

the class RyaStreamsCommandsTest method startQuery_alreadyRunning.

@Test
public void startQuery_alreadyRunning() throws Exception {
    // Mock the object that performs the rya streams operation.
    final RyaStreamsClient mockClient = mock(RyaStreamsClient.class);
    final StartQuery startQuery = mock(StartQuery.class);
    when(mockClient.getStartQuery()).thenReturn(startQuery);
    final GetQuery getQuery = mock(GetQuery.class);
    when(mockClient.getGetQuery()).thenReturn(getQuery);
    // 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);
    // Report the query as running.
    final UUID queryId = UUID.randomUUID();
    when(getQuery.getQuery(eq(queryId))).thenReturn(java.util.Optional.of(new StreamsQuery(queryId, "sparql", true, false)));
    // Execute the command.
    final RyaStreamsCommands commands = new RyaStreamsCommands(state, mock(SparqlPrompt.class), mock(ConsolePrinter.class));
    final String message = commands.startQuery(queryId.toString());
    // Verify the interactor was not invoked.
    verify(startQuery, never()).start(queryId);
    // Verify a message is printed to the user.
    final String expected = "That query is already running.";
    assertEquals(expected, message);
}
Also used : RyaStreamsClient(org.apache.rya.streams.api.RyaStreamsClient) ConsolePrinter(org.apache.rya.shell.util.ConsolePrinter) GetQuery(org.apache.rya.streams.api.interactor.GetQuery) AccumuloConnectionDetails(org.apache.rya.api.client.accumulo.AccumuloConnectionDetails) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) SparqlPrompt(org.apache.rya.shell.util.SparqlPrompt) RyaClient(org.apache.rya.api.client.RyaClient) UUID(java.util.UUID) StartQuery(org.apache.rya.streams.api.interactor.StartQuery) 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