use of org.zalando.nakadi.view.SubscriptionCursorWithoutToken in project nakadi by zalando.
the class VersionZeroConverter method convertBatched.
public List<NakadiCursor> convertBatched(final List<SubscriptionCursorWithoutToken> cursors) throws InvalidCursorException, InternalNakadiException, NoSuchEventTypeException, ServiceUnavailableException {
final NakadiCursor[] result = new NakadiCursor[cursors.size()];
for (int idx = 0; idx < cursors.size(); ++idx) {
final SubscriptionCursorWithoutToken cursor = cursors.get(idx);
if (Cursor.BEFORE_OLDEST_OFFSET.equalsIgnoreCase(cursor.getOffset())) {
// Preform begin checks afterwards to optimize calls
continue;
}
if (!NUMBERS_ONLY_PATTERN.matcher(cursor.getOffset()).matches()) {
throw new InvalidCursorException(CursorError.INVALID_OFFSET, cursor);
}
}
// now it is time for massive convert.
final LinkedHashMap<SubscriptionCursorWithoutToken, NakadiCursor> beginsToConvert = new LinkedHashMap<>();
final Map<SubscriptionCursorWithoutToken, Timeline> cursorTimelines = new HashMap<>();
final Map<TopicRepository, List<SubscriptionCursorWithoutToken>> repos = new HashMap<>();
for (int i = 0; i < result.length; ++i) {
if (null == result[i]) {
// cursor requires database hit
final SubscriptionCursorWithoutToken cursor = cursors.get(i);
final Timeline timeline = timelineService.getActiveTimelinesOrdered(cursor.getEventType()).get(0);
final TopicRepository topicRepo = timelineService.getTopicRepository(timeline);
beginsToConvert.put(cursor, null);
cursorTimelines.put(cursor, timeline);
repos.computeIfAbsent(topicRepo, k -> new ArrayList<>()).add(cursor);
}
}
for (final Map.Entry<TopicRepository, List<SubscriptionCursorWithoutToken>> entry : repos.entrySet()) {
final List<Optional<PartitionStatistics>> stats = entry.getKey().loadPartitionStatistics(entry.getValue().stream().map(scwt -> new TopicRepository.TimelinePartition(cursorTimelines.get(scwt), scwt.getPartition())).collect(Collectors.toList()));
for (int idx = 0; idx < entry.getValue().size(); ++idx) {
// Reinsert doesn't change the order
beginsToConvert.put(entry.getValue().get(idx), stats.get(idx).orElseThrow(() -> new InvalidCursorException(PARTITION_NOT_FOUND)).getBeforeFirst());
}
}
final Iterator<NakadiCursor> missingBegins = beginsToConvert.values().iterator();
return Stream.of(result).map(it -> null == it ? missingBegins.next() : it).collect(Collectors.toList());
}
use of org.zalando.nakadi.view.SubscriptionCursorWithoutToken in project nakadi by zalando.
the class CursorsServiceAT method whenGetSubscriptionCursorsThenOk.
@Test
public void whenGetSubscriptionCursorsThenOk() throws Exception {
setPartitions(new Partition[] { new Partition(etName, P1, "", "", Partition.State.UNASSIGNED), new Partition(etName, P2, "", "", Partition.State.UNASSIGNED) });
final List<SubscriptionCursorWithoutToken> cursors = cursorsService.getSubscriptionCursors(sid);
assertThat(ImmutableSet.copyOf(cursors), equalTo(ImmutableSet.of(new SubscriptionCursorWithoutToken(etName, P1, OLD_OFFSET), new SubscriptionCursorWithoutToken(etName, P2, OLD_OFFSET))));
}
use of org.zalando.nakadi.view.SubscriptionCursorWithoutToken in project nakadi by zalando.
the class SubscriptionAT method testSubscriptionWithInitialCursors.
@Test
public void testSubscriptionWithInitialCursors() throws Exception {
final EventType et1 = createBusinessEventTypeWithPartitions(2);
final EventType et2 = createBusinessEventTypeWithPartitions(2);
// write 10 events to each partition of two event-types
publishBusinessEventWithUserDefinedPartition(et1.getName(), 10, i -> "dummy", i -> "0");
publishBusinessEventWithUserDefinedPartition(et1.getName(), 10, i -> "dummy", i -> "1");
publishBusinessEventWithUserDefinedPartition(et2.getName(), 10, i -> "dummy", i -> "0");
publishBusinessEventWithUserDefinedPartition(et2.getName(), 10, i -> "dummy", i -> "1");
// create subscription with initial cursors
final SubscriptionBase subscriptionBase = RandomSubscriptionBuilder.builder().withEventTypes(ImmutableSet.of(et1.getName(), et2.getName())).withStartFrom(SubscriptionBase.InitialPosition.CURSORS).withInitialCursors(ImmutableList.of(new SubscriptionCursorWithoutToken(et1.getName(), "0", "000000000000000007"), new SubscriptionCursorWithoutToken(et1.getName(), "1", "000000000000000002"), new SubscriptionCursorWithoutToken(et2.getName(), "0", Cursor.BEFORE_OLDEST_OFFSET), new SubscriptionCursorWithoutToken(et2.getName(), "1", "000000000000000009"))).buildSubscriptionBase();
final Subscription subscription = createSubscription(subscriptionBase);
final TestStreamingClient client = TestStreamingClient.create(URL, subscription.getId(), "max_uncommitted_events=100").start();
// we should read 19 events in total
waitFor(() -> assertThat(client.getBatches(), hasSize(19)));
final List<StreamBatch> batches = client.getBatches();
// check that first events of each partition have correct offsets
assertThat(getFirstBatchOffsetFor(batches, new EventTypePartition(et1.getName(), "0")), equalTo(Optional.of("001-0001-000000000000000008")));
assertThat(getFirstBatchOffsetFor(batches, new EventTypePartition(et1.getName(), "1")), equalTo(Optional.of("001-0001-000000000000000003")));
assertThat(getFirstBatchOffsetFor(batches, new EventTypePartition(et2.getName(), "0")), equalTo(Optional.of("001-0001-000000000000000000")));
assertThat(getFirstBatchOffsetFor(batches, new EventTypePartition(et2.getName(), "1")), equalTo(Optional.empty()));
}
use of org.zalando.nakadi.view.SubscriptionCursorWithoutToken in project nakadi by zalando.
the class SubscriptionConsumptionTest method testAllEventsConsumed.
@Test
public void testAllEventsConsumed() throws IOException, InterruptedException {
final String[] expected = new String[] { "001-0001-000000000000000000", "001-0001-000000000000000001", "001-0002-000000000000000000", "001-0002-000000000000000001", "001-0003-000000000000000000", "001-0003-000000000000000001", "001-0004-000000000000000000", "001-0004-000000000000000001" };
// Do not test last case, because it makes no sense...
for (int idx = -1; idx < expected.length - 1; ++idx) {
final CountDownLatch finished = new CountDownLatch(1);
final AtomicReference<String[]> receivedOffset = new AtomicReference<>();
final Subscription subscription = createSubscription(RandomSubscriptionBuilder.builder().withEventType(eventType.getName()).withStartFrom(SubscriptionBase.InitialPosition.CURSORS).withInitialCursors(Collections.singletonList(new SubscriptionCursorWithoutToken(eventType.getName(), "0", idx == -1 ? "BEGIN" : expected[idx]))).build());
createParallelConsumer(subscription, expected.length - 1 - idx, finished, receivedOffset::set);
finished.await();
final String[] testedOffsets = Arrays.copyOfRange(expected, idx + 1, expected.length);
Assert.assertArrayEquals(testedOffsets, receivedOffset.get());
}
}
use of org.zalando.nakadi.view.SubscriptionCursorWithoutToken in project nakadi by zalando.
the class HilaAT method whenPatchThenCursorsAreInitializedAndPatched.
@Test(timeout = 15000)
public void whenPatchThenCursorsAreInitializedAndPatched() throws Exception {
final EventType et = createEventType();
publishEvents(et.getName(), 10, i -> "{\"foo\": \"bar\"}");
final List<EventTypePartitionView> etStats = MAPPER.readValue(given().get("/event-types/{et}/partitions", et.getName()).getBody().asString(), new TypeReference<List<EventTypePartitionView>>() {
});
final EventTypePartitionView begin = etStats.get(0);
final Subscription s = createSubscription(RandomSubscriptionBuilder.builder().withEventType(et.getName()).withStartFrom(END).buildSubscriptionBase());
given().body(MAPPER.writeValueAsString(new ItemsWrapper<>(Collections.singletonList(new SubscriptionCursorWithoutToken(et.getName(), begin.getPartitionId(), begin.getOldestAvailableOffset()))))).contentType(JSON).patch("/subscriptions/{id}/cursors", s.getId()).then().statusCode(SC_NO_CONTENT);
final ItemsWrapper<SubscriptionCursor> subscriptionCursors = MAPPER.readValue(given().get("/subscriptions/{id}/cursors", s.getId()).getBody().asString(), new TypeReference<ItemsWrapper<SubscriptionCursor>>() {
});
Assert.assertEquals(subscriptionCursors.getItems().size(), etStats.size());
subscriptionCursors.getItems().forEach(item -> {
if (item.getPartition().equals(begin.getPartitionId())) {
Assert.assertEquals(begin.getOldestAvailableOffset(), item.getOffset());
} else {
Assert.assertEquals(begin.getNewestAvailableOffset(), item.getOffset());
}
});
}
Aggregations