Search in sources :

Example 31 with NakadiCursor

use of org.zalando.nakadi.domain.NakadiCursor in project nakadi by zalando.

the class PartitionsController method getCursorLag.

private CursorLag getCursorLag(final String eventTypeName, final String partition, final String consumedOffset) throws InternalNakadiException, NoSuchEventTypeException, InvalidCursorException, ServiceUnavailableException {
    final Cursor consumedCursor = new Cursor(partition, consumedOffset);
    final NakadiCursor consumedNakadiCursor = cursorConverter.convert(eventTypeName, consumedCursor);
    return cursorOperationsService.cursorsLag(eventTypeName, Lists.newArrayList(consumedNakadiCursor)).stream().findFirst().map(this::toCursorLag).orElseThrow(MyNakadiRuntimeException1::new);
}
Also used : NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) MyNakadiRuntimeException1(org.zalando.nakadi.exceptions.runtime.MyNakadiRuntimeException1) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) Cursor(org.zalando.nakadi.view.Cursor)

Example 32 with NakadiCursor

use of org.zalando.nakadi.domain.NakadiCursor in project nakadi by zalando.

the class CursorsServiceAT method whenCommitOldCursorsThenFalse.

@Test
public void whenCommitOldCursorsThenFalse() throws Exception {
    final NakadiCursor cursor = NakadiCursor.of(buildTimeline(etName, topic, CREATED_AT), P1, OLDEST_OFFSET);
    registerNakadiCursor(cursor);
    testCursors = ImmutableList.of(cursor);
    setPartitions(new Partition[] { new Partition(etName, P1, streamId, null, Partition.State.ASSIGNED) });
    final List<Boolean> commitResult = cursorsService.commitCursors(streamId, sid, testCursors);
    assertThat(commitResult, equalTo(ImmutableList.of(false)));
    checkCurrentOffsetInZk(P1, OLD_OFFSET);
}
Also used : Partition(org.zalando.nakadi.service.subscription.model.Partition) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) Test(org.junit.Test)

Example 33 with NakadiCursor

use of org.zalando.nakadi.domain.NakadiCursor in project nakadi by zalando.

the class CursorsServiceAT method whenFirstCursorIsNotCommittedThenNextCursorsAreNotSkipped.

@Test
public void whenFirstCursorIsNotCommittedThenNextCursorsAreNotSkipped() throws Exception {
    final NakadiCursor c1 = NakadiCursor.of(timeline, P1, OLDEST_OFFSET);
    final NakadiCursor c2 = NakadiCursor.of(timeline, P2, NEW_OFFSET);
    testCursors = ImmutableList.of(c1, c2);
    testCursors.forEach(this::registerNakadiCursor);
    setPartitions(new Partition[] { new Partition(etName, P1, streamId, null, Partition.State.ASSIGNED), new Partition(etName, P2, streamId, null, Partition.State.ASSIGNED) });
    final List<Boolean> result = cursorsService.commitCursors(streamId, sid, testCursors);
    assertFalse(result.get(0));
    assertTrue(result.get(1));
    checkCurrentOffsetInZk(P1, OLD_OFFSET);
    checkCurrentOffsetInZk(P2, NEW_OFFSET);
}
Also used : Partition(org.zalando.nakadi.service.subscription.model.Partition) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) Test(org.junit.Test)

Example 34 with NakadiCursor

use of org.zalando.nakadi.domain.NakadiCursor in project nakadi by zalando.

the class MultiTimelineEventConsumer method electTopicRepositories.

