From 3506f59a768faeeb1c8dd58ce6fbeefa9a731529 Mon Sep 17 00:00:00 2001 From: Istvan Toth Date: Mon, 22 Apr 2024 09:56:50 +0200 Subject: [PATCH] HBASE-28540 Cache Results in org.apache.hadoop.hbase.rest.client.RemoteHTable.Scanner --- .../hbase/rest/client/RemoteHTable.java | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) 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 bb80996b3194..53b5742ca93d 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 @@ -23,6 +23,7 @@ import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.List; @@ -505,6 +506,8 @@ public TableDescriptor getDescriptor() throws IOException { class Scanner implements ResultScanner { String uri; + private Result[] cachedResults; + private int nextCachedResultsRow = 0; public Scanner(Scan scan) throws IOException { ScannerModel model; @@ -540,11 +543,8 @@ public Scanner(Scan scan) throws IOException { throw new IOException("scan request timed out"); } - @Override - public Result[] next(int nbRows) throws IOException { + public Result[] nextBatch() throws IOException { StringBuilder sb = new StringBuilder(uri); - sb.append("?n="); - sb.append(nbRows); for (int i = 0; i < maxRetries; i++) { Response response = client.get(sb.toString(), Constants.MIMETYPE_PROTOBUF); int code = response.getCode(); @@ -570,13 +570,31 @@ public Result[] next(int nbRows) throws IOException { throw new IOException("scanner.next request timed out"); } + private boolean updateCachedResults() throws IOException { + if (cachedResults == null || nextCachedResultsRow >= cachedResults.length) { + nextCachedResultsRow = 0; + cachedResults = nextBatch(); + } + return !(cachedResults == null || cachedResults.length < 1); + } + + @Override + public Result[] next(int nbRows) throws IOException { + if (!updateCachedResults()) { + return null; + } + int endIndex = Math.min(cachedResults.length, nextCachedResultsRow + nbRows); + Result[] chunk = Arrays.copyOfRange(cachedResults, nextCachedResultsRow, endIndex); + nextCachedResultsRow = endIndex; + return chunk; + } + @Override public Result next() throws IOException { - Result[] results = next(1); - if (results == null || results.length < 1) { + if (!updateCachedResults()) { return null; } - return results[0]; + return cachedResults[nextCachedResultsRow++]; } class Iter implements Iterator {