Skip to content

Commit

Permalink
Properly close Glue client in tests
Browse files Browse the repository at this point in the history
Fix potential resource leak during tests' execution.
  • Loading branch information
findepi committed May 13, 2024
1 parent 1c6c7ef commit d0a716f
Show file tree
Hide file tree
Showing 13 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public class TestDeltaLakeSharedGlueMetastoreViews
@Override
protected HiveMetastore createTestMetastore(Path dataDirectory)
{
return createTestingGlueHiveMetastore(dataDirectory);
return createTestingGlueHiveMetastore(dataDirectory, this::closeAfterClass);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected QueryRunner createQueryRunner()
.put("delta.hive-catalog-name", "hive_with_redirections")
.buildOrThrow());

this.glueMetastore = createTestingGlueHiveMetastore(dataDirectory);
this.glueMetastore = createTestingGlueHiveMetastore(dataDirectory, this::closeAfterClass);
queryRunner.installPlugin(new TestingHivePlugin(queryRunner.getCoordinator().getBaseDataDir().resolve("hive_data"), glueMetastore));
queryRunner.createCatalog(
"hive_with_redirections",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected QueryRunner createQueryRunner()
{
Path warehouseDir = Files.createTempDirectory("warehouse-dir");
closeAfterClass(() -> deleteRecursively(warehouseDir, ALLOW_INSECURE));
metastore = createTestingGlueHiveMetastore(warehouseDir);
metastore = createTestingGlueHiveMetastore(warehouseDir, this::closeAfterClass);
schema = "test_tables_with_custom_location" + randomNameSuffix();
return DeltaLakeQueryRunner.builder(schema)
.addDeltaProperty("hive.metastore", "glue")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected QueryRunner createQueryRunner()
GlueHiveMetastoreConfig glueConfig = new GlueHiveMetastoreConfig()
.setDefaultWarehouseDir(dataDirectory.toUri().toString());

GlueClient glueClient = createGlueClient(new GlueHiveMetastoreConfig(), OpenTelemetry.noop());
GlueClient glueClient = closeAfterClass(createGlueClient(new GlueHiveMetastoreConfig(), OpenTelemetry.noop()));
GlueClient proxiedGlueClient = newProxy(GlueClient.class, (proxy, method, args) -> {
Object result;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected QueryRunner createQueryRunner()
{
Path warehouseDir = Files.createTempDirectory("warehouse-dir");
closeAfterClass(() -> deleteRecursively(warehouseDir, ALLOW_INSECURE));
metastore = createTestingGlueHiveMetastore(warehouseDir);
metastore = createTestingGlueHiveMetastore(warehouseDir, this::closeAfterClass);
schema = "test_delta_lake_register_table" + randomNameSuffix();
return DeltaLakeQueryRunner.builder(schema)
.addDeltaProperty("hive.metastore", "glue")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected QueryRunner createQueryRunner()
{
Path warehouseDir = Files.createTempDirectory("warehouse-dir");
closeAfterClass(() -> deleteRecursively(warehouseDir, ALLOW_INSECURE));
metastore = createTestingGlueHiveMetastore(warehouseDir);
metastore = createTestingGlueHiveMetastore(warehouseDir, this::closeAfterClass);
schema = "test_delta_lake_glue_views_" + randomNameSuffix();
return DeltaLakeQueryRunner.builder(schema)
.addDeltaProperty("hive.metastore", "glue")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public TestDeltaS3AndGlueMetastoreTest()
protected QueryRunner createQueryRunner()
throws Exception
{
metastore = createTestingGlueHiveMetastore(URI.create(schemaPath()));
metastore = createTestingGlueHiveMetastore(URI.create(schemaPath()), this::closeAfterClass);
return DeltaLakeQueryRunner.builder(schemaName)
.setDeltaProperties(ImmutableMap.<String, String>builder()
.put("hive.metastore", "glue")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected QueryRunner createQueryRunner()
GlueHiveMetastoreConfig glueConfig = new GlueHiveMetastoreConfig()
.setDefaultWarehouseDir(dataDirectory.toUri().toString());

GlueClient glueClient = createGlueClient(new GlueHiveMetastoreConfig(), OpenTelemetry.noop());
GlueClient glueClient = closeAfterClass(createGlueClient(new GlueHiveMetastoreConfig(), OpenTelemetry.noop()));
GlueClient proxiedGlueClient = newProxy(GlueClient.class, (proxy, method, args) -> {
try {
if (method.getName().equals("updateTable")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package io.trino.plugin.hive.metastore;

import io.trino.plugin.base.util.AutoCloseableCloser;
import io.trino.plugin.hive.metastore.glue.GlueHiveMetastore;
import org.junit.jupiter.api.AfterAll;

Expand All @@ -27,22 +28,24 @@
final class TestGlueHiveMetastore
extends AbstractTestHiveMetastore
{
private final AutoCloseableCloser closer = AutoCloseableCloser.create();
private final Path tempDir;
private final GlueHiveMetastore metastore;

TestGlueHiveMetastore()
throws IOException
{
tempDir = createTempDirectory("test");
metastore = createTestingGlueHiveMetastore(tempDir);
metastore = createTestingGlueHiveMetastore(tempDir, closer::register);
}

@AfterAll
void tearDown()
throws IOException
throws Exception
{
metastore.shutdown();
deleteRecursively(tempDir, ALLOW_INSECURE);
closer.close();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@

import io.opentelemetry.api.OpenTelemetry;
import io.trino.plugin.hive.metastore.glue.GlueHiveMetastore.TableKind;
import software.amazon.awssdk.services.glue.GlueClient;

import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
import java.util.EnumSet;
import java.util.function.Consumer;

import static com.google.common.base.Verify.verify;
import static io.trino.plugin.hive.HiveTestUtils.HDFS_FILE_SYSTEM_FACTORY;
Expand All @@ -32,7 +34,7 @@ public final class TestingGlueHiveMetastore
{
private TestingGlueHiveMetastore() {}

public static GlueHiveMetastore createTestingGlueHiveMetastore(Path defaultWarehouseDir)
public static GlueHiveMetastore createTestingGlueHiveMetastore(Path defaultWarehouseDir, Consumer<AutoCloseable> registerResource)
{
if (!exists(defaultWarehouseDir)) {
try {
Expand All @@ -43,15 +45,17 @@ public static GlueHiveMetastore createTestingGlueHiveMetastore(Path defaultWareh
}
}
verify(isDirectory(defaultWarehouseDir), "%s is not a directory", defaultWarehouseDir);
return createTestingGlueHiveMetastore(defaultWarehouseDir.toUri());
return createTestingGlueHiveMetastore(defaultWarehouseDir.toUri(), registerResource);
}

public static GlueHiveMetastore createTestingGlueHiveMetastore(URI warehouseUri)
public static GlueHiveMetastore createTestingGlueHiveMetastore(URI warehouseUri, Consumer<AutoCloseable> registerResource)
{
GlueHiveMetastoreConfig glueConfig = new GlueHiveMetastoreConfig()
.setDefaultWarehouseDir(warehouseUri.toString());
GlueClient glueClient = createGlueClient(glueConfig, OpenTelemetry.noop());
registerResource.accept(glueClient);
return new GlueHiveMetastore(
createGlueClient(glueConfig, OpenTelemetry.noop()),
glueClient,
new GlueContext(glueConfig),
GlueCache.NOOP,
HDFS_FILE_SYSTEM_FACTORY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected QueryRunner createQueryRunner()
queryRunner.installPlugin(new TestingIcebergPlugin(dataDirectory, Optional.of(new TestingIcebergGlueCatalogModule(awsGlueAsyncAdapterProvider))));
queryRunner.createCatalog(ICEBERG_CATALOG, "iceberg", ImmutableMap.of());

glueHiveMetastore = createTestingGlueHiveMetastore(dataDirectory);
glueHiveMetastore = createTestingGlueHiveMetastore(dataDirectory, this::closeAfterClass);

Database database = Database.builder()
.setDatabaseName(schemaName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public TestIcebergS3AndGlueMetastoreTest()
protected QueryRunner createQueryRunner()
throws Exception
{
metastore = createTestingGlueHiveMetastore(URI.create(schemaPath()));
metastore = createTestingGlueHiveMetastore(URI.create(schemaPath()), this::closeAfterClass);
return IcebergQueryRunner.builder()
.setIcebergProperties(ImmutableMap.<String, String>builder()
.put("iceberg.catalog.type", "glue")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected QueryRunner createQueryRunner()
"hive.metastore.glue.default-warehouse-dir", dataDirectory.toString(),
"iceberg.hive-catalog-name", "hive"));

this.glueMetastore = createTestingGlueHiveMetastore(dataDirectory);
this.glueMetastore = createTestingGlueHiveMetastore(dataDirectory, this::closeAfterClass);
queryRunner.installPlugin(new TestingHivePlugin(queryRunner.getCoordinator().getBaseDataDir().resolve("hive_data"), glueMetastore));
queryRunner.createCatalog(HIVE_CATALOG, "hive");
queryRunner.createCatalog(
Expand Down

0 comments on commit d0a716f

Please sign in to comment.