Repository navigation
Description
Background
MqttKafkaSessionFactory currently coordinates MQTT session ownership using a custom Kafka consumer-group protocol ("highlander"). This issue replaces that mechanism with StoreHandler-based coordination and folds in the related cross-replica session-lookup, redirect-hint plumbing, and the #migrate compacted-topic takeover signal.
A previous release added an optional store field and a deprecation warning announcing this change. This issue completes the migration.
Proposal
Coordinate session ownership via the Store SPI using the existing StoreHandler surface (lock, renew, unlock, put, watch). No SPI changes are required.
Cooperative takeover protocol
- New owner candidate
puts its ownership record (replica identity, acquisition timestamp, identity strength, optional externally-reachable address) on the session key. - Current owner's
watchfires with the new value. - Current owner detects displacement (value owner != self), stops lease renewal, drains pending state writes, calls
unlock. - New candidate
locks the session key — succeeds. - New candidate writes its initial session state and begins serving.
Strong vs weak session ownership
The ownership record includes an identity strength classification:
- Strong identity: this replica has an externally-addressable identity (
serverRefconfigured). The address is published in the ownership record. - Weak identity: no externally-addressable address is configured. The ownership record carries only an opaque replica identity.
The strength classification drives whether a CONNECT arriving on a non-owner replica should redirect the client back to the owner or initiate a takeover.
CONNECT handling with strong/weak ownership
When CONNECT for clientId X arrives at replica B:
- B reads the current ownership record for X from the Store.
- If the current owner A has strong identity → B responds with MQTT 5 CONNACK reason
SERVER_MOVED(0x9C) andServer Referenceproperty pointing to A. No takeover claim is put. Client reconnects to A; session locality is preserved. - If the current owner A has weak identity (or there is no current owner) → B proceeds with the cooperative takeover protocol above.
If a CONNECT later arrives directly at A for the same clientId (either via redirect from B, or directly via the load balancer), normal MQTT session-takeover semantics apply locally at A — A kicks any stale prior connection and accepts the new one.
If A becomes unreachable or hung, the lease TTL ensures the lock is released within bounded time, after which a subsequent CONNECT at B (or any other replica) acquires the lock via Case (3) above.
Failure recovery via TTL
The lease TTL bounds recovery when the cooperative path cannot complete:
- Owner dead — ceases renewal → lease expires → new candidate locks.
- Owner drain hung — watch handler disables renewal → lease expires → new candidate locks.
Renewal must be gated on continued ownership: the watch handler sets a flag that suppresses further renewals.
Replacement of the #migrate compacted-topic takeover signal
MqttKafkaSessionFactory currently writes a <clientId>#migrate record on mqtt-sessions with a sender-id header during session establishment. Other replicas serving the same clientId observe this via a merged-stream filter (excluding their own sender-id) and yield ownership. This is an alternate channel for the same takeover signal carried by the highlander group, using the compacted topic instead of a Kafka consumer group.
The Store-based cooperative takeover protocol replaces this mechanism directly:
#migrate mechanism |
Store-based replacement |
|---|---|
New owner writes <clientId>#migrate record with own sender-id |
New owner puts ownership record on session key |
| Existing owner's merged-stream filter triggers | Existing owner's watch fires |
| Sender-id filter distinguishes self from others | value.owner != self check |
After this issue, the #migrate write, the merged-stream filter on clientIdMigrate, the associated SENDER_ID exclude clause, the MIGRATE_KEY_POSTFIX constant, and the clientIdMigrate field are removed.
Configuration
stores:
memory0:
type: memory
bindings:
mqtt-kafka0:
type: mqtt-kafka
kind: proxy
options:
...
store: memory0
serverRef: mqtt-node-1.example.com:1883 # optional; presence makes this replica's identity strongserverRef continues to be the externally-reachable address advertised by this replica. Its presence classifies this replica's ownership as strong; its absence classifies it as weak. The option no longer drives any Kafka consumer-coordinator plumbing — it is published directly in the Store ownership record.
New mqtt-kafka options for lease duration and renewal interval, with defaults sized for fast cooperative takeover and bounded TTL fallback.
In scope
- Schema change:
storebecomes required onmqtt-kafkabinding; validation fails if absent. - Implement cooperative takeover protocol in
MqttKafkaSessionFactoryusingStoreHandler. - Watch-gated renewal lifecycle.
- Configuration knobs for lease duration and renewal interval.
- Ownership record schema includes replica identity, acquisition timestamp, identity strength, and (for strong identity) the externally-reachable address.
- On CONNECT in
MqttServerFactory/MqttKafkaSessionFactory, consult the Store for current owner; if owner has strong identity, respond with MQTT 5 CONNACKSERVER_MOVED+Server Referencerather than initiating takeover. If owner has weak identity (or no owner), proceed with cooperative takeover. - Replace the cross-replica session lookup (currently the
mqtt-clientsKafka consumer group withconsumerId=serverRef) with the ownership record contents from the Store. - Remove
KafkaResetEx.consumerId → MqttResetEx.serverRefplumbing for session redirect; the redirect hint now flows via the Store watch event payload. - Remove the
#migratecompacted-topic takeover signal: stop writing<clientId>#migraterecords; remove the merged-stream filter onclientIdMigrateand the associatedSENDER_IDexclude clause; remove theMIGRATE_KEY_POSTFIXconstant andclientIdMigratefield. - Remove the highlander-group code path from
MqttKafkaSessionFactory. - Remove
"highlander"protocol references fromKafkaClientGroupFactoryif no other callers remain. - Tests: cooperative takeover, TTL-recovery failure modes,
Clean Start=truedisplacement, CONNACK redirect when owner has strong identity, takeover when owner has weak identity, fallback to takeover after lease expiry when a strong-identity owner becomes unreachable. - Update existing k3po specs that exercise session takeover, server redirect, and
#migratesignaling. - Documentation update.
Acceptance criteria
- All existing
MqttKafkaSessionFactorytests pass under the new ownership coordination. - New tests cover: cooperative takeover, TTL-recovery failure modes,
Clean Start=truedisplacement, CONNACKSERVER_MOVEDredirect for strong-identity owners, cooperative takeover for weak-identity owners, and takeover via TTL expiry when a strong-identity owner is unreachable. - The
"highlander"protocol name is no longer referenced bybinding-mqtt-kafka; the corresponding election code inMqttKafkaSessionFactoryis removed. - The
mqtt-clientsKafka consumer group is no longer created by the binding. - No
<clientId>#migraterecords are written by the binding;MIGRATE_KEY_POSTFIXandclientIdMigrateare removed fromMqttKafkaSessionFactory. - Client redirect hint (when owner identity is strong) is sourced from the Store ownership record, not from
KafkaResetEx.consumerId. - Configurations missing the
storeoption fail validation with a clear error message. - Documentation updated for the new session ownership model, strong/weak identity classification, CONNACK redirect behavior, and configuration knobs.
References
runtime/binding-mqtt-kafka/src/main/java/io/aklivity/zilla/runtime/binding/mqtt/kafka/internal/stream/MqttKafkaSessionFactory.javaruntime/binding-mqtt/src/main/java/io/aklivity/zilla/runtime/binding/mqtt/internal/stream/MqttServerFactory.javaruntime/binding-kafka/src/main/java/io/aklivity/zilla/runtime/binding/kafka/internal/stream/KafkaClientGroupFactory.javaruntime/engine/src/main/java/io/aklivity/zilla/runtime/engine/store/StoreHandler.javaruntime/store-memory/src/main/java/io/aklivity/zilla/runtime/store/memory/internal/MemoryStoreHandler.java
Activity
feat(binding-mqtt-kafka): add session lease/renew tuning knobs (#1798)
feat(binding-mqtt-kafka): make store required on mqtt-kafka binding (#…
feat(binding-mqtt-kafka): resolve StoreHandler on binding config (#1798)
feat(binding-mqtt-kafka): acquire session ownership lease via store (#…
feat(binding-mqtt-kafka): cooperative session takeover via store (#1798)
test(binding-mqtt-kafka): remove highlander group-only session specs (#…
fix(binding-asyncapi): inject store into generated mqtt-kafka binding (…
build(binding-mqtt-kafka): temporarily lower coverage gate to 0.88 (#…
fix(binding-asyncapi): drop mqtt_kafka_store0 injection from generate…
[2.0] Refactor MQTT session management to use store-based persistence (…