Search in sources :

Example 11 with Subscription

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

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

the class SubscriptionService method listSubscriptions.

public Result listSubscriptions(@Nullable final String owningApplication, @Nullable final Set<String> eventTypes, final int limit, final int offset) {
    if (limit < 1 || limit > 1000) {
        final Problem problem = Problem.valueOf(Response.Status.BAD_REQUEST, "'limit' parameter should have value from 1 to 1000");
        return Result.problem(problem);
    }
    if (offset < 0) {
        final Problem problem = Problem.valueOf(Response.Status.BAD_REQUEST, "'offset' parameter can't be lower than 0");
        return Result.problem(problem);
    }
    try {
        final Set<String> eventTypesFilter = eventTypes == null ? ImmutableSet.of() : eventTypes;
        final Optional<String> owningAppOption = Optional.ofNullable(owningApplication);
        final List<Subscription> subscriptions = subscriptionRepository.listSubscriptions(eventTypesFilter, owningAppOption, offset, limit);
        final PaginationLinks paginationLinks = SubscriptionsUriHelper.createSubscriptionPaginationLinks(owningAppOption, eventTypesFilter, offset, limit, subscriptions.size());
        return Result.ok(new PaginationWrapper<>(subscriptions, paginationLinks));
    } catch (final ServiceUnavailableException e) {
        LOG.error("Error occurred during listing of subscriptions", e);
        return Result.problem(e.asProblem());
    }
}
Also used : PaginationLinks(org.zalando.nakadi.domain.PaginationLinks) Problem(org.zalando.problem.Problem) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) Subscription(org.zalando.nakadi.domain.Subscription)

Example 13 with Subscription

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

the class SubscriptionService method getSubscriptionStat.

public ItemsWrapper<SubscriptionEventTypeStats> getSubscriptionStat(final String subscriptionId) throws InconsistentStateException, NoSuchSubscriptionException, ServiceTemporarilyUnavailableException {
    final Subscription subscription;
    try {
        subscription = subscriptionRepository.getSubscription(subscriptionId);
    } catch (final ServiceUnavailableException ex) {
        throw new InconsistentStateException(ex.getMessage());
    }
    final List<SubscriptionEventTypeStats> subscriptionStat = createSubscriptionStat(subscription);
    return new ItemsWrapper<>(subscriptionStat);
}
Also used : ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) SubscriptionEventTypeStats(org.zalando.nakadi.domain.SubscriptionEventTypeStats) Subscription(org.zalando.nakadi.domain.Subscription) ItemsWrapper(org.zalando.nakadi.domain.ItemsWrapper) InconsistentStateException(org.zalando.nakadi.exceptions.runtime.InconsistentStateException)

Example 14 with Subscription

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

the class CursorsServiceAT method before.

@Before
public void before() throws Exception {
    sid = randomUUID();
    streamId = randomUUID();
    etName = randomValidEventTypeName();
    topic = randomUUID();
    cursorConverter = mock(CursorConverter.class);
    testCursors = ImmutableList.of(NakadiCursor.of(buildTimeline(etName, topic, CREATED_AT), P1, NEW_OFFSET));
    final EventType eventType = mock(EventType.class);
    when(eventType.getName()).thenReturn(etName);
    final ZooKeeperHolder zkHolder = mock(ZooKeeperHolder.class);
    when(zkHolder.get()).thenReturn(CURATOR);
    final TopicRepository topicRepository = mock(TopicRepository.class);
    final TimelineService timelineService = mock(TimelineService.class);
    when(timelineService.getTopicRepository((Timeline) any())).thenReturn(topicRepository);
    timeline = buildTimeline(etName, topic, CREATED_AT);
    when(timelineService.getActiveTimeline(any(EventType.class))).thenReturn(timeline);
    final Subscription subscription = mock(Subscription.class);
    when(subscription.getId()).thenReturn(sid);
    when(subscription.getEventTypes()).thenReturn(ImmutableSet.of(etName));
    final SubscriptionDbRepository subscriptionRepo = mock(SubscriptionDbRepository.class);
    when(subscriptionRepo.getSubscription(sid)).thenReturn(subscription);
    final SubscriptionClientFactory zkSubscriptionFactory = new SubscriptionClientFactory(zkHolder, MAPPER);
    uuidGenerator = mock(UUIDGenerator.class);
    when(uuidGenerator.isUUID(any())).thenReturn(true);
    cursorsService = new CursorsService(subscriptionRepo, null, mock(NakadiSettings.class), zkSubscriptionFactory, cursorConverter, uuidGenerator, null);
    // Register cursors in converter
    registerNakadiCursor(NakadiCursor.of(buildTimeline(etName, topic, CREATED_AT), P1, NEW_OFFSET));
    registerNakadiCursor(NakadiCursor.of(buildTimeline(etName, topic, CREATED_AT), P1, OLD_OFFSET));
    registerNakadiCursor(NakadiCursor.of(buildTimeline(etName, topic, CREATED_AT), P2, NEW_OFFSET));
    registerNakadiCursor(NakadiCursor.of(buildTimeline(etName, topic, CREATED_AT), P2, OLD_OFFSET));
    // bootstrap data in ZK
    CURATOR.create().creatingParentsIfNeeded().forPath(offsetPath(P1), OLD_OFFSET.getBytes(UTF_8));
    CURATOR.create().creatingParentsIfNeeded().forPath(offsetPath(P2), OLD_OFFSET.getBytes(UTF_8));
    CURATOR.create().creatingParentsIfNeeded().forPath(sessionPath(streamId));
}
Also used : CursorsService(org.zalando.nakadi.service.CursorsService) EventType(org.zalando.nakadi.domain.EventType) SubscriptionDbRepository(org.zalando.nakadi.repository.db.SubscriptionDbRepository) TimelineService(org.zalando.nakadi.service.timeline.TimelineService) UUIDGenerator(org.zalando.nakadi.util.UUIDGenerator) TopicRepository(org.zalando.nakadi.repository.TopicRepository) Subscription(org.zalando.nakadi.domain.Subscription) CursorConverter(org.zalando.nakadi.service.CursorConverter) SubscriptionClientFactory(org.zalando.nakadi.service.subscription.zk.SubscriptionClientFactory) ZooKeeperHolder(org.zalando.nakadi.repository.zookeeper.ZooKeeperHolder) Before(org.junit.Before)

