Querying stores
A running application can answer questions from its stores: a user's latest balance in a table, the count of a page's views, and so on. Each member holds only part of each store, so the member you ask may not hold the key. xkafka finds the member that does and asks it, through a connection your application provides.
A query sees each update as soon as the member applies it, before the update is committed.
Setting up
Every member of the application needs:
- An endpoint, the host and port other members reach it at, given with
StreamsSettings'withQueries. A member without one cannot be asked, so other members cannot answer queries for the keys it holds. - A
Forward, given toKafkaClient.streams, which sends the bytes of a query to another member's endpoint and gives back the bytes it answers. Without one, a query that needs another member fails withUnreachable. - An internal route on that endpoint, which passes the bytes it receives to
streams.answerand sends back what it returns.
With http4s, the xkafka-streams-http4s module gives the
last two, on every platform:
// In build.sbt
libraryDependencies += "io.github.pmbmegalom" %%% "xkafka-streams-http4s" % "0.7.1"
Http4sQueries.forward(client) posts each query to the member that holds the
key, through your http4s client, and Http4sQueries.routes(streams) is the
internal route that answers them. Both use the path /xkafka/query unless you
give another. An endpoint's host may be a name or an IPv4 or IPv6 address, the
IPv6 one with or without brackets.
This example serves the route and reaches other members with Ember, from
http4s-ember-server and http4s-ember-client, which you add yourself. Any http4s
client and server work:
// Run a member that answers other members' queries over HTTP, and asks them for the keys they hold
import cats.effect.IO
import com.comcast.ip4s.*
import org.http4s.ember.client.EmberClientBuilder
import org.http4s.ember.server.EmberServerBuilder
import xkafka.*
import xkafka.streams.*
import xkafka.streams.http4s.Http4sQueries
def member(settings: StreamsSettings, topology: Topology[IO]): IO[Unit] =
Endpoint.from("orders-3.internal", 8080).liftTo[IO].flatMap: here =>
EmberClientBuilder.default[IO].build.use: http =>
KafkaClient[IO].streams(settings.withQueries(here), topology, Http4sQueries.forward(http)).use: streams =>
val internal = Http4sQueries.routes(streams).orNotFound
val server = EmberServerBuilder.default[IO].withHost(ipv4"0.0.0.0").withPort(port"8080").withHttpApp(internal).build
server.surround(streams.run) // Serve the route for as long as the member runs
To reach members over TLS, pass scheme = Uri.Scheme.https to forward, with a
client set up for it, and serve the route over TLS. The client also decides on
authentication and timeouts. A member that answers with an error status fails
the query with Unreachable.
Members can talk any other way. A Forward is a function
(Endpoint, Chunk[Byte]) => F[Chunk[Byte]] from a member's endpoint and a
query's bytes to the bytes it answers, and fails the query with Unreachable
when it raises an error. The internal route only needs to pass the bytes it
receives to streams.answer and send back what it returns.
Keep the internal route away from the outside world: it answers any query, for
any store. Http4sQueries.routes answers bytes that are not a query with
400 Bad Request, and a body over Http4sQueries.MaxQuery (2 MiB) with
413 Payload Too Large, without reading the rest of it.
Querying a store
Describe a key-value store with QueryableStore.keyValue, giving its name and the
serdes its keys and values are written with, then pass a query to query. The call
is the same whichever member holds the key. Here a public route answers with a
user's balance:
// Answer GET /balance/<user> with the user's balance, wherever it is held
import cats.effect.IO
import org.http4s.*
import org.http4s.dsl.io.*
import org.http4s.headers.`Retry-After`
import xkafka.Serde
import xkafka.streams.*
val balances = QueryableStore.keyValue(Store("balances"), Serde.utf8[IO], Serde.long[IO])
def public(streams: KafkaStreams[IO]): HttpRoutes[IO] =
HttpRoutes.of[IO]:
case GET -> Root / "balance" / user =>
streams.query(balances.get(user)).flatMap:
case Right(Some(balance)) => Ok(balance.toString)
case Right(None) => NotFound()
case Left(QueryProblem.Rebalancing) => ServiceUnavailable().map(_.putHeaders(`Retry-After`.unsafeFromLong(1))) // Moving: ask again shortly
case Left(problem) => BadGateway(problem.toString)
These stores can be queried:
- a table's store, given when the table is read or made;
- the store of an aggregation, such as
count,reduceoraggregate; - a processor's
KeyValueStore; - a global table's store, which every member holds whole, so it is always answered by the member you ask.
Each store offers:
| query | answer |
|---|---|
get(key) |
what the store holds for the key, if anything |
range(from, to) |
the keys from from to to, both included, with their values |
all |
every key with its value |
range and all ask every member holding part of the store. Their keys come in
the order of their serialized bytes. For text keys that is the order of their
characters, capitals before lower-case letters, so Zoe comes before adam. For
numbers written with Serde.long, negative numbers come after the positive ones.
Window and session stores
A windowed aggregation keeps a value for each key and window, and a session
aggregation one for each key and session. Query them with
QueryableStore.window and QueryableStore.session, giving the serdes of the key
itself, not the windowed serde your output topic uses:
// Look up a page's views per minute over the last hour, and a user's visits
import scala.concurrent.duration.*
import cats.effect.IO
import xkafka.*
import xkafka.streams.*
val perMinute = QueryableStore.window(Store("views-per-minute"), Serde.utf8[IO], Serde.long[IO])
val visits = QueryableStore.session(Store("visits"), Serde.utf8[IO], Serde.long[IO])
def lastHour(streams: KafkaStreams[IO], page: String): IO[Either[QueryProblem, Vector[(Long, Long)]]] =
IO.realTime.flatMap: now =>
val from = Timestamp.fromEpochMillis((now - 1.hour).toMillis)
streams.query(perMinute.fetch(page, from, Timestamp.fromEpochMillis(now.toMillis))) // Each window's start, with its count
def visitsOf(streams: KafkaStreams[IO], user: String): IO[Either[QueryProblem, Vector[(Window, Long)]]] =
streams.query(visits.fetch(user)) // Each visit's window, with its count
| store | query | answer |
|---|---|---|
| window | fetch(key, from, to) |
the key's windows starting from from to to, both included: each window's start time with its value, earliest first |
| window | fetchAll(from, to) |
the windows of every key starting in that time: each key and window with its value, earliest start first, then by key |
| session | fetch(key) |
the key's sessions: each session's window with its value, the earliest ending first |
fetchAll gives each window's start and end; the end is the start plus the window
size your program sets. A window store keeps windows only for its retention, so
older windows are no longer found.
When a query cannot be answered
query returns a QueryProblem when it cannot answer:
| problem | meaning | what to do |
|---|---|---|
Rebalancing |
the part of the store the query needs is moving between members, or is still being filled | ask again shortly |
Unreachable(member, reason) |
the member holding it could not be reached, or failed to answer | check that member and your Forward |
NotXkafka(member) |
the member holding it answered in a format xkafka does not read | see Java members |
UnknownStore(store) |
the program has no store of that name of the kind queried | check the store's name, and that it is a key-value, window or session store as queried |
FedByPattern(store) |
a Java member's program fills the store from a pattern of topics, and xkafka cannot tell which members hold it | read the store from Java members with Java's own queries |
A range fails as a whole if any member it needs cannot answer, so you never get part of a store without being told.
Standby copies
If the broker keeps standby copies of the
stores, query(..., allowStandby = true) lets a copy answer. A copy can be a
little behind, but it can answer while its store is moving between members.
Java members
An application can mix xkafka and Java Kafka Streams members. A Java member
answers xkafka's queries through JavaQueries, from the
xkafka-streams-java module. Give it the
instance, the application's id, and each store that can be queried with its Java
serdes, then serve its answer as the member's internal route: at the path
xkafka members post to, /xkafka/query with Http4sQueries.forward, taking the
posted bytes and answering with the bytes it returns. The member's
application.server setting is the endpoint xkafka members reach it at.
// Let a Java member answer xkafka members' queries of its page view counts
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import xkafka.streams.interop.JavaQueries;
final class Answering {
private final JavaQueries queries;
Answering(KafkaStreams streams) {
queries =
JavaQueries.of(streams, "page-view-counter")
.keyValue("views-per-page", Serdes.String(), Serdes.Long()) // A table or a count
.window("views-per-minute", Serdes.String(), Serdes.Long()); // A windowed count
}
// Serve this as the internal route: the bytes posted to it in, the answer's bytes out
byte[] answer(byte[] query) {
return queries.answer(query);
}
}
JavaQueries also takes session stores. It reads each store through Java's own
interactive queries on that instance. A Java member without it does not answer
in a format xkafka reads, so a query that needs it fails with NotXkafka, or with
Unreachable if nothing answers at its endpoint.
A Java member asks with JavaQuerying, giving it the instance, its topology, the
application's id, a Kafka Admin (used to look up each topic's partition count),
and a function that posts a query to another member's internal route. It finds the
member holding each key, xkafka or Java, and asks it, even when that member is
itself. A global table's store, which every member holds whole, it answers from
its own. While a partition moves between members, a query that needs it fails
with Rebalancing:
// Let a Java member ask for a page's view count, wherever it is held
import java.util.Optional;
import java.util.function.BiFunction;
import org.apache.kafka.clients.admin.Admin;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.Topology;
import org.apache.kafka.streams.state.HostInfo;
import xkafka.streams.interop.JavaQuerying;
final class Asking {
private final JavaQuerying querying;
// `admin` is an Admin for the same cluster, which you close after the instance.
// `post` is your HTTP client: it posts the bytes to the member's internal route and returns what it answers
Asking(KafkaStreams streams, Topology topology, Admin admin, BiFunction<HostInfo, byte[], byte[]> post) {
querying = JavaQuerying.of(streams, topology, "page-view-counter", admin, post);
}
// Throws a QueryProblemException when it cannot answer, whose problem says why, as xkafka's queries do
Optional<Long> views(String page) {
return querying.get("views-per-page", page, Serdes.String(), Serdes.Long());
}
}
| method | answer |
|---|---|
get(store, key, ...) |
what a key-value store holds for the key, if anything |
range(store, from, to, ...) |
the keys of a key-value store from from to to, both included, as a list of Kafka's KeyValue |
all(store, ...) |
every key of a key-value store with its value, as a list of Kafka's KeyValue |
fetch(store, key, from, to, ...) |
a key's windows starting in that time: each window's start time with its value |
fetchAll(store, from, to, windowSize, ...) |
the windows of every key starting in that time, each as a Windowed key with its value |
sessions(store, key, ...) |
a key's sessions, each as a Windowed key with its value |
Java's metadata does not say how long a store's windows are, so fetchAll needs
the size from you, and ends each window that long after it starts. Give it the
size your program's windows have: a wrong size gives wrong ends, and nothing can
tell.