Search in sources :

Example 1 with MyNakadiRuntimeException1

use of org.zalando.nakadi.exceptions.runtime.MyNakadiRuntimeException1 in project nakadi by zalando.

the class AbstractZkSubscriptionClient method runLocked.

@Override
public final <T> T runLocked(final Callable<T> function) {
    try {
        Exception releaseException = null;
        if (null == lock) {
            lock = new InterProcessSemaphoreMutex(curatorFramework, "/nakadi/locks/subscription_" + subscriptionId);
        }
        final boolean acquired = lock.acquire(SECONDS_TO_WAIT_FOR_LOCK, TimeUnit.SECONDS);
        if (!acquired) {
            throw new ServiceUnavailableException("Failed to acquire subscription lock within " + SECONDS_TO_WAIT_FOR_LOCK + " seconds");
        }
        final T result;
        try {
            result = function.call();
        } finally {
            try {
                lock.release();
            } catch (final Exception e) {
                log.error("Failed to release lock", e);
                releaseException = e;
            }
        }
        if (releaseException != null) {
            throw releaseException;
        }
        return result;
    } catch (final NakadiRuntimeException | MyNakadiRuntimeException1 e) {
        throw e;
    } catch (final Exception e) {
        throw new NakadiRuntimeException(e);
    }
}
Also used : NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) InterProcessSemaphoreMutex(org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex) MyNakadiRuntimeException1(org.zalando.nakadi.exceptions.runtime.MyNakadiRuntimeException1) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) RequestInProgressException(org.zalando.nakadi.exceptions.runtime.RequestInProgressException) UnableProcessException(org.zalando.nakadi.exceptions.UnableProcessException) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) OperationInterruptedException(org.zalando.nakadi.exceptions.runtime.OperationInterruptedException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) OperationTimeoutException(org.zalando.nakadi.exceptions.runtime.OperationTimeoutException) ZookeeperException(org.zalando.nakadi.exceptions.runtime.ZookeeperException) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) ServiceTemporarilyUnavailableException(org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException)

Example 2 with MyNakadiRuntimeException1

use of org.zalando.nakadi.exceptions.runtime.MyNakadiRuntimeException1 in project nakadi by zalando.

the class CursorOperationsService method cursorsLag.

public List<NakadiCursorLag> cursorsLag(final String eventTypeName, final List<NakadiCursor> cursors) throws InvalidCursorOperation {
    try {
        final List<Timeline> timelines = timelineService.getActiveTimelinesOrdered(eventTypeName);
        // Next 2 calls could be optimized to 1 storage call, instead of possible 2 calls.
        // But it is simpler not to do anything, cause timelines are not switched every day and almost all the time
        // (except retention time after switch) there will be only 1 active timeline, and this option is covered.
        final List<PartitionStatistics> oldestStats = getStatsForTimeline(timelines.get(0));
        final List<PartitionStatistics> newestStats = timelines.size() == 1 ? oldestStats : getStatsForTimeline(timelines.get(timelines.size() - 1));
        return cursors.stream().map(c -> {
            final PartitionStatistics oldestStat = oldestStats.stream().filter(item -> item.getPartition().equalsIgnoreCase(c.getPartition())).findAny().orElseThrow(() -> new InvalidCursorOperation(PARTITION_NOT_FOUND));
            NakadiCursor newestPosition = newestStats.stream().filter(item -> item.getPartition().equalsIgnoreCase(c.getPartition())).map(PartitionEndStatistics::getLast).findAny().orElseThrow(() -> new InvalidCursorOperation(PARTITION_NOT_FOUND));
            // it
            while (numberOfEventsBeforeCursor(newestPosition) == -1) {
                final int prevOrder = newestPosition.getTimeline().getOrder() - 1;
                final Timeline prevTimeline = timelines.stream().filter(t -> t.getOrder() == prevOrder).findAny().orElse(null);
                if (null == prevTimeline) {
                    break;
                }
                // We moved back, so timeline definitely have latest position set
                newestPosition = prevTimeline.getLatestPosition().toNakadiCursor(prevTimeline, newestPosition.getPartition());
            }
            // calls (in case of kafka)
            return new NakadiCursorLag(oldestStat.getFirst(), newestPosition, calculateDistance(c, newestPosition));
        }).collect(Collectors.toList());
    } catch (final NakadiException e) {
        throw new MyNakadiRuntimeException1("error", e);
    }
}
Also used : Storage(org.zalando.nakadi.domain.Storage) Logger(org.slf4j.Logger) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) LoggerFactory(org.slf4j.LoggerFactory) PartitionEndStatistics(org.zalando.nakadi.domain.PartitionEndStatistics) PartitionStatistics(org.zalando.nakadi.domain.PartitionStatistics) Autowired(org.springframework.beans.factory.annotation.Autowired) NakadiException(org.zalando.nakadi.exceptions.NakadiException) MyNakadiRuntimeException1(org.zalando.nakadi.exceptions.runtime.MyNakadiRuntimeException1) InvalidCursorOperation(org.zalando.nakadi.exceptions.runtime.InvalidCursorOperation) Collectors(java.util.stream.Collectors) KafkaCursor(org.zalando.nakadi.repository.kafka.KafkaCursor) TIMELINE_NOT_FOUND(org.zalando.nakadi.exceptions.runtime.InvalidCursorOperation.Reason.TIMELINE_NOT_FOUND) List(java.util.List) UnknownStorageTypeException(org.zalando.nakadi.exceptions.runtime.UnknownStorageTypeException) Timeline(org.zalando.nakadi.domain.Timeline) PARTITION_NOT_FOUND(org.zalando.nakadi.exceptions.runtime.InvalidCursorOperation.Reason.PARTITION_NOT_FOUND) Service(org.springframework.stereotype.Service) NakadiCursorLag(org.zalando.nakadi.domain.NakadiCursorLag) ShiftedNakadiCursor(org.zalando.nakadi.domain.ShiftedNakadiCursor) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) TimelineService(org.zalando.nakadi.service.timeline.TimelineService) CURSORS_WITH_DIFFERENT_PARTITION(org.zalando.nakadi.exceptions.runtime.InvalidCursorOperation.Reason.CURSORS_WITH_DIFFERENT_PARTITION) Collections(java.util.Collections) Timeline(org.zalando.nakadi.domain.Timeline) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) ShiftedNakadiCursor(org.zalando.nakadi.domain.ShiftedNakadiCursor) MyNakadiRuntimeException1(org.zalando.nakadi.exceptions.runtime.MyNakadiRuntimeException1) PartitionStatistics(org.zalando.nakadi.domain.PartitionStatistics) InvalidCursorOperation(org.zalando.nakadi.exceptions.runtime.InvalidCursorOperation) NakadiCursorLag(org.zalando.nakadi.domain.NakadiCursorLag) NakadiException(org.zalando.nakadi.exceptions.NakadiException)

