ndimiduk commented on code in PR #5447:
URL: https://github.com/apache/hbase/pull/5447#discussion_r1345778406


##########
hbase-server/src/main/java/org/apache/hadoop/hbase/ipc/ServerRpcConnection.java:
##########
@@ -656,6 +663,15 @@ protected void processRequest(ByteBuff buf) throws 
IOException, InterruptedExcep
     }
   }
 
+  private void sendErrorResponseForCall(int id, long totalRequestSize, Span 
span, String msg,

Review Comment:
   nice cleanup.



##########
hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestRegionServerRejectDuringAbort.java:
##########
@@ -0,0 +1,253 @@
+/*
+ * 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.regionserver;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.IOException;
+import java.util.Optional;
+import java.util.concurrent.atomic.AtomicReference;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hbase.HBaseClassTestRule;
+import org.apache.hadoop.hbase.HBaseTestingUtil;
+import org.apache.hadoop.hbase.HConstants;
+import org.apache.hadoop.hbase.HRegionLocation;
+import org.apache.hadoop.hbase.ServerName;
+import org.apache.hadoop.hbase.StartTestingClusterOption;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
+import org.apache.hadoop.hbase.client.Connection;
+import org.apache.hadoop.hbase.client.ConnectionFactory;
+import org.apache.hadoop.hbase.client.Put;
+import org.apache.hadoop.hbase.client.RegionInfo;
+import org.apache.hadoop.hbase.client.RetriesExhaustedException;
+import org.apache.hadoop.hbase.client.Table;
+import org.apache.hadoop.hbase.client.TableDescriptor;
+import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
+import org.apache.hadoop.hbase.coprocessor.ObserverContext;
+import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor;
+import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
+import org.apache.hadoop.hbase.coprocessor.RegionObserver;
+import org.apache.hadoop.hbase.ipc.CallRunner;
+import org.apache.hadoop.hbase.ipc.PluggableBlockingQueue;
+import org.apache.hadoop.hbase.ipc.PriorityFunction;
+import org.apache.hadoop.hbase.ipc.TestPluggableQueueImpl;
+import org.apache.hadoop.hbase.testclassification.MediumTests;
+import org.apache.hadoop.hbase.testclassification.RegionServerTests;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.hadoop.hbase.util.JVMClusterUtil;
+import org.apache.hadoop.hbase.util.Threads;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Category({ RegionServerTests.class, MediumTests.class })
+public class TestRegionServerRejectDuringAbort {
+
+  @ClassRule
+  public static final HBaseClassTestRule CLASS_RULE =
+    HBaseClassTestRule.forClass(TestRegionServerRejectDuringAbort.class);
+
+  private static final Logger LOG =
+    LoggerFactory.getLogger(TestRegionServerRejectDuringAbort.class);
+
+  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();

Review Comment:
   The HBaseTestingUtil has a library of helper methods that you may find 
useful. For example, `createMultiRegionTable` and `loadRegion`.



##########
hbase-server/src/main/java/org/apache/hadoop/hbase/ipc/ServerRpcConnection.java:
##########
@@ -548,6 +549,19 @@ protected void processRequest(ByteBuff buf) throws 
IOException, InterruptedExcep
     Span span = TraceUtil.createRemoteSpan("RpcServer.process", traceCtx);
     try (Scope ignored = span.makeCurrent()) {
       int id = header.getCallId();
+      // HBASE-28128 - if server is aborting, don't bother trying to process. 
It will
+      // fail at the handler layer, but worse might result in 
CallQueueTooBigException if the
+      // queue is full but server is not properly processing requests. Better 
to throw an aborted
+      // exception here so that the client can properly react.
+      if (rpcServer.server != null && rpcServer.server.isAborted()) {

Review Comment:
   `rpcServer.server` can be null? I guess it doesn't hurt to check.



##########
hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestRegionServerRejectDuringAbort.java:
##########
@@ -0,0 +1,253 @@
+/*
+ * 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.regionserver;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.IOException;
+import java.util.Optional;
+import java.util.concurrent.atomic.AtomicReference;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hbase.HBaseClassTestRule;
+import org.apache.hadoop.hbase.HBaseTestingUtil;
+import org.apache.hadoop.hbase.HConstants;
+import org.apache.hadoop.hbase.HRegionLocation;
+import org.apache.hadoop.hbase.ServerName;
+import org.apache.hadoop.hbase.StartTestingClusterOption;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
+import org.apache.hadoop.hbase.client.Connection;
+import org.apache.hadoop.hbase.client.ConnectionFactory;
+import org.apache.hadoop.hbase.client.Put;
+import org.apache.hadoop.hbase.client.RegionInfo;
+import org.apache.hadoop.hbase.client.RetriesExhaustedException;
+import org.apache.hadoop.hbase.client.Table;
+import org.apache.hadoop.hbase.client.TableDescriptor;
+import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
+import org.apache.hadoop.hbase.coprocessor.ObserverContext;
+import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor;
+import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
+import org.apache.hadoop.hbase.coprocessor.RegionObserver;
+import org.apache.hadoop.hbase.ipc.CallRunner;
+import org.apache.hadoop.hbase.ipc.PluggableBlockingQueue;
+import org.apache.hadoop.hbase.ipc.PriorityFunction;
+import org.apache.hadoop.hbase.ipc.TestPluggableQueueImpl;
+import org.apache.hadoop.hbase.testclassification.MediumTests;
+import org.apache.hadoop.hbase.testclassification.RegionServerTests;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.hadoop.hbase.util.JVMClusterUtil;
+import org.apache.hadoop.hbase.util.Threads;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Category({ RegionServerTests.class, MediumTests.class })
+public class TestRegionServerRejectDuringAbort {
+
+  @ClassRule
+  public static final HBaseClassTestRule CLASS_RULE =
+    HBaseClassTestRule.forClass(TestRegionServerRejectDuringAbort.class);
+
+  private static final Logger LOG =
+    LoggerFactory.getLogger(TestRegionServerRejectDuringAbort.class);
+
+  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
+
+  private static TableName TABLE_NAME = TableName.valueOf("RSRejectOnAbort");
+
+  private static byte[] CF = Bytes.toBytes("cf");
+
+  private static final int REGIONS_NUM = 5;
+
+  private static final AtomicReference<Exception> THROWN_EXCEPTION = new 
AtomicReference<>(null);
+
+  private static volatile boolean shouldThrowTooBig = false;
+
+  @BeforeClass
+  public static void setUp() throws Exception {
+    // Will schedule a abort timeout task after SLEEP_TIME_WHEN_CLOSE_REGION ms
+    UTIL.getConfiguration().set("hbase.ipc.server.callqueue.type", 
"pluggable");
+    
UTIL.getConfiguration().setClass("hbase.ipc.server.callqueue.pluggable.queue.class.name",
+      CallQueueTooBigThrowingQueue.class, PluggableBlockingQueue.class);
+    StartTestingClusterOption option =
+      StartTestingClusterOption.builder().numRegionServers(2).build();
+    UTIL.startMiniCluster(option);
+    TableDescriptor td = TableDescriptorBuilder.newBuilder(TABLE_NAME)
+      .setCoprocessor(SleepWhenCloseCoprocessor.class.getName())
+      
.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(CF).build()).build();
+    UTIL.getAdmin().createTable(td, Bytes.toBytes("0"), Bytes.toBytes("9"), 
REGIONS_NUM);
+  }
+
+  public static final class CallQueueTooBigThrowingQueue extends 
TestPluggableQueueImpl {
+
+    public CallQueueTooBigThrowingQueue(int maxQueueLength, PriorityFunction 
priority,
+      Configuration conf) {
+      super(maxQueueLength, priority, conf);
+    }
+
+    @Override
+    public boolean offer(CallRunner callRunner) {
+      if (shouldThrowTooBig && 
callRunner.getRpcCall().getRequestAttribute("test") != null) {
+        return false;
+      }
+      return super.offer(callRunner);
+    }
+  }
+
+  @AfterClass
+  public static void tearDown() throws Exception {
+    UTIL.shutdownMiniCluster();
+  }
+
+  /**
+   * Tests that the logic in ServerRpcConnection works such that if the server 
is aborted, it short
+   * circuits any other logic. This means we no longer even attempt to enqueue 
the request onto the
+   * call queue. We verify this by using a special call queue which we can 
trigger to always return
+   * CallQueueTooBigException. If the logic works, despite forcing those 
exceptions, we should not
+   * see them.
+   */
+  @Test
+  public void testRejectRequestsOnAbort() throws Exception {
+    // We don't want to disrupt the server carrying meta, because we plan to 
disrupt requests to
+    // the server. Disrupting meta requests messes with the test.
+    HRegionServer serverWithoutMeta = null;
+    for (JVMClusterUtil.RegionServerThread regionServerThread : 
UTIL.getMiniHBaseCluster()
+      .getRegionServerThreads()) {
+      HRegionServer regionServer = regionServerThread.getRegionServer();
+      if (
+        regionServer.getRegions(TableName.META_TABLE_NAME).isEmpty()
+          && !regionServer.getRegions(TABLE_NAME).isEmpty()
+      ) {
+        serverWithoutMeta = regionServer;
+        break;
+      }
+    }
+
+    assertNotNull("couldn't find a server without meta, but with test table 
regions",
+      serverWithoutMeta);
+
+    Thread writer = new 
Thread(getWriterThreadRunnable(serverWithoutMeta.getServerName()));
+    writer.setDaemon(true);
+    writer.start();
+
+    // Trigger the abort. Our WriterThread will detect the first 
RegionServerAbortedException
+    // and trigger our custom queue to reject any more requests. This would 
typically result in
+    // CallQueueTooBigException, unless our logic in ServerRpcConnection to 
preempt the processing
+    // of a request is working.
+    serverWithoutMeta.abort("Abort RS for test");
+
+    UTIL.waitFor(60_000, () -> THROWN_EXCEPTION.get() != null);
+    assertEquals(THROWN_EXCEPTION.get().getCause().getClass(), 
RegionServerAbortedException.class);
+  }
+
+  private Runnable getWriterThreadRunnable(ServerName loadServer) {
+    return () -> {
+      try {
+        Configuration conf = UTIL.getConfiguration();
+        conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 5);
+        try (Connection conn = ConnectionFactory.createConnection(conf);
+          Table table = conn.getTableBuilder(TABLE_NAME, null)
+            .setRequestAttribute("test", new byte[] { 0 }).build()) {
+          // find the first region to exist on our test server, then submit 
requests to it
+          for (HRegionLocation regionLocation : 
table.getRegionLocator().getAllRegionLocations()) {
+            if (regionLocation.getServerName().equals(loadServer)) {
+              submitRequestsToRegion(table, regionLocation.getRegion());
+              return;
+            }
+          }
+          throw new RuntimeException("Failed to find any regions for 
loadServer " + loadServer);
+        }
+      } catch (Exception e) {
+        LOG.warn("Failed to load data", e);
+        synchronized (THROWN_EXCEPTION) {

Review Comment:
   Maybe a 
[CountdownLatch](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/concurrent/CountDownLatch.html)
 is less ham-handed than the synchronized/notify/waitFor dance?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to