Example 15 with Subscription

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

the class UserJourneyAT method userJourneyHila.

@Test(timeout = 15000)
public void userJourneyHila() throws InterruptedException, IOException {
    postEvents(rangeClosed(0, 3).mapToObj(x -> "{\"foo\":\"bar" + x + "\"}").collect(Collectors.toList()).toArray(new String[4]));
    // create subscription
    final SubscriptionBase subscriptionToCreate = RandomSubscriptionBuilder.builder().withOwningApplication("stups_aruha-test-end2end-nakadi").withEventType(eventTypeName).withStartFrom(BEGIN).buildSubscriptionBase();
    final Subscription subscription = createSubscription(jsonRequestSpec(), subscriptionToCreate);
    // list subscriptions
    jsonRequestSpec().param("event_type", eventTypeName).get("/subscriptions").then().statusCode(OK.value()).body("items.size()", equalTo(1)).body("items[0].id", equalTo(subscription.getId()));
    // create client and wait till we receive all events
    final TestStreamingClient client = new TestStreamingClient(RestAssured.baseURI + ":" + RestAssured.port, subscription.getId(), "", oauthToken).start();
    waitFor(() -> assertThat(client.getBatches(), hasSize(4)));
    final List<StreamBatch> batches = client.getBatches();
    // validate the content of events
    for (int i = 0; i < batches.size(); i++) {
        final SubscriptionCursor cursor = new SubscriptionCursor("0", TestUtils.toTimelineOffset(i), eventTypeName, "");
        final StreamBatch expectedBatch = new StreamBatch(cursor, ImmutableList.of(ImmutableMap.of("foo", "bar" + i)), i == 0 ? new StreamMetadata("Stream started") : null);
        final StreamBatch batch = batches.get(i);
        assertThat(batch, equalToBatchIgnoringToken(expectedBatch));
    }
    // as we didn't commit, there should be still 4 unconsumed events
    jsonRequestSpec().get("/subscriptions/{sid}/stats", subscription.getId()).then().statusCode(OK.value()).body("items[0].partitions[0].unconsumed_events", equalTo(4));
    // commit cursor of latest event
    final StreamBatch lastBatch = batches.get(batches.size() - 1);
    final int commitCode = commitCursors(jsonRequestSpec(), subscription.getId(), ImmutableList.of(lastBatch.getCursor()), client.getSessionId());
    assertThat(commitCode, equalTo(NO_CONTENT.value()));
    // now there should be 0 unconsumed events
    jsonRequestSpec().get("/subscriptions/{sid}/stats", subscription.getId()).then().statusCode(OK.value()).body("items[0].partitions[0].unconsumed_events", equalTo(0));
    // get cursors
    jsonRequestSpec().get("/subscriptions/{sid}/cursors", subscription.getId()).then().statusCode(OK.value()).body("items[0].partition", equalTo("0")).body("items[0].offset", equalTo("001-0001-000000000000000003"));
    // delete subscription
    jsonRequestSpec().delete("/subscriptions/{sid}", subscription.getId()).then().statusCode(NO_CONTENT.value());
}
Also used : TestUtils.randomTextString(org.zalando.nakadi.utils.TestUtils.randomTextString) CREATED(org.springframework.http.HttpStatus.CREATED) RequestSpecification(com.jayway.restassured.specification.RequestSpecification) IntStream.rangeClosed(java.util.stream.IntStream.rangeClosed) NOT_FOUND(org.springframework.http.HttpStatus.NOT_FOUND) RandomSubscriptionBuilder(org.zalando.nakadi.utils.RandomSubscriptionBuilder) NO_CONTENT(org.springframework.http.HttpStatus.NO_CONTENT) BEGIN(org.zalando.nakadi.domain.SubscriptionBase.InitialPosition.BEGIN) IsEqual.equalTo(org.hamcrest.core.IsEqual.equalTo) Subscription(org.zalando.nakadi.domain.Subscription) Retryer.executeWithRetry(org.echocat.jomon.runtime.concurrent.Retryer.executeWithRetry) JsonConfig(org.zalando.nakadi.config.JsonConfig) ImmutableList(com.google.common.collect.ImmutableList) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) RetryForSpecifiedTimeStrategy(org.echocat.jomon.runtime.concurrent.RetryForSpecifiedTimeStrategy) Before(org.junit.Before) Charsets(com.google.common.base.Charsets) EventType(org.zalando.nakadi.domain.EventType) StreamBatch(org.zalando.nakadi.webservice.hila.StreamBatch) ImmutableMap(com.google.common.collect.ImmutableMap) Resources(com.google.common.io.Resources) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Matchers(org.hamcrest.Matchers) TestUtils.waitFor(org.zalando.nakadi.utils.TestUtils.waitFor) Test(org.junit.Test) IOException(java.io.IOException) JSON(com.jayway.restassured.http.ContentType.JSON) StreamMetadata(org.zalando.nakadi.domain.StreamMetadata) TestUtils(org.zalando.nakadi.utils.TestUtils) Collectors(java.util.stream.Collectors) NakadiTestUtils.commitCursors(org.zalando.nakadi.webservice.utils.NakadiTestUtils.commitCursors) MatcherIgnoringToken.equalToBatchIgnoringToken(org.zalando.nakadi.webservice.hila.StreamBatch.MatcherIgnoringToken.equalToBatchIgnoringToken) List(java.util.List) SubscriptionBase(org.zalando.nakadi.domain.SubscriptionBase) Header(com.jayway.restassured.response.Header) TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) SubscriptionCursor(org.zalando.nakadi.view.SubscriptionCursor) RestAssured(com.jayway.restassured.RestAssured) OK(org.springframework.http.HttpStatus.OK) TestUtils.randomValidEventTypeName(org.zalando.nakadi.utils.TestUtils.randomValidEventTypeName) SubscriptionBase(org.zalando.nakadi.domain.SubscriptionBase) SubscriptionCursor(org.zalando.nakadi.view.SubscriptionCursor) TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) StreamBatch(org.zalando.nakadi.webservice.hila.StreamBatch) StreamMetadata(org.zalando.nakadi.domain.StreamMetadata) TestUtils.randomTextString(org.zalando.nakadi.utils.TestUtils.randomTextString) Subscription(org.zalando.nakadi.domain.Subscription) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) Test(org.junit.Test)

