Search in sources :

Example 1 with ProduceController

use of io.confluent.kafkarest.controllers.ProduceController in project kafka-rest by confluentinc.

the class ProduceActionTest method getProduceAction.

private static ProduceAction getProduceAction(ProduceRateLimiters produceRateLimiters, ChunkedOutputFactory chunkedOutputFactory, int times, int producerId, boolean errorSchemaRegistry) {
    Provider<SchemaManager> schemaManagerProvider = mock(Provider.class);
    SchemaManager schemaManagerMock = mock(SchemaManager.class);
    expect(schemaManagerProvider.get()).andReturn(schemaManagerMock);
    expect(schemaManagerMock.getSchema("topicName", Optional.of(EmbeddedFormat.AVRO), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of("bob"), true)).andThrow(Errors.invalidPayloadException("Schema Registry must be configured when using schemas."));
    replay(schemaManagerProvider, schemaManagerMock);
    Provider<ProducerMetrics> producerMetricsProvider = mock(Provider.class);
    getProducerMetricsProvider(producerMetricsProvider);
    Provider<RecordSerializer> recordSerializerProvider = getRecordSerializerProvider(errorSchemaRegistry);
    Provider<ProduceController> produceControllerProvider = mock(Provider.class);
    ProduceController produceController = getProduceControllerMock(produceControllerProvider);
    setupExpectsMockCallsForProduce(produceController, times, producerId);
    replay(producerMetricsProvider, produceControllerProvider, produceController);
    StreamingResponseFactory streamingResponseFactory = new StreamingResponseFactory(chunkedOutputFactory);
    // get the current thread so that the call counts can be seen by easy mock
    ExecutorService executorService = MoreExecutors.newDirectExecutorService();
    ProduceAction produceAction = new ProduceAction(schemaManagerProvider, recordSerializerProvider, produceControllerProvider, producerMetricsProvider, streamingResponseFactory, produceRateLimiters, executorService);
    produceRateLimiters.clear();
    return produceAction;
}
Also used : ProduceController(io.confluent.kafkarest.controllers.ProduceController) StreamingResponseFactory(io.confluent.kafkarest.response.StreamingResponseFactory) ExecutorService(java.util.concurrent.ExecutorService) SchemaManager(io.confluent.kafkarest.controllers.SchemaManager) ProducerMetrics(io.confluent.kafkarest.ProducerMetrics) RecordSerializer(io.confluent.kafkarest.controllers.RecordSerializer)

Example 2 with ProduceController

use of io.confluent.kafkarest.controllers.ProduceController in project kafka-rest by confluentinc.

the class ProduceAction method produce.

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@PerformanceMetric("v3.produce.produce-to-topic")
@ResourceName("api.v3.produce.produce-to-topic")
public void produce(@Suspended AsyncResponse asyncResponse, @PathParam("clusterId") String clusterId, @PathParam("topicName") String topicName, MappingIterator<ProduceRequest> requests) throws Exception {
    if (requests == null) {
        throw Errors.invalidPayloadException("Null input provided. Data is required.");
    }
    ProduceController controller = produceControllerProvider.get();
    streamingResponseFactory.from(requests).compose(request -> produce(clusterId, topicName, request, controller)).resume(asyncResponse);
}
Also used : PathParam(javax.ws.rs.PathParam) Provider(javax.inject.Provider) Produces(javax.ws.rs.Produces) ProduceRequest(io.confluent.kafkarest.entities.v3.ProduceRequest) SerializationException(org.apache.kafka.common.errors.SerializationException) MappingIterator(com.fasterxml.jackson.databind.MappingIterator) StacklessCompletionException(io.confluent.kafkarest.exceptions.StacklessCompletionException) Path(javax.ws.rs.Path) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) PerformanceMetric(io.confluent.rest.annotations.PerformanceMetric) ProduceRequestHeader(io.confluent.kafkarest.entities.v3.ProduceRequest.ProduceRequestHeader) Function(java.util.function.Function) Inject(javax.inject.Inject) NullNode(com.fasterxml.jackson.databind.node.NullNode) RateLimitExceededException(io.confluent.kafkarest.ratelimit.RateLimitExceededException) MediaType(javax.ws.rs.core.MediaType) Consumes(javax.ws.rs.Consumes) ProduceRequestData(io.confluent.kafkarest.entities.v3.ProduceRequest.ProduceRequestData) Objects.requireNonNull(java.util.Objects.requireNonNull) Duration(java.time.Duration) ProduceResponseData(io.confluent.kafkarest.entities.v3.ProduceResponse.ProduceResponseData) Collector(java.util.stream.Collector) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) RegisteredSchema(io.confluent.kafkarest.entities.RegisteredSchema) BadRequestException(io.confluent.kafkarest.exceptions.BadRequestException) ProducerMetricsRegistry(io.confluent.kafkarest.ProducerMetricsRegistry) ExecutorService(java.util.concurrent.ExecutorService) EmbeddedFormat(io.confluent.kafkarest.entities.EmbeddedFormat) SchemaManager(io.confluent.kafkarest.controllers.SchemaManager) POST(javax.ws.rs.POST) Logger(org.slf4j.Logger) ProduceController(io.confluent.kafkarest.controllers.ProduceController) ProduceResult(io.confluent.kafkarest.entities.ProduceResult) AsyncResponse(javax.ws.rs.container.AsyncResponse) DoNotRateLimit(io.confluent.kafkarest.ratelimit.DoNotRateLimit) ResourceName(io.confluent.kafkarest.extension.ResourceAccesslistFeature.ResourceName) Instant(java.time.Instant) Suspended(javax.ws.rs.container.Suspended) ByteString(com.google.protobuf.ByteString) ProducerMetrics(io.confluent.kafkarest.ProducerMetrics) StreamingResponseFactory(io.confluent.kafkarest.response.StreamingResponseFactory) Errors(io.confluent.kafkarest.Errors) ProduceResponse(io.confluent.kafkarest.entities.v3.ProduceResponse) Optional(java.util.Optional) RecordSerializer(io.confluent.kafkarest.controllers.RecordSerializer) ProduceResponseThreadPool(io.confluent.kafkarest.resources.v3.V3ResourcesModule.ProduceResponseThreadPool) Collections(java.util.Collections) ProduceController(io.confluent.kafkarest.controllers.ProduceController) PerformanceMetric(io.confluent.rest.annotations.PerformanceMetric) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ResourceName(io.confluent.kafkarest.extension.ResourceAccesslistFeature.ResourceName)

