Search in sources :

Example 1 with QueryChangeLogException

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

the class KafkaQueryChangeLogIT method readFromPosition_positionStartsBegining.

@Test
public void readFromPosition_positionStartsBegining() throws Exception {
    final List<QueryChange> expected = write10ChangesToChangeLog().subList(5, 10);
    // set the position to some non-0 position
    final TopicPartition partition = new TopicPartition(topic, 0);
    consumer.assign(Lists.newArrayList(partition));
    consumer.seekToBeginning(Lists.newArrayList(partition));
    final CloseableIteration<ChangeLogEntry<QueryChange>, QueryChangeLogException> iter = changeLog.readFromPosition(5L);
    final List<QueryChange> actual = new ArrayList<>();
    while (iter.hasNext()) {
        final ChangeLogEntry<QueryChange> entry = iter.next();
        actual.add(entry.getEntry());
    }
    assertEquals(expected, actual);
}
Also used : QueryChangeLogException(org.apache.rya.streams.api.queries.QueryChangeLog.QueryChangeLogException) TopicPartition(org.apache.kafka.common.TopicPartition) ArrayList(java.util.ArrayList) QueryChange(org.apache.rya.streams.api.queries.QueryChange) ChangeLogEntry(org.apache.rya.streams.api.queries.ChangeLogEntry) Test(org.junit.Test)

Example 2 with QueryChangeLogException

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

the class KafkaQueryChangeLogIT method readFromPosition_positionStartsNotBegining.

@Test
public void readFromPosition_positionStartsNotBegining() throws Exception {
    final List<QueryChange> expected = write10ChangesToChangeLog().subList(5, 10);
    // set the position to some non-0 position
    final TopicPartition partition = new TopicPartition(topic, 0);
    consumer.assign(Lists.newArrayList(partition));
    consumer.seekToEnd(Lists.newArrayList(partition));
    final CloseableIteration<ChangeLogEntry<QueryChange>, QueryChangeLogException> iter = changeLog.readFromPosition(5L);
    final List<QueryChange> actual = new ArrayList<>();
    while (iter.hasNext()) {
        final ChangeLogEntry<QueryChange> entry = iter.next();
        actual.add(entry.getEntry());
    }
    assertEquals(expected, actual);
}
Also used : QueryChangeLogException(org.apache.rya.streams.api.queries.QueryChangeLog.QueryChangeLogException) TopicPartition(org.apache.kafka.common.TopicPartition) ArrayList(java.util.ArrayList) QueryChange(org.apache.rya.streams.api.queries.QueryChange) ChangeLogEntry(org.apache.rya.streams.api.queries.ChangeLogEntry) Test(org.junit.Test)

Example 3 with QueryChangeLogException

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

the class KafkaQueryChangeLogIT method readFromBegining_positionStartsNotBegining.

@Test
public void readFromBegining_positionStartsNotBegining() throws Exception {
    final List<QueryChange> expected = write10ChangesToChangeLog();
    // set the position to some non-0 position
    final TopicPartition partition = new TopicPartition(topic, 0);
    consumer.assign(Lists.newArrayList(partition));
    consumer.seek(partition, 5L);
    final CloseableIteration<ChangeLogEntry<QueryChange>, QueryChangeLogException> iter = changeLog.readFromStart();
    final List<QueryChange> actual = new ArrayList<>();
    while (iter.hasNext()) {
        final ChangeLogEntry<QueryChange> entry = iter.next();
        actual.add(entry.getEntry());
    }
    assertEquals(expected, actual);
}
Also used : QueryChangeLogException(org.apache.rya.streams.api.queries.QueryChangeLog.QueryChangeLogException) TopicPartition(org.apache.kafka.common.TopicPartition) ArrayList(java.util.ArrayList) QueryChange(org.apache.rya.streams.api.queries.QueryChange) ChangeLogEntry(org.apache.rya.streams.api.queries.ChangeLogEntry) Test(org.junit.Test)

Example 4 with QueryChangeLogException

use of org.apache.rya.streams.api.queries.QueryChangeLog.QueryChangeLogException 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 5 with QueryChangeLogException

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

the class InMemoryQueryRepositoryTest method changeLogThrowsExceptions.

@Test(expected = RuntimeException.class)
public void changeLogThrowsExceptions() throws Exception {
    // Create a mock change log that throws an exception when you try to list what is in it.
    final QueryChangeLog changeLog = mock(QueryChangeLog.class);
    when(changeLog.readFromStart()).thenThrow(new QueryChangeLogException("Mocked exception."));
    // Create the QueryRepository and invoke one of the methods.
    final QueryRepository queries = new InMemoryQueryRepository(changeLog, SCHEDULE);
    queries.list();
}
Also used : QueryChangeLogException(org.apache.rya.streams.api.queries.QueryChangeLog.QueryChangeLogException) Test(org.junit.Test)

Aggregations

QueryChangeLogException (org.apache.rya.streams.api.queries.QueryChangeLog.QueryChangeLogException)7 Test (org.junit.Test)5 TopicPartition (org.apache.kafka.common.TopicPartition)4 ChangeLogEntry (org.apache.rya.streams.api.queries.ChangeLogEntry)4 ArrayList (java.util.ArrayList)3 QueryChange (org.apache.rya.streams.api.queries.QueryChange)3 UUID (java.util.UUID)2 CloseableIteration (info.aduna.iteration.CloseableIteration)1 StreamsQuery (org.apache.rya.streams.api.entity.StreamsQuery)1