mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
refactor(kafka): share the Kafka REST Proxy bridge and add partition assignment and seeking (#9170)
Move KarapaceManager and the Kafka REST Proxy v2 client out of the package-private services/pipes into core/common/kafka so other services can consume Kafka topics. Pipes behavior, the floci.services.pipes.kafka-rest-bridge.* config keys and the container naming and labels are unchanged.
Extend the client with the operations Redshift streaming ingestion needs: topic partition discovery (a missing topic raises KafkaTopicNotFoundException, distinct from broker or proxy failures), manual partition assignment, and explicit per-partition offset seeking. Partitions are listed through GET /topics/{topic}/partitions, where Karapace answers a missing topic with error_code 40401.
Part of #7814
This commit is contained in:
1 parent
f41fad028c
commit
351973d3f8
19 files changed
+412
-64
No files matched your search
@@ -0,0 +1,219 @@
|
||||
package io.floci.aws.core.common.kafka;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Exercises the partition discovery, manual assignment and offset seeking calls of
|
||||
* {@link KafkaRestClient} against a fake server speaking Karapace's REST Proxy API.
|
||||
*/
|
||||
class KafkaRestClientAssignmentTest {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final String INSTANCE_PATH = "/consumers/refresh-group/instances/rest-consumer-1";
|
||||
|
||||
private HttpServer fakeServer;
|
||||
private KafkaRestClient client;
|
||||
private final AtomicReference<String> lastRequestBody = new AtomicReference<>();
|
||||
private final AtomicReference<String> lastRequestMethod = new AtomicReference<>();
|
||||
private final AtomicReference<String> lastRequestPath = new AtomicReference<>();
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
fakeServer = HttpServer.create(new InetSocketAddress("localhost", 0), 0);
|
||||
|
||||
fakeServer.createContext("/topics/orders/partitions", exchange -> {
|
||||
record(exchange);
|
||||
respond(exchange, 200, """
|
||||
[
|
||||
{"partition": 2, "leader": 1, "replicas": []},
|
||||
{"partition": 0, "leader": 1, "replicas": []},
|
||||
{"partition": 1, "leader": 1, "replicas": []}
|
||||
]
|
||||
""");
|
||||
});
|
||||
// Karapace answers a missing topic with 40401 on /partitions but 40403 on /topics/{topic}
|
||||
fakeServer.createContext("/topics/missing/partitions", exchange -> {
|
||||
record(exchange);
|
||||
respond(exchange, 404, "{\"error_code\":40401,\"message\":\"Topic missing not found\"}");
|
||||
});
|
||||
fakeServer.createContext("/topics/missing", exchange -> {
|
||||
record(exchange);
|
||||
respond(exchange, 404, "{\"error_code\":40403,\"message\":\"Unknown topic or partition\"}");
|
||||
});
|
||||
fakeServer.createContext("/topics/other-404-code/partitions", exchange -> {
|
||||
record(exchange);
|
||||
respond(exchange, 404, "{\"error_code\":40402,\"message\":\"Partition not found\"}");
|
||||
});
|
||||
fakeServer.createContext("/topics/broken/partitions", exchange -> {
|
||||
record(exchange);
|
||||
respond(exchange, 503, "{\"error_code\":50301,\"message\":\"broker unavailable\"}");
|
||||
});
|
||||
fakeServer.createContext("/consumers/refresh-group", exchange -> {
|
||||
record(exchange);
|
||||
respond(exchange, 200, "{\"instance_id\":\"rest-consumer-1\","
|
||||
+ "\"base_uri\":\"consumers/refresh-group/instances/rest-consumer-1\"}");
|
||||
});
|
||||
fakeServer.createContext(INSTANCE_PATH + "/assignments", exchange -> {
|
||||
record(exchange);
|
||||
respond(exchange, 204, "");
|
||||
});
|
||||
fakeServer.createContext(INSTANCE_PATH + "/positions", exchange -> {
|
||||
record(exchange);
|
||||
respond(exchange, 204, "");
|
||||
});
|
||||
|
||||
fakeServer.start();
|
||||
client = new KafkaRestClient(MAPPER, HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).build());
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
fakeServer.stop(0);
|
||||
}
|
||||
|
||||
private URI restBaseUri() {
|
||||
return URI.create("http://localhost:" + fakeServer.getAddress().getPort() + "/");
|
||||
}
|
||||
|
||||
private KafkaConsumerHandle newConsumer() {
|
||||
return client.createConsumer(restBaseUri(), "refresh-group", "earliest");
|
||||
}
|
||||
|
||||
private void record(HttpExchange exchange) throws IOException {
|
||||
lastRequestMethod.set(exchange.getRequestMethod());
|
||||
lastRequestPath.set(exchange.getRequestURI().toString());
|
||||
lastRequestBody.set(new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private static void respond(HttpExchange exchange, int status, String body) throws IOException {
|
||||
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
|
||||
exchange.sendResponseHeaders(status, bytes.length == 0 ? -1 : bytes.length);
|
||||
if (bytes.length > 0) {
|
||||
exchange.getResponseBody().write(bytes);
|
||||
}
|
||||
exchange.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPartitionsReturnsSortedPartitionIds() {
|
||||
List<Integer> partitions = client.listPartitions(restBaseUri(), "orders");
|
||||
|
||||
assertEquals(List.of(0, 1, 2), partitions);
|
||||
assertEquals("GET", lastRequestMethod.get());
|
||||
assertEquals("/topics/orders/partitions", lastRequestPath.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPartitionsOfMissingTopicRaisesTopicNotFound() {
|
||||
KafkaTopicNotFoundException thrown = assertThrows(KafkaTopicNotFoundException.class,
|
||||
() -> client.listPartitions(restBaseUri(), "missing"));
|
||||
|
||||
assertEquals("missing", thrown.getTopic());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPartitionsReportsUnrelated404AsRestClientFailureNotMissingTopic() {
|
||||
URI wrongPrefix = URI.create(restBaseUri() + "wrong-prefix/");
|
||||
|
||||
KafkaRestClientException thrown = assertThrows(KafkaRestClientException.class,
|
||||
() -> client.listPartitions(wrongPrefix, "orders"));
|
||||
|
||||
assertEquals(404, thrown.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPartitionsReports404WithOtherErrorCodeAsRestClientFailureNotMissingTopic() {
|
||||
KafkaRestClientException thrown = assertThrows(KafkaRestClientException.class,
|
||||
() -> client.listPartitions(restBaseUri(), "other-404-code"));
|
||||
|
||||
assertEquals(404, thrown.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPartitionsReportsBrokerErrorAsRestClientFailureNotMissingTopic() {
|
||||
KafkaRestClientException thrown = assertThrows(KafkaRestClientException.class,
|
||||
() -> client.listPartitions(restBaseUri(), "broken"));
|
||||
|
||||
assertEquals(503, thrown.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPartitionsAgainstStoppedProxyRaisesConnectivityError() {
|
||||
URI deadUri = restBaseUri();
|
||||
fakeServer.stop(0);
|
||||
|
||||
assertThrows(UncheckedIOException.class, () -> client.listPartitions(deadUri, "orders"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void assignSendsEveryTopicPartition() throws Exception {
|
||||
KafkaConsumerHandle consumer = newConsumer();
|
||||
|
||||
client.assign(consumer, List.of(new KafkaTopicPartition("orders", 0), new KafkaTopicPartition("orders", 1)));
|
||||
|
||||
assertEquals("POST", lastRequestMethod.get());
|
||||
assertTrue(lastRequestPath.get().endsWith("/assignments"));
|
||||
JsonNode partitions = MAPPER.readTree(lastRequestBody.get()).path("partitions");
|
||||
assertEquals(2, partitions.size());
|
||||
assertEquals("orders", partitions.get(1).path("topic").asText());
|
||||
assertEquals(1, partitions.get(1).path("partition").asInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void seekSendsExplicitOffsetPerPartition() throws Exception {
|
||||
KafkaConsumerHandle consumer = newConsumer();
|
||||
|
||||
client.seek(consumer, List.of(new KafkaOffsetDto("orders", 0, 11L), new KafkaOffsetDto("orders", 1, 5L)));
|
||||
|
||||
assertTrue(lastRequestPath.get().endsWith("/positions"));
|
||||
JsonNode offsets = MAPPER.readTree(lastRequestBody.get()).path("offsets");
|
||||
assertEquals(2, offsets.size());
|
||||
assertEquals(11L, offsets.get(0).path("offset").asLong());
|
||||
assertEquals(1, offsets.get(1).path("partition").asInt());
|
||||
assertEquals(5L, offsets.get(1).path("offset").asLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
void seekToBeginningTargetsTheBeginningPositionsResource() throws Exception {
|
||||
KafkaConsumerHandle consumer = newConsumer();
|
||||
|
||||
client.seekToBeginning(consumer, List.of(new KafkaTopicPartition("orders", 2)));
|
||||
|
||||
assertTrue(lastRequestPath.get().endsWith("/positions/beginning"));
|
||||
JsonNode partition = MAPPER.readTree(lastRequestBody.get()).path("partitions").get(0);
|
||||
assertEquals("orders", partition.path("topic").asText());
|
||||
assertEquals(2, partition.path("partition").asInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void seekWithNothingToPositionSendsNoRequest() {
|
||||
KafkaConsumerHandle consumer = newConsumer();
|
||||
lastRequestPath.set(null);
|
||||
|
||||
client.seek(consumer, List.of());
|
||||
client.seekToBeginning(consumer, List.of());
|
||||
|
||||
assertNull(lastRequestPath.get());
|
||||
}
|
||||
}
|
||||
+5
-5
@@ -1,4 +1,4 @@
|
||||
package io.floci.aws.services.pipes;
|
||||
package io.floci.aws.core.common.kafka;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
@@ -22,13 +22,13 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/** Exercises {@link PipesKafkaRestClient} against a fake server speaking Karapace's REST Proxy API. */
|
||||
class PipesKafkaRestClientTest {
|
||||
/** Exercises {@link KafkaRestClient} against a fake server speaking Karapace's REST Proxy API. */
|
||||
class KafkaRestClientTest {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
private HttpServer fakeServer;
|
||||
private PipesKafkaRestClient client;
|
||||
private KafkaRestClient client;
|
||||
private final AtomicReference<String> lastRequestBody = new AtomicReference<>();
|
||||
private final AtomicReference<String> lastRequestMethod = new AtomicReference<>();
|
||||
private final AtomicReference<String> lastRequestPath = new AtomicReference<>();
|
||||
@@ -88,7 +88,7 @@ class PipesKafkaRestClientTest {
|
||||
});
|
||||
|
||||
fakeServer.start();
|
||||
client = new PipesKafkaRestClient(MAPPER, HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).build());
|
||||
client = new KafkaRestClient(MAPPER, HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).build());
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package io.floci.aws.services.pipes;
|
||||
package io.floci.aws.core.common.kafka;
|
||||
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
@@ -2,6 +2,9 @@ package io.floci.aws.services.pipes;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.kafka.KafkaConsumerHandle;
|
||||
import io.floci.aws.core.common.kafka.KafkaRestClient;
|
||||
import io.floci.aws.core.common.kafka.KarapaceManager;
|
||||
import io.floci.aws.services.msk.MskService;
|
||||
import io.floci.aws.services.pipes.model.Pipe;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -28,14 +31,14 @@ class PipesKafkaConsumerManagerTest {
|
||||
|
||||
private MskService mskService;
|
||||
private KarapaceManager karapaceManager;
|
||||
private PipesKafkaRestClient restClient;
|
||||
private KafkaRestClient restClient;
|
||||
private PipesKafkaConsumerManager manager;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
mskService = mock(MskService.class);
|
||||
karapaceManager = mock(KarapaceManager.class);
|
||||
restClient = mock(PipesKafkaRestClient.class);
|
||||
restClient = mock(KafkaRestClient.class);
|
||||
manager = new PipesKafkaConsumerManager(mskService, karapaceManager, restClient);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,9 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.kafka.KafkaHeaderDto;
|
||||
import io.floci.aws.core.common.kafka.KafkaOffsetDto;
|
||||
import io.floci.aws.core.common.kafka.KafkaRecordDto;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.dynamodb.backend.DynamoDbStreamReader;
|
||||
|
||||
Reference in new issue
Block a user