-
Notifications
You must be signed in to change notification settings - Fork 15.1k
KAFKA-10864: Convert end txn marker schema to use auto-generated protocol #9766
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
|---|---|---|
|
|
@@ -17,10 +17,9 @@ | |
| package org.apache.kafka.common.record; | ||
|
|
||
| import org.apache.kafka.common.InvalidRecordException; | ||
| import org.apache.kafka.common.protocol.types.Field; | ||
| import org.apache.kafka.common.protocol.types.Schema; | ||
| import org.apache.kafka.common.protocol.types.Struct; | ||
| import org.apache.kafka.common.protocol.types.Type; | ||
| import org.apache.kafka.common.message.EndTxnMarker; | ||
| import org.apache.kafka.common.protocol.ByteBufferAccessor; | ||
| import org.apache.kafka.common.protocol.MessageUtil; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
@@ -35,23 +34,16 @@ | |
| public class EndTransactionMarker { | ||
| private static final Logger log = LoggerFactory.getLogger(EndTransactionMarker.class); | ||
|
|
||
| private static final short CURRENT_END_TXN_MARKER_VERSION = 0; | ||
| private static final Schema END_TXN_MARKER_SCHEMA_VERSION_V0 = new Schema( | ||
| new Field("version", Type.INT16), | ||
| new Field("coordinator_epoch", Type.INT32)); | ||
| static final int CURRENT_END_TXN_MARKER_VALUE_SIZE = 6; | ||
| static final int CURRENT_END_TXN_SCHEMA_RECORD_SIZE = DefaultRecord.sizeInBytes(0, 0L, | ||
| ControlRecordType.CURRENT_CONTROL_RECORD_KEY_SIZE, | ||
| EndTransactionMarker.CURRENT_END_TXN_MARKER_VALUE_SIZE, | ||
| Record.EMPTY_HEADERS); | ||
|
|
||
| private final ControlRecordType type; | ||
| private final int coordinatorEpoch; | ||
| private final ByteBuffer buffer; | ||
|
|
||
| public EndTransactionMarker(ControlRecordType type, int coordinatorEpoch) { | ||
| ensureTransactionMarkerControlType(type); | ||
| this.type = type; | ||
| this.coordinatorEpoch = coordinatorEpoch; | ||
| EndTxnMarker marker = new EndTxnMarker().setCoordinatorEpoch(coordinatorEpoch); | ||
| this.buffer = MessageUtil.toVersionPrefixedByteBuffer(EndTxnMarker.HIGHEST_SUPPORTED_VERSION, marker); | ||
| } | ||
|
|
||
| public int coordinatorEpoch() { | ||
|
|
@@ -62,19 +54,8 @@ public ControlRecordType controlType() { | |
| return type; | ||
| } | ||
|
|
||
| private Struct buildRecordValue() { | ||
| Struct struct = new Struct(END_TXN_MARKER_SCHEMA_VERSION_V0); | ||
| struct.set("version", CURRENT_END_TXN_MARKER_VERSION); | ||
| struct.set("coordinator_epoch", coordinatorEpoch); | ||
| return struct; | ||
| } | ||
|
|
||
| public ByteBuffer serializeValue() { | ||
| Struct valueStruct = buildRecordValue(); | ||
| ByteBuffer value = ByteBuffer.allocate(valueStruct.sizeOf()); | ||
| valueStruct.writeTo(value); | ||
| value.flip(); | ||
| return value; | ||
| return buffer.duplicate(); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -95,32 +76,35 @@ public int hashCode() { | |
|
|
||
| private static void ensureTransactionMarkerControlType(ControlRecordType type) { | ||
| if (type != ControlRecordType.COMMIT && type != ControlRecordType.ABORT) | ||
| throw new IllegalArgumentException("Invalid control record type for end transaction marker" + type); | ||
| throw new IllegalArgumentException("Invalid control record type for end transaction marker " + type); | ||
| } | ||
|
|
||
| public static EndTransactionMarker deserialize(Record record) { | ||
| ControlRecordType type = ControlRecordType.parse(record.key()); | ||
| return deserializeValue(type, record.value()); | ||
| } | ||
|
|
||
| // Visible for testing | ||
| static EndTransactionMarker deserializeValue(ControlRecordType type, ByteBuffer value) { | ||
| ensureTransactionMarkerControlType(type); | ||
|
|
||
| if (value.remaining() < CURRENT_END_TXN_MARKER_VALUE_SIZE) | ||
| throw new InvalidRecordException("Invalid value size found for end transaction marker. Must have " + | ||
| "at least " + CURRENT_END_TXN_MARKER_VALUE_SIZE + " bytes, but found only " + value.remaining()); | ||
|
|
||
| short version = value.getShort(0); | ||
| if (version < 0) | ||
| short version = value.getShort(); | ||
| if (version < EndTxnMarker.LOWEST_SUPPORTED_VERSION) | ||
| throw new InvalidRecordException("Invalid version found for end transaction marker: " + version + | ||
| ". May indicate data corruption"); | ||
|
|
||
| if (version > CURRENT_END_TXN_MARKER_VERSION) | ||
| if (version > EndTxnMarker.HIGHEST_SUPPORTED_VERSION) | ||
| log.debug("Received end transaction marker value version {}. Parsing as version {}", version, | ||
| CURRENT_END_TXN_MARKER_VERSION); | ||
| EndTxnMarker.HIGHEST_SUPPORTED_VERSION); | ||
| EndTxnMarker marker = new EndTxnMarker(new ByteBufferAccessor(value), EndTxnMarker.HIGHEST_SUPPORTED_VERSION); | ||
| return new EndTransactionMarker(type, marker.coordinatorEpoch()); | ||
| } | ||
|
|
||
| int coordinatorEpoch = value.getInt(2); | ||
| return new EndTransactionMarker(type, coordinatorEpoch); | ||
| public int endTxnMarkerValueSize() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please add unit test for this method?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it's good to test it since it's public. |
||
| return DefaultRecord.sizeInBytes(0, 0L, | ||
| ControlRecordType.CURRENT_CONTROL_RECORD_KEY_SIZE, | ||
| buffer.remaining(), | ||
| Record.EMPTY_HEADERS); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or 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
26 changes: 26 additions & 0 deletions
26
clients/src/main/resources/common/message/EndTxnMarker.json
This file contains hidden or 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,26 @@ | ||
| // 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. | ||
|
|
||
| { | ||
| "type": "data", | ||
| "name": "EndTxnMarker", | ||
| "validVersions": "0", | ||
| "flexibleVersions": "none", | ||
| "fields": [ | ||
| { "name": "CoordinatorEpoch", "type": "int32", "versions": "0+", | ||
| "about": "The coordinator epoch when appending the record" | ||
| } | ||
| ] | ||
| } |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.