Search in sources :

Example 1 with NoSuchEventTypeException

use of org.zalando.nakadi.exceptions.NoSuchEventTypeException in project nakadi by zalando.

the class EventTypeControllerTest method askingForANonExistingEventTypeResultsIn404.

@Test
public void askingForANonExistingEventTypeResultsIn404() throws Exception {
    final String eventTypeName = randomValidEventTypeName();
    when(eventTypeRepository.findByName(anyString())).thenThrow(new NoSuchEventTypeException(String.format("EventType '%s' does not exist.", eventTypeName)));
    final MockHttpServletRequestBuilder requestBuilder = get("/event-types/" + eventTypeName).accept(APPLICATION_JSON);
    final ThrowableProblem expectedProblem = Problem.valueOf(NOT_FOUND, "EventType '" + eventTypeName + "' does not exist.");
    mockMvc.perform(requestBuilder).andExpect(status().is(404)).andExpect(content().contentTypeCompatibleWith("application/problem+json")).andExpect(content().string(matchesProblem(expectedProblem)));
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) ThrowableProblem(org.zalando.problem.ThrowableProblem) Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) NoSuchEventTypeException(org.zalando.nakadi.exceptions.NoSuchEventTypeException) Test(org.junit.Test)

Example 2 with NoSuchEventTypeException

use of org.zalando.nakadi.exceptions.NoSuchEventTypeException 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);
    }
}
Also used : InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException) EventType(org.zalando.nakadi.domain.EventType) InconsistentStateException(org.zalando.nakadi.exceptions.runtime.InconsistentStateException) Date(java.util.Date) NoSuchEventTypeException(org.zalando.nakadi.exceptions.NoSuchEventTypeException)

Example 3 with NoSuchEventTypeException

use of org.zalando.nakadi.exceptions.NoSuchEventTypeException 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);
    }
}
Also used : StartingState(org.zalando.nakadi.service.subscription.state.StartingState) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) NakadiSettings(org.zalando.nakadi.config.NakadiSettings) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) Subscription(org.zalando.nakadi.domain.Subscription) NakadiException(org.zalando.nakadi.exceptions.NakadiException) SubscriptionNotInitializedException(org.zalando.nakadi.service.subscription.zk.SubscriptionNotInitializedException) NoSuchEventTypeException(org.zalando.nakadi.exceptions.NoSuchEventTypeException) ZkSubscriptionClient(org.zalando.nakadi.service.subscription.zk.ZkSubscriptionClient) UnableProcessException(org.zalando.nakadi.exceptions.UnableProcessException) Partition(org.zalando.nakadi.service.subscription.model.Partition) UUIDGenerator(org.zalando.nakadi.util.UUIDGenerator) ImmutableList(com.google.common.collect.ImmutableList) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) Map(java.util.Map) TimelineService(org.zalando.nakadi.service.timeline.TimelineService) TopicRepository(org.zalando.nakadi.repository.TopicRepository) SubscriptionClientFactory(org.zalando.nakadi.service.subscription.zk.SubscriptionClientFactory) EventTypeCache(org.zalando.nakadi.repository.db.EventTypeCache) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) OperationTimeoutException(org.zalando.nakadi.exceptions.runtime.OperationTimeoutException) ZookeeperException(org.zalando.nakadi.exceptions.runtime.ZookeeperException) Collectors(java.util.stream.Collectors) TimeLogger(org.zalando.nakadi.util.TimeLogger) TimeUnit(java.util.concurrent.TimeUnit) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) Component(org.springframework.stereotype.Component) List(java.util.List) Stream(java.util.stream.Stream) ServiceTemporarilyUnavailableException(org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException) SubscriptionDbRepository(org.zalando.nakadi.repository.db.SubscriptionDbRepository) InvalidStreamIdException(org.zalando.nakadi.exceptions.InvalidStreamIdException) NoSuchSubscriptionException(org.zalando.nakadi.exceptions.NoSuchSubscriptionException) CursorError(org.zalando.nakadi.domain.CursorError) Comparator(java.util.Comparator) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) ZkSubscriptionClient(org.zalando.nakadi.service.subscription.zk.ZkSubscriptionClient) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) TopicRepository(org.zalando.nakadi.repository.TopicRepository) Subscription(org.zalando.nakadi.domain.Subscription) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with NoSuchEventTypeException

use of org.zalando.nakadi.exceptions.NoSuchEventTypeException in project nakadi by zalando.

the class CachingEventTypeRepository method removeEventType.

@Override
public void removeEventType(final String name) throws InternalNakadiException, NoSuchEventTypeException {
    final EventType original = this.repository.findByName(name);
    this.repository.removeEventType(name);
    try {
        this.cache.removed(name);
    } catch (Exception e) {
        LOG.error("Failed to remove entry from cache '" + name + "'");
        try {
            this.repository.saveEventType(original);
        } catch (DuplicatedEventTypeNameException e1) {
            LOG.error("Failed to rollback db removal", e);
        }
        throw new InternalNakadiException("Failed to remove event type", e);
    }
}
Also used : InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException) DuplicatedEventTypeNameException(org.zalando.nakadi.exceptions.DuplicatedEventTypeNameException) EventType(org.zalando.nakadi.domain.EventType) DuplicatedEventTypeNameException(org.zalando.nakadi.exceptions.DuplicatedEventTypeNameException) NoSuchEventTypeException(org.zalando.nakadi.exceptions.NoSuchEventTypeException) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException)

