Skip to content

KAFKA-18409: ShareGroupStateMessageFormatter should use CoordinatorRecordMessageFormatter #18510

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 12 commits into from
Mar 31, 2025

Conversation

brandboat
Copy link
Member

@brandboat brandboat commented Jan 13, 2025

ShareGroupStateMessageFormatter should extend CoordinatorRecordMessageFormatter in order to have a consistent handling of records of coordinators.

Reviewers: Ken Huang [email protected], David Jacot [email protected]

@github-actions github-actions bot added the tools label Jan 13, 2025
Copy link
Collaborator

@m1a2st m1a2st left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, left few comments. PTAL

@@ -38,7 +38,7 @@ public abstract class ApiMessageFormatter implements MessageFormatter {
private static final String DATA = "data";
private static final String KEY = "key";
private static final String VALUE = "value";
static final String UNKNOWN = "unknown";
public static final String UNKNOWN = "unknown";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use the modifier protected.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, thanks.

@@ -79,5 +79,5 @@ public void writeTo(ConsumerRecord<byte[], byte[]> consumerRecord, PrintStream o
}

protected abstract JsonNode readToKeyJson(ByteBuffer byteBuffer);
protected abstract JsonNode readToValueJson(ByteBuffer byteBuffer);
}
protected abstract JsonNode readToValueJson(ByteBuffer byteBuffer, short keyVersion);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the keyVersion parameter is only used by the ShareGroupStateMessageFormatter class, we should refactor this to make the method less abstract. Instead of requiring all subclasses to implement the keyVersion parameter, we could:

  1. Move keyVersion out of the abstract definition
  2. Provide a default implementation in the base class

WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I've thought about that before, but that means we need to add below methods to ApiMessageFormatter.java.

    protected abstract JsonNode readToValueJson(ByteBuffer byteBuffer);

    protected JsonNode readToValueJson(ByteBuffer byteBuffer, short keyVersion) {
        return readToValueJson(byteBuffer);
    }

But doing that means I need to override two methods in ShareGroupStateMessageFormatter.java, particularly readToValueJson(ByteBuffer byteBuffer). This method will essentially be a dummy method, which feels somewhat awkward to implement, so I decided to add an extra parameter to it instead.

    @Override
    protected JsonNode readToValueJson(ByteBuffer byteBuffer, short keyVersion) {
        short valueVersion = byteBuffer.getShort();
        return readToSnapshotMessageValue(byteBuffer, keyVersion, valueVersion)
            .map(logValue -> transferValueMessageToJsonNode(logValue, valueVersion))
            .orElseGet(() -> new TextNode(UNKNOWN));
    }

    @Override
    protected JsonNode readToValueJson(ByteBuffer byteBuffer) {
        return null;
    }

Or you have other ideas? Let me know if you'd like any further adjustments!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose changing the JsonNode readToValueJson signature to accept a ConsumerRecord<byte[], byte[]>parameter. This would standardize the method signature across all subclasses. WDYT?

protected JsonNode readToValueJson(ConsumerRecord<byte[], byte[]> consumerRecord)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is a good idea, perhaps we can wait for others opinion.

@dajac
Copy link
Member

dajac commented Jan 24, 2025

@brandboat What do you think about this one: #18695?

@brandboat
Copy link
Member Author

@brandboat What do you think about this one: #18695?

I think this is a better approach. The entire refactor looks great to me. I'll hold off on this PR until #18695 is merged. Thank you for letting me know!

@brandboat brandboat changed the title KAFKA-18409: ShareGroupStateMessageFormatter should use ApiMessageFormatter (WIP) KAFKA-18409: ShareGroupStateMessageFormatter should use ApiMessageFormatter Jan 26, 2025
@dajac
Copy link
Member

dajac commented Mar 26, 2025

@brandboat We have merged #18695. We can proceed with your PR. Please ping me when the PR is ready for review.

@brandboat
Copy link
Member Author

