ndimiduk commented on code in PR #6055: URL: https://github.com/apache/hbase/pull/6055#discussion_r1740597548
########## hbase-server/src/test/java/org/apache/hadoop/hbase/quotas/TestScannerLeaseCount.java: ########## @@ -0,0 +1,222 @@ +/* + * 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.quotas; + +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.List; +import java.util.concurrent.atomic.AtomicLong; +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.SingleProcessHBaseCluster; +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.ResultScanner; +import org.apache.hadoop.hbase.client.Scan; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; +import org.apache.hadoop.hbase.regionserver.LeaseManager; +import org.apache.hadoop.hbase.regionserver.Region; +import org.apache.hadoop.hbase.regionserver.RegionServerServices; +import org.apache.hadoop.hbase.testclassification.ClientTests; +import org.apache.hadoop.hbase.testclassification.MediumTests; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.JVMClusterUtil; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos; + +@Category({ MediumTests.class, ClientTests.class }) +public class TestScannerLeaseCount { + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestScannerLeaseCount.class); + + private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); + private static final TableName TABLE_NAME = TableName.valueOf("ScannerLeaseCount"); + private static final byte[] FAM = Bytes.toBytes("Fam"); + + private static volatile boolean SHOULD_THROW = false; + + private static Connection CONN; + private static Table TABLE; + + private static SingleProcessHBaseCluster cluster; + + @BeforeClass + public static void setUp() throws Exception { + StartTestingClusterOption option = + StartTestingClusterOption.builder().rsClass(MockedQuotaManagerRegionServer.class).build(); + cluster = UTIL.startMiniCluster(option); + UTIL.getAdmin().createTable(TableDescriptorBuilder.newBuilder(TABLE_NAME) + .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAM)).build()); + Configuration conf = new Configuration(UTIL.getConfiguration()); + CONN = ConnectionFactory.createConnection(conf); + TABLE = CONN.getTable(TABLE_NAME); + UTIL.loadTable(TABLE, FAM); + } + + @AfterClass + public static void tearDown() throws Exception { + try { + TABLE.close(); + } catch (Exception ignore) { + } + try { + CONN.close(); + } catch (Exception ignore) { + } + UTIL.shutdownMiniCluster(); + } + + @Before + public void before() { + SHOULD_THROW = false; + } + + @Test + public void itIncreasesScannerCount() throws Exception { + long beforeScan = countLeases(); + ResultScanner scanner = TABLE.getScanner(new Scan()); + Thread.sleep(1000); + scanner.close(); + long afterScan = countLeases(); + assertTrue(beforeScan < afterScan); + } + + @Test + public void itDoesNotIncreaseScannerLeaseCount() throws Exception { + SHOULD_THROW = true; + long beforeScan = countLeases(); + ResultScanner scanner = TABLE.getScanner(new Scan()); + Thread.sleep(1000); + scanner.close(); + long afterScan = countLeases(); + assertSame(beforeScan, afterScan); + } + + public static final class MockedQuotaManagerRegionServer + extends SingleProcessHBaseCluster.MiniHBaseClusterRegionServer { + private final MockedRpcQuotaManager rpcQuotaManager; + private final LeaseManager leaseManager; + + public MockedQuotaManagerRegionServer(Configuration conf) + throws IOException, InterruptedException { + super(conf); + this.rpcQuotaManager = new MockedRpcQuotaManager(this); + this.leaseManager = + new MockedLeaseManager(conf.getInt(HConstants.THREAD_WAKE_FREQUENCY, 10 * 1000)); + } + + @Override + public RegionServerRpcQuotaManager getRegionServerRpcQuotaManager() { + return rpcQuotaManager; + } + + @Override + public LeaseManager getLeaseManager() { + return leaseManager; + } + } + + public static final class MockedLeaseManager extends LeaseManager { + private final AtomicLong leasesCreated = new AtomicLong(0); + + public MockedLeaseManager(int leaseCheckFrequency) { + super(leaseCheckFrequency); + } + + public long getLeasesCreated() { + return leasesCreated.get(); + } + + @Override + public void addLease(Lease lease) throws LeaseStillHeldException { + super.addLease(lease); + leasesCreated.incrementAndGet(); + } + } + + private long countLeases() { Review Comment: This is really coarse-grained. Even limiting the check to a specific region server would prove a more reliable test. Extracting the scannerId and looking for it specifically will be the most reliable way to test here. I don't know if we have reasonable utilities to expose that information on the client through. ########## hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/LeaseManager.java: ########## @@ -216,7 +216,7 @@ public String getName() { } /** This class tracks a single Lease. */ - static class Lease implements Delayed { + protected static class Lease implements Delayed { Review Comment: Isn't `package protected` is a super-set of `protected` permissions? The `LeaseManager$Lease` class should already be accessible to anything in the `o.a.h.h.regionserver` package. ########## hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/RSRpcServices.java: ########## @@ -418,6 +418,18 @@ public void run() { } } + static class QuotaAndRegionScannerHolder { + private final String scannerName; + private final RegionScannerHolder rsh; + private final OperationQuota quota; + + QuotaAndRegionScannerHolder(String scannerName, RegionScannerHolder rsh, OperationQuota quota) { Review Comment: Instead of wrapping the existing holder class, does it make sense to push the Quota and name information down into the `RegionScannerHolder`? I imagine that there's other related places where things could be cleaner if the Quota was automagically along for the ride. Maybe rename it to a `Context` objects instead of "holder". ########## hbase-server/src/test/java/org/apache/hadoop/hbase/quotas/TestScannerLeaseCount.java: ########## @@ -0,0 +1,215 @@ +/* + * 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.quotas; + +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.List; +import java.util.concurrent.atomic.AtomicLong; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.HBaseTestingUtil; +import org.apache.hadoop.hbase.HConstants; +import org.apache.hadoop.hbase.SingleProcessHBaseCluster; +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.ResultScanner; +import org.apache.hadoop.hbase.client.Scan; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; +import org.apache.hadoop.hbase.regionserver.LeaseManager; +import org.apache.hadoop.hbase.regionserver.Region; +import org.apache.hadoop.hbase.regionserver.RegionServerServices; +import org.apache.hadoop.hbase.testclassification.ClientTests; +import org.apache.hadoop.hbase.testclassification.MediumTests; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.JVMClusterUtil; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos; + +@Category({ MediumTests.class, ClientTests.class }) +public class TestScannerLeaseCount { + private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); + private static final TableName TABLE_NAME = TableName.valueOf("ScannerLeaseCount"); + private static final byte[] FAM = Bytes.toBytes("Fam"); + + private static volatile boolean SHOULD_THROW = false; + + private static Connection CONN; + private static Table TABLE; + + private static SingleProcessHBaseCluster cluster; + + @BeforeClass + public static void setUp() throws Exception { + StartTestingClusterOption option = + StartTestingClusterOption.builder().rsClass(MockedQuotaManagerRegionServer.class).build(); + cluster = UTIL.startMiniCluster(option); + UTIL.getAdmin().createTable(TableDescriptorBuilder.newBuilder(TABLE_NAME) + .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAM)).build()); + Configuration conf = new Configuration(UTIL.getConfiguration()); + CONN = ConnectionFactory.createConnection(conf); + TABLE = CONN.getTable(TABLE_NAME); + UTIL.loadTable(TABLE, FAM); + } + + @AfterClass + public static void tearDown() throws Exception { + try { + TABLE.close(); + } catch (Exception ignore) { + } + try { + CONN.close(); + } catch (Exception ignore) { + } + UTIL.shutdownMiniCluster(); + } + + @Before + public void before() { + SHOULD_THROW = false; + } + + @Test + public void itIncreasesScannerCount() throws Exception { + long beforeScan = countLeases(); + ResultScanner scanner = TABLE.getScanner(new Scan()); + Thread.sleep(1000); Review Comment: Tests with fixed timed waits like this are guaranteed to show up on our flakey dashboard. Instead of this, I would loop the `beforeScan < afterScan` in a retry-with-timeout structure. We have the `Waiter` class and `HBaseCommonTestingUtil#waitFor` methods for exactly this purpose. Whatever hacks you required, make sure you include a comment in code, not just on the PR. -- 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]
