use of io.confluent.kafkarest.entities.EmbeddedFormat 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);
}
use of io.confluent.kafkarest.entities.EmbeddedFormat in project kafka-rest by confluentinc.
the class SchemaManagerImplTest method errorRawSchemaNotSupportedWithSchema.
@Test
public void errorRawSchemaNotSupportedWithSchema() {
EmbeddedFormat embeddedFormatMock = mock(EmbeddedFormat.class);
SchemaProvider schemaProviderMock = mock(SchemaProvider.class);
expect(embeddedFormatMock.requiresSchema()).andReturn(true);
expect(embeddedFormatMock.getSchemaProvider()).andThrow(new UnsupportedOperationException("Reason here"));
expect(schemaProviderMock.parseSchema(TextNode.valueOf("rawSchema").toString(), emptyList(), true)).andReturn(Optional.empty());
replay(embeddedFormatMock, schemaProviderMock);
BadRequestException bre = assertThrows(BadRequestException.class, () -> schemaManager.getSchema(TOPIC_NAME, /* format= */
Optional.of(embeddedFormatMock), /* subject= */
Optional.empty(), /* subjectNameStrategy= */
Optional.empty(), /* schemaId= */
Optional.empty(), /* schemaVersion= */
Optional.empty(), /* rawSchema= */
Optional.of(TextNode.valueOf("rawSchema").toString()), /* isKey= */
true));
assertEquals("Raw schema not supported with format = EasyMock for class io.confluent.kafkarest.entities.EmbeddedFormat", bre.getMessage());
assertEquals(400, bre.getCode());
}
use of io.confluent.kafkarest.entities.EmbeddedFormat in project kafka-rest by confluentinc.
the class SchemaManagerImplTest method errorRegisteringSchema.
@Test
public void errorRegisteringSchema() throws RestClientException, IOException {
SchemaRegistryClient schemaRegistryClientMock = mock(SchemaRegistryClient.class);
ParsedSchema parsedSchemaMock = mock(ParsedSchema.class);
EmbeddedFormat embeddedFormatMock = mock(EmbeddedFormat.class);
SchemaProvider schemaProviderMock = mock(SchemaProvider.class);
expect(embeddedFormatMock.requiresSchema()).andReturn(true);
expect(embeddedFormatMock.getSchemaProvider()).andReturn(schemaProviderMock);
expect(schemaProviderMock.parseSchema(TextNode.valueOf("rawString").toString(), emptyList(), true)).andReturn(Optional.of(parsedSchemaMock));
expect(schemaRegistryClientMock.getId("subject1", parsedSchemaMock)).andThrow(new IOException("Can't get Schema"));
expect(schemaRegistryClientMock.register("subject1", parsedSchemaMock)).andThrow(new IOException("Can't register Schema"));
replay(schemaRegistryClientMock, embeddedFormatMock, schemaProviderMock);
SchemaManager mySchemaManager = new SchemaManagerImpl(schemaRegistryClientMock, new TopicNameStrategy());
RestConstraintViolationException rcve = assertThrows(RestConstraintViolationException.class, () -> mySchemaManager.getSchema(TOPIC_NAME, /* format= */
Optional.of(embeddedFormatMock), /* subject= */
Optional.of("subject1"), /* subjectNameStrategy= */
Optional.empty(), /* schemaId= */
Optional.empty(), /* schemaVersion= */
Optional.empty(), /* rawSchema= */
Optional.of(TextNode.valueOf("rawString").toString()), /* isKey= */
true));
assertEquals("Error serializing message. Error when registering schema. format = EasyMock for class io.confluent.kafkarest.entities.EmbeddedFormat, subject = subject1, schema = null\n" + "Can't register Schema", rcve.getMessage());
assertEquals(42207, rcve.getErrorCode());
}
use of io.confluent.kafkarest.entities.EmbeddedFormat in project kafka-rest by confluentinc.
the class SchemaManagerImplTest method errorRawSchemaCantParseSchema.
@Test
public void errorRawSchemaCantParseSchema() {
SchemaRegistryClient schemaRegistryClientMock = mock(SchemaRegistryClient.class);
EmbeddedFormat embeddedFormatMock = mock(EmbeddedFormat.class);
SchemaProvider schemaProviderMock = mock(SchemaProvider.class);
expect(embeddedFormatMock.requiresSchema()).andReturn(true);
expect(embeddedFormatMock.getSchemaProvider()).andThrow(new UnsupportedOperationException("Unsupported"));
replay(embeddedFormatMock, schemaProviderMock, schemaRegistryClientMock);
SchemaManager mySchemaManager = new SchemaManagerImpl(schemaRegistryClientMock, new TopicNameStrategy());
BadRequestException rcve = assertThrows(BadRequestException.class, () -> mySchemaManager.getSchema(TOPIC_NAME, /* format= */
Optional.of(embeddedFormatMock), /* subject= */
Optional.empty(), /* subjectNameStrategy= */
Optional.empty(), /* schemaId= */
Optional.empty(), /* schemaVersion= */
Optional.empty(), /* rawSchema= */
Optional.of(TextNode.valueOf("rawSchema").toString()), /* isKey= */
true));
assertEquals("Raw schema not supported with format = EasyMock for class io.confluent.kafkarest.entities.EmbeddedFormat", rcve.getMessage());
assertEquals(400, rcve.getCode());
}
Aggregations