Aggregations

Subscription (org.zalando.nakadi.domain.Subscription)55 Test (org.junit.Test)40 NakadiTestUtils.createSubscription (org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription)21 EventType (org.zalando.nakadi.domain.EventType)15 SubscriptionBase (org.zalando.nakadi.domain.SubscriptionBase)12 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)11 List (java.util.List)10 SubscriptionCursorWithoutToken (org.zalando.nakadi.view.SubscriptionCursorWithoutToken)9 TestStreamingClient (org.zalando.nakadi.webservice.utils.TestStreamingClient)9 ImmutableList (com.google.common.collect.ImmutableList)8 Collectors (java.util.stream.Collectors)8 SubscriptionCursor (org.zalando.nakadi.view.SubscriptionCursor)8 ItemsWrapper (org.zalando.nakadi.domain.ItemsWrapper)7 PaginationLinks (org.zalando.nakadi.domain.PaginationLinks)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 IOException (java.io.IOException)6 Optional (java.util.Optional)6 PaginationWrapper (org.zalando.nakadi.domain.PaginationWrapper)6 SubscriptionEventTypeStats (org.zalando.nakadi.domain.SubscriptionEventTypeStats)6 NakadiTestUtils.createEventType (org.zalando.nakadi.webservice.utils.NakadiTestUtils.createEventType)5