-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-14499: [2/N] Add OffsetCommit record & related #14047
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
3 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
125 changes: 125 additions & 0 deletions
125
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/OffsetAndMetadata.java
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,125 @@ | ||
| /* | ||
| * 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.coordinator.group; | ||
|
|
||
| import org.apache.kafka.common.record.RecordBatch; | ||
| import org.apache.kafka.common.requests.OffsetCommitRequest; | ||
| import org.apache.kafka.coordinator.group.generated.OffsetCommitValue; | ||
|
|
||
| import java.util.Objects; | ||
| import java.util.OptionalInt; | ||
| import java.util.OptionalLong; | ||
|
|
||
| /** | ||
| * Represents a committed offset with its metadata. | ||
| */ | ||
| public class OffsetAndMetadata { | ||
| public static final String NO_METADATA = ""; | ||
|
dajac marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * The committed offset. | ||
| */ | ||
| public final long offset; | ||
|
|
||
| /** | ||
| * The leader epoch in use when the offset was committed. | ||
| */ | ||
| public final OptionalInt leaderEpoch; | ||
|
dajac marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * The committed metadata. The Kafka offset commit API allows users to provide additional | ||
| * metadata (in the form of a string) when an offset is committed. This can be useful | ||
| * (for example) to store information about which node made the commit, what time the | ||
| * commit was made, etc. | ||
| */ | ||
| public final String metadata; | ||
|
|
||
| /** | ||
| * The commit timestamp in milliseconds. | ||
| */ | ||
| public final long commitTimestampMs; | ||
|
|
||
| /** | ||
| * The expire timestamp in milliseconds. | ||
| */ | ||
| public final OptionalLong expireTimestampMs; | ||
|
|
||
| public OffsetAndMetadata( | ||
| long offset, | ||
| OptionalInt leaderEpoch, | ||
| String metadata, | ||
| long commitTimestampMs, | ||
| OptionalLong expireTimestampMs | ||
| ) { | ||
| this.offset = offset; | ||
| this.leaderEpoch = Objects.requireNonNull(leaderEpoch); | ||
| this.metadata = Objects.requireNonNull(metadata); | ||
| this.commitTimestampMs = commitTimestampMs; | ||
| this.expireTimestampMs = Objects.requireNonNull(expireTimestampMs); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "OffsetAndMetadata(offset=" + offset + | ||
| ", leaderEpoch=" + leaderEpoch + | ||
| ", metadata=" + metadata + | ||
| ", commitTimestampMs=" + commitTimestampMs + | ||
| ", expireTimestampMs=" + expireTimestampMs + | ||
| ')'; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
|
|
||
| OffsetAndMetadata that = (OffsetAndMetadata) o; | ||
|
|
||
| if (offset != that.offset) return false; | ||
| if (commitTimestampMs != that.commitTimestampMs) return false; | ||
| if (!leaderEpoch.equals(that.leaderEpoch)) return false; | ||
| if (!metadata.equals(that.metadata)) return false; | ||
| return expireTimestampMs.equals(that.expireTimestampMs); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| int result = (int) (offset ^ (offset >>> 32)); | ||
| result = 31 * result + leaderEpoch.hashCode(); | ||
| result = 31 * result + metadata.hashCode(); | ||
| result = 31 * result + (int) (commitTimestampMs ^ (commitTimestampMs >>> 32)); | ||
| result = 31 * result + expireTimestampMs.hashCode(); | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * @return An OffsetAndMetadata created from a OffsetCommitValue record. | ||
| */ | ||
| public static OffsetAndMetadata fromRecord( | ||
| OffsetCommitValue record | ||
| ) { | ||
| return new OffsetAndMetadata( | ||
| record.offset(), | ||
| record.leaderEpoch() == RecordBatch.NO_PARTITION_LEADER_EPOCH ? | ||
| OptionalInt.empty() : OptionalInt.of(record.leaderEpoch()), | ||
| record.metadata(), | ||
| record.commitTimestamp(), | ||
| record.expireTimestamp() == OffsetCommitRequest.DEFAULT_TIMESTAMP ? | ||
| OptionalLong.empty() : OptionalLong.of(record.expireTimestamp()) | ||
| ); | ||
| } | ||
| } | ||
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
74 changes: 74 additions & 0 deletions
74
...p-coordinator/src/test/java/org/apache/kafka/coordinator/group/OffsetAndMetadataTest.java
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,74 @@ | ||
| /* | ||
| * 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.coordinator.group; | ||
|
|
||
| import org.apache.kafka.coordinator.group.generated.OffsetCommitValue; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.OptionalInt; | ||
| import java.util.OptionalLong; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| public class OffsetAndMetadataTest { | ||
| @Test | ||
| public void testAttributes() { | ||
| OffsetAndMetadata offsetAndMetadata = new OffsetAndMetadata( | ||
| 100L, | ||
| OptionalInt.of(10), | ||
| "metadata", | ||
| 1234L, | ||
| OptionalLong.of(5678L) | ||
| ); | ||
|
|
||
| assertEquals(100L, offsetAndMetadata.offset); | ||
| assertEquals(OptionalInt.of(10), offsetAndMetadata.leaderEpoch); | ||
| assertEquals("metadata", offsetAndMetadata.metadata); | ||
| assertEquals(1234L, offsetAndMetadata.commitTimestampMs); | ||
| assertEquals(OptionalLong.of(5678L), offsetAndMetadata.expireTimestampMs); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFromRecord() { | ||
| OffsetCommitValue record = new OffsetCommitValue() | ||
| .setOffset(100L) | ||
| .setLeaderEpoch(-1) | ||
| .setMetadata("metadata") | ||
| .setCommitTimestamp(1234L) | ||
| .setExpireTimestamp(-1L); | ||
|
|
||
| assertEquals(new OffsetAndMetadata( | ||
| 100L, | ||
| OptionalInt.empty(), | ||
| "metadata", | ||
| 1234L, | ||
| OptionalLong.empty() | ||
| ), OffsetAndMetadata.fromRecord(record)); | ||
|
|
||
| record | ||
| .setLeaderEpoch(12) | ||
| .setExpireTimestamp(5678L); | ||
|
|
||
| assertEquals(new OffsetAndMetadata( | ||
| 100L, | ||
| OptionalInt.of(12), | ||
| "metadata", | ||
| 1234L, | ||
| OptionalLong.of(5678L) | ||
| ), OffsetAndMetadata.fromRecord(record)); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.