New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HBASE-26157 Expose some IA.LimitedPrivate interface in TestingHBaseCl… #3643
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,13 +18,20 @@ | |
package org.apache.hadoop.hbase.testing; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.stream.Collectors; | ||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.hadoop.hbase.HBaseTestingUtil; | ||
import org.apache.hadoop.hbase.ServerName; | ||
import org.apache.hadoop.hbase.StartTestingClusterOption; | ||
import org.apache.hadoop.hbase.client.RegionInfo; | ||
import org.apache.hadoop.hbase.master.HMaster; | ||
import org.apache.hadoop.hbase.regionserver.HRegion; | ||
import org.apache.hadoop.hbase.regionserver.OnlineRegions; | ||
import org.apache.hadoop.hbase.regionserver.Region; | ||
import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread; | ||
import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread; | ||
import org.apache.yetus.audience.InterfaceAudience; | ||
|
@@ -172,4 +179,38 @@ public void startRegionServer() throws Exception { | |
public void startRegionServer(String hostname, int port) throws Exception { | ||
util.getMiniHBaseCluster().startRegionServer(hostname, port); | ||
} | ||
|
||
@Override | ||
public Optional<ServerName> getActiveMasterAddress() { | ||
return Optional.ofNullable(util.getMiniHBaseCluster().getMaster()).map(HMaster::getServerName); | ||
} | ||
|
||
@Override | ||
public List<ServerName> getBackupMasterAddresses() { | ||
return util.getMiniHBaseCluster().getMasterThreads().stream().map(MasterThread::getMaster) | ||
.filter(m -> !m.isActiveMaster()).map(HMaster::getServerName).collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public List<ServerName> getRegionServerAddresses() { | ||
return util.getMiniHBaseCluster().getRegionServerThreads().stream() | ||
.map(t -> t.getRegionServer().getServerName()).collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public Optional<Region> getRegion(RegionInfo regionInfo) { | ||
for (RegionServerThread t : util.getMiniHBaseCluster().getRegionServerThreads()) { | ||
for (HRegion region : t.getRegionServer().getRegions()) { | ||
if (region.getRegionInfo().equals(regionInfo)) { | ||
return Optional.of(region); | ||
} | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public Optional<OnlineRegions> getOnlineRegionsInterface(ServerName serverName) { | ||
return Optional.ofNullable(util.getMiniHBaseCluster().getRegionServer(serverName)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getRegionServer returns an HRegionServer...don't we need to keep going to the HRegionServer's RS coprocessor host to get the OnlineRegions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The HRegionServers extends RegionServerServices, RegionServerServices extends MutableOnlineRegions, and finally MutableOnlineRegions extends OnlineRegions, so HRegionServer itself is 'OnlineRegions', we do not need to go to RS coprocessor to get it. The design here is not give CP implementations a way to get the Region interface, but do not want to give the CP implementations the ability to add or remove from the online regions directly as it could mess up the state. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* 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.testing; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertSame; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import org.apache.hadoop.hbase.HBaseClassTestRule; | ||
import org.apache.hadoop.hbase.ServerName; | ||
import org.apache.hadoop.hbase.TableName; | ||
import org.apache.hadoop.hbase.client.Admin; | ||
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.RegionInfo; | ||
import org.apache.hadoop.hbase.client.RegionInfoBuilder; | ||
import org.apache.hadoop.hbase.client.RegionLocator; | ||
import org.apache.hadoop.hbase.client.TableDescriptorBuilder; | ||
import org.apache.hadoop.hbase.regionserver.OnlineRegions; | ||
import org.apache.hadoop.hbase.regionserver.Region; | ||
import org.apache.hadoop.hbase.testclassification.LargeTests; | ||
import org.apache.hadoop.hbase.testclassification.MiscTests; | ||
import org.apache.hadoop.hbase.util.Bytes; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
|
||
import org.apache.hbase.thirdparty.com.google.common.io.Closeables; | ||
|
||
@Category({ MiscTests.class, LargeTests.class }) | ||
public class TestTestingHBaseClusterImplForCPs { | ||
|
||
@ClassRule | ||
public static final HBaseClassTestRule CLASS_RULE = | ||
HBaseClassTestRule.forClass(TestTestingHBaseClusterImplForCPs.class); | ||
|
||
private static TestingHBaseCluster CLUSTER; | ||
|
||
private static TableName NAME = TableName.valueOf("test"); | ||
|
||
private static byte[] CF = Bytes.toBytes("cf"); | ||
|
||
private static Connection CONN; | ||
|
||
private static Admin ADMIN; | ||
|
||
@BeforeClass | ||
public static void setUpBeforeClass() throws Exception { | ||
CLUSTER = TestingHBaseCluster.create(TestingHBaseClusterOption.builder().numMasters(2) | ||
.numRegionServers(3).numDataNodes(3).build()); | ||
CLUSTER.start(); | ||
CONN = ConnectionFactory.createConnection(CLUSTER.getConf()); | ||
ADMIN = CONN.getAdmin(); | ||
ADMIN.createTable(TableDescriptorBuilder.newBuilder(NAME) | ||
.setColumnFamily(ColumnFamilyDescriptorBuilder.of(CF)).build()); | ||
ADMIN.balancerSwitch(false, true); | ||
} | ||
|
||
@AfterClass | ||
public static void tearDownAfterClass() throws Exception { | ||
Closeables.close(ADMIN, true); | ||
Closeables.close(CONN, true); | ||
if (CLUSTER.isClusterRunning()) { | ||
CLUSTER.stop(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testGetRegion() throws IOException { | ||
List<RegionInfo> infos = ADMIN.getRegions(NAME); | ||
assertEquals(1, infos.size()); | ||
RegionInfo info = infos.get(0); | ||
Region region = CLUSTER.getRegion(info).get(); | ||
ServerName loc; | ||
try (RegionLocator locator = CONN.getRegionLocator(NAME)) { | ||
loc = locator.getRegionLocation(info.getStartKey()).getServerName(); | ||
} | ||
OnlineRegions onlineRegionsInterface = CLUSTER.getOnlineRegionsInterface(loc).get(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm confused why this isn't a ClassCastException going between HRegionServer and OnlineRegions but I see the test is passing in at least some of the test runs. Is there something interesting going on that I'm not understanding? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As said above, the HRegionServer just extends the OnlineRegions interface, so there is no problem. So actually in the CP implementation, when you get an OnlineRegions instance, it is actually a HRegionServer :) |
||
List<? extends Region> regions = onlineRegionsInterface.getRegions(NAME); | ||
assertEquals(1, regions.size()); | ||
assertSame(region, regions.get(0)); | ||
|
||
assertFalse(CLUSTER | ||
.getRegion(RegionInfoBuilder.newBuilder(TableName.valueOf("whatever")).build()).isPresent()); | ||
assertFalse(CLUSTER.getOnlineRegionsInterface(ServerName.valueOf("whatever,1,1")).isPresent()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: does the "Interface" in the method name add any information? Seems like it could be "getOnlineRegions" to return an OnlineRegions. That's just like above, and in general throughout HBase, we have "getRegion" not "getRegionInterface" when we return a Region.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why I added the 'Interface' suffix is because I assumed the name 'getOnlineRegions ' is a bit confusing. It could also be explained as get the online regions, where we should return a list of Regions.
WDYT? I'm not a native English speaker so I'm fine with changing the name, if you think 'getOnlineRegions' is enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that because the return type is Optional(OnlineRegions), the method name getOnlineRegions is clear without the Interface, and we usually don't append Interface. But it's not a big deal either way.