Search in sources :

Example 16 with StreamsQuery

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

the class ListQueriesCommand method execute.

@Override
public void execute(final String[] args) throws ArgumentsException, ExecutionException {
    requireNonNull(args);
    // Parse the command line arguments.
    final KafkaParameters params = new KafkaParameters();
    try {
        new JCommander(params, args);
    } catch (final ParameterException e) {
        throw new ArgumentsException("Could not list the queries because of invalid command line parameters.", e);
    }
    // Create the Kafka backed QueryChangeLog.
    final String bootstrapServers = params.kafkaIP + ":" + params.kafkaPort;
    final String topic = KafkaTopics.queryChangeLogTopic(params.ryaInstance);
    final QueryChangeLog queryChangeLog = KafkaQueryChangeLogFactory.make(bootstrapServers, topic);
    // The ListQueries command doesn't use the scheduled service feature.
    final Scheduler scheduler = Scheduler.newFixedRateSchedule(0L, 5, TimeUnit.SECONDS);
    final QueryRepository queryRepo = new InMemoryQueryRepository(queryChangeLog, scheduler);
    // Execute the list queries command.
    try {
        final ListQueries listQueries = new DefaultListQueries(queryRepo);
        try {
            final Set<StreamsQuery> queries = listQueries.all();
            System.out.println(formatQueries(queries));
        } catch (final RyaStreamsException e) {
            System.err.println("Unable to retrieve the queries.");
            e.printStackTrace();
            System.exit(1);
        }
    } catch (final Exception e) {
        System.err.println("Problem encountered while closing the QueryRepository.");
        e.printStackTrace();
        System.exit(1);
    }
}
Also used : StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) Scheduler(com.google.common.util.concurrent.AbstractScheduledService.Scheduler) ListQueries(org.apache.rya.streams.api.interactor.ListQueries) DefaultListQueries(org.apache.rya.streams.api.interactor.defaults.DefaultListQueries) InMemoryQueryRepository(org.apache.rya.streams.api.queries.InMemoryQueryRepository) QueryChangeLog(org.apache.rya.streams.api.queries.QueryChangeLog) RyaStreamsException(org.apache.rya.streams.api.exception.RyaStreamsException) ParameterException(com.beust.jcommander.ParameterException) JCommander(com.beust.jcommander.JCommander) RyaStreamsException(org.apache.rya.streams.api.exception.RyaStreamsException) ParameterException(com.beust.jcommander.ParameterException) InMemoryQueryRepository(org.apache.rya.streams.api.queries.InMemoryQueryRepository) QueryRepository(org.apache.rya.streams.api.queries.QueryRepository) DefaultListQueries(org.apache.rya.streams.api.interactor.defaults.DefaultListQueries)

Example 17 with StreamsQuery

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

the class DeleteQueryCommandIT method longParams.

@Test
public void longParams() throws Exception {
    // Add a few queries to Rya Streams.
    queryRepo.add("query1", true, true);
    final UUID query2Id = queryRepo.add("query2", false, true).getQueryId();
    queryRepo.add("query3", true, false);
    // Show that all three of the queries were added.
    Set<StreamsQuery> queries = queryRepo.list();
    assertEquals(3, queries.size());
    // Delete query 2 using the delete query command.
    final String[] deleteArgs = new String[] { "--ryaInstance", "" + ryaInstance, "--kafkaHostname", kafka.getKafkaHostname(), "--kafkaPort", kafka.getKafkaPort(), "--queryID", query2Id.toString() };
    final DeleteQueryCommand deleteCommand = new DeleteQueryCommand();
    deleteCommand.execute(deleteArgs);
    // Show query2 was deleted.
    queries = queryRepo.list();
    assertEquals(2, queries.size());
    for (final StreamsQuery query : queries) {
        assertNotEquals(query2Id, query.getQueryId());
    }
}
Also used : StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) UUID(java.util.UUID) Test(org.junit.Test)

Example 18 with StreamsQuery

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

the class InMemoryQueryRepository method updateCache.

/**
 * Updates the {@link #queriesCache} to reflect the latest position within the {@link #changeLog}.
 */
