Search in sources :

Example 16 with SubscriptionCursorWithoutToken

use of org.zalando.nakadi.view.SubscriptionCursorWithoutToken in project nakadi by zalando.

the class CursorsServiceAT method registerNakadiCursor.

private void registerNakadiCursor(final NakadiCursor cursor) {
    try {
        final SubscriptionCursorWithoutToken view = new SubscriptionCursorWithoutToken(cursor.getEventType(), cursor.getPartition(), cursor.getOffset());
        when(cursorConverter.convert(eq(view))).thenReturn(cursor);
        when(cursorConverter.convertToNoToken(eq(cursor))).thenReturn(view);
    } catch (final Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) InvalidStreamIdException(org.zalando.nakadi.exceptions.InvalidStreamIdException)

Example 17 with SubscriptionCursorWithoutToken

use of org.zalando.nakadi.view.SubscriptionCursorWithoutToken in project nakadi by zalando.

the class NewZkSubscriptionClient method createTopologyAndOffsets.

@Override
protected byte[] createTopologyAndOffsets(final Collection<SubscriptionCursorWithoutToken> cursors) throws Exception {
    for (final SubscriptionCursorWithoutToken cursor : cursors) {
        getCurator().create().creatingParentsIfNeeded().forPath(getOffsetPath(cursor.getEventTypePartition()), cursor.getOffset().getBytes(UTF_8));
    }
    final Partition[] partitions = cursors.stream().map(cursor -> new Partition(cursor.getEventType(), cursor.getPartition(), null, null, Partition.State.UNASSIGNED)).toArray(Partition[]::new);
    final Topology topology = new Topology(partitions, "", 0);
    getLog().info("Generating topology {}", topology);
    return objectMapper.writeValueAsBytes(topology);
}
Also used : KeeperException(org.apache.zookeeper.KeeperException) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Session(org.zalando.nakadi.service.subscription.model.Session) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) UTF_8(com.google.common.base.Charsets.UTF_8) ArrayList(java.util.ArrayList) Partition(org.zalando.nakadi.service.subscription.model.Partition) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) List(java.util.List) CuratorFramework(org.apache.curator.framework.CuratorFramework) Stream(java.util.stream.Stream) ImmutableList(com.google.common.collect.ImmutableList) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) ServiceTemporarilyUnavailableException(org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException) Map(java.util.Map) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Partition(org.zalando.nakadi.service.subscription.model.Partition)

Example 18 with SubscriptionCursorWithoutToken

use of org.zalando.nakadi.view.SubscriptionCursorWithoutToken in project nakadi by zalando.

the class SubscriptionControllerTest method whenGetSubscriptionStatThenOk.

@Test
public void whenGetSubscriptionStatThenOk() throws Exception {
    final Subscription subscription = builder().withEventType(TIMELINE.getEventType()).build();
    final Collection<Partition> partitions = Collections.singleton(new Partition(TIMELINE.getEventType(), "0", "xz", null, Partition.State.ASSIGNED));
    final ZkSubscriptionNode zkSubscriptionNode = new ZkSubscriptionNode(partitions, Arrays.asList(new Session("xz", 0)));
    when(subscriptionRepository.getSubscription(subscription.getId())).thenReturn(subscription);
    when(zkSubscriptionClient.getZkSubscriptionNodeLocked()).thenReturn(Optional.of(zkSubscriptionNode));
    final SubscriptionCursorWithoutToken currentOffset = new SubscriptionCursorWithoutToken(TIMELINE.getEventType(), "0", "3");
    final EventTypePartition etp = new EventTypePartition(TIMELINE.getEventType(), "0");
    final Map<EventTypePartition, SubscriptionCursorWithoutToken> offsets = new HashMap<>();
    offsets.put(etp, currentOffset);
    when(zkSubscriptionClient.getOffsets(Collections.singleton(etp))).thenReturn(offsets);
    when(eventTypeRepository.findByName(TIMELINE.getEventType())).thenReturn(EventTypeTestBuilder.builder().name(TIMELINE.getEventType()).build());
    final List<PartitionEndStatistics> statistics = Collections.singletonList(new KafkaPartitionEndStatistics(TIMELINE, 0, 13));
    when(topicRepository.loadTopicEndStatistics(eq(Collections.singletonList(TIMELINE)))).thenReturn(statistics);
    final NakadiCursor currentCursor = mock(NakadiCursor.class);
    when(currentCursor.getEventTypePartition()).thenReturn(new EventTypePartition(TIMELINE.getEventType(), "0"));
    when(cursorConverter.convert((List<SubscriptionCursorWithoutToken>) any())).thenReturn(Collections.singletonList(currentCursor));
    when(cursorOperationsService.calculateDistance(eq(currentCursor), eq(statistics.get(0).getLast()))).thenReturn(10L);
    final List<SubscriptionEventTypeStats> expectedStats = Collections.singletonList(new SubscriptionEventTypeStats(TIMELINE.getEventType(), Collections.singletonList(new SubscriptionEventTypeStats.Partition("0", "assigned", 10L, "xz", AUTO))));
    getSubscriptionStats(subscription.getId()).andExpect(status().isOk()).andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(new ItemsWrapper<>(expectedStats))));
}
Also used : EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Partition(org.zalando.nakadi.service.subscription.model.Partition) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) HashMap(java.util.HashMap) SubscriptionEventTypeStats(org.zalando.nakadi.domain.SubscriptionEventTypeStats) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) KafkaPartitionEndStatistics(org.zalando.nakadi.repository.kafka.KafkaPartitionEndStatistics) KafkaPartitionEndStatistics(org.zalando.nakadi.repository.kafka.KafkaPartitionEndStatistics) PartitionEndStatistics(org.zalando.nakadi.domain.PartitionEndStatistics) Subscription(org.zalando.nakadi.domain.Subscription) ZkSubscriptionNode(org.zalando.nakadi.service.subscription.zk.ZkSubscriptionNode) Session(org.zalando.nakadi.service.subscription.model.Session) Test(org.junit.Test)

