use of org.zalando.nakadi.service.subscription.zk.ZkSubscriptionClient 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();
}
use of org.zalando.nakadi.service.subscription.zk.ZkSubscriptionClient in project nakadi by zalando.
the class CursorsService method resetCursors.
public void resetCursors(final String subscriptionId, final List<NakadiCursor> cursors) throws ServiceUnavailableException, NoSuchSubscriptionException, UnableProcessException, OperationTimeoutException, ZookeeperException, InternalNakadiException, NoSuchEventTypeException, InvalidCursorException {
final Subscription subscription = subscriptionRepository.getSubscription(subscriptionId);
validateCursorsBelongToSubscription(subscription, cursors);
for (final NakadiCursor cursor : cursors) {
cursor.checkStorageAvailability();
}
final Map<TopicRepository, List<NakadiCursor>> topicRepositories = cursors.stream().collect(Collectors.groupingBy(c -> timelineService.getTopicRepository(c.getTimeline())));
for (final Map.Entry<TopicRepository, List<NakadiCursor>> entry : topicRepositories.entrySet()) {
entry.getKey().validateReadCursors(entry.getValue());
}
final ZkSubscriptionClient zkClient = zkSubscriptionFactory.createClient(subscription, "subscription." + subscriptionId + ".reset_cursors");
// In case if subscription was never initialized - initialize it
zkClient.runLocked(() -> StartingState.initializeSubscriptionLocked(zkClient, subscription, timelineService, cursorConverter));
// add 1 second to commit timeout in order to give time to finish reset if there is uncommitted events
if (!cursors.isEmpty()) {
final long timeout = TimeUnit.SECONDS.toMillis(nakadiSettings.getDefaultCommitTimeoutSeconds()) + TimeUnit.SECONDS.toMillis(1);
zkClient.resetCursors(cursors.stream().map(cursorConverter::convertToNoToken).collect(Collectors.toList()), timeout);
}
}
use of org.zalando.nakadi.service.subscription.zk.ZkSubscriptionClient in project nakadi by zalando.
the class CursorsService method commitCursors.
/**
* It is guaranteed, that len(cursors) == len(result)
*/
public List<Boolean> commitCursors(final String streamId, final String subscriptionId, final List<NakadiCursor> cursors) throws ServiceUnavailableException, InvalidCursorException, InvalidStreamIdException, NoSuchEventTypeException, InternalNakadiException, NoSuchSubscriptionException, UnableProcessException {
TimeLogger.addMeasure("getSubscription");
final Subscription subscription = subscriptionRepository.getSubscription(subscriptionId);
TimeLogger.addMeasure("validateSubscriptionCursors");
validateSubscriptionCommitCursors(subscription, cursors);
TimeLogger.addMeasure("createSubscriptionClient");
final ZkSubscriptionClient zkClient = zkSubscriptionFactory.createClient(subscription, "subscription." + subscriptionId + "." + streamId + ".offsets");
TimeLogger.addMeasure("validateStreamId");
validateStreamId(cursors, streamId, zkClient);
TimeLogger.addMeasure("writeToZK");
return zkClient.commitOffsets(cursors.stream().map(cursorConverter::convertToNoToken).collect(Collectors.toList()), new SubscriptionCursorComparator(new NakadiCursorComparator(eventTypeCache)));
}
use of org.zalando.nakadi.service.subscription.zk.ZkSubscriptionClient in project nakadi by zalando.
the class SubscriptionService method deleteSubscription.
public Result<Void> deleteSubscription(final String subscriptionId) throws DbWriteOperationsBlockedException {
if (featureToggleService.isFeatureEnabled(FeatureToggleService.Feature.DISABLE_DB_WRITE_OPERATIONS)) {
throw new DbWriteOperationsBlockedException("Cannot delete subscription: write operations on DB " + "are blocked by feature flag.");
}
try {
final Subscription subscription = subscriptionRepository.getSubscription(subscriptionId);
subscriptionRepository.deleteSubscription(subscriptionId);
final ZkSubscriptionClient zkSubscriptionClient = subscriptionClientFactory.createClient(subscription, "subscription." + subscriptionId + ".delete_subscription");
zkSubscriptionClient.deleteSubscription();
nakadiKpiPublisher.publish(subLogEventType, () -> new JSONObject().put("subscription_id", subscriptionId).put("status", "deleted"));
return Result.ok();
} catch (final NoSuchSubscriptionException e) {
LOG.debug("Failed to find subscription: {}", subscriptionId, e);
return Result.problem(e.asProblem());
} catch (final ServiceUnavailableException e) {
LOG.error("Error occurred when trying to delete subscription: {}", subscriptionId, e);
return Result.problem(e.asProblem());
} catch (final NoSuchEventTypeException | InternalNakadiException e) {
LOG.error("Exception can not occur", e);
return Result.problem(e.asProblem());
}
}
use of org.zalando.nakadi.service.subscription.zk.ZkSubscriptionClient in project nakadi by zalando.
the class SubscriptionService method createSubscriptionStat.
private List<SubscriptionEventTypeStats> createSubscriptionStat(final Subscription subscription) throws InconsistentStateException, ServiceTemporarilyUnavailableException {
final List<EventType> eventTypes = subscription.getEventTypes().stream().map(Try.wrap(eventTypeRepository::findByName)).map(Try::getOrThrow).sorted(Comparator.comparing(EventType::getName)).collect(Collectors.toList());
final List<PartitionEndStatistics> topicPartitions;
try {
topicPartitions = loadPartitionEndStatistics(eventTypes);
} catch (final ServiceUnavailableException ex) {
throw new ServiceTemporarilyUnavailableException(ex);
}
final ZkSubscriptionClient subscriptionClient;
try {
subscriptionClient = subscriptionClientFactory.createClient(subscription, "subscription." + subscription.getId() + ".stats");
} catch (final InternalNakadiException | NoSuchEventTypeException e) {
throw new ServiceTemporarilyUnavailableException(e);
}
final Optional<ZkSubscriptionNode> zkSubscriptionNode = subscriptionClient.getZkSubscriptionNodeLocked();
return loadStats(eventTypes, zkSubscriptionNode, subscriptionClient, topicPartitions);
}
Aggregations