Skip to content

Commit 72b5c8f

Browse files
committed
HBASE-28558 Fix constructors for sub classes of Connection (#5861)
Signed-off-by: Guanghao Zhang <[email protected]> Signed-off-by: GeorryHuang <[email protected]> (cherry picked from commit e9ced39)
1 parent 49aad64 commit 72b5c8f

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

hbase-client/src/main/java/org/apache/hadoop/hbase/client/ConnectionFactory.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
import org.apache.hadoop.hbase.util.FutureUtils;
3737
import org.apache.hadoop.hbase.util.ReflectionUtils;
3838
import org.apache.yetus.audience.InterfaceAudience;
39+
import org.slf4j.Logger;
40+
import org.slf4j.LoggerFactory;
41+
42+
import org.apache.hbase.thirdparty.com.google.common.base.Throwables;
3943

4044
/**
4145
* A non-instantiable class that manages creation of {@link Connection}s. Managing the lifecycle of
@@ -74,6 +78,8 @@
7478
@InterfaceAudience.Public
7579
public class ConnectionFactory {
7680

81+
private static final Logger LOG = LoggerFactory.getLogger(ConnectionFactory.class);
82+
7783
public static final String HBASE_CLIENT_ASYNC_CONNECTION_IMPL =
7884
"hbase.client.async.connection.impl";
7985

@@ -399,15 +405,35 @@ public static Connection createConnection(URI connectionUri, Configuration conf,
399405
} catch (ClassNotFoundException e) {
400406
throw new IOException(e);
401407
}
402-
ConnectionRegistry registry = createConnectionRegistry(connectionUri, conf, user);
403408
try {
404409
// Default HCM#HCI is not accessible; make it so before invoking.
405410
Constructor<?> constructor = clazz.getDeclaredConstructor(Configuration.class,
406411
ExecutorService.class, User.class, ConnectionRegistry.class, Map.class);
407412
constructor.setAccessible(true);
413+
ConnectionRegistry registry = connectionUri != null
414+
? ConnectionRegistryFactory.create(connectionUri, conf, user)
415+
: ConnectionRegistryFactory.create(conf, user);
408416
return user.runAs((PrivilegedExceptionAction<Connection>) () -> (Connection) constructor
409417
.newInstance(conf, pool, user, registry, connectionAttributes));
418+
} catch (NoSuchMethodException e) {
419+
LOG.debug("Constructor with connection registry not found for class {},"
420+
+ " fallback to use old constructor", clazz.getName(), e);
421+
} catch (Exception e) {
422+
Throwables.throwIfInstanceOf(e, IOException.class);
423+
Throwables.throwIfUnchecked(e);
424+
throw new IOException(e);
425+
}
426+
427+
try {
428+
// Default HCM#HCI is not accessible; make it so before invoking.
429+
Constructor<?> constructor = clazz.getDeclaredConstructor(Configuration.class,
430+
ExecutorService.class, User.class, Map.class);
431+
constructor.setAccessible(true);
432+
return user.runAs((PrivilegedExceptionAction<Connection>) () -> (Connection) constructor
433+
.newInstance(conf, pool, user, connectionAttributes));
410434
} catch (Exception e) {
435+
Throwables.throwIfInstanceOf(e, IOException.class);
436+
Throwables.throwIfUnchecked(e);
411437
throw new IOException(e);
412438
}
413439
}, () -> TraceUtil.createSpan(ConnectionFactory.class.getSimpleName() + ".createConnection"));

hbase-mapreduce/src/test/java/org/apache/hadoop/hbase/mapreduce/TestHFileOutputFormat2.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import org.apache.hadoop.hbase.client.ClusterConnection;
7777
import org.apache.hadoop.hbase.client.Connection;
7878
import org.apache.hadoop.hbase.client.ConnectionFactory;
79+
import org.apache.hadoop.hbase.client.ConnectionRegistry;
7980
import org.apache.hadoop.hbase.client.Hbck;
8081
import org.apache.hadoop.hbase.client.Put;
8182
import org.apache.hadoop.hbase.client.RegionLocator;
@@ -1606,7 +1607,9 @@ private static class ConfigurationCaptorConnection implements Connection {
16061607
private final Connection delegate;
16071608

16081609
public ConfigurationCaptorConnection(Configuration conf, ExecutorService es, User user,
1609-
Map<String, byte[]> connectionAttributes) throws IOException {
1610+
ConnectionRegistry registry, Map<String, byte[]> connectionAttributes) throws IOException {
1611+
// here we do not use this registry, so close it...
1612+
registry.close();
16101613
Configuration confForDelegate = new Configuration(conf);
16111614
confForDelegate.unset(ClusterConnection.HBASE_CLIENT_CONNECTION_IMPL);
16121615
delegate = createConnection(confForDelegate, es, user, connectionAttributes);

hbase-mapreduce/src/test/java/org/apache/hadoop/hbase/mapreduce/TestMultiTableInputFormatBase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.apache.hadoop.hbase.client.BufferedMutatorParams;
3939
import org.apache.hadoop.hbase.client.ClusterConnection;
4040
import org.apache.hadoop.hbase.client.Connection;
41+
import org.apache.hadoop.hbase.client.ConnectionRegistry;
4142
import org.apache.hadoop.hbase.client.RegionInfoBuilder;
4243
import org.apache.hadoop.hbase.client.RegionLocator;
4344
import org.apache.hadoop.hbase.client.Result;
@@ -123,7 +124,7 @@ public static class MRSplitsConnection implements Connection {
123124
static final AtomicInteger creations = new AtomicInteger(0);
124125

125126
MRSplitsConnection(Configuration conf, ExecutorService pool, User user,
126-
Map<String, byte[]> connectionAttributes) throws IOException {
127+
ConnectionRegistry registry, Map<String, byte[]> connectionAttributes) throws IOException {
127128
this.configuration = conf;
128129
creations.incrementAndGet();
129130
}

hbase-mapreduce/src/test/java/org/apache/hadoop/hbase/mapreduce/TestTableInputFormatBase.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
*/
1818
package org.apache.hadoop.hbase.mapreduce;
1919

20-
import static org.junit.Assert.*;
21-
import static org.mockito.Mockito.any;
22-
import static org.mockito.Mockito.anyBoolean;
20+
import static org.junit.Assert.assertEquals;
21+
import static org.mockito.ArgumentMatchers.any;
22+
import static org.mockito.ArgumentMatchers.anyBoolean;
2323
import static org.mockito.Mockito.mock;
2424
import static org.mockito.Mockito.when;
2525

@@ -42,6 +42,7 @@
4242
import org.apache.hadoop.hbase.client.BufferedMutatorParams;
4343
import org.apache.hadoop.hbase.client.ClusterConnection;
4444
import org.apache.hadoop.hbase.client.Connection;
45+
import org.apache.hadoop.hbase.client.ConnectionRegistry;
4546
import org.apache.hadoop.hbase.client.RegionInfo;
4647
import org.apache.hadoop.hbase.client.RegionInfoBuilder;
4748
import org.apache.hadoop.hbase.client.RegionLocator;
@@ -212,7 +213,7 @@ private static class ConnectionForMergeTesting implements Connection {
212213
}
213214

214215
ConnectionForMergeTesting(Configuration conf, ExecutorService pool, User user,
215-
Map<String, byte[]> connectionAttributes) throws IOException {
216+
ConnectionRegistry registry, Map<String, byte[]> connectionAttributes) throws IOException {
216217
}
217218

218219
@Override

0 commit comments

Comments
 (0)