private void electTopicRepositories() throws NakadiException, InvalidCursorException {
    final Map<TopicRepository, List<NakadiCursor>> newAssignment = new HashMap<>();
    borderOffsets.clear();
    // Purpose of this collection is to hold tr that definitely changed their positions and should be recreated.
    final Set<TopicPartition> actualReadPositionChanged = new HashSet<>();
    // load new topic repositories and possibly replace cursors to newer timelines.
    for (final NakadiCursor cursor : latestOffsets.values()) {
        final AtomicReference<NakadiCursor> cursorReplacement = new AtomicReference<>();
        final TopicRepository topicRepository = selectCorrectTopicRepo(cursor, cursorReplacement::set, nc -> Optional.ofNullable(nc).ifPresent(itm -> borderOffsets.put(itm.getEventTypePartition(), itm.getOffset())));
        if (!newAssignment.containsKey(topicRepository)) {
            newAssignment.put(topicRepository, new ArrayList<>());
        }
        if (cursorReplacement.get() != null) {
            actualReadPositionChanged.add(cursor.getTopicPartition());
            newAssignment.get(topicRepository).add(cursorReplacement.get());
        } else {
            newAssignment.get(topicRepository).add(cursor);
        }
    }
    final Set<TopicRepository> removedTopicRepositories = eventConsumers.keySet().stream().filter(tr -> !newAssignment.containsKey(tr)).collect(Collectors.toSet());
    // Stop and remove event consumers that are not needed anymore
    for (final TopicRepository toRemove : removedTopicRepositories) {
        stopAndRemoveConsumer(toRemove);
    }
    // Stop and remove event consumers with changed configuration
    for (final Map.Entry<TopicRepository, List<NakadiCursor>> entry : newAssignment.entrySet()) {
        final EventConsumer.LowLevelConsumer existingEventConsumer = eventConsumers.get(entry.getKey());
        if (null != existingEventConsumer) {
            final Set<TopicPartition> newTopicPartitions = entry.getValue().stream().map(NakadiCursor::getTopicPartition).collect(Collectors.toSet());
            final Set<TopicPartition> oldAssignment = existingEventConsumer.getAssignment();
            if (!oldAssignment.equals(newTopicPartitions) || oldAssignment.stream().anyMatch(actualReadPositionChanged::contains)) {
                stopAndRemoveConsumer(entry.getKey());
            }
        }
    }
    // Start new consumers with changed configuration.
    for (final Map.Entry<TopicRepository, List<NakadiCursor>> entry : newAssignment.entrySet()) {
        if (!eventConsumers.containsKey(entry.getKey())) {
            final TopicRepository repo = entry.getKey();
            LOG.info("Creating underlying consumer for client id {} and cursors {}", clientId, Arrays.deepToString(entry.getValue().toArray()));
            final EventConsumer.LowLevelConsumer consumer = repo.createEventConsumer(clientId, entry.getValue());
            eventConsumers.put(repo, consumer);
        }
    }
}
Also used : ConsumedEvent(org.zalando.nakadi.domain.ConsumedEvent) Arrays(java.util.Arrays) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) ListIterator(java.util.ListIterator) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) NakadiCollectionUtils(org.zalando.nakadi.util.NakadiCollectionUtils) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) NakadiException(org.zalando.nakadi.exceptions.NakadiException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TopicPartition(org.zalando.nakadi.domain.TopicPartition) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) Map(java.util.Map) TimelineService(org.zalando.nakadi.service.timeline.TimelineService) Logger(org.slf4j.Logger) TimelineSync(org.zalando.nakadi.service.timeline.TimelineSync) Collection(java.util.Collection) PartitionStatistics(org.zalando.nakadi.domain.PartitionStatistics) Set(java.util.Set) IOException(java.io.IOException) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) List(java.util.List) Timeline(org.zalando.nakadi.domain.Timeline) Optional(java.util.Optional) Comparator(java.util.Comparator) Collections(java.util.Collections) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) TopicPartition(org.zalando.nakadi.domain.TopicPartition) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 35 with NakadiCursor

use of org.zalando.nakadi.domain.NakadiCursor in project nakadi by zalando.

the class PartitionsControllerTest method mockCursorLag.

private List<NakadiCursorLag> mockCursorLag() {
    final Timeline timeline = mock(Timeline.class);
    when(timeline.getStorage()).thenReturn(new Storage("ccc", Storage.Type.KAFKA));
    final NakadiCursorLag lag = mock(NakadiCursorLag.class);
    final NakadiCursor firstCursor = NakadiCursor.of(timeline, "0", "0");
    final NakadiCursor lastCursor = NakadiCursor.of(timeline, "0", "1");
    when(lag.getLag()).thenReturn(42L);
    when(lag.getPartition()).thenReturn("0");
    when(lag.getFirstCursor()).thenReturn(firstCursor);
    when(lag.getLastCursor()).thenReturn(lastCursor);
    return Lists.newArrayList(lag);
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) TestUtils.buildTimeline(org.zalando.nakadi.utils.TestUtils.buildTimeline) Storage(org.zalando.nakadi.domain.Storage) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) NakadiCursorLag(org.zalando.nakadi.domain.NakadiCursorLag)

Aggregations

NakadiCursor (org.zalando.nakadi.domain.NakadiCursor)56 Timeline (org.zalando.nakadi.domain.Timeline)31 Test (org.junit.Test)27 ShiftedNakadiCursor (org.zalando.nakadi.domain.ShiftedNakadiCursor)21 InvalidCursorException (org.zalando.nakadi.exceptions.InvalidCursorException)14 ServiceUnavailableException (org.zalando.nakadi.exceptions.ServiceUnavailableException)12 SubscriptionCursorWithoutToken (org.zalando.nakadi.view.SubscriptionCursorWithoutToken)12 PartitionStatistics (org.zalando.nakadi.domain.PartitionStatistics)11 List (java.util.List)10 Map (java.util.Map)10 Collectors (java.util.stream.Collectors)10 Storage (org.zalando.nakadi.domain.Storage)10 TopicRepository (org.zalando.nakadi.repository.TopicRepository)10 Optional (java.util.Optional)8 LoggerFactory (org.slf4j.LoggerFactory)8 EventTypePartition (org.zalando.nakadi.domain.EventTypePartition)8 TimelineService (org.zalando.nakadi.service.timeline.TimelineService)8 Collections (java.util.Collections)7 Logger (org.slf4j.Logger)7 InternalNakadiException (org.zalando.nakadi.exceptions.InternalNakadiException)7