Example 3 with MyNakadiRuntimeException1

use of org.zalando.nakadi.exceptions.runtime.MyNakadiRuntimeException1 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());
}
Also used : CursorConversionException(org.zalando.nakadi.exceptions.runtime.CursorConversionException) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) ShiftedNakadiCursor(org.zalando.nakadi.domain.ShiftedNakadiCursor) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException) MyNakadiRuntimeException1(org.zalando.nakadi.exceptions.runtime.MyNakadiRuntimeException1) EventType(org.zalando.nakadi.domain.EventType) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) NotFoundException(org.zalando.nakadi.exceptions.NotFoundException) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) NoSuchEventTypeException(org.zalando.nakadi.exceptions.NoSuchEventTypeException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with MyNakadiRuntimeException1

use of org.zalando.nakadi.exceptions.runtime.MyNakadiRuntimeException1 in project nakadi by zalando.

the class PartitionsController method getCursorLag.

private CursorLag getCursorLag(final String eventTypeName, final String partition, final String consumedOffset) throws InternalNakadiException, NoSuchEventTypeException, InvalidCursorException, ServiceUnavailableException {
    final Cursor consumedCursor = new Cursor(partition, consumedOffset);
    final NakadiCursor consumedNakadiCursor = cursorConverter.convert(eventTypeName, consumedCursor);
    return cursorOperationsService.cursorsLag(eventTypeName, Lists.newArrayList(consumedNakadiCursor)).stream().findFirst().map(this::toCursorLag).orElseThrow(MyNakadiRuntimeException1::new);
}
Also used : NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) MyNakadiRuntimeException1(org.zalando.nakadi.exceptions.runtime.MyNakadiRuntimeException1) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) Cursor(org.zalando.nakadi.view.Cursor)

Aggregations

MyNakadiRuntimeException1 (org.zalando.nakadi.exceptions.runtime.MyNakadiRuntimeException1)4 NakadiCursor (org.zalando.nakadi.domain.NakadiCursor)3 ServiceUnavailableException (org.zalando.nakadi.exceptions.ServiceUnavailableException)3 ShiftedNakadiCursor (org.zalando.nakadi.domain.ShiftedNakadiCursor)2 IOException (java.io.IOException)1 Collections (java.util.Collections)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 InterProcessSemaphoreMutex (org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex)1 KeeperException (org.apache.zookeeper.KeeperException)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Service (org.springframework.stereotype.Service)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 EventType (org.zalando.nakadi.domain.EventType)1 NakadiCursorLag (org.zalando.nakadi.domain.NakadiCursorLag)1 PartitionEndStatistics (org.zalando.nakadi.domain.PartitionEndStatistics)1 PartitionStatistics (org.zalando.nakadi.domain.PartitionStatistics)1 Storage (org.zalando.nakadi.domain.Storage)1