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

zhangduo pushed a commit to branch branch-2.6
in repository https://gitbox.apache.org/repos/asf/hbase.git


The following commit(s) were added to refs/heads/branch-2.6 by this push:
     new bbc8fc4312b HBASE-29189 NPE in RawAsyncTableAdmin.isTableDisabled 
(#6798) (#6804)
bbc8fc4312b is described below

commit bbc8fc4312b02f268b861838a42f7ef226cdb7f8
Author: Duo Zhang <[email protected]>
AuthorDate: Mon Mar 17 09:47:30 2025 +0800

    HBASE-29189 NPE in RawAsyncTableAdmin.isTableDisabled (#6798) (#6804)
    
    Signed-off-by: Nick Dimiduk <[email protected]>
    (cherry picked from commit d8f71d019d781063c7888f51de18827effb13516)
    (cherry picked from commit ef44bccb33f0526723330ff938aa2549f56a2433)
---
 hbase-client/pom.xml                               |  5 ++
 .../hadoop/hbase/client/RawAsyncHBaseAdmin.java    | 12 ++-
 .../hbase/client/TestTableEnableDisableError.java  | 91 ++++++++++++++++++++++
 3 files changed, 101 insertions(+), 7 deletions(-)

diff --git a/hbase-client/pom.xml b/hbase-client/pom.xml
index 8f476fd4815..8343037a233 100644
--- a/hbase-client/pom.xml
+++ b/hbase-client/pom.xml
@@ -182,6 +182,11 @@
       <artifactId>mockito-core</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-inline</artifactId>
+      <scope>test</scope>
+    </dependency>
     <dependency>
       <groupId>org.hamcrest</groupId>
       <artifactId>hamcrest-library</artifactId>
diff --git 
a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RawAsyncHBaseAdmin.java
 
b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RawAsyncHBaseAdmin.java
index 640ef17d499..bd01b2d247b 100644
--- 
a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RawAsyncHBaseAdmin.java
+++ 
b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RawAsyncHBaseAdmin.java
@@ -724,13 +724,13 @@ class RawAsyncHBaseAdmin implements AsyncAdmin {
    * targetState).
    */
   private static CompletableFuture<Boolean> completeCheckTableState(
-    CompletableFuture<Boolean> future, TableState tableState, Throwable error,
+    CompletableFuture<Boolean> future, Optional<TableState> tableState, 
Throwable error,
     TableState.State targetState, TableName tableName) {
     if (error != null) {
       future.completeExceptionally(error);
     } else {
-      if (tableState != null) {
-        future.complete(tableState.inStates(targetState));
+      if (tableState.isPresent()) {
+        future.complete(tableState.get().inStates(targetState));
       } else {
         future.completeExceptionally(new TableNotFoundException(tableName));
       }
@@ -745,8 +745,7 @@ class RawAsyncHBaseAdmin implements AsyncAdmin {
     }
     CompletableFuture<Boolean> future = new CompletableFuture<>();
     addListener(AsyncMetaTableAccessor.getTableState(metaTable, tableName), 
(tableState, error) -> {
-      completeCheckTableState(future, tableState.isPresent() ? 
tableState.get() : null, error,
-        TableState.State.ENABLED, tableName);
+      completeCheckTableState(future, tableState, error, 
TableState.State.ENABLED, tableName);
     });
     return future;
   }
@@ -758,8 +757,7 @@ class RawAsyncHBaseAdmin implements AsyncAdmin {
     }
     CompletableFuture<Boolean> future = new CompletableFuture<>();
     addListener(AsyncMetaTableAccessor.getTableState(metaTable, tableName), 
(tableState, error) -> {
-      completeCheckTableState(future, tableState.isPresent() ? 
tableState.get() : null, error,
-        TableState.State.DISABLED, tableName);
+      completeCheckTableState(future, tableState, error, 
TableState.State.DISABLED, tableName);
     });
     return future;
   }
diff --git 
a/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestTableEnableDisableError.java
 
b/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestTableEnableDisableError.java
new file mode 100644
index 00000000000..4a49abaf2cc
--- /dev/null
+++ 
b/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestTableEnableDisableError.java
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.client;
+
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+
+import org.apache.hadoop.hbase.AsyncMetaTableAccessor;
+import org.apache.hadoop.hbase.HBaseClassTestRule;
+import org.apache.hadoop.hbase.HBaseConfiguration;
+import org.apache.hadoop.hbase.HBaseIOException;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.testclassification.ClientTests;
+import org.apache.hadoop.hbase.testclassification.SmallTests;
+import org.apache.hadoop.hbase.util.FutureUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.mockito.MockedStatic;
+
+@Category({ ClientTests.class, SmallTests.class })
+public class TestTableEnableDisableError {
+
+  @ClassRule
+  public static final HBaseClassTestRule CLASS_RULE =
+    HBaseClassTestRule.forClass(TestTableEnableDisableError.class);
+
+  private MockedStatic<AsyncMetaTableAccessor> mockedClientMetaTableAccessor;
+
+  private AsyncAdmin admin;
+
+  @Before
+  public void setUp() {
+    mockedClientMetaTableAccessor = mockStatic(AsyncMetaTableAccessor.class);
+    AsyncConnectionImpl conn = mock(AsyncConnectionImpl.class);
+    AsyncAdminBuilderBase builder =
+      new AsyncAdminBuilderBase(new 
AsyncConnectionConfiguration(HBaseConfiguration.create())) {
+
+        @Override
+        public AsyncAdmin build() {
+          return new RawAsyncHBaseAdmin(conn, null, this);
+        }
+      };
+    admin = builder.build();
+  }
+
+  @After
+  public void tearDown() {
+    mockedClientMetaTableAccessor.closeOnDemand();
+  }
+
+  @Test
+  public void testIsTableEnabledError() {
+    HBaseIOException expected = new HBaseIOException("expected");
+    mockedClientMetaTableAccessor.when(() -> 
AsyncMetaTableAccessor.getTableState(any(), any()))
+      .thenReturn(FutureUtils.failedFuture(expected));
+    HBaseIOException actual = assertThrows(HBaseIOException.class,
+      () -> 
FutureUtils.get(admin.isTableEnabled(TableName.valueOf("whatever"))));
+    assertSame(expected, actual);
+  }
+
+  @Test
+  public void testIsTableDisabledError() {
+    HBaseIOException expected = new HBaseIOException("expected");
+    mockedClientMetaTableAccessor.when(() -> 
AsyncMetaTableAccessor.getTableState(any(), any()))
+      .thenReturn(FutureUtils.failedFuture(expected));
+    HBaseIOException actual = assertThrows(HBaseIOException.class,
+      () -> 
FutureUtils.get(admin.isTableDisabled(TableName.valueOf("whatever"))));
+    assertSame(expected, actual);
+  }
+}

Reply via email to