Example 3 with ProduceController

use of io.confluent.kafkarest.controllers.ProduceController in project kafka-rest by confluentinc.

the class ProduceAction method produce.

private CompletableFuture<ProduceResponse> produce(String clusterId, String topicName, ProduceRequest request, ProduceController controller) {
    try {
        produceRateLimiters.rateLimit(clusterId, request.getOriginalSize());
    } catch (RateLimitExceededException e) {
        // KREST-4356 Use our own CompletionException that will avoid the costly stack trace fill.
        throw new StacklessCompletionException(e);
    }
    Instant requestInstant = Instant.now();
    Optional<RegisteredSchema> keySchema = request.getKey().flatMap(key -> getSchema(topicName, /* isKey= */
    true, key));
    Optional<EmbeddedFormat> keyFormat = keySchema.map(schema -> Optional.of(schema.getFormat())).orElse(request.getKey().flatMap(ProduceRequestData::getFormat));
    Optional<ByteString> serializedKey = serialize(topicName, keyFormat, keySchema, request.getKey(), /* isKey= */
    true);
    Optional<RegisteredSchema> valueSchema = request.getValue().flatMap(value -> getSchema(topicName, /* isKey= */
    false, value));
    Optional<EmbeddedFormat> valueFormat = valueSchema.map(schema -> Optional.of(schema.getFormat())).orElse(request.getValue().flatMap(ProduceRequestData::getFormat));
    Optional<ByteString> serializedValue = serialize(topicName, valueFormat, valueSchema, request.getValue(), /* isKey= */
    false);
    recordRequestMetrics(request.getOriginalSize());
    CompletableFuture<ProduceResult> produceResult = controller.produce(clusterId, topicName, request.getPartitionId(), request.getHeaders().stream().collect(PRODUCE_REQUEST_HEADER_COLLECTOR), serializedKey, serializedValue, request.getTimestamp().orElse(Instant.now()));
    return produceResult.handleAsync((result, error) -> {
        if (error != null) {
            long latency = Duration.between(requestInstant, Instant.now()).toMillis();
            recordErrorMetrics(latency);
            throw new StacklessCompletionException(error);
        }
        return result;
    }, executorService).thenApplyAsync(result -> {
        ProduceResponse response = toProduceResponse(clusterId, topicName, keyFormat, keySchema, valueFormat, valueSchema, result);
        long latency = Duration.between(requestInstant, result.getCompletionTimestamp()).toMillis();
        recordResponseMetrics(latency);
        return response;
    }, executorService);
}
Also used : PathParam(javax.ws.rs.PathParam) Provider(javax.inject.Provider) Produces(javax.ws.rs.Produces) ProduceRequest(io.confluent.kafkarest.entities.v3.ProduceRequest) SerializationException(org.apache.kafka.common.errors.SerializationException) MappingIterator(com.fasterxml.jackson.databind.MappingIterator) StacklessCompletionException(io.confluent.kafkarest.exceptions.StacklessCompletionException) Path(javax.ws.rs.Path) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) PerformanceMetric(io.confluent.rest.annotations.PerformanceMetric) ProduceRequestHeader(io.confluent.kafkarest.entities.v3.ProduceRequest.ProduceRequestHeader) Function(java.util.function.Function) Inject(javax.inject.Inject) NullNode(com.fasterxml.jackson.databind.node.NullNode) RateLimitExceededException(io.confluent.kafkarest.ratelimit.RateLimitExceededException) MediaType(javax.ws.rs.core.MediaType) Consumes(javax.ws.rs.Consumes) ProduceRequestData(io.confluent.kafkarest.entities.v3.ProduceRequest.ProduceRequestData) Objects.requireNonNull(java.util.Objects.requireNonNull) Duration(java.time.Duration) ProduceResponseData(io.confluent.kafkarest.entities.v3.ProduceResponse.ProduceResponseData) Collector(java.util.stream.Collector) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) RegisteredSchema(io.confluent.kafkarest.entities.RegisteredSchema) BadRequestException(io.confluent.kafkarest.exceptions.BadRequestException) ProducerMetricsRegistry(io.confluent.kafkarest.ProducerMetricsRegistry) ExecutorService(java.util.concurrent.ExecutorService) EmbeddedFormat(io.confluent.kafkarest.entities.EmbeddedFormat) SchemaManager(io.confluent.kafkarest.controllers.SchemaManager) POST(javax.ws.rs.POST) Logger(org.slf4j.Logger) ProduceController(io.confluent.kafkarest.controllers.ProduceController) ProduceResult(io.confluent.kafkarest.entities.ProduceResult) AsyncResponse(javax.ws.rs.container.AsyncResponse) DoNotRateLimit(io.confluent.kafkarest.ratelimit.DoNotRateLimit) ResourceName(io.confluent.kafkarest.extension.ResourceAccesslistFeature.ResourceName) Instant(java.time.Instant) Suspended(javax.ws.rs.container.Suspended) ByteString(com.google.protobuf.ByteString) ProducerMetrics(io.confluent.kafkarest.ProducerMetrics) StreamingResponseFactory(io.confluent.kafkarest.response.StreamingResponseFactory) Errors(io.confluent.kafkarest.Errors) ProduceResponse(io.confluent.kafkarest.entities.v3.ProduceResponse) Optional(java.util.Optional) RecordSerializer(io.confluent.kafkarest.controllers.RecordSerializer) ProduceResponseThreadPool(io.confluent.kafkarest.resources.v3.V3ResourcesModule.ProduceResponseThreadPool) Collections(java.util.Collections) EmbeddedFormat(io.confluent.kafkarest.entities.EmbeddedFormat) ByteString(com.google.protobuf.ByteString) ProduceResponse(io.confluent.kafkarest.entities.v3.ProduceResponse) Instant(java.time.Instant) RateLimitExceededException(io.confluent.kafkarest.ratelimit.RateLimitExceededException) RegisteredSchema(io.confluent.kafkarest.entities.RegisteredSchema) ProduceResult(io.confluent.kafkarest.entities.ProduceResult) StacklessCompletionException(io.confluent.kafkarest.exceptions.StacklessCompletionException)

