Copilot commented on code in PR #1476:
URL: https://github.com/apache/ratis/pull/1476#discussion_r3346122869
##########
ratis-examples/src/main/java/org/apache/ratis/examples/membership/server/RaftCluster.java:
##########
@@ -143,15 +143,24 @@ public void counterIncrement() throws IOException {
}
public void queryCounter() throws IOException {
- RaftClient client = createClient();
- try {
- RaftClientReply reply =
client.io().sendReadOnly(CounterCommand.GET.getMessage());
- String count = reply.getMessage().getContent().toStringUtf8();
- System.out.println("Current counter value: " + count);
- } finally {
- client.close();
- }
+ RaftClient client = createClient();
+ try {
+ RaftClientReply reply = client.io().sendReadOnly(
+ CounterCommand.GET.getMessage());
+
+ ByteBuffer buffer =
+ reply.getMessage()
+ .getContent()
+ .asReadOnlyByteBuffer();
+
+ int count = buffer.getInt();
Review Comment:
This changes the wire decoding assumption from a UTF-8 encoded number to a
4-byte binary int. If the server still sends the counter as a string (as the
previous code implied), `getInt()` will produce incorrect values (or throw if
fewer than 4 bytes). Please keep the decoding consistent with the server
encoding (e.g., parse `toStringUtf8()` as an int), or update the server/command
contract to explicitly return a fixed-width binary int and document that
expectation.
##########
ratis-examples/src/main/java/org/apache/ratis/examples/membership/server/RaftCluster.java:
##########
@@ -143,15 +143,24 @@ public void counterIncrement() throws IOException {
}
public void queryCounter() throws IOException {
- RaftClient client = createClient();
- try {
- RaftClientReply reply =
client.io().sendReadOnly(CounterCommand.GET.getMessage());
- String count = reply.getMessage().getContent().toStringUtf8();
- System.out.println("Current counter value: " + count);
- } finally {
- client.close();
- }
+ RaftClient client = createClient();
+ try {
Review Comment:
Resource handling would be simpler and less error-prone using
try-with-resources for `RaftClient` (it already existed as a manual `finally`,
but the refactor is a good opportunity). Consider `try (RaftClient client =
createClient()) { ... }` to avoid explicit `close()`.
##########
ratis-examples/src/main/java/org/apache/ratis/examples/membership/server/RaftCluster.java:
##########
@@ -143,15 +143,24 @@ public void counterIncrement() throws IOException {
}
public void queryCounter() throws IOException {
- RaftClient client = createClient();
- try {
- RaftClientReply reply =
client.io().sendReadOnly(CounterCommand.GET.getMessage());
- String count = reply.getMessage().getContent().toStringUtf8();
- System.out.println("Current counter value: " + count);
- } finally {
- client.close();
- }
+ RaftClient client = createClient();
+ try {
+ RaftClientReply reply = client.io().sendReadOnly(
+ CounterCommand.GET.getMessage());
Review Comment:
The method body indentation appears inconsistent with surrounding code
(statements start at the same indentation level as the method declaration).
This may violate the project's formatter/checkstyle rules. Please reformat
`queryCounter()` so its contents are indented consistently with other methods
in this class.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]