Search in sources :

Example 86 with EventType

use of org.zalando.nakadi.domain.EventType 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 87 with EventType

use of org.zalando.nakadi.domain.EventType in project nakadi by zalando.

the class SubscriptionAT method testSubscriptionWithInitialCursors.

@Test
public void testSubscriptionWithInitialCursors() throws Exception {
    final EventType et1 = createBusinessEventTypeWithPartitions(2);
    final EventType et2 = createBusinessEventTypeWithPartitions(2);
    // write 10 events to each partition of two event-types
    publishBusinessEventWithUserDefinedPartition(et1.getName(), 10, i -> "dummy", i -> "0");
    publishBusinessEventWithUserDefinedPartition(et1.getName(), 10, i -> "dummy", i -> "1");
    publishBusinessEventWithUserDefinedPartition(et2.getName(), 10, i -> "dummy", i -> "0");
    publishBusinessEventWithUserDefinedPartition(et2.getName(), 10, i -> "dummy", i -> "1");
    // create subscription with initial cursors
    final SubscriptionBase subscriptionBase = RandomSubscriptionBuilder.builder().withEventTypes(ImmutableSet.of(et1.getName(), et2.getName())).withStartFrom(SubscriptionBase.InitialPosition.CURSORS).withInitialCursors(ImmutableList.of(new SubscriptionCursorWithoutToken(et1.getName(), "0", "000000000000000007"), new SubscriptionCursorWithoutToken(et1.getName(), "1", "000000000000000002"), new SubscriptionCursorWithoutToken(et2.getName(), "0", Cursor.BEFORE_OLDEST_OFFSET), new SubscriptionCursorWithoutToken(et2.getName(), "1", "000000000000000009"))).buildSubscriptionBase();
    final Subscription subscription = createSubscription(subscriptionBase);
    final TestStreamingClient client = TestStreamingClient.create(URL, subscription.getId(), "max_uncommitted_events=100").start();
    // we should read 19 events in total
    waitFor(() -> assertThat(client.getBatches(), hasSize(19)));
    final List<StreamBatch> batches = client.getBatches();
    // check that first events of each partition have correct offsets
    assertThat(getFirstBatchOffsetFor(batches, new EventTypePartition(et1.getName(), "0")), equalTo(Optional.of("001-0001-000000000000000008")));
    assertThat(getFirstBatchOffsetFor(batches, new EventTypePartition(et1.getName(), "1")), equalTo(Optional.of("001-0001-000000000000000003")));
    assertThat(getFirstBatchOffsetFor(batches, new EventTypePartition(et2.getName(), "0")), equalTo(Optional.of("001-0001-000000000000000000")));
    assertThat(getFirstBatchOffsetFor(batches, new EventTypePartition(et2.getName(), "1")), equalTo(Optional.empty()));
}
Also used : SubscriptionBase(org.zalando.nakadi.domain.SubscriptionBase) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) TestUtils.buildDefaultEventType(org.zalando.nakadi.utils.TestUtils.buildDefaultEventType) NakadiTestUtils.createSubscriptionForEventType(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscriptionForEventType) EventType(org.zalando.nakadi.domain.EventType) Subscription(org.zalando.nakadi.domain.Subscription) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Test(org.junit.Test)

Example 88 with EventType

use of org.zalando.nakadi.domain.EventType in project nakadi by zalando.

the class TimelineConsumptionTest method test2TimelinesInaRow.

