Skip to content

Commit

Permalink
Add HS2Container
Browse files Browse the repository at this point in the history
  • Loading branch information
linghengqian committed Nov 30, 2024
1 parent 2fff316 commit 8cecb18
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
if: github.repository == 'linghengqian/hive-server2-jdbc-driver'
strategy:
matrix:
java: [ '8', '23' ]
java: [ '17', '23' ]
os: [ 'ubuntu-latest' ]
runs-on: ${{ matrix.os }}
steps:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.github.linghengqian.hive.server2.jdbc.driver.thin;

import com.github.dockerjava.api.command.InspectContainerResponse;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.images.builder.Transferable;

import java.io.IOException;
import java.net.ServerSocket;

@SuppressWarnings({"resource", "OctalInteger"})
public class HS2Container extends GenericContainer<HS2Container> {
private static final String STARTER_SCRIPT = "/testcontainers_start.sh";
private final int randomPortFirst = getRandomPort();

public HS2Container(final String dockerImageName) {
super(dockerImageName);
withEnv("SERVICE_NAME", "hiveserver2");
withExposedPorts(randomPortFirst);
withCreateContainerCmdModifier(cmd ->
cmd.withEntrypoint("sh", "-c", "while [ ! -f " + STARTER_SCRIPT + " ]; do sleep 0.1; done; " + STARTER_SCRIPT)
);
}

@Override
protected void containerIsStarting(InspectContainerResponse containerInfo) {
Integer mappedPort = getMappedPort(randomPortFirst);
String command = """
#!/bin/bash
export SERVICE_OPTS='-Dhive.server2.support.dynamic.service.discovery=true -Dhive.zookeeper.quorum=foo:2181 -Dhive.server2.thrift.bind.host=0.0.0.0 -Dhive.server2.thrift.port=%s'
/usr/local/bin/docker-entrypoint.sh
""".formatted(mappedPort);
copyFileToContainer(Transferable.of(command, 0777), STARTER_SCRIPT);
}

private int getRandomPort() {
try (ServerSocket server = new ServerSocket(0)) {
server.setReuseAddress(true);
return server.getLocalPort();
} catch (IOException exception) {
throw new Error(exception);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import org.testcontainers.junit.jupiter.Testcontainers;

import javax.sql.DataSource;
import java.io.IOException;
import java.net.ServerSocket;
import java.sql.*;
import java.time.Duration;
import java.util.List;
Expand Down Expand Up @@ -63,35 +61,14 @@ static void afterAll() {

@Test
void assertShardingInLocalTransactions() throws SQLException {
int randomPortFirst = getRandomPort();
GenericContainer<?> hs2FirstContainer = new GenericContainer<>("apache/hive:4.0.1")
GenericContainer<?> hs2FirstContainer = new HS2Container("apache/hive:4.0.1")
.withNetwork(NETWORK)
.withEnv("SERVICE_NAME", "hiveserver2")
.withExposedPorts(randomPortFirst)
.dependsOn(ZOOKEEPER_CONTAINER);
hs2FirstContainer.withEnv("SERVICE_OPTS", "-Dhive.server2.support.dynamic.service.discovery=true" + " "
+ "-Dhive.zookeeper.quorum=" + ZOOKEEPER_CONTAINER.getNetworkAliases().get(0) + ":2181" + " "
+ "-Dhive.server2.thrift.bind.host=0.0.0.0" + " "
+ "-Dhive.server2.thrift.port=" + hs2FirstContainer.getMappedPort(randomPortFirst));
hs2FirstContainer.start();
awaitHS2(hs2FirstContainer.getMappedPort(randomPortFirst));
awaitHS2(hs2FirstContainer.getFirstMappedPort());
DataSource dataSource = createDataSource();
extractedSQL(dataSource);
hs2FirstContainer.stop();
int randomPortSecond = getRandomPort();
GenericContainer<?> hs2SecondContainer = new GenericContainer<>("apache/hive:4.0.1")
.withNetwork(NETWORK)
.withEnv("SERVICE_NAME", "hiveserver2")
.withExposedPorts(randomPortSecond)
.dependsOn(ZOOKEEPER_CONTAINER);
hs2SecondContainer.withEnv("SERVICE_OPTS", "-Dhive.server2.support.dynamic.service.discovery=true" + " "
+ "-Dhive.zookeeper.quorum=" + ZOOKEEPER_CONTAINER.getNetworkAliases().get(0) + ":2181" + " "
+ "-Dhive.server2.thrift.bind.host=0.0.0.0" + " "
+ "-Dhive.server2.thrift.port=" + hs2SecondContainer.getMappedPort(randomPortSecond));
hs2SecondContainer.start();
awaitHS2(hs2SecondContainer.getMappedPort(randomPortSecond));
extractedSQL(dataSource);
hs2SecondContainer.stop();
}

private DataSource createDataSource() {
Expand Down Expand Up @@ -142,13 +119,4 @@ private void awaitHS2(final int hiveServer2Port) {
return true;
});
}

private int getRandomPort() {
try (ServerSocket server = new ServerSocket(0)) {
server.setReuseAddress(true);
return server.getLocalPort();
} catch (IOException exception) {
throw new Error(exception);
}
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
</scm>

<properties>
<maven.compiler.release>8</maven.compiler.release>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<apache-hive-jdbc.version>4.0.1</apache-hive-jdbc.version>
Expand Down

0 comments on commit 8cecb18

Please sign in to comment.