Example 5 with NoSuchEventTypeException

use of org.zalando.nakadi.exceptions.NoSuchEventTypeException in project nakadi by zalando.

the class VersionZeroConverter method convertBatched.

public List<NakadiCursor> convertBatched(final List<SubscriptionCursorWithoutToken> cursors) throws InvalidCursorException, InternalNakadiException, NoSuchEventTypeException, ServiceUnavailableException {
    final NakadiCursor[] result = new NakadiCursor[cursors.size()];
    for (int idx = 0; idx < cursors.size(); ++idx) {
        final SubscriptionCursorWithoutToken cursor = cursors.get(idx);
        if (Cursor.BEFORE_OLDEST_OFFSET.equalsIgnoreCase(cursor.getOffset())) {
            // Preform begin checks afterwards to optimize calls
            continue;
        }
        if (!NUMBERS_ONLY_PATTERN.matcher(cursor.getOffset()).matches()) {
            throw new InvalidCursorException(CursorError.INVALID_OFFSET, cursor);
        }
    }
    // now it is time for massive convert.
    final LinkedHashMap<SubscriptionCursorWithoutToken, NakadiCursor> beginsToConvert = new LinkedHashMap<>();
    final Map<SubscriptionCursorWithoutToken, Timeline> cursorTimelines = new HashMap<>();
    final Map<TopicRepository, List<SubscriptionCursorWithoutToken>> repos = new HashMap<>();
    for (int i = 0; i < result.length; ++i) {
        if (null == result[i]) {
            // cursor requires database hit
            final SubscriptionCursorWithoutToken cursor = cursors.get(i);
            final Timeline timeline = timelineService.getActiveTimelinesOrdered(cursor.getEventType()).get(0);
            final TopicRepository topicRepo = timelineService.getTopicRepository(timeline);
            beginsToConvert.put(cursor, null);
            cursorTimelines.put(cursor, timeline);
            repos.computeIfAbsent(topicRepo, k -> new ArrayList<>()).add(cursor);
        }
    }
    for (final Map.Entry<TopicRepository, List<SubscriptionCursorWithoutToken>> entry : repos.entrySet()) {
        final List<Optional<PartitionStatistics>> stats = entry.getKey().loadPartitionStatistics(entry.getValue().stream().map(scwt -> new TopicRepository.TimelinePartition(cursorTimelines.get(scwt), scwt.getPartition())).collect(Collectors.toList()));
        for (int idx = 0; idx < entry.getValue().size(); ++idx) {
            // Reinsert doesn't change the order
            beginsToConvert.put(entry.getValue().get(idx), stats.get(idx).orElseThrow(() -> new InvalidCursorException(PARTITION_NOT_FOUND)).getBeforeFirst());
        }
    }
    final Iterator<NakadiCursor> missingBegins = beginsToConvert.values().iterator();
    return Stream.of(result).map(it -> null == it ? missingBegins.next() : it).collect(Collectors.toList());
}
Also used : NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) HashMap(java.util.HashMap) NUMBERS_ONLY_PATTERN(org.zalando.nakadi.util.CursorConversionUtils.NUMBERS_ONLY_PATTERN) Cursor(org.zalando.nakadi.view.Cursor) StringUtils(org.apache.commons.lang3.StringUtils) NoSuchEventTypeException(org.zalando.nakadi.exceptions.NoSuchEventTypeException) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) PARTITION_NOT_FOUND(org.zalando.nakadi.domain.CursorError.PARTITION_NOT_FOUND) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) Map(java.util.Map) TimelineService(org.zalando.nakadi.service.timeline.TimelineService) TopicRepository(org.zalando.nakadi.repository.TopicRepository) Iterator(java.util.Iterator) PartitionStatistics(org.zalando.nakadi.domain.PartitionStatistics) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) Timeline(org.zalando.nakadi.domain.Timeline) Optional(java.util.Optional) CursorError(org.zalando.nakadi.domain.CursorError) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException) CursorConverter(org.zalando.nakadi.service.CursorConverter) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) Optional(java.util.Optional) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Timeline(org.zalando.nakadi.domain.Timeline) ArrayList(java.util.ArrayList) List(java.util.List) TopicRepository(org.zalando.nakadi.repository.TopicRepository) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

NoSuchEventTypeException (org.zalando.nakadi.exceptions.NoSuchEventTypeException)18 InternalNakadiException (org.zalando.nakadi.exceptions.InternalNakadiException)14 EventType (org.zalando.nakadi.domain.EventType)10 ServiceUnavailableException (org.zalando.nakadi.exceptions.ServiceUnavailableException)9 InvalidCursorException (org.zalando.nakadi.exceptions.InvalidCursorException)7 NakadiCursor (org.zalando.nakadi.domain.NakadiCursor)6 NakadiException (org.zalando.nakadi.exceptions.NakadiException)6 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 PartitionStatistics (org.zalando.nakadi.domain.PartitionStatistics)5 Timeline (org.zalando.nakadi.domain.Timeline)5 TopicRepository (org.zalando.nakadi.repository.TopicRepository)5 TimelineService (org.zalando.nakadi.service.timeline.TimelineService)5 Map (java.util.Map)4 Optional (java.util.Optional)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 CursorError (org.zalando.nakadi.domain.CursorError)4 ServiceTemporarilyUnavailableException (org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException)4 CursorConverter (org.zalando.nakadi.service.CursorConverter)4