use of org.zalando.nakadi.exceptions.TimelineException in project nakadi by zalando.
the class TimelineService method getActiveTimeline.
public Timeline getActiveTimeline(final String eventTypeName) throws TimelineException {
try {
final List<Timeline> timelines = eventTypeCache.getTimelinesOrdered(eventTypeName);
final ListIterator<Timeline> rIterator = timelines.listIterator(timelines.size());
while (rIterator.hasPrevious()) {
final Timeline toCheck = rIterator.previous();
if (toCheck.getSwitchedAt() != null) {
return toCheck;
}
}
throw new TimelineException(String.format("No timelines for event type %s", eventTypeName));
} catch (final NakadiException e) {
LOG.error("Failed to get timeline for event type {}", eventTypeName, e);
throw new TimelineException("Failed to get timeline", e);
}
}
use of org.zalando.nakadi.exceptions.TimelineException 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.TimelineException in project nakadi by zalando.
the class TimelineService method switchTimelines.
private void switchTimelines(final Timeline activeTimeline, final Timeline nextTimeline) throws InconsistentStateException, RepositoryProblemException, TimelineException, ConflictException {
LOG.info("Switching timelines from {} to {}", activeTimeline, nextTimeline);
try {
timelineSync.startTimelineUpdate(activeTimeline.getEventType(), nakadiSettings.getTimelineWaitTimeoutMs());
} catch (final InterruptedException ie) {
Thread.currentThread().interrupt();
throw new TimelineException("Failed to switch timeline for: " + activeTimeline.getEventType());
} catch (final IllegalStateException ie) {
throw new ConflictException("Timeline is already being created for: " + activeTimeline.getEventType(), ie);
}
try {
transactionTemplate.execute(status -> {
timelineDbRepository.createTimeline(nextTimeline);
nextTimeline.setSwitchedAt(new Date());
final Timeline.StoragePosition sp = topicRepositoryHolder.createStoragePosition(activeTimeline);
activeTimeline.setLatestPosition(sp);
scheduleTimelineCleanup(activeTimeline);
timelineDbRepository.updateTimelime(activeTimeline);
timelineDbRepository.updateTimelime(nextTimeline);
return null;
});
} catch (final TransactionException tx) {
LOG.error(tx.getMessage(), tx);
throw new TimelineException("Failed to create timeline in DB for: " + activeTimeline.getEventType(), tx);
} finally {
finishTimelineUpdate(activeTimeline.getEventType());
}
}
use of org.zalando.nakadi.exceptions.TimelineException in project nakadi by zalando.
the class TimelineService method createTimeline.
public void createTimeline(final String eventTypeName, final String storageId) throws AccessDeniedException, TimelineException, TopicRepositoryException, InconsistentStateException, RepositoryProblemException, DbWriteOperationsBlockedException {
if (featureToggleService.isFeatureEnabled(FeatureToggleService.Feature.DISABLE_DB_WRITE_OPERATIONS)) {
throw new DbWriteOperationsBlockedException("Cannot create timeline: write operations on DB " + "are blocked by feature flag.");
}
try {
final EventType eventType = eventTypeCache.getEventType(eventTypeName);
if (!adminService.isAdmin(AuthorizationService.Operation.WRITE)) {
final Resource resource = new EventTypeResource(eventTypeName, eventType.getAuthorization());
throw new AccessDeniedException(AuthorizationService.Operation.ADMIN, resource);
}
final Storage storage = storageDbRepository.getStorage(storageId).orElseThrow(() -> new UnableProcessException("No storage with id: " + storageId));
final Timeline activeTimeline = getActiveTimeline(eventType);
final TopicRepository currentTopicRepo = topicRepositoryHolder.getTopicRepository(activeTimeline.getStorage());
final TopicRepository nextTopicRepo = topicRepositoryHolder.getTopicRepository(storage);
final List<PartitionStatistics> partitionStatistics = currentTopicRepo.loadTopicStatistics(Collections.singleton(activeTimeline));
final String newTopic = nextTopicRepo.createTopic(partitionStatistics.size(), eventType.getOptions().getRetentionTime());
final Timeline nextTimeline = Timeline.createTimeline(activeTimeline.getEventType(), activeTimeline.getOrder() + 1, storage, newTopic, new Date());
switchTimelines(activeTimeline, nextTimeline);
} catch (final TopicCreationException | ServiceUnavailableException | InternalNakadiException e) {
throw new TimelineException("Internal service error", e);
} catch (final NoSuchEventTypeException e) {
throw new NotFoundException("EventType \"" + eventTypeName + "\" does not exist", e);
}
}
use of org.zalando.nakadi.exceptions.TimelineException in project nakadi by zalando.
the class EventTypeService method deleteEventType.
private Multimap<TopicRepository, String> deleteEventType(final String eventTypeName) throws EventTypeUnavailableException, EventTypeDeletionException {
try {
final Multimap<TopicRepository, String> topicsToDelete = timelineService.deleteAllTimelinesForEventType(eventTypeName);
eventTypeRepository.removeEventType(eventTypeName);
return topicsToDelete;
} catch (TimelineException | NotFoundException e) {
LOG.error("Problem deleting timeline for event type " + eventTypeName, e);
throw new EventTypeDeletionException("Failed to delete timelines for event type " + eventTypeName);
} catch (NakadiException e) {
LOG.error("Error deleting event type " + eventTypeName, e);
throw new EventTypeDeletionException("Failed to delete event type " + eventTypeName);
}
}
Aggregations