You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kudu.apache.org by da...@apache.org on 2017/11/21 23:05:28 UTC

[2/2] kudu git commit: [java] Add ReplicaSelection in KuduScanToken

[java] Add ReplicaSelection in KuduScanToken

This patch adds ReplicaSelection in KuduScanToken for java client,
so that deserializing a ScanToken results in propagating the replica
selection policy of the serializer into the deserializer.

Change-Id: I860fcc73e486642ab5177cfd0dc0bdc98fdc6914
Reviewed-on: http://gerrit.cloudera.org:8080/8559
Reviewed-by: Dan Burkert <da...@apache.org>
Tested-by: Kudu Jenkins


Project: http://git-wip-us.apache.org/repos/asf/kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/kudu/commit/9269e0ab
Tree: http://git-wip-us.apache.org/repos/asf/kudu/tree/9269e0ab
Diff: http://git-wip-us.apache.org/repos/asf/kudu/diff/9269e0ab

Branch: refs/heads/master
Commit: 9269e0abc39c5143e5c7e5fff5facc2d61192fd2
Parents: f343a67
Author: hahao <ha...@cloudera.com>
Authored: Wed Nov 15 14:26:09 2017 -0800
Committer: Dan Burkert <da...@apache.org>
Committed: Tue Nov 21 23:05:15 2017 +0000

----------------------------------------------------------------------
 .../org/apache/kudu/client/KuduScanToken.java   | 20 ++++++++++++++++++++
 .../kudu/client/TestScannerMultiTablet.java     | 16 ++++++++++++++++
 src/kudu/client/client.proto                    |  4 ++++
 src/kudu/common/common.proto                    | 10 ++++++++++
 4 files changed, 50 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kudu/blob/9269e0ab/java/kudu-client/src/main/java/org/apache/kudu/client/KuduScanToken.java
----------------------------------------------------------------------
diff --git a/java/kudu-client/src/main/java/org/apache/kudu/client/KuduScanToken.java b/java/kudu-client/src/main/java/org/apache/kudu/client/KuduScanToken.java
index 2e3d1d6..b85c37f 100644
--- a/java/kudu-client/src/main/java/org/apache/kudu/client/KuduScanToken.java
+++ b/java/kudu-client/src/main/java/org/apache/kudu/client/KuduScanToken.java
@@ -223,6 +223,20 @@ public class KuduScanToken implements Comparable<KuduScanToken> {
       }
     }
 
