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

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


The following commit(s) were added to refs/heads/master by this push:
     new b7d11a74451 HBASE-28716 Users of QuotaRetriever should pass an 
existing connection (#6065)
b7d11a74451 is described below

commit b7d11a74451d7b5a132445ad75b4463e8ca0f3c5
Author: Charles Connell <cconn...@hubspot.com>
AuthorDate: Fri Jul 19 06:46:36 2024 -0400

    HBASE-28716 Users of QuotaRetriever should pass an existing connection 
(#6065)
    
    Signed-off-by: Nick Dimiduk <ndimi...@apache.org>
    Signed-off-by: Pankaj Kumar <pankajku...@apache.org>
---
 .../apache/hadoop/hbase/quotas/QuotaRetriever.java | 31 ++++++++++++----
 .../hadoop/hbase/quotas/QuotaObserverChore.java    |  3 +-
 .../hbase/quotas/SnapshotQuotaObserverChore.java   |  4 +-
 .../main/resources/hbase-webapps/master/quotas.jsp |  3 +-
 .../hbase/quotas/SpaceQuotaHelperForTests.java     | 18 ++-------
 .../hbase/quotas/TestMasterQuotasObserver.java     | 26 +++++++------
 .../apache/hadoop/hbase/quotas/TestQuotaAdmin.java | 43 ++++++----------------
 7 files changed, 56 insertions(+), 72 deletions(-)

diff --git 
a/hbase-client/src/main/java/org/apache/hadoop/hbase/quotas/QuotaRetriever.java 
b/hbase-client/src/main/java/org/apache/hadoop/hbase/quotas/QuotaRetriever.java
index 1dd5bf275bb..1d902ff9b71 100644
--- 
a/hbase-client/src/main/java/org/apache/hadoop/hbase/quotas/QuotaRetriever.java
+++ 
b/hbase-client/src/main/java/org/apache/hadoop/hbase/quotas/QuotaRetriever.java
@@ -55,19 +55,29 @@ public class QuotaRetriever implements Closeable, 
Iterable<QuotaSettings> {
   /**
    * Should QutoaRetriever manage the state of the connection, or leave it be.
    */
-  private boolean isManagedConnection = false;
+  private final boolean isManagedConnection;
 
-  QuotaRetriever() {
+  public QuotaRetriever(final Connection conn) throws IOException {
+    this(conn, (QuotaFilter) null);
   }
 
-  void init(final Configuration conf, final Scan scan) throws IOException {
+  public QuotaRetriever(final Connection conn, final QuotaFilter filter) 
throws IOException {
+    this(conn, QuotaTableUtil.makeScan(filter));
+  }
+
+  public QuotaRetriever(final Connection conn, final Scan scan) throws 
IOException {
+    isManagedConnection = false;
+    init(conn, scan);
+  }
+
+  QuotaRetriever(final Configuration conf, final Scan scan) throws IOException 
{
     // Set this before creating the connection and passing it down to make sure
     // it's cleaned up if we fail to construct the Scanner.
-    this.isManagedConnection = true;
+    isManagedConnection = true;
     init(ConnectionFactory.createConnection(conf), scan);
   }
 
-  void init(final Connection conn, final Scan scan) throws IOException {
+  private void init(final Connection conn, final Scan scan) throws IOException 
{
     this.connection = Objects.requireNonNull(conn);
     this.table = this.connection.getTable(QuotaTableUtil.QUOTA_TABLE_NAME);
     try {
@@ -159,7 +169,10 @@ public class QuotaRetriever implements Closeable, 
Iterable<QuotaSettings> {
    * @param conf Configuration object to use.
    * @return the QuotaRetriever
    * @throws IOException if a remote or network exception occurs
+   * @deprecated Since 3.0.0, will be removed in 4.0.0. Use
+   *             {@link #QuotaRetriever(Configuration, Scan)} instead.
    */
+  @Deprecated
   public static QuotaRetriever open(final Configuration conf) throws 
IOException {
     return open(conf, null);
   }
@@ -170,12 +183,14 @@ public class QuotaRetriever implements Closeable, 
Iterable<QuotaSettings> {
    * @param filter the QuotaFilter
    * @return the QuotaRetriever
    * @throws IOException if a remote or network exception occurs
+   * @deprecated Since 3.0.0, will be removed in 4.0.0. Use
+   *             {@link #QuotaRetriever(Configuration, Scan)} instead.
    */
+  @Deprecated
   public static QuotaRetriever open(final Configuration conf, final 
QuotaFilter filter)
     throws IOException {
     Scan scan = QuotaTableUtil.makeScan(filter);
-    QuotaRetriever scanner = new QuotaRetriever();
-    scanner.init(conf, scan);
-    return scanner;
+    return new QuotaRetriever(conf, scan);
   }
+
 }
diff --git 
a/hbase-server/src/main/java/org/apache/hadoop/hbase/quotas/QuotaObserverChore.java
 
b/hbase-server/src/main/java/org/apache/hadoop/hbase/quotas/QuotaObserverChore.java
index 32bfcebcb9e..a89db895cb4 100644
--- 
a/hbase-server/src/main/java/org/apache/hadoop/hbase/quotas/QuotaObserverChore.java
+++ 
b/hbase-server/src/main/java/org/apache/hadoop/hbase/quotas/QuotaObserverChore.java
@@ -475,8 +475,7 @@ public class QuotaObserverChore extends ScheduledChore {
   TablesWithQuotas fetchAllTablesWithQuotasDefined() throws IOException {
     final Scan scan = QuotaTableUtil.makeScan(null);
     final TablesWithQuotas tablesWithQuotas = new TablesWithQuotas(conn, conf);
-    try (final QuotaRetriever scanner = new QuotaRetriever()) {
-      scanner.init(conn, scan);
+    try (final QuotaRetriever scanner = new QuotaRetriever(conn, scan)) {
       for (QuotaSettings quotaSettings : scanner) {
         // Only one of namespace and tablename should be 'null'
         final String namespace = quotaSettings.getNamespace();
diff --git 
a/hbase-server/src/main/java/org/apache/hadoop/hbase/quotas/SnapshotQuotaObserverChore.java
 
b/hbase-server/src/main/java/org/apache/hadoop/hbase/quotas/SnapshotQuotaObserverChore.java
index cfdcb52db71..c198f3d9d32 100644
--- 
a/hbase-server/src/main/java/org/apache/hadoop/hbase/quotas/SnapshotQuotaObserverChore.java
+++ 
b/hbase-server/src/main/java/org/apache/hadoop/hbase/quotas/SnapshotQuotaObserverChore.java
@@ -166,9 +166,9 @@ public class SnapshotQuotaObserverChore extends 
ScheduledChore {
     Set<TableName> tablesToFetchSnapshotsFrom = new HashSet<>();
     QuotaFilter filter = new QuotaFilter();
     filter.addTypeFilter(QuotaType.SPACE);
-    try (Admin admin = conn.getAdmin()) {
+    try (Admin admin = conn.getAdmin(); QuotaRetriever qr = new 
QuotaRetriever(conn, filter)) {
       // Pull all of the tables that have quotas (direct, or from namespace)
-      for (QuotaSettings qs : QuotaRetriever.open(conf, filter)) {
+      for (QuotaSettings qs : qr) {
         if (qs.getQuotaType() == QuotaType.SPACE) {
           String ns = qs.getNamespace();
           TableName tn = qs.getTableName();
diff --git a/hbase-server/src/main/resources/hbase-webapps/master/quotas.jsp 
b/hbase-server/src/main/resources/hbase-webapps/master/quotas.jsp
index 780a8d4b360..a52085b529f 100644
--- a/hbase-server/src/main/resources/hbase-webapps/master/quotas.jsp
+++ b/hbase-server/src/main/resources/hbase-webapps/master/quotas.jsp
@@ -30,7 +30,6 @@
 %>
 <%
   HMaster master = (HMaster) getServletContext().getAttribute(HMaster.MASTER);
-  Configuration conf = master.getConfiguration();
   pageContext.setAttribute("pageTitle", "HBase Master Quotas: " + 
master.getServerName());
   List<ThrottleSettings> regionServerThrottles = new ArrayList<>();
   List<ThrottleSettings> namespaceThrottles = new ArrayList<>();
@@ -39,7 +38,7 @@
   boolean exceedThrottleQuotaEnabled = false;
   if (quotaManager != null) {
     exceedThrottleQuotaEnabled = quotaManager.isExceedThrottleQuotaEnabled();
-    try (QuotaRetriever scanner = QuotaRetriever.open(conf, null)) {
+    try (QuotaRetriever scanner = new QuotaRetriever(master.getConnection())) {
       for (QuotaSettings quota : scanner) {
         if (quota instanceof ThrottleSettings) {
           ThrottleSettings throttle = (ThrottleSettings) quota;
diff --git 
a/hbase-server/src/test/java/org/apache/hadoop/hbase/quotas/SpaceQuotaHelperForTests.java
 
b/hbase-server/src/test/java/org/apache/hadoop/hbase/quotas/SpaceQuotaHelperForTests.java
index 0dda78d26d3..5c29748bf14 100644
--- 
a/hbase-server/src/test/java/org/apache/hadoop/hbase/quotas/SpaceQuotaHelperForTests.java
+++ 
b/hbase-server/src/test/java/org/apache/hadoop/hbase/quotas/SpaceQuotaHelperForTests.java
@@ -120,13 +120,8 @@ public class SpaceQuotaHelperForTests {
    * Returns the number of quotas defined in the HBase quota table.
    */
   long listNumDefinedQuotas(Connection conn) throws IOException {
-    QuotaRetriever scanner = QuotaRetriever.open(conn.getConfiguration());
-    try {
+    try (QuotaRetriever scanner = new QuotaRetriever(conn)) {
       return Iterables.size(scanner);
-    } finally {
-      if (scanner != null) {
-        scanner.close();
-      }
     }
   }
 
@@ -353,8 +348,7 @@ public class SpaceQuotaHelperForTests {
       waitForQuotaTable(conn);
     } else {
       // Or, clean up any quotas from previous test runs.
-      QuotaRetriever scanner = QuotaRetriever.open(conn.getConfiguration());
-      try {
+      try (QuotaRetriever scanner = new QuotaRetriever(conn);) {
         for (QuotaSettings quotaSettings : scanner) {
           final String namespace = quotaSettings.getNamespace();
           final TableName tableName = quotaSettings.getTableName();
@@ -370,17 +364,13 @@ public class SpaceQuotaHelperForTests {
             QuotaUtil.deleteUserQuota(conn, userName);
           }
         }
-      } finally {
-        if (scanner != null) {
-          scanner.close();
-        }
       }
     }
   }
 
   QuotaSettings getTableSpaceQuota(Connection conn, TableName tn) throws 
IOException {
-    try (QuotaRetriever scanner = QuotaRetriever.open(conn.getConfiguration(),
-      new QuotaFilter().setTableFilter(tn.getNameAsString()))) {
+    try (QuotaRetriever scanner =
+      new QuotaRetriever(conn, new 
QuotaFilter().setTableFilter(tn.getNameAsString()))) {
       for (QuotaSettings setting : scanner) {
         if (setting.getTableName().equals(tn) && setting.getQuotaType() == 
QuotaType.SPACE) {
           return setting;
diff --git 
a/hbase-server/src/test/java/org/apache/hadoop/hbase/quotas/TestMasterQuotasObserver.java
 
b/hbase-server/src/test/java/org/apache/hadoop/hbase/quotas/TestMasterQuotasObserver.java
index 0f01a43355b..a5d879ce77e 100644
--- 
a/hbase-server/src/test/java/org/apache/hadoop/hbase/quotas/TestMasterQuotasObserver.java
+++ 
b/hbase-server/src/test/java/org/apache/hadoop/hbase/quotas/TestMasterQuotasObserver.java
@@ -327,25 +327,27 @@ public class TestMasterQuotasObserver {
   }
 
   public int getNumSpaceQuotas() throws Exception {
-    QuotaRetriever scanner = QuotaRetriever.open(TEST_UTIL.getConfiguration());
-    int numSpaceQuotas = 0;
-    for (QuotaSettings quotaSettings : scanner) {
-      if (quotaSettings.getQuotaType() == QuotaType.SPACE) {
-        numSpaceQuotas++;
+    try (QuotaRetriever scanner = new 
QuotaRetriever(TEST_UTIL.getConnection())) {
+      int numSpaceQuotas = 0;
+      for (QuotaSettings quotaSettings : scanner) {
+        if (quotaSettings.getQuotaType() == QuotaType.SPACE) {
+          numSpaceQuotas++;
+        }
       }
+      return numSpaceQuotas;
     }
-    return numSpaceQuotas;
   }
 
   public int getThrottleQuotas() throws Exception {
-    QuotaRetriever scanner = QuotaRetriever.open(TEST_UTIL.getConfiguration());
-    int throttleQuotas = 0;
-    for (QuotaSettings quotaSettings : scanner) {
-      if (quotaSettings.getQuotaType() == QuotaType.THROTTLE) {
-        throttleQuotas++;
+    try (QuotaRetriever scanner = new 
QuotaRetriever(TEST_UTIL.getConnection())) {
+      int throttleQuotas = 0;
+      for (QuotaSettings quotaSettings : scanner) {
+        if (quotaSettings.getQuotaType() == QuotaType.THROTTLE) {
+          throttleQuotas++;
+        }
       }
+      return throttleQuotas;
     }
-    return throttleQuotas;
   }
 
   private void createTable(Admin admin, TableName tn) throws Exception {
diff --git 
a/hbase-server/src/test/java/org/apache/hadoop/hbase/quotas/TestQuotaAdmin.java 
b/hbase-server/src/test/java/org/apache/hadoop/hbase/quotas/TestQuotaAdmin.java
index 817f135f0c9..cd266fa0baa 100644
--- 
a/hbase-server/src/test/java/org/apache/hadoop/hbase/quotas/TestQuotaAdmin.java
+++ 
b/hbase-server/src/test/java/org/apache/hadoop/hbase/quotas/TestQuotaAdmin.java
@@ -123,7 +123,7 @@ public class TestQuotaAdmin {
       QuotaSettingsFactory.throttleUser(userName, ThrottleType.WRITE_NUMBER, 
12, TimeUnit.MINUTES));
     admin.setQuota(QuotaSettingsFactory.bypassGlobals(userName, true));
 
-    try (QuotaRetriever scanner = 
QuotaRetriever.open(TEST_UTIL.getConfiguration())) {
+    try (QuotaRetriever scanner = new 
QuotaRetriever(TEST_UTIL.getConnection())) {
       int countThrottle = 0;
       int countGlobalBypass = 0;
       for (QuotaSettings settings : scanner) {
@@ -169,7 +169,7 @@ public class TestQuotaAdmin {
       TimeUnit.MINUTES));
     admin.setQuota(QuotaSettingsFactory.bypassGlobals(userName, true));
 
-    try (QuotaRetriever scanner = 
QuotaRetriever.open(TEST_UTIL.getConfiguration())) {
+    try (QuotaRetriever scanner = new 
QuotaRetriever(TEST_UTIL.getConnection())) {
       int countThrottle = 0;
       int countGlobalBypass = 0;
       for (QuotaSettings settings : scanner) {
@@ -345,11 +345,8 @@ public class TestQuotaAdmin {
     }
 
     // Verify we can retrieve it via the QuotaRetriever API
-    QuotaRetriever scanner = QuotaRetriever.open(admin.getConfiguration());
-    try {
+    try (QuotaRetriever scanner = new QuotaRetriever(admin.getConnection())) {
       assertSpaceQuota(sizeLimit, violationPolicy, 
Iterables.getOnlyElement(scanner));
-    } finally {
-      scanner.close();
     }
 
     // Now, remove the quota
@@ -367,11 +364,8 @@ public class TestQuotaAdmin {
     }
 
     // Verify that we can also not fetch it via the API
-    scanner = QuotaRetriever.open(admin.getConfiguration());
-    try {
+    try (QuotaRetriever scanner = new QuotaRetriever(admin.getConnection())) {
       assertNull("Did not expect to find a quota entry", scanner.next());
-    } finally {
-      scanner.close();
     }
   }
 
@@ -399,11 +393,8 @@ public class TestQuotaAdmin {
     }
 
     // Verify we can retrieve it via the QuotaRetriever API
-    QuotaRetriever quotaScanner = 
QuotaRetriever.open(admin.getConfiguration());
-    try {
+    try (QuotaRetriever quotaScanner = new 
QuotaRetriever(admin.getConnection())) {
       assertSpaceQuota(originalSizeLimit, violationPolicy, 
Iterables.getOnlyElement(quotaScanner));
-    } finally {
-      quotaScanner.close();
     }
 
     // Setting a new size and policy should be reflected
@@ -427,11 +418,8 @@ public class TestQuotaAdmin {
     }
 
     // Verify we can retrieve the new quota via the QuotaRetriever API
-    quotaScanner = QuotaRetriever.open(admin.getConfiguration());
-    try {
+    try (QuotaRetriever quotaScanner = new 
QuotaRetriever(admin.getConnection())) {
       assertSpaceQuota(newSizeLimit, newViolationPolicy, 
Iterables.getOnlyElement(quotaScanner));
-    } finally {
-      quotaScanner.close();
     }
 
     // Now, remove the quota
@@ -449,11 +437,8 @@ public class TestQuotaAdmin {
     }
 
     // Verify that we can also not fetch it via the API
-    quotaScanner = QuotaRetriever.open(admin.getConfiguration());
-    try {
+    try (QuotaRetriever quotaScanner = new 
QuotaRetriever(admin.getConnection())) {
       assertNull("Did not expect to find a quota entry", quotaScanner.next());
-    } finally {
-      quotaScanner.close();
     }
   }
 
@@ -549,8 +534,7 @@ public class TestQuotaAdmin {
     admin.setQuota(QuotaSettingsFactory.throttleRegionServer(regionServer, 
ThrottleType.READ_NUMBER,
       30, TimeUnit.SECONDS));
     int count = 0;
-    QuotaRetriever scanner = QuotaRetriever.open(TEST_UTIL.getConfiguration(), 
rsFilter);
-    try {
+    try (QuotaRetriever scanner = new 
QuotaRetriever(TEST_UTIL.getConnection(), rsFilter)) {
       for (QuotaSettings settings : scanner) {
         assertTrue(settings.getQuotaType() == QuotaType.THROTTLE);
         ThrottleSettings throttleSettings = (ThrottleSettings) settings;
@@ -564,8 +548,6 @@ public class TestQuotaAdmin {
           assertEquals(TimeUnit.SECONDS, throttleSettings.getTimeUnit());
         }
       }
-    } finally {
-      scanner.close();
     }
     assertEquals(2, count);
 
@@ -733,14 +715,14 @@ public class TestQuotaAdmin {
   private void verifyFetchableViaAPI(Admin admin, ThrottleType type, long 
limit, TimeUnit tu)
     throws Exception {
     // Verify we can retrieve the new quota via the QuotaRetriever API
-    try (QuotaRetriever quotaScanner = 
QuotaRetriever.open(admin.getConfiguration())) {
+    try (QuotaRetriever quotaScanner = new 
QuotaRetriever(admin.getConnection())) {
       assertRPCQuota(type, limit, tu, Iterables.getOnlyElement(quotaScanner));
     }
   }
 
   private void verifyNotFetchableViaAPI(Admin admin) throws Exception {
     // Verify that we can also not fetch it via the API
-    try (QuotaRetriever quotaScanner = 
QuotaRetriever.open(admin.getConfiguration())) {
+    try (QuotaRetriever quotaScanner = new 
QuotaRetriever(admin.getConnection())) {
       assertNull("Did not expect to find a quota entry", quotaScanner.next());
     }
   }
@@ -830,16 +812,13 @@ public class TestQuotaAdmin {
   }
 
   private int countResults(final QuotaFilter filter) throws Exception {
-    QuotaRetriever scanner = QuotaRetriever.open(TEST_UTIL.getConfiguration(), 
filter);
-    try {
+    try (QuotaRetriever scanner = new 
QuotaRetriever(TEST_UTIL.getConnection(), filter)) {
       int count = 0;
       for (QuotaSettings settings : scanner) {
         LOG.debug(Objects.toString(settings));
         count++;
       }
       return count;
-    } finally {
-      scanner.close();
     }
   }
 

Reply via email to