Example 4 with ProduceController

use of io.confluent.kafkarest.controllers.ProduceController in project kafka-rest by confluentinc.

the class ProduceActionTest method getProduceControllerMock.

private static ProduceController getProduceControllerMock(Provider produceControllerProvider) {
    ProduceController produceController = mock(ProduceController.class);
    expect(produceControllerProvider.get()).andReturn(produceController).anyTimes();
    return produceController;
}
Also used : ProduceController(io.confluent.kafkarest.controllers.ProduceController)

Aggregations

ProduceController (io.confluent.kafkarest.controllers.ProduceController)4 ProducerMetrics (io.confluent.kafkarest.ProducerMetrics)3 RecordSerializer (io.confluent.kafkarest.controllers.RecordSerializer)3 SchemaManager (io.confluent.kafkarest.controllers.SchemaManager)3 StreamingResponseFactory (io.confluent.kafkarest.response.StreamingResponseFactory)3 ExecutorService (java.util.concurrent.ExecutorService)3 MappingIterator (com.fasterxml.jackson.databind.MappingIterator)2 NullNode (com.fasterxml.jackson.databind.node.NullNode)2 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)2 ByteString (com.google.protobuf.ByteString)2 Errors (io.confluent.kafkarest.Errors)2 ProducerMetricsRegistry (io.confluent.kafkarest.ProducerMetricsRegistry)2 EmbeddedFormat (io.confluent.kafkarest.entities.EmbeddedFormat)2 ProduceResult (io.confluent.kafkarest.entities.ProduceResult)2 RegisteredSchema (io.confluent.kafkarest.entities.RegisteredSchema)2 ProduceRequest (io.confluent.kafkarest.entities.v3.ProduceRequest)2 ProduceRequestData (io.confluent.kafkarest.entities.v3.ProduceRequest.ProduceRequestData)2 ProduceRequestHeader (io.confluent.kafkarest.entities.v3.ProduceRequest.ProduceRequestHeader)2 ProduceResponse (io.confluent.kafkarest.entities.v3.ProduceResponse)2 ProduceResponseData (io.confluent.kafkarest.entities.v3.ProduceResponse.ProduceResponseData)2