Search in sources :

Example 71 with EventType

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

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

the class EventTypeDbRepository method saveEventType.

@Override
@Transactional
public EventType saveEventType(final EventTypeBase eventTypeBase) throws InternalNakadiException, DuplicatedEventTypeNameException {
    try {
        final DateTime now = new DateTime(DateTimeZone.UTC);
        final EventType eventType = new EventType(eventTypeBase, "1.0.0", now, now);
        jdbcTemplate.update("INSERT INTO zn_data.event_type (et_name, et_event_type_object) VALUES (?, ?::jsonb)", eventTypeBase.getName(), jsonMapper.writer().writeValueAsString(eventType));
        insertEventTypeSchema(eventType);
        return eventType;
    } catch (final JsonProcessingException e) {
        throw new InternalNakadiException("Serialization problem during persistence of event type", e);
    } catch (final DuplicateKeyException e) {
        throw new DuplicatedEventTypeNameException("EventType " + eventTypeBase.getName() + " already exists.", e);
    }
}
Also used : InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException) DuplicatedEventTypeNameException(org.zalando.nakadi.exceptions.DuplicatedEventTypeNameException) EventType(org.zalando.nakadi.domain.EventType) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) DateTime(org.joda.time.DateTime) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 73 with EventType

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

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

the class EventStreamReadingAT method whenMemoryOverflowEventsDumped.

@Test(timeout = 10000)
public void whenMemoryOverflowEventsDumped() throws IOException {
    // Create event type
    final EventType loadEt = EventTypeTestBuilder.builder().defaultStatistic(new EventTypeStatistics(PARTITIONS_NUM, PARTITIONS_NUM)).build();
    NakadiTestUtils.createEventTypeInNakadi(loadEt);
    // Publish events to event type, that are not fitting memory
    final String evt = "{\"foo\":\"barbarbar\"}";
    final int eventCount = 2 * (10000 / evt.length());
    NakadiTestUtils.publishEvents(loadEt.getName(), eventCount, i -> evt);
    // Configure streaming so it will:(more than 10s and batch_limit
    // - definitely wait for more than test timeout (10s)
    // - collect batch, which size is greater than events published to this event type
    final String url = RestAssured.baseURI + ":" + RestAssured.port + createStreamEndpointUrl(loadEt.getName()) + "?batch_limit=" + (10 * eventCount) + "&stream_limit=" + (10 * eventCount) + "&batch_flush_timeout=11" + "&stream_timeout=11";
    final HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    // Start from the begin.
    connection.setRequestProperty("X-Nakadi-Cursors", "[" + IntStream.range(0, PARTITIONS_NUM).mapToObj(i -> "{\"partition\": \"" + i + "\",\"offset\":\"begin\"}").collect(Collectors.joining(",")) + "]");
    Assert.assertEquals(HttpServletResponse.SC_OK, connection.getResponseCode());
    final InputStream inputStream = connection.getInputStream();
    final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    final String line = reader.readLine();
    Assert.assertNotNull(line);
// If we read at least one line, than it means, that we were able to read data before test timeout reached.
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) EventType(org.zalando.nakadi.domain.EventType) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) EventTypeStatistics(org.zalando.nakadi.domain.EventTypeStatistics) URL(java.net.URL) Test(org.junit.Test)

Example 75 with EventType

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

the class EventTypeAT method whenPUTValidEventTypeThenOK.

@Test
public void whenPUTValidEventTypeThenOK() throws Exception {
    final EventType eventType = buildDefaultEventType();
    final String body = MAPPER.writer().writeValueAsString(eventType);
    given().body(body).header("accept", "application/json").contentType(JSON).post(ENDPOINT).then().body(equalTo("")).statusCode(HttpStatus.SC_CREATED);
    final EventType retrievedEventType = MAPPER.readValue(given().header("accept", "application/json").get(ENDPOINT + "/" + eventType.getName()).getBody().asString(), EventType.class);
    final String updateBody = MAPPER.writer().writeValueAsString(retrievedEventType);
    given().body(updateBody).header("accept", "application/json").contentType(JSON).when().put(ENDPOINT + "/" + eventType.getName()).then().body(equalTo("")).statusCode(HttpStatus.SC_OK);
}
Also used : TestUtils.buildDefaultEventType(org.zalando.nakadi.utils.TestUtils.buildDefaultEventType) EventType(org.zalando.nakadi.domain.EventType) TestUtils.resourceAsString(org.zalando.nakadi.utils.TestUtils.resourceAsString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

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