Search in sources :

Example 6 with BatchItem

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

the class MetadataEnrichmentStrategyTest method setEventTypeSchemaVersion.

@Test
public void setEventTypeSchemaVersion() throws Exception {
    final EventType eventType = buildDefaultEventType();
    final JSONObject event = buildBusinessEvent();
    final BatchItem batchItem = createBatchItem(event);
    assertThat(batchItem.getEvent().getJSONObject("metadata").optString("version"), isEmptyString());
    strategy.enrich(batchItem, eventType);
    assertThat(batchItem.getEvent().getJSONObject("metadata").getString("version"), equalTo("1.0.0"));
}
Also used : JSONObject(org.json.JSONObject) EventType(org.zalando.nakadi.domain.EventType) TestUtils.buildDefaultEventType(org.zalando.nakadi.utils.TestUtils.buildDefaultEventType) TestUtils.createBatchItem(org.zalando.nakadi.utils.TestUtils.createBatchItem) BatchItem(org.zalando.nakadi.domain.BatchItem) Test(org.junit.Test)

Example 7 with BatchItem

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

the class KafkaTopicRepository method publishItem.

private static CompletableFuture<Exception> publishItem(final Producer<String, String> producer, final String topicId, final BatchItem item, final HystrixKafkaCircuitBreaker circuitBreaker) throws EventPublishingException {
    try {
        final CompletableFuture<Exception> result = new CompletableFuture<>();
        final ProducerRecord<String, String> kafkaRecord = new ProducerRecord<>(topicId, KafkaCursor.toKafkaPartition(item.getPartition()), item.getPartition(), item.dumpEventToString());
        circuitBreaker.markStart();
        producer.send(kafkaRecord, ((metadata, exception) -> {
            if (null != exception) {
                LOG.warn("Failed to publish to kafka topic {}", topicId, exception);
                item.updateStatusAndDetail(EventPublishingStatus.FAILED, "internal error");
                if (hasKafkaConnectionException(exception)) {
                    circuitBreaker.markFailure();
                } else {
                    circuitBreaker.markSuccessfully();
                }
                result.complete(exception);
            } else {
                item.updateStatusAndDetail(EventPublishingStatus.SUBMITTED, "");
                circuitBreaker.markSuccessfully();
                result.complete(null);
            }
        }));
        return result;
    } catch (final InterruptException e) {
        Thread.currentThread().interrupt();
        circuitBreaker.markSuccessfully();
        item.updateStatusAndDetail(EventPublishingStatus.FAILED, "internal error");
        throw new EventPublishingException("Error publishing message to kafka", e);
    } catch (final RuntimeException e) {
        circuitBreaker.markSuccessfully();
        item.updateStatusAndDetail(EventPublishingStatus.FAILED, "internal error");
        throw new EventPublishingException("Error publishing message to kafka", e);
    }
}
Also used : EventPublishingException(org.zalando.nakadi.exceptions.EventPublishingException) NotLeaderForPartitionException(org.apache.kafka.common.errors.NotLeaderForPartitionException) Collections.unmodifiableList(java.util.Collections.unmodifiableList) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) TopicRepositoryException(org.zalando.nakadi.exceptions.runtime.TopicRepositoryException) PARTITION_NOT_FOUND(org.zalando.nakadi.domain.CursorError.PARTITION_NOT_FOUND) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) Map(java.util.Map) RetryForSpecifiedTimeStrategy(org.echocat.jomon.runtime.concurrent.RetryForSpecifiedTimeStrategy) Consumer(org.apache.kafka.clients.consumer.Consumer) ZooKeeperHolder(org.zalando.nakadi.repository.zookeeper.ZooKeeperHolder) TopicPartition(org.apache.kafka.common.TopicPartition) TopicRepository(org.zalando.nakadi.repository.TopicRepository) Retryer(org.echocat.jomon.runtime.concurrent.Retryer) Collection(java.util.Collection) PartitionStatistics(org.zalando.nakadi.domain.PartitionStatistics) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) ConfigType(kafka.server.ConfigType) PartitionInfo(org.apache.kafka.common.PartitionInfo) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) Collectors(java.util.stream.Collectors) TopicDeletionException(org.zalando.nakadi.exceptions.TopicDeletionException) Objects(java.util.Objects) ZkUtils(kafka.utils.ZkUtils) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException) List(java.util.List) Stream(java.util.stream.Stream) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Timeline(org.zalando.nakadi.domain.Timeline) ZookeeperSettings(org.zalando.nakadi.repository.zookeeper.ZookeeperSettings) NULL_OFFSET(org.zalando.nakadi.domain.CursorError.NULL_OFFSET) BatchItem(org.zalando.nakadi.domain.BatchItem) Optional(java.util.Optional) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) AdminUtils(kafka.admin.AdminUtils) IntStream(java.util.stream.IntStream) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) NetworkException(org.apache.kafka.common.errors.NetworkException) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) NakadiSettings(org.zalando.nakadi.config.NakadiSettings) TopicCreationException(org.zalando.nakadi.exceptions.TopicCreationException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) TopicConfigException(org.zalando.nakadi.exceptions.runtime.TopicConfigException) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) UUIDGenerator(org.zalando.nakadi.util.UUIDGenerator) InterruptException(org.apache.kafka.common.errors.InterruptException) EventPublishingStep(org.zalando.nakadi.domain.EventPublishingStep) Nullable(javax.annotation.Nullable) UNAVAILABLE(org.zalando.nakadi.domain.CursorError.UNAVAILABLE) NULL_PARTITION(org.zalando.nakadi.domain.CursorError.NULL_PARTITION) Logger(org.slf4j.Logger) Properties(java.util.Properties) Producer(org.apache.kafka.clients.producer.Producer) PartitionEndStatistics(org.zalando.nakadi.domain.PartitionEndStatistics) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) EventConsumer(org.zalando.nakadi.repository.EventConsumer) Collectors.toList(java.util.stream.Collectors.toList) EventPublishingStatus(org.zalando.nakadi.domain.EventPublishingStatus) Preconditions(com.google.common.base.Preconditions) Collections(java.util.Collections) RackAwareMode(kafka.admin.RackAwareMode) CompletableFuture(java.util.concurrent.CompletableFuture) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) InterruptException(org.apache.kafka.common.errors.InterruptException) EventPublishingException(org.zalando.nakadi.exceptions.EventPublishingException) EventPublishingException(org.zalando.nakadi.exceptions.EventPublishingException) NotLeaderForPartitionException(org.apache.kafka.common.errors.NotLeaderForPartitionException) TimeoutException(java.util.concurrent.TimeoutException) TopicRepositoryException(org.zalando.nakadi.exceptions.runtime.TopicRepositoryException) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) TopicDeletionException(org.zalando.nakadi.exceptions.TopicDeletionException) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) NetworkException(org.apache.kafka.common.errors.NetworkException) TopicCreationException(org.zalando.nakadi.exceptions.TopicCreationException) TopicConfigException(org.zalando.nakadi.exceptions.runtime.TopicConfigException) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) InterruptException(org.apache.kafka.common.errors.InterruptException) ExecutionException(java.util.concurrent.ExecutionException)

