Search in sources :

Example 1 with EventTypePartitionView

use of org.zalando.nakadi.view.EventTypePartitionView in project nakadi by zalando.

the class PartitionsController method getTopicPartition.

private EventTypePartitionView getTopicPartition(final String eventTypeName, final String partition) throws InternalNakadiException, NoSuchEventTypeException, ServiceUnavailableException {
    final List<Timeline> timelines = timelineService.getActiveTimelinesOrdered(eventTypeName);
    final Optional<PartitionStatistics> firstStats = timelineService.getTopicRepository(timelines.get(0)).loadPartitionStatistics(timelines.get(0), partition);
    if (!firstStats.isPresent()) {
        throw new NotFoundException("partition not found");
    }
    final PartitionStatistics lastStats;
    if (timelines.size() == 1) {
        lastStats = firstStats.get();
    } else {
        lastStats = timelineService.getTopicRepository(timelines.get(timelines.size() - 1)).loadPartitionStatistics(timelines.get(timelines.size() - 1), partition).get();
    }
    return new EventTypePartitionView(eventTypeName, lastStats.getPartition(), cursorConverter.convert(firstStats.get().getFirst()).getOffset(), cursorConverter.convert(lastStats.getLast()).getOffset());
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) PartitionStatistics(org.zalando.nakadi.domain.PartitionStatistics) EventTypePartitionView(org.zalando.nakadi.view.EventTypePartitionView) NotFoundException(org.zalando.nakadi.exceptions.NotFoundException)

Example 2 with EventTypePartitionView

use of org.zalando.nakadi.view.EventTypePartitionView in project nakadi by zalando.

the class PartitionsController method listPartitions.

@RequestMapping(value = "/event-types/{name}/partitions", method = RequestMethod.GET)
public ResponseEntity<?> listPartitions(@PathVariable("name") final String eventTypeName, final NativeWebRequest request) {
    LOG.trace("Get partitions endpoint for event-type '{}' is called", eventTypeName);
    try {
        final EventType eventType = eventTypeRepository.findByName(eventTypeName);
        authorizationValidator.authorizeStreamRead(eventType);
        final List<Timeline> timelines = timelineService.getActiveTimelinesOrdered(eventTypeName);
        final List<PartitionStatistics> firstStats = timelineService.getTopicRepository(timelines.get(0)).loadTopicStatistics(Collections.singletonList(timelines.get(0)));
        final List<PartitionStatistics> lastStats;
        if (timelines.size() == 1) {
            lastStats = firstStats;
        } else {
            lastStats = timelineService.getTopicRepository(timelines.get(timelines.size() - 1)).loadTopicStatistics(Collections.singletonList(timelines.get(timelines.size() - 1)));
        }
        final List<EventTypePartitionView> result = firstStats.stream().map(first -> {
            final PartitionStatistics last = lastStats.stream().filter(l -> l.getPartition().equals(first.getPartition())).findAny().get();
            return new EventTypePartitionView(eventTypeName, first.getPartition(), cursorConverter.convert(first.getFirst()).getOffset(), cursorConverter.convert(last.getLast()).getOffset());
        }).collect(Collectors.toList());
        return ok().body(result);
    } catch (final NoSuchEventTypeException e) {
        return create(Problem.valueOf(NOT_FOUND, "topic not found"), request);
    } catch (final NakadiException e) {
        LOG.error("Could not list partitions. Respond with SERVICE_UNAVAILABLE.", e);
        return create(e.asProblem(), request);
    }
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) MoreStatus(org.zalando.problem.MoreStatus) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) LoggerFactory(org.slf4j.LoggerFactory) NotFoundException(org.zalando.nakadi.exceptions.NotFoundException) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) NakadiException(org.zalando.nakadi.exceptions.NakadiException) Cursor(org.zalando.nakadi.view.Cursor) MyNakadiRuntimeException1(org.zalando.nakadi.exceptions.runtime.MyNakadiRuntimeException1) Responses.create(org.zalando.problem.spring.web.advice.Responses.create) NoSuchEventTypeException(org.zalando.nakadi.exceptions.NoSuchEventTypeException) InvalidCursorOperation(org.zalando.nakadi.exceptions.runtime.InvalidCursorOperation) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) Lists(com.google.common.collect.Lists) NakadiCursorLag(org.zalando.nakadi.domain.NakadiCursorLag) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) Problem(org.zalando.problem.Problem) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) TimelineService(org.zalando.nakadi.service.timeline.TimelineService) Nullable(javax.annotation.Nullable) EventType(org.zalando.nakadi.domain.EventType) Logger(org.slf4j.Logger) NOT_FOUND(javax.ws.rs.core.Response.Status.NOT_FOUND) PartitionStatistics(org.zalando.nakadi.domain.PartitionStatistics) ResponseEntity.ok(org.springframework.http.ResponseEntity.ok) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Responses(org.zalando.problem.spring.web.advice.Responses) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) AuthorizationValidator(org.zalando.nakadi.service.AuthorizationValidator) List(java.util.List) CursorLag(org.zalando.nakadi.view.CursorLag) EventTypePartitionView(org.zalando.nakadi.view.EventTypePartitionView) Response(javax.ws.rs.core.Response) Timeline(org.zalando.nakadi.domain.Timeline) EventTypeRepository(org.zalando.nakadi.repository.EventTypeRepository) Optional(java.util.Optional) ResponseEntity(org.springframework.http.ResponseEntity) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException) Collections(java.util.Collections) CursorConverter(org.zalando.nakadi.service.CursorConverter) CursorOperationsService(org.zalando.nakadi.service.CursorOperationsService) Timeline(org.zalando.nakadi.domain.Timeline) EventType(org.zalando.nakadi.domain.EventType) PartitionStatistics(org.zalando.nakadi.domain.PartitionStatistics) EventTypePartitionView(org.zalando.nakadi.view.EventTypePartitionView) NoSuchEventTypeException(org.zalando.nakadi.exceptions.NoSuchEventTypeException) NakadiException(org.zalando.nakadi.exceptions.NakadiException) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with EventTypePartitionView