+    if (message.hasReplicaSelection()) {
+      switch (message.getReplicaSelection()) {
+        case LEADER_ONLY: {
+          builder.replicaSelection(ReplicaSelection.LEADER_ONLY);
+          break;
+        }
+        case CLOSEST_REPLICA: {
+          builder.replicaSelection(ReplicaSelection.CLOSEST_REPLICA);
+          break;
+        }
+        default: throw new IllegalArgumentException("unknown replica selection policy");
+      }
+    }
+
     if (message.hasPropagatedTimestamp() &&
         message.getPropagatedTimestamp() != AsyncKuduClient.NO_TIMESTAMP) {
       client.updateLastPropagatedTimestamp(message.getPropagatedTimestamp());
@@ -330,6 +344,12 @@ public class KuduScanToken implements Comparable<KuduScanToken> {
       proto.setLimit(limit);
       proto.setReadMode(readMode.pbVersion());
 
+      if (replicaSelection == ReplicaSelection.LEADER_ONLY) {
+        proto.setReplicaSelection(Common.ReplicaSelection.LEADER_ONLY);
+      } else if (replicaSelection == ReplicaSelection.CLOSEST_REPLICA) {
+        proto.setReplicaSelection(Common.ReplicaSelection.CLOSEST_REPLICA);
+      }
+
       // If the last propagated timestamp is set send it with the scan.
       if (table.getAsyncClient().getLastPropagatedTimestamp() != AsyncKuduClient.NO_TIMESTAMP) {
         proto.setPropagatedTimestamp(client.getLastPropagatedTimestamp());

http://git-wip-us.apache.org/repos/asf/kudu/blob/9269e0ab/java/kudu-client/src/test/java/org/apache/kudu/client/TestScannerMultiTablet.java
----------------------------------------------------------------------
diff --git a/java/kudu-client/src/test/java/org/apache/kudu/client/TestScannerMultiTablet.java b/java/kudu-client/src/test/java/org/apache/kudu/client/TestScannerMultiTablet.java
index e81d4bb..1bef824 100644
--- a/java/kudu-client/src/test/java/org/apache/kudu/client/TestScannerMultiTablet.java
+++ b/java/kudu-client/src/test/java/org/apache/kudu/client/TestScannerMultiTablet.java
@@ -32,6 +32,7 @@ import org.junit.Test;
 
 import org.apache.kudu.client.Client.ScanTokenPB;
 import org.apache.kudu.ColumnSchema;
+import org.apache.kudu.Common;
 import org.apache.kudu.Schema;
 
 public class TestScannerMultiTablet extends BaseKuduTest {
@@ -196,6 +197,21 @@ public class TestScannerMultiTablet extends BaseKuduTest {
   }
 
   @Test(timeout = 100000)
+  public void testScanTokenReplicaSelections() throws Exception {
+    ScanTokenPB.Builder pbBuilder = ScanTokenPB.newBuilder();
+    pbBuilder.setTableName(table.getName());
+    pbBuilder.setReplicaSelection(Common.ReplicaSelection.CLOSEST_REPLICA);
+    Client.ScanTokenPB scanTokenPB = pbBuilder.build();
+    final byte[] serializedToken = KuduScanToken.serialize(scanTokenPB);
+
+    // Deserialize the scan token into a scanner, and make sure it is using
+    // 'CLOSEST_REPLICA' selection policy.
+    KuduScanner scanner = KuduScanToken.deserializeIntoScanner(serializedToken, syncClient);
+    assertEquals(ReplicaSelection.CLOSEST_REPLICA, scanner.getReplicaSelection());
+    assertEquals(9, countRowsInScan(scanner));
+  }
+
+  @Test(timeout = 100000)
   public void testReadAtSnapshotNoTimestamp() throws Exception {
     // Perform scan in READ_AT_SNAPSHOT mode with no snapshot timestamp
     // specified. Verify that the scanner timestamp is set from the tablet

http://git-wip-us.apache.org/repos/asf/kudu/blob/9269e0ab/src/kudu/client/client.proto
----------------------------------------------------------------------
diff --git a/src/kudu/client/client.proto b/src/kudu/client/client.proto
index 37e57c9..0cab818 100644
--- a/src/kudu/client/client.proto
+++ b/src/kudu/client/client.proto
@@ -97,6 +97,10 @@ message ScanTokenPB {
   // This is a hint, not a requirement: the server may send
   // arbitrarily fewer or more bytes than requested.
   optional uint32 batch_size_bytes = 15;
+
+  // The replica selection policy for the scan request.
+  // See common.proto for further information about replica selections.
+  optional ReplicaSelection replica_selection = 16 [default = LEADER_ONLY];
 }
 
 

http://git-wip-us.apache.org/repos/asf/kudu/blob/9269e0ab/src/kudu/common/common.proto
----------------------------------------------------------------------
diff --git a/src/kudu/common/common.proto b/src/kudu/common/common.proto
index cfd7900..c156ecf 100644
--- a/src/kudu/common/common.proto
+++ b/src/kudu/common/common.proto
@@ -231,6 +231,16 @@ enum OrderMode {
   ORDERED = 2;
 }
 
+// Policy with which to choose among multiple replicas.
+enum ReplicaSelection {
+  UNKNOWN_REPLICA_SELECTION = 0;
+  // Select the LEADER replica.
+  LEADER_ONLY = 1;
+  // Select the closest replica to the client, or a random one if all replicas
+  // are equidistant.
+  CLOSEST_REPLICA = 2;
+}
+
 // The serialized format of a Kudu table partition schema.
 message PartitionSchemaPB {