From 2f0fa4ddbeddeeb1d72e3e42da5a2ea5abb698f0 Mon Sep 17 00:00:00 2001 From: Istvan Toth Date: Mon, 8 Jul 2024 13:59:49 +0200 Subject: [PATCH] HBASE-28685 Support non-root context in REST RemoteHTable and RemodeAdmin (#6013) Signed-off-by: Duo Zhang (cherry picked from commit 610af4855f475b2bfa3d526bf78873677ddcff84) --- .../hadoop/hbase/rest/client/RemoteAdmin.java | 23 +++++++++++----- .../hbase/rest/client/RemoteHTable.java | 26 ++++++++++++------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/client/RemoteAdmin.java b/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/client/RemoteAdmin.java index bccc97deca8a..cddbf1c2c46f 100644 --- a/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/client/RemoteAdmin.java +++ b/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/client/RemoteAdmin.java @@ -45,6 +45,7 @@ public class RemoteAdmin { final String accessToken; final int maxRetries; final long sleepTime; + private String pathPrefix = "/"; // This unmarshaller is necessary for getting the /version/cluster resource. // This resource does not support protobufs. Therefore this is necessary to @@ -79,6 +80,14 @@ public RemoteAdmin(Client client, Configuration conf, String accessToken) { this.sleepTime = conf.getLong("hbase.rest.client.sleep", 1000); } + /** + * Constructor + */ + public RemoteAdmin(Client client, Configuration conf, String accessToken, String pathPrefix) { + this(client, conf, accessToken); + this.pathPrefix = pathPrefix + "/"; + } + /** * @param tableName name of table to check * @return true if all regions of the table are available @@ -95,7 +104,7 @@ public boolean isTableAvailable(String tableName) throws IOException { public VersionModel getRestVersion() throws IOException { StringBuilder path = new StringBuilder(); - path.append('/'); + path.append(pathPrefix); if (accessToken != null) { path.append(accessToken); path.append('/'); @@ -136,7 +145,7 @@ public VersionModel getRestVersion() throws IOException { public StorageClusterStatusModel getClusterStatus() throws IOException { StringBuilder path = new StringBuilder(); - path.append('/'); + path.append(pathPrefix); if (accessToken != null) { path.append(accessToken); path.append('/'); @@ -175,7 +184,7 @@ public StorageClusterStatusModel getClusterStatus() throws IOException { public StorageClusterVersionModel getClusterVersion() throws IOException { StringBuilder path = new StringBuilder(); - path.append('/'); + path.append(pathPrefix); if (accessToken != null) { path.append(accessToken); path.append('/'); @@ -221,7 +230,7 @@ public StorageClusterVersionModel getClusterVersion() throws IOException { */ public boolean isTableAvailable(byte[] tableName) throws IOException { StringBuilder path = new StringBuilder(); - path.append('/'); + path.append(pathPrefix); if (accessToken != null) { path.append(accessToken); path.append('/'); @@ -260,7 +269,7 @@ public boolean isTableAvailable(byte[] tableName) throws IOException { public void createTable(TableDescriptor desc) throws IOException { TableSchemaModel model = new TableSchemaModel(desc); StringBuilder path = new StringBuilder(); - path.append('/'); + path.append(pathPrefix); if (accessToken != null) { path.append(accessToken); path.append('/'); @@ -306,7 +315,7 @@ public void deleteTable(final String tableName) throws IOException { */ public void deleteTable(final byte[] tableName) throws IOException { StringBuilder path = new StringBuilder(); - path.append('/'); + path.append(pathPrefix); if (accessToken != null) { path.append(accessToken); path.append('/'); @@ -342,7 +351,7 @@ public void deleteTable(final byte[] tableName) throws IOException { public TableListModel getTableList() throws IOException { StringBuilder path = new StringBuilder(); - path.append('/'); + path.append(pathPrefix); if (accessToken != null) { path.append(accessToken); path.append('/'); diff --git a/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/client/RemoteHTable.java b/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/client/RemoteHTable.java index 53b5742ca93d..c5db9a294926 100644 --- a/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/client/RemoteHTable.java +++ b/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/client/RemoteHTable.java @@ -93,12 +93,13 @@ public class RemoteHTable implements Table { final byte[] name; final int maxRetries; final long sleepTime; + private String pathPrefix = "/"; @SuppressWarnings("rawtypes") protected String buildRowSpec(final byte[] row, final Map familyMap, final long startTime, final long endTime, final int maxVersions) { - StringBuffer sb = new StringBuffer(); - sb.append('/'); + StringBuilder sb = new StringBuilder(); + sb.append(pathPrefix); sb.append(Bytes.toString(name)); sb.append('/'); sb.append(toURLEncodedBytes(row)); @@ -159,7 +160,7 @@ protected String buildRowSpec(final byte[] row, final Map familyMap, final long protected String buildMultiRowSpec(final byte[][] rows, int maxVersions) { StringBuilder sb = new StringBuilder(); - sb.append('/'); + sb.append(pathPrefix); sb.append(Bytes.toString(name)); sb.append("/multiget/"); if (rows == null || rows.length == 0) { @@ -242,6 +243,11 @@ public RemoteHTable(Client client, Configuration conf, byte[] name) { this.sleepTime = conf.getLong("hbase.rest.client.sleep", 1000); } + public RemoteHTable(Client client, Configuration conf, byte[] name, String pathPrefix) { + this(client, conf, name); + this.pathPrefix = pathPrefix + "/"; + } + public byte[] getTableName() { return name.clone(); } @@ -359,7 +365,7 @@ public boolean[] exists(List gets) throws IOException { public void put(Put put) throws IOException { CellSetModel model = buildModelFromPut(put); StringBuilder sb = new StringBuilder(); - sb.append('/'); + sb.append(pathPrefix); sb.append(Bytes.toString(name)); sb.append('/'); sb.append(toURLEncodedBytes(put.getRow())); @@ -415,7 +421,7 @@ public void put(List puts) throws IOException { // build path for multiput StringBuilder sb = new StringBuilder(); - sb.append('/'); + sb.append(pathPrefix); sb.append(Bytes.toString(name)); sb.append("/$multiput"); // can be any nonexistent row for (int i = 0; i < maxRetries; i++) { @@ -477,7 +483,7 @@ public void flushCommits() throws IOException { @Override public TableDescriptor getDescriptor() throws IOException { StringBuilder sb = new StringBuilder(); - sb.append('/'); + sb.append(pathPrefix); sb.append(Bytes.toString(name)); sb.append('/'); sb.append("schema"); @@ -516,8 +522,8 @@ public Scanner(Scan scan) throws IOException { } catch (Exception e) { throw new IOException(e); } - StringBuffer sb = new StringBuffer(); - sb.append('/'); + StringBuilder sb = new StringBuilder(); + sb.append(pathPrefix); sb.append(Bytes.toString(name)); sb.append('/'); sb.append("scanner"); @@ -688,7 +694,7 @@ private boolean doCheckAndPut(byte[] row, byte[] family, byte[] qualifier, byte[ CellSetModel model = buildModelFromPut(put); StringBuilder sb = new StringBuilder(); - sb.append('/'); + sb.append(pathPrefix); sb.append(Bytes.toString(name)); sb.append('/'); sb.append(toURLEncodedBytes(put.getRow())); @@ -724,7 +730,7 @@ private boolean doCheckAndDelete(byte[] row, byte[] family, byte[] qualifier, by put.add(new KeyValue(row, family, qualifier, value)); CellSetModel model = buildModelFromPut(put); StringBuilder sb = new StringBuilder(); - sb.append('/'); + sb.append(pathPrefix); sb.append(Bytes.toString(name)); sb.append('/'); sb.append(toURLEncodedBytes(row));