Skip to content

Commit 945ab47

Browse files
committed
HBASE-26834 Adapt ConnectionRule for both sync and async connections
1 parent 50e1230 commit 945ab47

4 files changed

Lines changed: 94 additions & 22 deletions

File tree

hbase-it/src/test/java/org/apache/hadoop/hbase/TestShellExecEndpointCoprocessor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class TestShellExecEndpointCoprocessor {
5656

5757
@Rule
5858
public final ConnectionRule connectionRule =
59-
new ConnectionRule(miniClusterRule::createConnection);
59+
ConnectionRule.createAsyncConnectionRule(miniClusterRule::createAsyncConnection);
6060

6161
@Test
6262
public void testShellExecUnspecified() {
@@ -69,7 +69,7 @@ public void testShellExecForeground() {
6969
}
7070

7171
private void testShellExecForeground(final Consumer<ShellExecRequest.Builder> consumer) {
72-
final AsyncConnection conn = connectionRule.getConnection();
72+
final AsyncConnection conn = connectionRule.getAsyncConnection();
7373
final AsyncAdmin admin = conn.getAdmin();
7474

7575
final String command = "echo -n \"hello world\"";
@@ -87,7 +87,7 @@ private void testShellExecForeground(final Consumer<ShellExecRequest.Builder> co
8787

8888
@Test
8989
public void testShellExecBackground() throws IOException {
90-
final AsyncConnection conn = connectionRule.getConnection();
90+
final AsyncConnection conn = connectionRule.getAsyncConnection();
9191
final AsyncAdmin admin = conn.getAdmin();
9292

9393
final File testDataDir = ensureTestDataDirExists(miniClusterRule.getTestingUtility());
@@ -121,7 +121,7 @@ private static File ensureTestDataDirExists(
121121
final Path testDataDir = Optional.of(testingUtility)
122122
.map(HBaseTestingUtility::getDataTestDir)
123123
.map(Object::toString)
124-
.map(val -> Paths.get(val))
124+
.map(Paths::get)
125125
.orElseThrow(() -> new RuntimeException("Unable to locate temp directory path."));
126126
final File testDataDirFile = Files.createDirectories(testDataDir).toFile();
127127
assertTrue(testDataDirFile.exists());

hbase-server/src/test/java/org/apache/hadoop/hbase/ConnectionRule.java

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.concurrent.CompletableFuture;
2222
import java.util.function.Supplier;
2323
import org.apache.hadoop.hbase.client.AsyncConnection;
24+
import org.apache.hadoop.hbase.client.Connection;
2425
import org.junit.ClassRule;
2526
import org.junit.Rule;
2627
import org.junit.rules.ExternalResource;
@@ -35,7 +36,7 @@
3536
* public class TestMyClass {
3637
* private static final MiniClusterRule miniClusterRule = MiniClusterRule.newBuilder().build();
3738
* private static final ConnectionRule connectionRule =
38-
* new ConnectionRule(miniClusterRule::createConnection);
39+
* ConnectionRule.createAsyncConnectionRule(miniClusterRule::createConnection);
3940
*
4041
* @ClassRule
4142
* public static final TestRule rule = RuleChain
@@ -44,32 +45,87 @@
4445
* }
4546
* }</pre>
4647
*/
47-
public class ConnectionRule extends ExternalResource {
48+
public final class ConnectionRule extends ExternalResource {
4849

49-
private final Supplier<CompletableFuture<AsyncConnection>> connectionSupplier;
50-
private AsyncConnection connection;
50+
private final Supplier<Connection> connectionSupplier;
51+
private final Supplier<CompletableFuture<AsyncConnection>> asyncConnectionSupplier;
5152

52-
public ConnectionRule(final Supplier<CompletableFuture<AsyncConnection>> connectionSupplier) {
53+
private Connection connection;
54+
private AsyncConnection asyncConnection;
55+
56+
public static ConnectionRule createSyncConnectionRule(
57+
final Supplier<Connection> connectionSupplier
58+
) {
59+
return new ConnectionRule(connectionSupplier, null);
60+
}
61+
62+
public static ConnectionRule createAsyncConnectionRule(
63+
final Supplier<CompletableFuture<AsyncConnection>> asyncConnectionSupplier
64+
) {
65+
return new ConnectionRule(null, asyncConnectionSupplier);
66+
}
67+
68+
public static ConnectionRule createConnectionRule(
69+
final Supplier<Connection> connectionSupplier,
70+
final Supplier<CompletableFuture<AsyncConnection>> asyncConnectionSupplier
71+
) {
72+
return new ConnectionRule(connectionSupplier, asyncConnectionSupplier);
73+
}
74+
75+
private ConnectionRule(
76+
final Supplier<Connection> connectionSupplier,
77+
final Supplier<CompletableFuture<AsyncConnection>> asyncConnectionSupplier
78+
) {
5379
this.connectionSupplier = connectionSupplier;
80+
this.asyncConnectionSupplier = asyncConnectionSupplier;
5481
}
5582

56-
public AsyncConnection getConnection() {
83+
public Connection getSyncConnection() {
84+
if (connection == null) {
85+
throw new IllegalStateException(
86+
"ConnectionRule not initialized with a synchronous connection.");
87+
}
5788
return connection;
5889
}
5990

91+
public AsyncConnection getAsyncConnection() {
92+
if (asyncConnection == null) {
93+
throw new IllegalStateException(
94+
"ConnectionRule not initialized with an asynchronous connection.");
95+
}
96+
return asyncConnection;
97+
}
98+
6099
@Override
61100
protected void before() throws Throwable {
62-
this.connection = connectionSupplier.get().join();
101+
if (connectionSupplier != null) {
102+
this.connection = connectionSupplier.get();
103+
}
104+
if (asyncConnectionSupplier != null) {
105+
this.asyncConnection = asyncConnectionSupplier.get().join();
106+
}
63107
}
64108

65109
@Override
66110
protected void after() {
67-
if (this.connection != null) {
68-
try {
69-
connection.close();
70-
} catch (IOException e) {
71-
throw new RuntimeException(e);
111+
CompletableFuture<Void> closeConnection = CompletableFuture.runAsync(() -> {
112+
if (this.connection != null) {
113+
try {
114+
connection.close();
115+
} catch (IOException e) {
116+
throw new RuntimeException(e);
117+
}
72118
}
73-
}
119+
});
120+
CompletableFuture<Void> closeAsyncConnection = CompletableFuture.runAsync(() -> {
121+
if (this.asyncConnection != null) {
122+
try {
123+
asyncConnection.close();
124+
} catch (IOException e) {
125+
throw new RuntimeException(e);
126+
}
127+
}
128+
});
129+
CompletableFuture.allOf(closeConnection, closeAsyncConnection).join();
74130
}
75131
}

hbase-server/src/test/java/org/apache/hadoop/hbase/MiniClusterRule.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.concurrent.CompletableFuture;
2222
import org.apache.hadoop.conf.Configuration;
2323
import org.apache.hadoop.hbase.client.AsyncConnection;
24+
import org.apache.hadoop.hbase.client.Connection;
2425
import org.apache.hadoop.hbase.client.ConnectionFactory;
2526
import org.junit.ClassRule;
2627
import org.junit.Rule;
@@ -41,7 +42,7 @@
4142
*
4243
* @Rule
4344
* public final ConnectionRule connectionRule =
44-
* new ConnectionRule(miniClusterRule::createConnection);
45+
* ConnectionRule.createAsyncConnectionRule(miniClusterRule::createAsyncConnection);
4546
* }
4647
* }</pre>
4748
*/
@@ -102,11 +103,26 @@ public HBaseTestingUtility getTestingUtility() {
102103
return testingUtility;
103104
}
104105

106+
/**
107+
* Create a {@link Connection} to the managed {@link MiniHBaseCluster}. It's up to the caller
108+
* to {@link Connection#close() close()} the connection when finished.
109+
*/
110+
public Connection createSyncConnection() {
111+
if (miniCluster == null) {
112+
throw new IllegalStateException("test cluster not initialized");
113+
}
114+
try {
115+
return ConnectionFactory.createConnection(miniCluster.getConf());
116+
} catch (IOException e) {
117+
throw new RuntimeException(e);
118+
}
119+
}
120+
105121
/**
106122
* Create a {@link AsyncConnection} to the managed {@link MiniHBaseCluster}. It's up to the caller
107123
* to {@link AsyncConnection#close() close()} the connection when finished.
108124
*/
109-
public CompletableFuture<AsyncConnection> createConnection() {
125+
public CompletableFuture<AsyncConnection> createAsyncConnection() {
110126
if (miniCluster == null) {
111127
throw new IllegalStateException("test cluster not initialized");
112128
}

hbase-server/src/test/java/org/apache/hadoop/hbase/master/http/TestMetaBrowser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ public class TestMetaBrowser {
6868
public static final MiniClusterRule miniClusterRule = MiniClusterRule.newBuilder().build();
6969

7070
private final ConnectionRule connectionRule =
71-
new ConnectionRule(miniClusterRule::createConnection);
71+
ConnectionRule.createAsyncConnectionRule(miniClusterRule::createAsyncConnection);
7272
private final ClearUserNamespacesAndTablesRule clearUserNamespacesAndTablesRule =
73-
new ClearUserNamespacesAndTablesRule(connectionRule::getConnection);
73+
new ClearUserNamespacesAndTablesRule(connectionRule::getAsyncConnection);
7474

7575
@Rule
7676
public TestRule rule = RuleChain.outerRule(connectionRule)
@@ -84,7 +84,7 @@ public class TestMetaBrowser {
8484

8585
@Before
8686
public void before() {
87-
connection = connectionRule.getConnection();
87+
connection = connectionRule.getAsyncConnection();
8888
admin = connection.getAdmin();
8989
}
9090

0 commit comments

Comments
 (0)