use of org.zalando.nakadi.view.EventTypePartitionView in project nakadi by zalando.

the class HilaAT method whenPatchThenCursorsAreInitializedToDefault.

@Test(timeout = 15000)
public void whenPatchThenCursorsAreInitializedToDefault() throws Exception {
    final EventType et = createEventType();
    publishEvents(et.getName(), 10, i -> "{\"foo\": \"bar\"}");
    Thread.sleep(1000L);
    final Subscription s = createSubscription(RandomSubscriptionBuilder.builder().withEventType(et.getName()).withStartFrom(END).buildSubscriptionBase());
    given().body(MAPPER.writeValueAsString(new ItemsWrapper<>(Collections.emptyList()))).contentType(JSON).patch("/subscriptions/{id}/cursors", s.getId()).then().statusCode(SC_NO_CONTENT);
    final ItemsWrapper<SubscriptionCursor> subscriptionCursors = MAPPER.readValue(given().get("/subscriptions/{id}/cursors", s.getId()).getBody().asString(), new TypeReference<ItemsWrapper<SubscriptionCursor>>() {
    });
    final List<EventTypePartitionView> etStats = MAPPER.readValue(given().get("/event-types/{et}/partitions", et.getName()).getBody().asString(), new TypeReference<List<EventTypePartitionView>>() {
    });
    Assert.assertEquals(subscriptionCursors.getItems().size(), etStats.size());
    subscriptionCursors.getItems().forEach(sCursor -> {
        final boolean offsetSame = etStats.stream().anyMatch(ss -> ss.getPartitionId().equals(sCursor.getPartition()) && ss.getNewestAvailableOffset().equals(sCursor.getOffset()));
        // Check that after patch cursors are the same as END
        Assert.assertTrue(offsetSame);
    });
}
Also used : SubscriptionCursor(org.zalando.nakadi.view.SubscriptionCursor) NakadiTestUtils.createEventType(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createEventType) EventType(org.zalando.nakadi.domain.EventType) EventTypePartitionView(org.zalando.nakadi.view.EventTypePartitionView) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Subscription(org.zalando.nakadi.domain.Subscription) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) ItemsWrapper(org.zalando.nakadi.domain.ItemsWrapper) Test(org.junit.Test)

Example 4 with EventTypePartitionView

use of org.zalando.nakadi.view.EventTypePartitionView in project nakadi by zalando.

the class HilaAT method whenPatchThenCursorsAreInitializedAndPatched.

