use of org.zalando.nakadi.view.SubscriptionCursorWithoutToken in project nakadi by zalando.
the class SubscriptionValidationServiceTest method whenServiceUnavailableThenException.
@Test(expected = RepositoryProblemException.class)
public void whenServiceUnavailableThenException() throws Exception {
subscriptionBase.setInitialCursors(ImmutableList.of(new SubscriptionCursorWithoutToken(ET1, P0, ""), new SubscriptionCursorWithoutToken(ET2, P0, ""), new SubscriptionCursorWithoutToken(ET3, P0, "")));
final NakadiCursor cursor = mockCursorWithTimeline();
when(cursorConverter.convert((SubscriptionCursorWithoutToken) any())).thenReturn(cursor);
doThrow(new ServiceUnavailableException("")).when(topicRepository).validateReadCursors(any());
subscriptionValidationService.validateSubscription(subscriptionBase);
}
use of org.zalando.nakadi.view.SubscriptionCursorWithoutToken in project nakadi by zalando.
the class HashGeneratorTest method whenGenerateHashForEqualSubscriptionsThenHashIsEqual.
@Test
public void whenGenerateHashForEqualSubscriptionsThenHashIsEqual() {
final SubscriptionBase s1 = createSubscription("my-app", "abc", "et1", "et2");
s1.setReadFrom(SubscriptionBase.InitialPosition.BEGIN);
final SubscriptionBase s2 = createSubscription("my-app", "abc", "et2", "et1");
s2.setReadFrom(SubscriptionBase.InitialPosition.CURSORS);
s2.setInitialCursors(ImmutableList.of(new SubscriptionCursorWithoutToken("et1", "p1", "0")));
assertThat(hashGenerator.generateSubscriptionKeyFieldsHash(s1), equalTo(hashGenerator.generateSubscriptionKeyFieldsHash(s2)));
}
use of org.zalando.nakadi.view.SubscriptionCursorWithoutToken in project nakadi by zalando.
the class AbstractZkSubscriptionClient method commitOffsets.
@Override
public List<Boolean> commitOffsets(final List<SubscriptionCursorWithoutToken> cursors, final Comparator<SubscriptionCursorWithoutToken> comparator) {
final Map<EventTypePartition, List<SubscriptionCursorWithoutToken>> grouped = cursors.stream().collect(Collectors.groupingBy(SubscriptionCursorWithoutToken::getEventTypePartition));
try {
final Map<EventTypePartition, Iterator<Boolean>> committedOverall = new HashMap<>();
for (final Map.Entry<EventTypePartition, List<SubscriptionCursorWithoutToken>> entry : grouped.entrySet()) {
final String offsetPath = getOffsetPath(entry.getKey());
final List<Boolean> committed;
committed = executeWithRetry(() -> {
final Stat stat = new Stat();
final byte[] currentOffsetData = getCurator().getData().storingStatIn(stat).forPath(offsetPath);
final String currentMaxOffset = new String(currentOffsetData, UTF_8);
SubscriptionCursorWithoutToken currentMaxCursor = new SubscriptionCursorWithoutToken(entry.getKey().getEventType(), entry.getKey().getPartition(), currentMaxOffset);
final List<Boolean> commits = Lists.newArrayList();
for (final SubscriptionCursorWithoutToken cursor : entry.getValue()) {
if (comparator.compare(cursor, currentMaxCursor) > 0) {
currentMaxCursor = cursor;
commits.add(true);
} else {
commits.add(false);
}
}
if (!currentMaxCursor.getOffset().equals(currentMaxOffset)) {
getLog().info("Committing {} to {}", currentMaxCursor.getOffset(), offsetPath);
getCurator().setData().withVersion(stat.getVersion()).forPath(offsetPath, currentMaxCursor.getOffset().getBytes(Charsets.UTF_8));
}
return commits;
}, new RetryForSpecifiedCountStrategy<List<Boolean>>(COMMIT_CONFLICT_RETRY_TIMES).withExceptionsThatForceRetry(KeeperException.BadVersionException.class));
committedOverall.put(entry.getKey(), Optional.ofNullable(committed).orElse(Collections.nCopies(entry.getValue().size(), false)).iterator());
}
return cursors.stream().map(cursor -> committedOverall.get(cursor.getEventTypePartition()).next()).collect(Collectors.toList());
} catch (final Exception ex) {
throw new NakadiRuntimeException(ex);
}
}
use of org.zalando.nakadi.view.SubscriptionCursorWithoutToken in project nakadi by zalando.
the class AbstractZkSubscriptionClient method subscribeForOffsetChanges.
@Override
public ZkSubscription<SubscriptionCursorWithoutToken> subscribeForOffsetChanges(final EventTypePartition key, final Runnable commitListener) {
final String path = getOffsetPath(key);
getLog().info("subscribeForOffsetChanges: {}, path: {}", key, path);
return new ZkSubscriptionImpl.ZkSubscriptionValueImpl<>(getCurator(), commitListener, data -> new SubscriptionCursorWithoutToken(key.getEventType(), key.getPartition(), new String(data, UTF_8)), path);
}
use of org.zalando.nakadi.view.SubscriptionCursorWithoutToken in project nakadi by zalando.
the class CursorsService method getSubscriptionCursors.
public List<SubscriptionCursorWithoutToken> getSubscriptionCursors(final String subscriptionId) throws NakadiException, ServiceTemporarilyUnavailableException {
final Subscription subscription = subscriptionRepository.getSubscription(subscriptionId);
final ZkSubscriptionClient zkSubscriptionClient = zkSubscriptionFactory.createClient(subscription, "subscription." + subscriptionId + ".get_cursors");
final ImmutableList.Builder<SubscriptionCursorWithoutToken> cursorsListBuilder = ImmutableList.builder();
Partition[] partitions;
try {
partitions = zkSubscriptionClient.getTopology().getPartitions();
} catch (final SubscriptionNotInitializedException ex) {
partitions = new Partition[] {};
}
final Map<EventTypePartition, SubscriptionCursorWithoutToken> positions = zkSubscriptionClient.getOffsets(Stream.of(partitions).map(Partition::getKey).collect(Collectors.toList()));
for (final Partition p : partitions) {
cursorsListBuilder.add(positions.get(p.getKey()));
}
return cursorsListBuilder.build();
}
Aggregations