kevin-wu24 commented on code in PR #22505:
URL: https://github.com/apache/kafka/pull/22505#discussion_r3374600665
##########
raft/src/main/java/org/apache/kafka/raft/internals/BlockingMessageQueue.java:
##########
@@ -17,60 +17,51 @@
package org.apache.kafka.raft.internals;
import org.apache.kafka.common.errors.InterruptException;
-import org.apache.kafka.common.protocol.ApiMessage;
-import org.apache.kafka.raft.RaftMessage;
import org.apache.kafka.raft.RaftMessageQueue;
+import java.util.Optional;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class BlockingMessageQueue implements RaftMessageQueue {
- private static final RaftMessage WAKEUP_MESSAGE = new RaftMessage() {
- @Override
- public int correlationId() {
- return 0;
- }
-
- @Override
- public ApiMessage data() {
- return null;
- }
- };
-
- private final BlockingQueue<RaftMessage> queue = new
LinkedBlockingQueue<>();
- private final AtomicInteger size = new AtomicInteger(0);
+ private final BlockingQueue<QueueEntry> queue = new
LinkedBlockingQueue<>();
+ private final AtomicInteger messageCount = new AtomicInteger(0);
@Override
- public RaftMessage poll(long timeoutMs) {
+ public Optional<QueueEntry> poll(long timeoutMs) {
try {
- RaftMessage message = queue.poll(timeoutMs, TimeUnit.MILLISECONDS);
- if (message == null || message == WAKEUP_MESSAGE) {
- return null;
- } else {
- size.decrementAndGet();
- return message;
+ var entry = queue.poll(timeoutMs, TimeUnit.MILLISECONDS);
+ while (entry != null && entry.message() == null) {
+ // Drain the queue of all of the wakeup events
+ entry = queue.poll();
+ }
Review Comment:
I think I agree with this change, but I'm curious about the motivation. From
my perspective, the motivation is to reduce extra invocations of `poll()` that
are "wasted" because a `WAKEUP_MESSAGE` would popped off the queue instead of
an actual `RaftMessage`. Are there other reasons for this?
##########
raft/src/main/java/org/apache/kafka/raft/internals/BlockingMessageQueue.java:
##########
@@ -17,60 +17,51 @@
package org.apache.kafka.raft.internals;
import org.apache.kafka.common.errors.InterruptException;
-import org.apache.kafka.common.protocol.ApiMessage;
-import org.apache.kafka.raft.RaftMessage;
import org.apache.kafka.raft.RaftMessageQueue;
+import java.util.Optional;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class BlockingMessageQueue implements RaftMessageQueue {
- private static final RaftMessage WAKEUP_MESSAGE = new RaftMessage() {
- @Override
- public int correlationId() {
- return 0;
- }
-
- @Override
- public ApiMessage data() {
- return null;
- }
- };
-
- private final BlockingQueue<RaftMessage> queue = new
LinkedBlockingQueue<>();
- private final AtomicInteger size = new AtomicInteger(0);
+ private final BlockingQueue<QueueEntry> queue = new
LinkedBlockingQueue<>();
+ private final AtomicInteger messageCount = new AtomicInteger(0);
@Override
- public RaftMessage poll(long timeoutMs) {
+ public Optional<QueueEntry> poll(long timeoutMs) {
try {
- RaftMessage message = queue.poll(timeoutMs, TimeUnit.MILLISECONDS);
- if (message == null || message == WAKEUP_MESSAGE) {
- return null;
- } else {
- size.decrementAndGet();
- return message;
+ var entry = queue.poll(timeoutMs, TimeUnit.MILLISECONDS);
+ while (entry != null && entry.message() == null) {
+ // Drain the queue of all of the wakeup events
+ entry = queue.poll();
+ }
+ if (entry != null) {
+ messageCount.decrementAndGet();
}
+ return Optional.ofNullable(entry);
} catch (InterruptedException e) {
throw new InterruptException(e);
}
}
@Override
- public void add(RaftMessage message) {
- queue.add(message);
- size.incrementAndGet();
+ public void add(QueueEntry entry) {
+ queue.add(entry);
+ if (entry.message() != null) {
+ messageCount.incrementAndGet();
+ }
Review Comment:
Isn't this if condition always met because this method is only used to add
non-sentinel `QueueEntries`?
##########
raft/src/main/java/org/apache/kafka/raft/internals/BlockingMessageQueue.java:
##########
@@ -17,60 +17,51 @@
package org.apache.kafka.raft.internals;
import org.apache.kafka.common.errors.InterruptException;
-import org.apache.kafka.common.protocol.ApiMessage;
-import org.apache.kafka.raft.RaftMessage;
import org.apache.kafka.raft.RaftMessageQueue;
+import java.util.Optional;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class BlockingMessageQueue implements RaftMessageQueue {
- private static final RaftMessage WAKEUP_MESSAGE = new RaftMessage() {
- @Override
- public int correlationId() {
- return 0;
- }
-
- @Override
- public ApiMessage data() {
- return null;
- }
- };
-
- private final BlockingQueue<RaftMessage> queue = new
LinkedBlockingQueue<>();
- private final AtomicInteger size = new AtomicInteger(0);
+ private final BlockingQueue<QueueEntry> queue = new
LinkedBlockingQueue<>();
+ private final AtomicInteger messageCount = new AtomicInteger(0);
@Override
- public RaftMessage poll(long timeoutMs) {
+ public Optional<QueueEntry> poll(long timeoutMs) {
try {
- RaftMessage message = queue.poll(timeoutMs, TimeUnit.MILLISECONDS);
- if (message == null || message == WAKEUP_MESSAGE) {
- return null;
- } else {
- size.decrementAndGet();
- return message;
+ var entry = queue.poll(timeoutMs, TimeUnit.MILLISECONDS);
+ while (entry != null && entry.message() == null) {
+ // Drain the queue of all of the wakeup events
+ entry = queue.poll();
+ }
+ if (entry != null) {
+ messageCount.decrementAndGet();
}
+ return Optional.ofNullable(entry);
} catch (InterruptedException e) {
throw new InterruptException(e);
}
}
@Override
- public void add(RaftMessage message) {
- queue.add(message);
- size.incrementAndGet();
+ public void add(QueueEntry entry) {
+ queue.add(entry);
+ if (entry.message() != null) {
+ messageCount.incrementAndGet();
+ }
}
@Override
public boolean isEmpty() {
- return size.get() == 0;
+ return messageCount.get() == 0;
}
@Override
public void wakeup() {
- queue.add(WAKEUP_MESSAGE);
+ queue.add(new QueueEntry(null));
Review Comment:
One thing that has changed is that we will call
`messageCount.decrementAndGet` even for wakeup messages. Previously, that did
not happen. The only readers of `messageCount` seem to be from tests, but
something to keep in mind.
In the previous and proposed code, `messageCount.incrementAndGet` is not
called for wakeup messages.
##########
raft/src/main/java/org/apache/kafka/raft/KafkaRaftClient.java:
##########
@@ -2883,24 +2896,12 @@ private RequestSendResult maybeSendRequest(
currentTimeMs
);
- requestMessage.completion.whenComplete((response, exception) -> {
- if (exception != null) {
- ApiKeys api = ApiKeys.forId(request.apiKey());
- Errors error = Errors.forException(exception);
- ApiMessage errorResponse = RaftUtil.errorResponse(api,
error);
-
- response = new RaftResponse.Inbound(
- correlationId,
- errorResponse,
- destination
- );
Review Comment:
It looks like we remove the client-side error response wrapping both here
and in the `DefaultRequestSender`. I see in `handleRequest` that when a
request's future completes with a null response, we do call
`RaftUtil.errorResponse` server-side before sending the response back over the
wire. Was this just some redundant code duplication then?
##########
raft/src/main/java/org/apache/kafka/raft/KafkaRaftClient.java:
##########
@@ -3677,14 +3690,12 @@ public void poll() {
long startWaitTimeMs = time.milliseconds();
kafkaRaftMetrics.updatePollStart(startWaitTimeMs);
- RaftMessage message = messageQueue.poll(pollTimeoutMs);
+ var maybeEntry = messageQueue.poll(pollTimeoutMs);
Review Comment:
What happens when we return a `Optional.of(QueueEntry(null))` here (i.e. a
wakeup message)? The optional is present, and I think we will throw an
IllegalArgumentException in `handleInboundMessage` because `entry.message() ==
null`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]