Browse Source
This patch does a few things: 1) It introduces the `OffsetAndMetadata` class which hold the committed offsets in the group coordinator. 2) It adds methods to deal with OffsetCommit records to `RecordHelpers`. 3) It adds `MetadataVersion#offsetCommitValueVersion` to get the version of the OffsetCommit value record that should be used. Reviewers: Jeff Kim <jeff.kim@confluent.io>, David Arthur <mumrah@gmail.com>, Justine Olshan <jolshan@confluent.io>pull/14069/head
David Jacot
1 year ago
committed by
GitHub
7 changed files with 409 additions and 0 deletions
@ -0,0 +1,125 @@
@@ -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 = ""; |
||||
|
||||
/** |
||||
* The committed offset. |
||||
*/ |
||||
public final long offset; |
||||
|
||||
/** |
||||
* The leader epoch in use when the offset was committed. |
||||
*/ |
||||
public final OptionalInt leaderEpoch; |
||||
|
||||
/** |
||||
* 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()) |
||||
); |
||||
} |
||||
} |
@ -0,0 +1,74 @@
@@ -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)); |
||||
} |
||||
} |
Loading…
Reference in new issue