Reputation: 73
Spring cloud stream (kafka) producer is does not throw exception when kafka broker is down.
boolean sent = streamBridge.send("sometopic", deliver);
log.info("-------------> message sent? {}",sent);
In the above example the boolean evaluates to true even when Kafka broker is down which results in message loss (even using for example outbox pattern). I know that it is extreme example but it just happened.
How can we solve this issue when we know that Spring cloud stream tends to be async and non-blocking? For my particular case i use the AdminClient
in order to check if the broker is available but it seems to be a naive solution. I am planing to use also transactional producers. So, how can we for example throw exception when kafka broker is down?
2023-10-06T00:06:37.372+01:00 INFO 58262 --- [ main] o.a.kafka.common.utils.AppInfoParser : Kafka startTimeMs: 1696547197372
2023-10-06T00:06:37.496+01:00 INFO 58262 --- [| adminclient-1] org.apache.kafka.clients.NetworkClient : [AdminClient clientId=adminclient-1] Node -1 disconnected.
2023-10-06T00:06:37.497+01:00 INFO 58262 --- [| adminclient-1] org.apache.kafka.clients.NetworkClient : [AdminClient clientId=adminclient-1] Cancelled in-flight API_VERSIONS request with correlation id 0 due to node -1 being disconnected (elapsed time since creation: 114ms, elapsed time since send: 114ms, request timeout: 3600000ms)
2023-10-06T00:06:37.601+01:00 INFO 58262 --- [| adminclient-1] org.apache.kafka.clients.NetworkClient : [AdminClient clientId=adminclient-1] Node -1 disconnected.
2023-10-06T00:06:37.601+01:00 INFO 58262 --- [| adminclient-1] org.apache.kafka.clients.NetworkClient : [AdminClient clientId=adminclient-1] Cancelled in-flight API_VERSIONS request with correlation id 1 due to node -1 being disconnected (elapsed time since creation: 2ms, elapsed time since send: 2ms, request timeout: 3600000ms)
2023-10-06T00:06:37.805+01:00 INFO 58262 --- [| adminclient-1] org.apache.kafka.clients.NetworkClient : [AdminClient clientId=adminclient-1] Node -1 disconnected.
2023-10-06T00:06:37.805+01:00 INFO 58262 --- [| adminclient-1] org.apache.kafka.clients.NetworkClient : [AdminClient clientId=adminclient-1] Cancelled in-flight API_VERSIONS request with correlation id 2 due to node -1 being disconnected (elapsed time since creation: 1ms, elapsed time since send: 1ms, request timeout: 3600000ms)
2023-10-06T00:06:38.009+01:00 INFO 58262 --- [| adminclient-1] org.apache.kafka.clients.NetworkClient : [AdminClient clientId=adminclient-1] Node -1 disconnected.
2023-10-06T00:06:38.009+01:00 INFO 58262 --- [| adminclient-1] org.apache.kafka.clients.NetworkClient : [AdminClient clientId=adminclient-1] Cancelled in-flight API_VERSIONS request with correlation id 3 due to node -1 being disconnected (elapsed time since creation: 1ms, elapsed time since send: 1ms, request timeout: 3600000ms)
2023-10-06T00:06:38.418+01:00 INFO 58262 --- [| adminclient-1] org.apache.kafka.clients.NetworkClient : [AdminClient clientId=adminclient-1] Node -1 disconnected.
2023-10-06T00:06:38.418+01:00 INFO 58262 --- [| adminclient-1] org.apache.kafka.clients.NetworkClient : [AdminClient clientId=adminclient-1] Cancelled in-flight API_VERSIONS request with correlation id 4 due to node -1 being disconnected (elapsed time since creation: 2ms, elapsed time since send: 2ms, request timeout: 3600000ms)
2023-10-06T00:06:39.129+01:00 INFO 58262 --- [| adminclient-1] org.apache.kafka.clients.NetworkClient : [AdminClient clientId=adminclient-1] Node -1 disconnected.
2023-10-06T00:06:39.129+01:00 INFO 58262 --- [| adminclient-1] org.apache.kafka.clients.NetworkClient : [AdminClient clientId=adminclient-1] Cancelled in-flight API_VERSIONS request with correlation id 5 due to node -1 being disconnected (elapsed time since creation: 2ms, elapsed time since send: 2ms, request timeout: 3600000ms)
2023-10-06T00:06:40.246+01:00 INFO 58262 --- [| adminclient-1] org.apache.kafka.clients.NetworkClient : [AdminClient clientId=adminclient-1] Node -1 disconnected.
2023-10-06T00:06:40.246+01:00 INFO 58262 --- [| adminclient-1] org.apache.kafka.clients.NetworkClient : [AdminClient clientId=adminclient-1] Cancelled in-flight API_VERSIONS request with correlation id 6 due to node -1 being disconnected (elapsed time since creation: 1ms, elapsed time since send: 1ms, request timeout: 3600000ms)
Upvotes: 0
Views: 313
Reputation: 174494
When using transactional producers, the commit will fail; for non-transactional producers, you can set the sync producer property to wait for the send result.
sync
Whether the producer is synchronous.
Default: false.
Upvotes: 1