@brandboat We have merged #18695. We can proceed with your PR. Please ping me when the PR is ready for review.

Thanks for the heads-up, I'll handle this ASAP!

@brandboat brandboat changed the title (WIP) KAFKA-18409: ShareGroupStateMessageFormatter should use ApiMessageFormatter KAFKA-18409: ShareGroupStateMessageFormatter should use ApiMessageFormatter Mar 28, 2025
@brandboat brandboat changed the title KAFKA-18409: ShareGroupStateMessageFormatter should use ApiMessageFormatter KAFKA-18409: ShareGroupStateMessageFormatter should use CoordinatorRecordMessageFormatter Mar 28, 2025
@brandboat
Copy link
Member Author

Gentle ping @dajac, CI passed, could you take a look when you are available? Thank you!

Copy link
Member

@dajac dajac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brandboat Thanks for the update. I left a few initial comments.

@@ -137,23 +137,23 @@ private static Stream<Arguments> parameters() {
Arguments.of(
MessageUtil.toVersionPrefixedByteBuffer((short) 0, SHARE_SNAPSHOT_KEY).array(),
MessageUtil.toVersionPrefixedByteBuffer((short) 0, SHARE_SNAPSHOT_VALUE).array(),
"{\"key\":{\"version\":0,\"data\":{\"groupId\":\"gs1\",\"topicId\":\"gtb2stGYRk-vWZ2zAozmoA\",\"partition\":0}},\"value\":{\"version\":0,\"data\":{\"snapshotEpoch\":0,\"stateEpoch\":1,\"leaderEpoch\":20,\"startOffset\":50,\"stateBatches\":[{\"firstOffset\":100,\"lastOffset\":200,\"deliveryState\":1,\"deliveryCount\":10},{\"firstOffset\":201,\"lastOffset\":210,\"deliveryState\":2,\"deliveryCount\":10}]}}}"
"{\"key\":{\"type\":0,\"data\":{\"groupId\":\"gs1\",\"topicId\":\"gtb2stGYRk-vWZ2zAozmoA\",\"partition\":0}},\"value\":{\"version\":0,\"data\":{\"snapshotEpoch\":0,\"stateEpoch\":1,\"leaderEpoch\":20,\"startOffset\":50,\"stateBatches\":[{\"firstOffset\":100,\"lastOffset\":200,\"deliveryState\":1,\"deliveryCount\":10},{\"firstOffset\":201,\"lastOffset\":210,\"deliveryState\":2,\"deliveryCount\":10}]}}}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Would it be possible to use the new test blocks like we did for the other formatters?

new RuntimeException("non-nullable field stateBatches was serialized as null")
new RuntimeException("Could not read record at offset 0 due to: " +
"Could not read record with version 0 from value's buffer due to: " +
"non-nullable field stateBatches was serialized as null.")
)
);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we remove testShareGroupStateMessageFormatter? We have it in the parent class now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I've addressed all the comments!

@dajac
Copy link
Member

dajac commented Mar 31, 2025

@brandboat It looks like https://github.com/apache/kafka/pull/18510/files#r2019007145 has not been addressed yet. Could you please address it?

@brandboat
Copy link
Member Author

@brandboat It looks like https://github.com/apache/kafka/pull/18510/files#r2019007145 has not been addressed yet. Could you please address it?

My apologies, I've replaced all long string in the test with text blocks.

Copy link
Member

@dajac dajac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thanks

@dajac dajac merged commit 5982943 into apache:trunk Mar 31, 2025
23 checks passed
@brandboat brandboat deleted the KAFKA-18409 branch March 31, 2025 15:00
janchilling pushed a commit to janchilling/kafka that referenced this pull request Apr 4, 2025
…cordMessageFormatter (apache#18510)

ShareGroupStateMessageFormatter should extend
CoordinatorRecordMessageFormatter in order to have a consistent handling
of records of coordinators.

Reviewers: Ken Huang <[email protected]>, David Jacot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants