jsancio commented on code in PR #16518:
URL: https://github.com/apache/kafka/pull/16518#discussion_r1664928188
##########
raft/src/main/java/org/apache/kafka/raft/KafkaRaftClient.java:
##########
@@ -151,6 +151,7 @@ public final class KafkaRaftClient<T> implements
RaftClient<T> {
public static final int MAX_FETCH_WAIT_MS = 500;
public static final int MAX_BATCH_SIZE_BYTES = 8 * 1024 * 1024;
public static final int MAX_FETCH_SIZE_BYTES = MAX_BATCH_SIZE_BYTES;
+ public static final OffsetAndEpoch BOOTSTRAP_SNAPSHOT_ID = new
OffsetAndEpoch(0, 0);
Review Comment:
Let's define this in `o.a.k.s.Snapshots` instead.
##########
raft/src/main/java/org/apache/kafka/raft/LeaderState.java:
##########
@@ -202,7 +214,46 @@ public void appendLeaderChangeMessage(long currentTimeMs) {
.setVoters(voters)
.setGrantingVoters(grantingVoters);
- accumulator.appendLeaderChangeMessage(leaderChangeMessage,
currentTimeMs);
+ accumulator.appendControlMessages((baseOffset, epoch, buffer) -> {
+ try (MemoryRecordsBuilder builder = new MemoryRecordsBuilder(
+ buffer,
+ RecordBatch.CURRENT_MAGIC_VALUE,
+ Compression.NONE,
+ TimestampType.CREATE_TIME,
+ baseOffset,
+ currentTimeMs,
+ RecordBatch.NO_PRODUCER_ID,
+ RecordBatch.NO_PRODUCER_EPOCH,
+ RecordBatch.NO_SEQUENCE,
+ false,
+ true,
+ epoch,
+ buffer.capacity()
+ )
+ ) {
+ builder.appendLeaderChangeMessage(
+ currentTimeMs,
+ leaderChangeMessage
+ );
+ VoterSetOffset voterSetOffset =
lastVoterSetOffset.orElse(null);
+ // if lastVoterOffset is -1 we know the leader hasn't written
the bootstrap snapshot records to the log yet
+ if (voterSetOffset != null && voterSetOffset.offset() == -1) {
Review Comment:
Avoid the use of `null`.
```java
lastVoterSetOffset.ifPresent(voterSetOffset -> {
// if lastVoterOffset is -1 we know the leader hasn't
written the bootstrap snapshot records to the log yet
if (voterSetOffset.offset() == -1) {
```
##########
raft/src/main/java/org/apache/kafka/raft/internals/VoterSetOffset.java:
##########
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.raft.internals;
+
+public class VoterSetOffset {
+ private final VoterSet voterSet;
+ private final Long offset;
Review Comment:
An object is not needed. Use `long` instead. It takes less space and doesn't
require a dereference.
##########
raft/src/main/java/org/apache/kafka/raft/internals/VoterSetOffset.java:
##########
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.raft.internals;
+
+public class VoterSetOffset {
Review Comment:
Why did you add this type instead of using `LogHistory.Entry<VoterSet>`?
##########
raft/src/main/java/org/apache/kafka/raft/KafkaRaftClient.java:
##########
@@ -454,6 +455,7 @@ public void initialize(
nodeId,
nodeDirectoryId,
partitionState::lastVoterSet,
+ partitionState::lastVoterSetOffset,
Review Comment:
It is unfortunate that we need to add a parameter to the constructor just so
that it can pass this value to `LeaderState`. I am starting to think that
`QuorumState` should just have access to the `partitionState`
(`KRaftControlRecordStateMachine`) object.
##########
raft/src/main/java/org/apache/kafka/raft/LeaderState.java:
##########
@@ -202,7 +214,46 @@ public void appendLeaderChangeMessage(long currentTimeMs) {
.setVoters(voters)
.setGrantingVoters(grantingVoters);
- accumulator.appendLeaderChangeMessage(leaderChangeMessage,
currentTimeMs);
+ accumulator.appendControlMessages((baseOffset, epoch, buffer) -> {
+ try (MemoryRecordsBuilder builder = new MemoryRecordsBuilder(
+ buffer,
+ RecordBatch.CURRENT_MAGIC_VALUE,
+ Compression.NONE,
+ TimestampType.CREATE_TIME,
+ baseOffset,
+ currentTimeMs,
+ RecordBatch.NO_PRODUCER_ID,
+ RecordBatch.NO_PRODUCER_EPOCH,
+ RecordBatch.NO_SEQUENCE,
+ false,
+ true,
+ epoch,
+ buffer.capacity()
+ )
+ ) {
+ builder.appendLeaderChangeMessage(
+ currentTimeMs,
+ leaderChangeMessage
+ );
+ VoterSetOffset voterSetOffset =
lastVoterSetOffset.orElse(null);
+ // if lastVoterOffset is -1 we know the leader hasn't written
the bootstrap snapshot records to the log yet
+ if (voterSetOffset != null && voterSetOffset.offset() == -1) {
+ if (kraftVersion > 0) {
Review Comment:
It is an `IllegalStateException` if a voters set exist in the bootstrap
checkpoint but the kraft.version is 0.
##########
raft/src/main/java/org/apache/kafka/raft/KafkaRaftClient.java:
##########
@@ -1396,12 +1398,12 @@ private FetchResponseData tryCompleteFetchRequest(
int lastFetchedEpoch = request.lastFetchedEpoch();
LeaderState<T> state = quorum.leaderStateOrThrow();
- Optional<OffsetAndEpoch> latestSnapshotId = log.latestSnapshotId();
+ OffsetAndEpoch latestSnapshotId =
log.latestSnapshotId().orElse(null);
final ValidOffsetAndEpoch validOffsetAndEpoch;
- if (fetchOffset == 0 && latestSnapshotId.isPresent()) {
- // If the follower has an empty log and a snapshot exist, it
is always more efficient
+ if (fetchOffset == 0 && latestSnapshotId != null &&
!latestSnapshotId.equals(BOOTSTRAP_SNAPSHOT_ID)) {
Review Comment:
Let's avoid the use of `null`.
```java
if (fetchOffset == 0 &&
latestSnapshotId.isPresent() &&
!latestSnapshotId.get().equals(BOOTSTRAP_SNAPSHOT_ID)
) {
```
Reference:
https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare/
--
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]