private void updateCache() {
    log.trace("updateCache() - Enter");
    CloseableIteration<ChangeLogEntry<QueryChange>, QueryChangeLogException> it = null;
    try {
        // Iterate over everything since the last position that was handled within the change log.
        log.debug("Starting cache position:" + cachePosition);
        if (cachePosition.isPresent()) {
            it = changeLog.readFromPosition(cachePosition.get() + 1);
        } else {
            it = changeLog.readFromStart();
        }
        // Apply each change to the cache.
        while (it.hasNext()) {
            final ChangeLogEntry<QueryChange> entry = it.next();
            final QueryChange change = entry.getEntry();
            final UUID queryId = change.getQueryId();
            log.debug("Updating the cache to reflect:\n" + change);
            switch(change.getChangeType()) {
                case CREATE:
                    final StreamsQuery query = new StreamsQuery(queryId, change.getSparql().get(), change.getIsActive().get(), change.getIsInsert().get());
                    queriesCache.put(queryId, query);
                    break;
                case UPDATE:
                    if (queriesCache.containsKey(queryId)) {
                        final StreamsQuery old = queriesCache.get(queryId);
                        final StreamsQuery updated = new StreamsQuery(old.getQueryId(), old.getSparql(), change.getIsActive().get(), old.isInsert());
                        queriesCache.put(queryId, updated);
                    }
                    break;
                case DELETE:
                    queriesCache.remove(queryId);
                    break;
            }
            log.debug("Notifying listeners with the updated state.");
            final Optional<StreamsQuery> newQueryState = Optional.ofNullable(queriesCache.get(queryId));
            listeners.forEach(listener -> listener.notify(entry, newQueryState));
            cachePosition = Optional.of(entry.getPosition());
            log.debug("New chache position: " + cachePosition);
        }
    } catch (final QueryChangeLogException e) {
        // Rethrow the exception because the object the supplier tried to create could not be created.
        throw new RuntimeException("Could not update the cache of " + InMemoryQueryRepository.class.getName(), e);
    } finally {
        // Try to close the iteration if it was opened.
        try {
            if (it != null) {
                it.close();
            }
        } catch (final QueryChangeLogException e) {
            log.error("Could not close the " + CloseableIteration.class.getName(), e);
        }
        log.trace("updateCache() - Exit");
    }
}
Also used : CloseableIteration(info.aduna.iteration.CloseableIteration) QueryChangeLogException(org.apache.rya.streams.api.queries.QueryChangeLog.QueryChangeLogException) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) UUID(java.util.UUID)

Example 19 with StreamsQuery

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

the class InMemoryQueryRepositoryTest method updateListenerNotify_multiClient.

@Test
public void updateListenerNotify_multiClient() throws Exception {
    // Setup a totally in memory QueryRepository.
    final QueryChangeLog changeLog = new InMemoryQueryChangeLog();
    final QueryRepository queries = new InMemoryQueryRepository(changeLog, SCHEDULE);
    final QueryRepository queries2 = new InMemoryQueryRepository(changeLog, SCHEDULE);
    try {
        queries.startAndWait();
        queries2.startAndWait();
        // show listener on repo that query was added to is being notified of the new query.
        final CountDownLatch repo1Latch = new CountDownLatch(1);
        queries.subscribe((queryChangeEvent, newQueryState) -> {
            final ChangeLogEntry<QueryChange> expected = new ChangeLogEntry<>(0L, QueryChange.create(queryChangeEvent.getEntry().getQueryId(), "query 2", true, false));
            final Optional<StreamsQuery> expectedQueryState = Optional.of(new StreamsQuery(queryChangeEvent.getEntry().getQueryId(), "query 2", true, false));
            assertEquals(expected, queryChangeEvent);
            assertEquals(expectedQueryState, newQueryState);
            repo1Latch.countDown();
        });
        // show listener not on the repo that query was added to is being notified as well.
        final CountDownLatch repo2Latch = new CountDownLatch(1);
        queries2.subscribe((queryChangeEvent, newQueryState) -> {
            final ChangeLogEntry<QueryChange> expected = new ChangeLogEntry<>(0L, QueryChange.create(queryChangeEvent.getEntry().getQueryId(), "query 2", true, false));
            final Optional<StreamsQuery> expectedQueryState = Optional.of(new StreamsQuery(queryChangeEvent.getEntry().getQueryId(), "query 2", true, false));
            assertEquals(expected, queryChangeEvent);
            assertEquals(expectedQueryState, newQueryState);
            repo2Latch.countDown();
        });
        queries.add("query 2", true, false);
        assertTrue(repo1Latch.await(5, TimeUnit.SECONDS));
        assertTrue(repo2Latch.await(5, TimeUnit.SECONDS));
    } catch (final InterruptedException e) {
    } finally {
        queries.stop();
        queries2.stop();
    }
}
Also used : StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 20 with StreamsQuery

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

the class InMemoryQueryRepositoryTest method update.

@Test
public void update() throws Exception {
    // Setup a totally in memory QueryRepository.
    final QueryRepository queries = new InMemoryQueryRepository(new InMemoryQueryChangeLog(), SCHEDULE);
    // Add a query to it.
    final StreamsQuery query = queries.add("query 1", true, false);
    // Change the isActive state of that query.
    queries.updateIsActive(query.getQueryId(), false);
    // Show the fetched query matches the expected one.
    final Optional<StreamsQuery> fetched = queries.get(query.getQueryId());
    final StreamsQuery expected = new StreamsQuery(query.getQueryId(), query.getSparql(), false, false);
    assertEquals(expected, fetched.get());
}
Also used : StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) 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