You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ratis.apache.org by am...@apache.org on 2026/10/14 01:44:16 UTC

(ratis) branch master updated: RATIS-2740. Ratis examples do not call Message#getContent directly (#1636)

This is an automated email from the ASF dual-hosted git repository.

amaliujia pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ratis.git


The following commit(s) were added to refs/heads/master by this push:
     new 427ebeaa1 RATIS-2740. Ratis examples do not call Message#getContent directly (#1636)
427ebeaa1 is described below

commit 427ebeaa117f55bd94b84a5db4a3c76e5b69e260
Author: Rui Wang <am...@apache.org>
AuthorDate: Wed Oct 14 09:44:11 2026 +0800

    RATIS-2740. Ratis examples do not call Message#getContent directly (#1636)
---
 .../apache/ratis/examples/arithmetic/ArithmeticStateMachine.java  | 2 +-
 .../main/java/org/apache/ratis/examples/arithmetic/cli/Get.java   | 3 ++-
 .../java/org/apache/ratis/examples/counter/CounterCommand.java    | 7 +++----
 .../org/apache/ratis/examples/counter/client/CounterClient.java   | 4 ++--
 .../apache/ratis/examples/counter/server/CounterStateMachine.java | 7 +++----
 .../java/org/apache/ratis/examples/filestore/FileStoreClient.java | 4 ++--
 .../apache/ratis/examples/filestore/FileStoreStateMachine.java    | 8 ++++----
 .../org/apache/ratis/examples/membership/server/RaftCluster.java  | 3 ++-
 .../java/org/apache/ratis/examples/arithmetic/TestArithmetic.java | 3 ++-
 .../test/java/org/apache/ratis/examples/counter/TestCounter.java  | 6 +++---
 .../java/org/apache/ratis/server/impl/TestReadAfterWrite.java     | 4 ++--
 11 files changed, 26 insertions(+), 25 deletions(-)