@Test
public void test2TimelinesInaRow() throws IOException, InterruptedException {
    final EventType eventType = createEventType();
    final CountDownLatch finished = new CountDownLatch(1);
    final AtomicReference<String[]> inTimelineCursors = new AtomicReference<>();
    createParallelConsumer(eventType.getName(), 5, finished, inTimelineCursors::set);
    publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}");
    // Still old topic
    createTimeline(eventType.getName());
    // New topic
    createTimeline(eventType.getName());
    // Another new topic
    createTimeline(eventType.getName());
    publishEvents(eventType.getName(), 1, i -> "{\"foo\":\"bar\"}");
    createTimeline(eventType.getName());
    createTimeline(eventType.getName());
    publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}");
    finished.await();
    Assert.assertArrayEquals(new String[] { "001-0001-000000000000000000", "001-0001-000000000000000001", "001-0004-000000000000000000", "001-0006-000000000000000000", "001-0006-000000000000000001" }, inTimelineCursors.get());
    final String[] receivedOffsets = readCursors(eventType.getName(), "BEGIN", 5);
    Assert.assertArrayEquals(new String[] { "001-0001-000000000000000000", "001-0001-000000000000000001", "001-0004-000000000000000000", "001-0006-000000000000000000", "001-0006-000000000000000001" }, receivedOffsets);
}
Also used : NakadiTestUtils.createEventType(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createEventType) EventType(org.zalando.nakadi.domain.EventType) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 89 with EventType

use of org.zalando.nakadi.domain.EventType in project nakadi by zalando.

the class NakadiTestUtils method createEventType.

public static EventType createEventType() throws JsonProcessingException {
    final EventType eventType = buildSimpleEventType();
    createEventTypeInNakadi(eventType);
    return eventType;
}
Also used : EventType(org.zalando.nakadi.domain.EventType)

Example 90 with EventType

use of org.zalando.nakadi.domain.EventType in project nakadi by zalando.

the class NakadiTestUtils method createBusinessEventTypeWithPartitions.

public static EventType createBusinessEventTypeWithPartitions(final int partitionNum) throws JsonProcessingException {
    final EventTypeStatistics statistics = new EventTypeStatistics();
    statistics.setMessageSize(1);
    statistics.setMessagesPerMinute(1);
    statistics.setReadParallelism(partitionNum);
    statistics.setWriteParallelism(1);
    final EventType eventType = buildSimpleEventType();
    eventType.setCategory(EventCategory.BUSINESS);
    eventType.setEnrichmentStrategies(ImmutableList.of(EnrichmentStrategyDescriptor.METADATA_ENRICHMENT));
    eventType.setPartitionStrategy(PartitionStrategy.USER_DEFINED_STRATEGY);
    eventType.setDefaultStatistic(statistics);
    createEventTypeInNakadi(eventType);
    return eventType;
}
Also used : EventType(org.zalando.nakadi.domain.EventType) EventTypeStatistics(org.zalando.nakadi.domain.EventTypeStatistics)

Aggregations

EventType (org.zalando.nakadi.domain.EventType)216 Test (org.junit.Test)183 TestUtils.buildDefaultEventType (org.zalando.nakadi.utils.TestUtils.buildDefaultEventType)138 JSONObject (org.json.JSONObject)40 Problem (org.zalando.problem.Problem)21 InternalNakadiException (org.zalando.nakadi.exceptions.InternalNakadiException)18 Matchers.containsString (org.hamcrest.Matchers.containsString)17 JSONArray (org.json.JSONArray)17 EventPublishResult (org.zalando.nakadi.domain.EventPublishResult)17 TestUtils.invalidProblem (org.zalando.nakadi.utils.TestUtils.invalidProblem)17 ThrowableProblem (org.zalando.problem.ThrowableProblem)17 Timeline (org.zalando.nakadi.domain.Timeline)16 NoSuchEventTypeException (org.zalando.nakadi.exceptions.NoSuchEventTypeException)14 EventTypeTestBuilder (org.zalando.nakadi.utils.EventTypeTestBuilder)14 BatchItem (org.zalando.nakadi.domain.BatchItem)12 TestUtils.resourceAsString (org.zalando.nakadi.utils.TestUtils.resourceAsString)12 List (java.util.List)11 Subscription (org.zalando.nakadi.domain.Subscription)11 TestUtils.createBatchItem (org.zalando.nakadi.utils.TestUtils.createBatchItem)11 Collectors (java.util.stream.Collectors)9