-
Notifications
You must be signed in to change notification settings - Fork 14.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
KAFKA-18723; Better handle invalid records during replication #18852
Open
jsancio
wants to merge
14
commits into
apache:trunk
Choose a base branch
from
jsancio:kafka-18723-kraft-invalid-records
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3da82cc
KAFKA-18723; Basic implementation and tests
jsancio 72aa70c
KAFKA-18723; Add test for invalid records
jsancio 330c72c
KAFKA-18723; Add epoch to appendAsFollower
jsancio 7a1169d
KAFKA-18723; Add max epoch to partition and replica fetcher
jsancio 1dcb514
Merge remote-tracking branch 'upstream/trunk' into kafka-18723-kraft-…
jsancio bcbbdf6
KAFKA-18723; Add max epoch to the replica fetcher
jsancio 74c5e3b
KAFKA-18723; Print record batch header
jsancio 3087b4f
KAFKA-18723; Improve random tests
jsancio 5e6db9e
KAFKA-18723; Add test for unexpected leader epoch
jsancio 26933df
Merge remote-tracking branch 'upstream/trunk' into kafka-18723-kraft-…
jsancio 1a79c7f
KAFKA-18723; Simplify KRaft's handling of corrupted records
jsancio 6faf1c8
KAFKA-18723; Rename to hasHigherPartitionLeaderEpoch
jsancio f09dfb1
KAFKA-18723; Add test for replicating higher leader epochs
jsancio ae5a895
KAFKA-18723; Re-word test name
jsancio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
clients/src/test/java/org/apache/kafka/common/record/ArbitraryMemoryRecords.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 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.common.record; | ||
|
||
import net.jqwik.api.Arbitraries; | ||
import net.jqwik.api.Arbitrary; | ||
import net.jqwik.api.ArbitrarySupplier; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.Random; | ||
|
||
public final class ArbitraryMemoryRecords implements ArbitrarySupplier<MemoryRecords> { | ||
@Override | ||
public Arbitrary<MemoryRecords> get() { | ||
return Arbitraries.randomValue(ArbitraryMemoryRecords::buildRandomRecords); | ||
} | ||
|
||
private static MemoryRecords buildRandomRecords(Random random) { | ||
int size = random.nextInt(128) + 1; | ||
byte[] bytes = new byte[size]; | ||
random.nextBytes(bytes); | ||
|
||
return MemoryRecords.readableRecords(ByteBuffer.wrap(bytes)); | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
clients/src/test/java/org/apache/kafka/common/record/InvalidMemoryRecordsProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* 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.common.record; | ||
|
||
import org.apache.kafka.common.errors.CorruptRecordException; | ||
|
||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.ArgumentsProvider; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
|
||
public final class InvalidMemoryRecordsProvider implements ArgumentsProvider { | ||
// Use a baseOffset that's not zero so that it is less likely to match the LEO | ||
private static final long BASE_OFFSET = 1234; | ||
private static final int EPOCH = 4321; | ||
|
||
/** | ||
* Returns a stream of arguments for invalid memory records and the expected exception. | ||
* | ||
* The first object in the {@code Arguments} is a {@code MemoryRecords}. | ||
* | ||
* The second object in the {@code Arguments} is an {@code Optional<Class<Exception>>} which is | ||
* the expected exception from the log layer. | ||
*/ | ||
@Override | ||
public Stream<? extends Arguments> provideArguments(ExtensionContext context) { | ||
return Stream.of( | ||
Arguments.of(MemoryRecords.readableRecords(notEnoughtBytes()), Optional.empty()), | ||
Arguments.of(MemoryRecords.readableRecords(recordsSizeTooSmall()), Optional.of(CorruptRecordException.class)), | ||
Arguments.of(MemoryRecords.readableRecords(notEnoughBytesToMagic()), Optional.empty()), | ||
Arguments.of(MemoryRecords.readableRecords(negativeMagic()), Optional.of(CorruptRecordException.class)), | ||
Arguments.of(MemoryRecords.readableRecords(largeMagic()), Optional.of(CorruptRecordException.class)), | ||
Arguments.of(MemoryRecords.readableRecords(lessBytesThanRecordSize()), Optional.empty()) | ||
); | ||
} | ||
|
||
private static ByteBuffer notEnoughtBytes() { | ||
var buffer = ByteBuffer.allocate(Records.LOG_OVERHEAD - 1); | ||
buffer.limit(buffer.capacity()); | ||
|
||
return buffer; | ||
} | ||
|
||
private static ByteBuffer recordsSizeTooSmall() { | ||
var buffer = ByteBuffer.allocate(256); | ||
// Write the base offset | ||
buffer.putLong(BASE_OFFSET); | ||
// Write record size | ||
buffer.putInt(LegacyRecord.RECORD_OVERHEAD_V0 - 1); | ||
buffer.position(0); | ||
buffer.limit(buffer.capacity()); | ||
|
||
return buffer; | ||
} | ||
|
||
private static ByteBuffer notEnoughBytesToMagic() { | ||
var buffer = ByteBuffer.allocate(256); | ||
// Write the base offset | ||
buffer.putLong(BASE_OFFSET); | ||
// Write record size | ||
buffer.putInt(buffer.capacity() - Records.LOG_OVERHEAD); | ||
buffer.position(0); | ||
buffer.limit(Records.HEADER_SIZE_UP_TO_MAGIC - 1); | ||
|
||
return buffer; | ||
} | ||
|
||
private static ByteBuffer negativeMagic() { | ||
var buffer = ByteBuffer.allocate(256); | ||
// Write the base offset | ||
buffer.putLong(BASE_OFFSET); | ||
// Write record size | ||
buffer.putInt(buffer.capacity() - Records.LOG_OVERHEAD); | ||
// Write the epoch | ||
buffer.putInt(EPOCH); | ||
// Write magic | ||
buffer.put((byte) -1); | ||
buffer.position(0); | ||
buffer.limit(buffer.capacity()); | ||
|
||
return buffer; | ||
} | ||
|
||
private static ByteBuffer largeMagic() { | ||
var buffer = ByteBuffer.allocate(256); | ||
// Write the base offset | ||
buffer.putLong(BASE_OFFSET); | ||
// Write record size | ||
buffer.putInt(buffer.capacity() - Records.LOG_OVERHEAD); | ||
// Write the epoch | ||
buffer.putInt(EPOCH); | ||
// Write magic | ||
buffer.put((byte) (RecordBatch.CURRENT_MAGIC_VALUE + 1)); | ||
buffer.position(0); | ||
buffer.limit(buffer.capacity()); | ||
|
||
return buffer; | ||
} | ||
|
||
private static ByteBuffer lessBytesThanRecordSize() { | ||
var buffer = ByteBuffer.allocate(256); | ||
// Write the base offset | ||
buffer.putLong(BASE_OFFSET); | ||
// Write record size | ||
buffer.putInt(buffer.capacity() - Records.LOG_OVERHEAD); | ||
// Write the epoch | ||
buffer.putInt(EPOCH); | ||
// Write magic | ||
buffer.put(RecordBatch.CURRENT_MAGIC_VALUE); | ||
buffer.position(0); | ||
buffer.limit(buffer.capacity() - Records.LOG_OVERHEAD - 1); | ||
|
||
return buffer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo:
Enought
->Enough