Example 8 with BatchItem

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

the class EventPublisher method validate.

private void validate(final List<BatchItem> batch, final EventType eventType) throws EventValidationException, InternalNakadiException, NoSuchEventTypeException {
    for (final BatchItem item : batch) {
        item.setStep(EventPublishingStep.VALIDATING);
        try {
            validateSchema(item.getEvent(), eventType);
            validateEventSize(item);
        } catch (final EventValidationException e) {
            item.updateStatusAndDetail(EventPublishingStatus.FAILED, e.getMessage());
            throw e;
        }
    }
}
Also used : EventValidationException(org.zalando.nakadi.exceptions.EventValidationException) BatchItem(org.zalando.nakadi.domain.BatchItem)

Example 9 with BatchItem

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

the class KafkaRepositoryAT method whenBulkSendSuccessfullyThenUpdateBatchItemStatus.

@Test(timeout = 10000)
public void whenBulkSendSuccessfullyThenUpdateBatchItemStatus() throws Exception {
    final List<BatchItem> items = new ArrayList<>();
    final String topicId = TestUtils.randomValidEventTypeName();
    kafkaHelper.createTopic(topicId, ZOOKEEPER_URL);
    for (int i = 0; i < 10; i++) {
        final BatchItem item = BatchFactory.from("[{}]").get(0);
        item.setPartition("0");
        items.add(item);
    }
    kafkaTopicRepository.syncPostBatch(topicId, items);
    for (int i = 0; i < 10; i++) {
        assertThat(items.get(i).getResponse().getPublishingStatus(), equalTo(EventPublishingStatus.SUBMITTED));
    }
}
Also used : BatchItem(org.zalando.nakadi.domain.BatchItem) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 10 with BatchItem

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

the class MetadataEnrichmentStrategyTest method setReceivedAtWithSystemTimeInUTC.

@Test
public void setReceivedAtWithSystemTimeInUTC() throws Exception {
    final EventType eventType = buildDefaultEventType();
    final JSONObject event = buildBusinessEvent();
    final BatchItem batch = TestUtils.createBatchItem(event);
    assertThat(event.getJSONObject("metadata").optString("received_at"), isEmptyString());
    try {
        DateTimeUtils.setCurrentMillisFixed(0);
        strategy.enrich(batch, eventType);
    } finally {
        DateTimeUtils.setCurrentMillisSystem();
    }
    assertThat(batch.getEvent().getJSONObject("metadata").getString("received_at"), equalTo("1970-01-01T00:00:00.000Z"));
}
Also used : JSONObject(org.json.JSONObject) EventType(org.zalando.nakadi.domain.EventType) TestUtils.buildDefaultEventType(org.zalando.nakadi.utils.TestUtils.buildDefaultEventType) TestUtils.createBatchItem(org.zalando.nakadi.utils.TestUtils.createBatchItem) BatchItem(org.zalando.nakadi.domain.BatchItem) Test(org.junit.Test)

Aggregations

BatchItem (org.zalando.nakadi.domain.BatchItem)23 Test (org.junit.Test)16 EventType (org.zalando.nakadi.domain.EventType)12 TestUtils.createBatchItem (org.zalando.nakadi.utils.TestUtils.createBatchItem)12 TestUtils.buildDefaultEventType (org.zalando.nakadi.utils.TestUtils.buildDefaultEventType)11 JSONObject (org.json.JSONObject)10 ArrayList (java.util.ArrayList)8 EventPublishingException (org.zalando.nakadi.exceptions.EventPublishingException)7 PartitionInfo (org.apache.kafka.common.PartitionInfo)6 Collections (java.util.Collections)3 List (java.util.List)3 Set (java.util.Set)3 TimeoutException (java.util.concurrent.TimeoutException)3 Collectors (java.util.stream.Collectors)3 Collectors.toList (java.util.stream.Collectors.toList)3 Consumer (org.apache.kafka.clients.consumer.Consumer)3 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)3 Node (org.apache.kafka.common.Node)3 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)3 InvalidCursorException (org.zalando.nakadi.exceptions.InvalidCursorException)3