@Test(timeout = 15000)
public void whenPatchThenCursorsAreInitializedAndPatched() throws Exception {
    final EventType et = createEventType();
    publishEvents(et.getName(), 10, i -> "{\"foo\": \"bar\"}");
    final List<EventTypePartitionView> etStats = MAPPER.readValue(given().get("/event-types/{et}/partitions", et.getName()).getBody().asString(), new TypeReference<List<EventTypePartitionView>>() {
    });
    final EventTypePartitionView begin = etStats.get(0);
    final Subscription s = createSubscription(RandomSubscriptionBuilder.builder().withEventType(et.getName()).withStartFrom(END).buildSubscriptionBase());
    given().body(MAPPER.writeValueAsString(new ItemsWrapper<>(Collections.singletonList(new SubscriptionCursorWithoutToken(et.getName(), begin.getPartitionId(), begin.getOldestAvailableOffset()))))).contentType(JSON).patch("/subscriptions/{id}/cursors", s.getId()).then().statusCode(SC_NO_CONTENT);
    final ItemsWrapper<SubscriptionCursor> subscriptionCursors = MAPPER.readValue(given().get("/subscriptions/{id}/cursors", s.getId()).getBody().asString(), new TypeReference<ItemsWrapper<SubscriptionCursor>>() {
    });
    Assert.assertEquals(subscriptionCursors.getItems().size(), etStats.size());
    subscriptionCursors.getItems().forEach(item -> {
        if (item.getPartition().equals(begin.getPartitionId())) {
            Assert.assertEquals(begin.getOldestAvailableOffset(), item.getOffset());
        } else {
            Assert.assertEquals(begin.getNewestAvailableOffset(), item.getOffset());
        }
    });
}
Also used : SubscriptionCursor(org.zalando.nakadi.view.SubscriptionCursor) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) NakadiTestUtils.createEventType(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createEventType) EventType(org.zalando.nakadi.domain.EventType) EventTypePartitionView(org.zalando.nakadi.view.EventTypePartitionView) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Subscription(org.zalando.nakadi.domain.Subscription) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) ItemsWrapper(org.zalando.nakadi.domain.ItemsWrapper) Test(org.junit.Test)

Example 5 with EventTypePartitionView

use of org.zalando.nakadi.view.EventTypePartitionView in project nakadi by zalando.

the class PartitionsController method getPartition.

@RequestMapping(value = "/event-types/{name}/partitions/{partition}", method = RequestMethod.GET)
public ResponseEntity<?> getPartition(@PathVariable("name") final String eventTypeName, @PathVariable("partition") final String partition, @Nullable @RequestParam(value = "consumed_offset", required = false) final String consumedOffset, final NativeWebRequest request) {
    LOG.trace("Get partition endpoint for event-type '{}', partition '{}' is called", eventTypeName, partition);
    try {
        final EventType eventType = eventTypeRepository.findByName(eventTypeName);
        authorizationValidator.authorizeStreamRead(eventType);
        if (consumedOffset != null) {
            final CursorLag cursorLag = getCursorLag(eventTypeName, partition, consumedOffset);
            return ok().body(cursorLag);
        } else {
            final EventTypePartitionView result = getTopicPartition(eventTypeName, partition);
            return ok().body(result);
        }
    } catch (final NoSuchEventTypeException e) {
        return create(Problem.valueOf(NOT_FOUND, "topic not found"), request);
    } catch (final NakadiException e) {
        LOG.error("Could not get partition. Respond with SERVICE_UNAVAILABLE.", e);
        return create(e.asProblem(), request);
    } catch (final InvalidCursorException e) {
        return create(Problem.valueOf(MoreStatus.UNPROCESSABLE_ENTITY, INVALID_CURSOR_MESSAGE), request);
    }
}
Also used : EventType(org.zalando.nakadi.domain.EventType) EventTypePartitionView(org.zalando.nakadi.view.EventTypePartitionView) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) NakadiCursorLag(org.zalando.nakadi.domain.NakadiCursorLag) CursorLag(org.zalando.nakadi.view.CursorLag) NoSuchEventTypeException(org.zalando.nakadi.exceptions.NoSuchEventTypeException) NakadiException(org.zalando.nakadi.exceptions.NakadiException) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

EventTypePartitionView (org.zalando.nakadi.view.EventTypePartitionView)5 EventType (org.zalando.nakadi.domain.EventType)4 List (java.util.List)3 ImmutableList (com.google.common.collect.ImmutableList)2 Test (org.junit.Test)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ItemsWrapper (org.zalando.nakadi.domain.ItemsWrapper)2 NakadiCursorLag (org.zalando.nakadi.domain.NakadiCursorLag)2 PartitionStatistics (org.zalando.nakadi.domain.PartitionStatistics)2 Subscription (org.zalando.nakadi.domain.Subscription)2 Timeline (org.zalando.nakadi.domain.Timeline)2 InternalNakadiException (org.zalando.nakadi.exceptions.InternalNakadiException)2 InvalidCursorException (org.zalando.nakadi.exceptions.InvalidCursorException)2 NakadiException (org.zalando.nakadi.exceptions.NakadiException)2 NoSuchEventTypeException (org.zalando.nakadi.exceptions.NoSuchEventTypeException)2 NotFoundException (org.zalando.nakadi.exceptions.NotFoundException)2 Lists (com.google.common.collect.Lists)1 Collections (java.util.Collections)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1