Search in sources :

Example 1 with RegisteredSchema

use of io.confluent.kafkarest.entities.RegisteredSchema 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 2 with RegisteredSchema

use of io.confluent.kafkarest.entities.RegisteredSchema in project kafka-rest by confluentinc.

the class SchemaManagerImplTest method getSchema_avro_latestSchema_subject.

@Test
public void getSchema_avro_latestSchema_subject() throws Exception {
    String subject = "my-subject";
    ParsedSchema schema = new AvroSchema("{\"type\": \"int\"}");
    int schemaId = schemaRegistryClient.register(subject, schema);
    int schemaVersion = schemaRegistryClient.getVersion(subject, schema);
    RegisteredSchema actual = schemaManager.getSchema(TOPIC_NAME, /* format= */
    Optional.empty(), /* subject= */
    Optional.of(subject), /* subjectNameStrategy= */
    Optional.empty(), /* schemaId= */
    Optional.empty(), /* schemaVersion= */
    Optional.empty(), /* rawSchema= */
    Optional.empty(), /* isKey= */
    true);
    assertEquals(RegisteredSchema.create(subject, schemaId, schemaVersion, schema), actual);
}
Also used : RegisteredSchema(io.confluent.kafkarest.entities.RegisteredSchema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) Test(org.junit.jupiter.api.Test)

Example 3 with RegisteredSchema

use of io.confluent.kafkarest.entities.RegisteredSchema in project kafka-rest by confluentinc.

the class SchemaManagerImplTest method getSchema_jsonschema_rawSchema.

@Test
public void getSchema_jsonschema_rawSchema() throws Exception {
    RegisteredSchema actual = schemaManager.getSchema(TOPIC_NAME, /* format= */
    Optional.of(EmbeddedFormat.JSONSCHEMA), /* subject= */
    Optional.empty(), /* subjectNameStrategy= */
    Optional.empty(), /* schemaId= */
    Optional.empty(), /* schemaVersion= */
    Optional.empty(), /* rawSchema= */
    Optional.of("{\"type\": \"string\"}"), /* isKey= */
    true);
    ParsedSchema schema = schemaRegistryClient.getSchemaById(actual.getSchemaId());
    int schemaId = schemaRegistryClient.getId(KEY_SUBJECT, schema);
    int schemaVersion = schemaRegistryClient.getVersion(KEY_SUBJECT, schema);
    assertEquals(RegisteredSchema.create(KEY_SUBJECT, schemaId, schemaVersion, schema), actual);
}
Also used : RegisteredSchema(io.confluent.kafkarest.entities.RegisteredSchema) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) Test(org.junit.jupiter.api.Test)

Example 4 with RegisteredSchema

use of io.confluent.kafkarest.entities.RegisteredSchema in project kafka-rest by confluentinc.

the class SchemaManagerImplTest method getSchema_avro_latestSchema_subjectNameStrategy.

@Test
public void getSchema_avro_latestSchema_subjectNameStrategy() throws Exception {
    ParsedSchema schema = new AvroSchema("{\"type\": \"int\"}");
    SubjectNameStrategy strategy = new MySubjectNameStrategy();
    String subject = strategy.subjectName(TOPIC_NAME, /* isKey= */
    true, /* schema= */
    null);
    int schemaId = schemaRegistryClient.register(subject, schema);
    int schemaVersion = schemaRegistryClient.getVersion(subject, schema);
    RegisteredSchema actual = schemaManager.getSchema(TOPIC_NAME, /* format= */
    Optional.empty(), /* subject= */
    Optional.empty(), /* subjectNameStrategy= */
    Optional.of(strategy), /* schemaId= */
    Optional.empty(), /* schemaVersion= */
    Optional.empty(), /* rawSchema= */
    Optional.empty(), /* isKey= */
    true);
    assertEquals(RegisteredSchema.create(subject, schemaId, schemaVersion, schema), actual);
}
Also used : RegisteredSchema(io.confluent.kafkarest.entities.RegisteredSchema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) SubjectNameStrategy(io.confluent.kafka.serializers.subject.strategy.SubjectNameStrategy) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) Test(org.junit.jupiter.api.Test)

Example 5 with RegisteredSchema

use of io.confluent.kafkarest.entities.RegisteredSchema in project kafka-rest by confluentinc.

the class SchemaManagerImplTest method getSchema_avro_latestSchema_notIsKey.

@Test
public void getSchema_avro_latestSchema_notIsKey() throws Exception {
    ParsedSchema schema = new AvroSchema("{\"type\": \"int\"}");
    int schemaId = schemaRegistryClient.register(VALUE_SUBJECT, schema);
    int schemaVersion = schemaRegistryClient.getVersion(VALUE_SUBJECT, schema);
    RegisteredSchema actual = schemaManager.getSchema(TOPIC_NAME, /* format= */
    Optional.empty(), /* subject= */
    Optional.empty(), /* subjectNameStrategy= */
    Optional.empty(), /* schemaId= */
    Optional.empty(), /* schemaVersion= */
    Optional.empty(), /* rawSchema= */
    Optional.empty(), /* isKey= */
    false);
    assertEquals(RegisteredSchema.create(VALUE_SUBJECT, schemaId, schemaVersion, schema), actual);
}
Also used : RegisteredSchema(io.confluent.kafkarest.entities.RegisteredSchema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) Test(org.junit.jupiter.api.Test)

Aggregations

RegisteredSchema (io.confluent.kafkarest.entities.RegisteredSchema)18 ParsedSchema (io.confluent.kafka.schemaregistry.ParsedSchema)16 Test (org.junit.jupiter.api.Test)15 AvroSchema (io.confluent.kafka.schemaregistry.avro.AvroSchema)10 SubjectNameStrategy (io.confluent.kafka.serializers.subject.strategy.SubjectNameStrategy)4 ByteString (com.google.protobuf.ByteString)2 BadRequestException (io.confluent.kafkarest.exceptions.BadRequestException)2 Instant (java.time.Instant)2 MappingIterator (com.fasterxml.jackson.databind.MappingIterator)1 NullNode (com.fasterxml.jackson.databind.node.NullNode)1 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)1 SchemaProvider (io.confluent.kafka.schemaregistry.SchemaProvider)1 Schema (io.confluent.kafka.schemaregistry.client.rest.entities.Schema)1 Errors (io.confluent.kafkarest.Errors)1 ProducerMetrics (io.confluent.kafkarest.ProducerMetrics)1 ProducerMetricsRegistry (io.confluent.kafkarest.ProducerMetricsRegistry)1 TestUtils.assertOKResponse (io.confluent.kafkarest.TestUtils.assertOKResponse)1 ProduceController (io.confluent.kafkarest.controllers.ProduceController)1 RecordSerializer (io.confluent.kafkarest.controllers.RecordSerializer)1 SchemaManager (io.confluent.kafkarest.controllers.SchemaManager)1