use of org.zalando.nakadi.exceptions.runtime.InconsistentStateException in project nakadi by zalando.
the class TimelineServiceTest method shouldDeleteTopicWhenTimelineCreationFails.
@Test
public void shouldDeleteTopicWhenTimelineCreationFails() throws Exception {
final TopicRepository repository = mock(TopicRepository.class);
Mockito.when(topicRepositoryHolder.getTopicRepository(any())).thenReturn(repository);
Mockito.when(timelineDbRepository.createTimeline(any())).thenThrow(new InconsistentStateException("shouldDeleteTopicWhenTimelineCreationFails"));
try {
timelineService.createDefaultTimeline("event_type_1", 1, 1);
} catch (final InconsistentStateException e) {
}
Mockito.verify(repository, Mockito.times(1)).deleteTopic(any());
}
use of org.zalando.nakadi.exceptions.runtime.InconsistentStateException in project nakadi by zalando.
the class TimelineService method createDefaultTimeline.
public Timeline createDefaultTimeline(final String eventTypeName, final int partitionsCount, final long retentionTime) throws TopicCreationException, InconsistentStateException, RepositoryProblemException, DuplicatedTimelineException, TimelineException, DbWriteOperationsBlockedException {
if (featureToggleService.isFeatureEnabled(FeatureToggleService.Feature.DISABLE_DB_WRITE_OPERATIONS)) {
throw new DbWriteOperationsBlockedException("Cannot create default timeline: write operations on DB " + "are blocked by feature flag.");
}
final TopicRepository repository = topicRepositoryHolder.getTopicRepository(defaultStorage.getStorage());
final String topic = repository.createTopic(partitionsCount, retentionTime);
try {
final Timeline timeline = Timeline.createTimeline(eventTypeName, 1, defaultStorage.getStorage(), topic, new Date());
timeline.setSwitchedAt(new Date());
timelineDbRepository.createTimeline(timeline);
eventTypeCache.updated(eventTypeName);
return timeline;
} catch (final InconsistentStateException | RepositoryProblemException | DuplicatedTimelineException e) {
rollbackTopic(repository, topic);
throw e;
} catch (final Exception e) {
rollbackTopic(repository, topic);
throw new TimelineException("Failed to update event type cache, while creating timeline", e);
}
}
use of org.zalando.nakadi.exceptions.runtime.InconsistentStateException in project nakadi by zalando.
the class TimelineService method scheduleTimelineCleanup.
private void scheduleTimelineCleanup(final Timeline timeline) throws InconsistentStateException {
try {
final EventType eventType = eventTypeCache.getEventType(timeline.getEventType());
final Long retentionTime = eventType.getOptions().getRetentionTime();
if (retentionTime == null) {
throw new InconsistentStateException("Event type should has information about its retention time");
}
final Date cleanupDate = new Date(System.currentTimeMillis() + retentionTime);
timeline.setCleanedUpAt(cleanupDate);
} catch (final InternalNakadiException | NoSuchEventTypeException e) {
throw new InconsistentStateException("Unexpected error occurred when scheduling timeline cleanup", e);
}
}
use of org.zalando.nakadi.exceptions.runtime.InconsistentStateException in project nakadi by zalando.
the class SubscriptionService method getSubscriptionStat.
public ItemsWrapper<SubscriptionEventTypeStats> getSubscriptionStat(final String subscriptionId) throws InconsistentStateException, NoSuchSubscriptionException, ServiceTemporarilyUnavailableException {
final Subscription subscription;
try {
subscription = subscriptionRepository.getSubscription(subscriptionId);
} catch (final ServiceUnavailableException ex) {
throw new InconsistentStateException(ex.getMessage());
}
final List<SubscriptionEventTypeStats> subscriptionStat = createSubscriptionStat(subscription);
return new ItemsWrapper<>(subscriptionStat);
}
use of org.zalando.nakadi.exceptions.runtime.InconsistentStateException in project nakadi by zalando.
the class SubscriptionService method loadStats.
private List<SubscriptionEventTypeStats> loadStats(final Collection<EventType> eventTypes, final Optional<ZkSubscriptionNode> subscriptionNode, final ZkSubscriptionClient client, final List<PartitionEndStatistics> stats) throws ServiceTemporarilyUnavailableException, InconsistentStateException {
final List<SubscriptionEventTypeStats> result = new ArrayList<>(eventTypes.size());
final Collection<NakadiCursor> committedPositions = subscriptionNode.map(node -> loadCommittedPositions(node.getPartitions(), client)).orElse(Collections.emptyList());
for (final EventType eventType : eventTypes) {
final List<SubscriptionEventTypeStats.Partition> resultPartitions = new ArrayList<>(stats.size());
for (final PartitionEndStatistics stat : stats) {
final NakadiCursor lastPosition = stat.getLast();
if (!lastPosition.getEventType().equals(eventType.getName())) {
continue;
}
final Long distance = committedPositions.stream().filter(pos -> pos.getEventTypePartition().equals(lastPosition.getEventTypePartition())).findAny().map(committed -> {
try {
return cursorOperationsService.calculateDistance(committed, lastPosition);
} catch (final InvalidCursorOperation ex) {
throw new InconsistentStateException("Unexpected exception while calculating distance", ex);
}
}).orElse(null);
final Partition.State state = subscriptionNode.map(node -> node.guessState(stat.getTimeline().getEventType(), stat.getPartition())).orElse(Partition.State.UNASSIGNED);
final String streamId = subscriptionNode.map(node -> node.guessStream(stat.getTimeline().getEventType(), stat.getPartition())).orElse("");
final SubscriptionEventTypeStats.Partition.AssignmentType assignmentType = subscriptionNode.map(node -> node.getPartitionAssignmentType(stat.getTimeline().getEventType(), stat.getPartition())).orElse(null);
resultPartitions.add(new SubscriptionEventTypeStats.Partition(lastPosition.getPartition(), state.getDescription(), distance, streamId, assignmentType));
}
resultPartitions.sort(Comparator.comparing(SubscriptionEventTypeStats.Partition::getPartition));
result.add(new SubscriptionEventTypeStats(eventType.getName(), resultPartitions));
}
return result;
}
Aggregations