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);
}
}
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);
}
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))));
}
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);
}
}
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;
}
}
Aggregations