use of org.zalando.nakadi.exceptions.ServiceUnavailableException in project nakadi by zalando.
the class CursorOperationsController method getDistance.
@RequestMapping(path = "/event-types/{eventTypeName}/cursor-distances", method = RequestMethod.POST)
public ResponseEntity<?> getDistance(@PathVariable("eventTypeName") final String eventTypeName, @Valid @RequestBody final ValidListWrapper<CursorDistance> queries) throws InternalNakadiException, NoSuchEventTypeException {
final EventType eventType = eventTypeRepository.findByName(eventTypeName);
authorizationValidator.authorizeStreamRead(eventType);
queries.getList().forEach(query -> {
try {
final NakadiCursor initialCursor = cursorConverter.convert(eventTypeName, query.getInitialCursor());
final NakadiCursor finalCursor = cursorConverter.convert(eventTypeName, query.getFinalCursor());
final Long distance = cursorOperationsService.calculateDistance(initialCursor, finalCursor);
query.setDistance(distance);
} catch (InternalNakadiException | ServiceUnavailableException e) {
throw new MyNakadiRuntimeException1("problem calculating cursors distance", e);
} catch (final NoSuchEventTypeException e) {
throw new NotFoundException("event type not found", e);
} catch (final InvalidCursorException e) {
throw new CursorConversionException("problem converting cursors", e);
}
});
return status(OK).body(queries.getList());
}
use of org.zalando.nakadi.exceptions.ServiceUnavailableException in project nakadi by zalando.
the class EventStreamController method getStreamingStart.
@VisibleForTesting
List<NakadiCursor> getStreamingStart(final EventType eventType, final String cursorsStr) throws UnparseableCursorException, ServiceUnavailableException, InvalidCursorException, InternalNakadiException, NoSuchEventTypeException {
List<Cursor> cursors = null;
if (cursorsStr != null) {
try {
cursors = jsonMapper.readValue(cursorsStr, new TypeReference<ArrayList<Cursor>>() {
});
} catch (final IOException ex) {
throw new UnparseableCursorException("incorrect syntax of X-nakadi-cursors header", ex, cursorsStr);
}
// Unfortunately, In order to have consistent exception checking, one can not just call validator
for (final Cursor cursor : cursors) {
if (null == cursor.getPartition()) {
throw new InvalidCursorException(CursorError.NULL_PARTITION, cursor);
} else if (null == cursor.getOffset()) {
throw new InvalidCursorException(CursorError.NULL_OFFSET, cursor);
}
}
}
final Timeline latestTimeline = timelineService.getActiveTimeline(eventType);
if (null != cursors) {
if (cursors.isEmpty()) {
throw new InvalidCursorException(CursorError.INVALID_FORMAT);
}
final List<NakadiCursor> result = new ArrayList<>();
for (final Cursor c : cursors) {
result.add(cursorConverter.convert(eventType.getName(), c));
}
for (final NakadiCursor c : result) {
if (c.getTimeline().isDeleted()) {
throw new InvalidCursorException(CursorError.UNAVAILABLE, c);
}
}
final Map<Storage, List<NakadiCursor>> groupedCursors = result.stream().collect(Collectors.groupingBy(c -> c.getTimeline().getStorage()));
for (final Map.Entry<Storage, List<NakadiCursor>> entry : groupedCursors.entrySet()) {
timelineService.getTopicRepository(entry.getKey()).validateReadCursors(entry.getValue());
}
return result;
} else {
final TopicRepository latestTopicRepository = timelineService.getTopicRepository(latestTimeline);
// if no cursors provided - read from the newest available events
return latestTopicRepository.loadTopicStatistics(Collections.singletonList(latestTimeline)).stream().map(PartitionStatistics::getLast).collect(Collectors.toList());
}
}
use of org.zalando.nakadi.exceptions.ServiceUnavailableException in project nakadi by zalando.
the class CursorsControllerTest method whenServiceUnavailableExceptionThenServiceUnavailable.
@Test
public void whenServiceUnavailableExceptionThenServiceUnavailable() throws Exception {
when(cursorsService.commitCursors(any(), any(), any())).thenThrow(new ServiceUnavailableException("dummy-message"));
final Problem expectedProblem = Problem.valueOf(SERVICE_UNAVAILABLE, "dummy-message");
checkForProblem(postCursors(DUMMY_CURSORS), expectedProblem);
}
use of org.zalando.nakadi.exceptions.ServiceUnavailableException 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.ServiceUnavailableException in project nakadi by zalando.
the class SubscriptionControllerTest method whenListSubscriptionsAndExceptionThenServiceUnavailable.
@Test
public void whenListSubscriptionsAndExceptionThenServiceUnavailable() throws Exception {
when(subscriptionRepository.listSubscriptions(any(), any(), anyInt(), anyInt())).thenThrow(new ServiceUnavailableException("dummy message"));
final Problem expectedProblem = Problem.valueOf(SERVICE_UNAVAILABLE, "dummy message");
checkForProblem(getSubscriptions(), expectedProblem);
}
Aggregations