diff --git a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/ArithmeticStateMachine.java b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/ArithmeticStateMachine.java
index 46c07fd37..23d0abb79 100644
--- a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/ArithmeticStateMachine.java
+++ b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/ArithmeticStateMachine.java
@@ -149,7 +149,7 @@ public class ArithmeticStateMachine extends BaseStateMachine {
 
   @Override
   public CompletableFuture<Message> query(Message request) {
-    final Expression q = Expression.Utils.bytes2Expression(request.getContent().toByteArray(), 0);
+    final Expression q = Expression.Utils.bytes2Expression(Message.toByteString(request).toByteArray(), 0);
     final Double result;
     try(AutoCloseableLock readLock = readLock()) {
       result = q.evaluate(variables);
diff --git a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/cli/Get.java b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/cli/Get.java
index 89050f159..b72073f93 100644
--- a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/cli/Get.java
+++ b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/cli/Get.java
@@ -23,6 +23,7 @@ import org.apache.ratis.client.RaftClient;
 import org.apache.ratis.examples.arithmetic.expression.DoubleValue;
 import org.apache.ratis.examples.arithmetic.expression.Expression;
 import org.apache.ratis.examples.arithmetic.expression.Variable;
+import org.apache.ratis.protocol.Message;
 import org.apache.ratis.protocol.RaftClientReply;
 
 import java.io.IOException;
@@ -42,7 +43,7 @@ public class Get extends Client {
     RaftClientReply getValue =
         client.io().sendReadOnly(Expression.Utils.toMessage(new Variable(name)));
     Expression response =
-        Expression.Utils.bytes2Expression(getValue.getMessage().getContent().toByteArray(), 0);
+        Expression.Utils.bytes2Expression(Message.toByteString(getValue.getMessage()).toByteArray(), 0);
     System.out.println(String.format("%s=%s", name, (DoubleValue) response).toString());
   }
 }
diff --git a/ratis-examples/src/main/java/org/apache/ratis/examples/counter/CounterCommand.java b/ratis-examples/src/main/java/org/apache/ratis/examples/counter/CounterCommand.java
index 905cc004b..11875c40d 100644
--- a/ratis-examples/src/main/java/org/apache/ratis/examples/counter/CounterCommand.java
+++ b/ratis-examples/src/main/java/org/apache/ratis/examples/counter/CounterCommand.java
@@ -18,7 +18,6 @@
 package org.apache.ratis.examples.counter;
 
 import org.apache.ratis.protocol.Message;
-import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
 
 /**
  * The supported commands the Counter example.
@@ -40,8 +39,8 @@ public enum CounterCommand {
     return name().equalsIgnoreCase(command);
   }
 
-  /** Does the given command string match this command? */
-  public boolean matches(ByteString command) {
-    return message.getContent().equals(command);
+  /** Does the given command message match this command? */
+  public boolean matches(Message command) {
+    return message.equals(command);
   }
 }
diff --git a/ratis-examples/src/main/java/org/apache/ratis/examples/counter/client/CounterClient.java b/ratis-examples/src/main/java/org/apache/ratis/examples/counter/client/CounterClient.java
index cb6c72725..98a11dc40 100644
--- a/ratis-examples/src/main/java/org/apache/ratis/examples/counter/client/CounterClient.java
+++ b/ratis-examples/src/main/java/org/apache/ratis/examples/counter/client/CounterClient.java
@@ -135,7 +135,7 @@ public final class CounterClient implements Closeable {
 
     // reply is success
     final TimeDuration readElapsed = readStarted.elapsedTime();
-    final int countValue = reply.getMessage().getContent().asReadOnlyByteBuffer().getInt();
+    final int countValue = reply.getMessage().asReadOnlyByteBuffer().getInt();
     System.out.printf("read from %s and get counter value: %d, time elapsed: %s.%n",
         server, countValue, readElapsed.toString(TimeUnit.SECONDS, 3));
   }
@@ -162,7 +162,7 @@ public final class CounterClient implements Closeable {
 
     //send a GET command and print the reply
     final RaftClientReply reply = client.io().sendReadOnly(CounterCommand.GET.getMessage());
-    final int count = reply.getMessage().getContent().asReadOnlyByteBuffer().getInt();
+    final int count = reply.getMessage().asReadOnlyByteBuffer().getInt();
     System.out.println("Current counter value: " + count);
 
     // using Linearizable Read
diff --git a/ratis-examples/src/main/java/org/apache/ratis/examples/counter/server/CounterStateMachine.java b/ratis-examples/src/main/java/org/apache/ratis/examples/counter/server/CounterStateMachine.java
index 207306ff5..9ed4e63f5 100644
--- a/ratis-examples/src/main/java/org/apache/ratis/examples/counter/server/CounterStateMachine.java
+++ b/ratis-examples/src/main/java/org/apache/ratis/examples/counter/server/CounterStateMachine.java
@@ -235,9 +235,8 @@ public class CounterStateMachine extends BaseStateMachine {
    */
   @Override
   public CompletableFuture<Message> query(Message request) {
-    final String command = request.getContent().toStringUtf8();
-    if (!CounterCommand.GET.matches(command)) {
-      return JavaUtils.completeExceptionally(new IllegalArgumentException("Invalid Command: " + command));
+    if (!CounterCommand.GET.matches(request)) {
+      return JavaUtils.completeExceptionally(new IllegalArgumentException("Invalid Command: " + request));
     }
     return CompletableFuture.completedFuture(Message.valueOf(toByteString(counter.get())));
   }
@@ -249,7 +248,7 @@ public class CounterStateMachine extends BaseStateMachine {
   public TransactionContext startTransaction(RaftClientRequest request) throws IOException {
     final TransactionContext transaction = super.startTransaction(request);
     //check if the command is valid
-    final ByteString command = request.getMessage().getContent();
+    final Message command = request.getMessage();
     if (!CounterCommand.INCREMENT.matches(command)) {
       transaction.setException(new IllegalArgumentException("Invalid Command: " + command));
     }
diff --git a/ratis-examples/src/main/java/org/apache/ratis/examples/filestore/FileStoreClient.java b/ratis-examples/src/main/java/org/apache/ratis/examples/filestore/FileStoreClient.java
index 8eb6e3c17..c774a029f 100644
--- a/ratis-examples/src/main/java/org/apache/ratis/examples/filestore/FileStoreClient.java
+++ b/ratis-examples/src/main/java/org/apache/ratis/examples/filestore/FileStoreClient.java
@@ -96,7 +96,7 @@ public class FileStoreClient implements Closeable {
       throw new IOException("Failed to send request " + request, sme);
     }
     Preconditions.assertTrue(reply.isSuccess(), () -> "Failed " + request + ", reply=" + reply);
-    return reply.getMessage().getContent();
+    return Message.toByteString(reply.getMessage());
   }
 
   static CompletableFuture<ByteString> sendAsync(
@@ -107,7 +107,7 @@ public class FileStoreClient implements Closeable {
         throw new CompletionException("Failed to send request " + request, sme);
       }
       Preconditions.assertTrue(reply.isSuccess(), () -> "Failed " + request + ", reply=" + reply);
-      return reply.getMessage().getContent();
+      return Message.toByteString(reply.getMessage());
     });
   }
 
diff --git a/ratis-examples/src/main/java/org/apache/ratis/examples/filestore/FileStoreStateMachine.java b/ratis-examples/src/main/java/org/apache/ratis/examples/filestore/FileStoreStateMachine.java
index 5e9bc07e0..5cb67a0a3 100644
--- a/ratis-examples/src/main/java/org/apache/ratis/examples/filestore/FileStoreStateMachine.java
+++ b/ratis-examples/src/main/java/org/apache/ratis/examples/filestore/FileStoreStateMachine.java
@@ -96,7 +96,7 @@ public class FileStoreStateMachine extends BaseStateMachine {
   public CompletableFuture<Message> query(Message request) {
     final ReadRequestProto proto;
     try {
-      proto = ReadRequestProto.parseFrom(request.getContent());
+      proto = ReadRequestProto.parseFrom(Message.toByteString(request));
     } catch (InvalidProtocolBufferException e) {
       return FileStoreCommon.completeExceptionally("Failed to parse " + request, e);
     }
@@ -110,7 +110,7 @@ public class FileStoreStateMachine extends BaseStateMachine {
   @Override
   public long transferTo(Message request, WritableByteChannel stream) throws IOException {
     try {
-      final ReadRequestProto proto = ReadRequestProto.parseFrom(request.getContent());
+      final ReadRequestProto proto = ReadRequestProto.parseFrom(Message.toByteString(request));
       if (proto.getIsWatch()) {
         throw new IOException("Watch is not supported for streaming read: " + proto);
       }
@@ -130,7 +130,7 @@ public class FileStoreStateMachine extends BaseStateMachine {
 
   @Override
   public TransactionContext startTransaction(RaftClientRequest request) throws IOException {
-    final ByteString content = request.getMessage().getContent();
+    final ByteString content = Message.toByteString(request.getMessage());
     final FileStoreRequestProto proto = FileStoreRequestProto.parseFrom(content);
     final TransactionContext.Builder b = TransactionContext.newBuilder()
         .setStateMachine(this)
@@ -247,7 +247,7 @@ public class FileStoreStateMachine extends BaseStateMachine {
 
   @Override
   public CompletableFuture<DataStream> stream(RaftClientRequest request) {
-    final ByteString reqByteString = request.getMessage().getContent();
+    final ByteString reqByteString = Message.toByteString(request.getMessage());
     final FileStoreRequestProto proto;
     try {
       proto = FileStoreRequestProto.parseFrom(reqByteString);
diff --git a/ratis-examples/src/main/java/org/apache/ratis/examples/membership/server/RaftCluster.java b/ratis-examples/src/main/java/org/apache/ratis/examples/membership/server/RaftCluster.java
index ec1c6e095..3352d7d44 100644
--- a/ratis-examples/src/main/java/org/apache/ratis/examples/membership/server/RaftCluster.java
+++ b/ratis-examples/src/main/java/org/apache/ratis/examples/membership/server/RaftCluster.java
@@ -23,6 +23,7 @@ import org.apache.ratis.conf.RaftProperties;
 import org.apache.ratis.examples.counter.CounterCommand;
 import org.apache.ratis.netty.NettyFactory;
 import org.apache.ratis.protocol.ClientId;
+import org.apache.ratis.protocol.Message;
 import org.apache.ratis.protocol.RaftClientReply;
 import org.apache.ratis.protocol.RaftGroup;
 import org.apache.ratis.protocol.RaftPeer;
@@ -146,7 +147,7 @@ public class RaftCluster {
     RaftClient client = createClient();
     try {
       RaftClientReply reply = client.io().sendReadOnly(CounterCommand.GET.getMessage());
-      String count = reply.getMessage().getContent().toStringUtf8();
+      String count = Message.toByteString(reply.getMessage()).toStringUtf8();
       System.out.println("Current counter value: " + count);
     } finally {
       client.close();
diff --git a/ratis-examples/src/test/java/org/apache/ratis/examples/arithmetic/TestArithmetic.java b/ratis-examples/src/test/java/org/apache/ratis/examples/arithmetic/TestArithmetic.java
index 3d9d89a7d..808869ad7 100644
--- a/ratis-examples/src/test/java/org/apache/ratis/examples/arithmetic/TestArithmetic.java
+++ b/ratis-examples/src/test/java/org/apache/ratis/examples/arithmetic/TestArithmetic.java
@@ -24,6 +24,7 @@ import org.apache.ratis.examples.arithmetic.expression.DoubleValue;
 import org.apache.ratis.examples.arithmetic.expression.Expression;
 import org.apache.ratis.examples.arithmetic.expression.NullValue;
 import org.apache.ratis.examples.arithmetic.expression.Variable;
+import org.apache.ratis.protocol.Message;
 import org.apache.ratis.protocol.RaftClientReply;
 import org.apache.ratis.util.Slf4jUtils;
 import org.apache.ratis.util.Preconditions;
@@ -159,7 +160,7 @@ public class TestArithmetic extends ParameterizedBaseTest {
   static Expression assertRaftClientReply(RaftClientReply reply, Double expected) {
     Assertions.assertTrue(reply.isSuccess());
     final Expression e = Expression.Utils.bytes2Expression(
-        reply.getMessage().getContent().toByteArray(), 0);
+        Message.toByteString(reply.getMessage()).toByteArray(), 0);
     if (expected != null) {
       Assertions.assertEquals(expected, e.evaluate(null));
     }
diff --git a/ratis-examples/src/test/java/org/apache/ratis/examples/counter/TestCounter.java b/ratis-examples/src/test/java/org/apache/ratis/examples/counter/TestCounter.java
index 3d188010d..fa2b309ea 100644
--- a/ratis-examples/src/test/java/org/apache/ratis/examples/counter/TestCounter.java
+++ b/ratis-examples/src/test/java/org/apache/ratis/examples/counter/TestCounter.java
@@ -46,17 +46,17 @@ public class TestCounter extends ParameterizedBaseTest {
         client.io().send(Message.valueOf("INCREMENT"));
       }
       RaftClientReply reply1 = client.io().sendReadOnly(Message.valueOf("GET"));
-      Assertions.assertEquals(10, reply1.getMessage().getContent().asReadOnlyByteBuffer().getInt());
+      Assertions.assertEquals(10, reply1.getMessage().asReadOnlyByteBuffer().getInt());
       for (int i = 0; i < 10; i++) {
         client.io().send(Message.valueOf("INCREMENT"));
       }
       RaftClientReply reply2 = client.io().sendReadOnly(Message.valueOf("GET"));
-      Assertions.assertEquals(20, reply2.getMessage().getContent().asReadOnlyByteBuffer().getInt());
+      Assertions.assertEquals(20, reply2.getMessage().asReadOnlyByteBuffer().getInt());
       for (int i = 0; i < 10; i++) {
         client.io().send(Message.valueOf("INCREMENT"));
       }
       RaftClientReply reply3 = client.io().sendReadOnly(Message.valueOf("GET"));
-      Assertions.assertEquals(30, reply3.getMessage().getContent().asReadOnlyByteBuffer().getInt());
+      Assertions.assertEquals(30, reply3.getMessage().asReadOnlyByteBuffer().getInt());
     }
   }
 }
diff --git a/ratis-examples/src/test/java/org/apache/ratis/server/impl/TestReadAfterWrite.java b/ratis-examples/src/test/java/org/apache/ratis/server/impl/TestReadAfterWrite.java
index 025c7ed0d..f25e537ae 100644
--- a/ratis-examples/src/test/java/org/apache/ratis/server/impl/TestReadAfterWrite.java
+++ b/ratis-examples/src/test/java/org/apache/ratis/server/impl/TestReadAfterWrite.java
@@ -130,7 +130,7 @@ public class TestReadAfterWrite
       // readAfterWrite should time out
       final RaftClientReply reply = readAfterWrite.get(100, TimeUnit.MILLISECONDS);
       final DoubleValue result = (DoubleValue) Expression.Utils.bytes2Expression(
-          reply.getMessage().getContent().toByteArray(), 0);
+          Message.toByteString(reply.getMessage()).toByteArray(), 0);
       Assertions.fail("result=" + result + ", reply=" + reply);
     } catch (TimeoutException e) {
       LOG.info("Good", e);
@@ -154,7 +154,7 @@ public class TestReadAfterWrite
     Assertions.assertTrue(reply.isSuccess());
     LOG.info("reply {}", reply);
     final DoubleValue result = (DoubleValue) Expression.Utils.bytes2Expression(
-        reply.getMessage().getContent().toByteArray(), 0);
+        Message.toByteString(reply.getMessage()).toByteArray(), 0);
     Assertions.assertEquals(expected, (int) (double) result.evaluate(null));
   }
 }