Example 19 with SubscriptionCursorWithoutToken

use of org.zalando.nakadi.view.SubscriptionCursorWithoutToken in project nakadi by zalando.

the class SubscriptionValidationService method validateInitialCursors.

private void validateInitialCursors(final SubscriptionBase subscription, final List<EventTypePartition> allPartitions) throws WrongInitialCursorsException, RepositoryProblemException {
    final boolean cursorsMissing = allPartitions.stream().anyMatch(p -> !subscription.getInitialCursors().stream().anyMatch(p::ownsCursor));
    if (cursorsMissing) {
        throw new WrongInitialCursorsException("initial_cursors should contain cursors for all partitions of subscription");
    }
    final boolean hasCursorForWrongPartition = subscription.getInitialCursors().stream().anyMatch(c -> !allPartitions.contains(new EventTypePartition(c.getEventType(), c.getPartition())));
    if (hasCursorForWrongPartition) {
        throw new WrongInitialCursorsException("initial_cursors should contain cursors only for partitions of this subscription");
    }
    if (subscription.getInitialCursors().size() > allPartitions.size()) {
        throw new WrongInitialCursorsException("there should be no more than 1 cursor for each partition in initial_cursors");
    }
    try {
        for (final SubscriptionCursorWithoutToken cursor : subscription.getInitialCursors()) {
            final NakadiCursor nakadiCursor = cursorConverter.convert(cursor);
            if (nakadiCursor.getTimeline().isDeleted()) {
                throw new InvalidCursorException(UNAVAILABLE, nakadiCursor);
            }
            timelineService.getTopicRepository(nakadiCursor.getTimeline()).validateReadCursors(Collections.singletonList(nakadiCursor));
        }
    } catch (final InvalidCursorException ex) {
        throw new WrongInitialCursorsException(ex.getMessage(), ex);
    } catch (final NakadiException ex) {
        throw new RepositoryProblemException("Topic repository problem occurred when validating cursors", ex);
    }
}
Also used : Map(java.util.Map) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) WrongInitialCursorsException(org.zalando.nakadi.exceptions.runtime.WrongInitialCursorsException) RepositoryProblemException(org.zalando.nakadi.exceptions.runtime.RepositoryProblemException) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) NakadiException(org.zalando.nakadi.exceptions.NakadiException) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException)

Example 20 with SubscriptionCursorWithoutToken

use of org.zalando.nakadi.view.SubscriptionCursorWithoutToken in project nakadi by zalando.

the class ClosingState method freePartitions.

private void freePartitions(final Collection<EventTypePartition> keys) {
    RuntimeException exceptionCaught = null;
    for (final EventTypePartition partitionKey : keys) {
        uncommittedOffsets.remove(partitionKey);
        final ZkSubscription<SubscriptionCursorWithoutToken> listener = listeners.remove(partitionKey);
        if (null != listener) {
            try {
                listener.close();
            } catch (final RuntimeException ex) {
                exceptionCaught = ex;
                getLog().error("Failed to cancel offsets listener {}", listener, ex);
            }
        }
    }
    getZk().runLocked(() -> getZk().transfer(getSessionId(), keys));
    if (null != exceptionCaught) {
        throw exceptionCaught;
    }
}
Also used : SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition)

Aggregations

SubscriptionCursorWithoutToken (org.zalando.nakadi.view.SubscriptionCursorWithoutToken)21 Test (org.junit.Test)12 NakadiCursor (org.zalando.nakadi.domain.NakadiCursor)9 EventTypePartition (org.zalando.nakadi.domain.EventTypePartition)8 Subscription (org.zalando.nakadi.domain.Subscription)5 Partition (org.zalando.nakadi.service.subscription.model.Partition)5 List (java.util.List)4 Map (java.util.Map)4 PartitionStatistics (org.zalando.nakadi.domain.PartitionStatistics)4 TopicRepository (org.zalando.nakadi.repository.TopicRepository)4 ImmutableList (com.google.common.collect.ImmutableList)3 HashMap (java.util.HashMap)3 Storage (org.zalando.nakadi.domain.Storage)3 InvalidCursorException (org.zalando.nakadi.exceptions.InvalidCursorException)3 ServiceUnavailableException (org.zalando.nakadi.exceptions.ServiceUnavailableException)3 NakadiTestUtils.createSubscription (org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription)3 UTF_8 (com.google.common.base.Charsets.UTF_8)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2