Skip to content

Commit

Permalink
Cleanup Glue databases left eventually behind by other test runs
Browse files Browse the repository at this point in the history
Co-authored-by: Marius Grama <findinpath@gmail.com>
  • Loading branch information
2 people authored and findepi committed Apr 27, 2022
1 parent 940b6d9 commit 2ccf3ad
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
2 changes: 2 additions & 0 deletions plugin/trino-delta-lake/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@
<exclude>**/TestDeltaLakeAdlsStorage.java</exclude>
<exclude>**/TestDeltaLakeAdlsConnectorSmokeTest.java</exclude>
<exclude>**/TestDeltaLakeGlueMetastore.java</exclude>
<exclude>**/TestDeltaLakeCleanUpGlueMetastore.java</exclude>
<exclude>**/TestDelta*FailureRecoveryTest.java</exclude>
</excludes>
</configuration>
Expand Down Expand Up @@ -442,6 +443,7 @@
<include>**/TestDeltaLakeAdlsStorage.java</include>
<include>**/TestDeltaLakeAdlsConnectorSmokeTest.java</include>
<include>**/TestDeltaLakeGlueMetastore.java</include>
<include>**/TestDeltaLakeCleanUpGlueMetastore.java</include>
</includes>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Licensed 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 io.trino.plugin.deltalake.metastore.glue;

import com.amazonaws.services.glue.AWSGlueAsync;
import com.amazonaws.services.glue.AWSGlueAsyncClientBuilder;
import com.amazonaws.services.glue.model.DeleteDatabaseRequest;
import com.amazonaws.services.glue.model.EntityNotFoundException;
import com.amazonaws.services.glue.model.GetDatabasesRequest;
import com.amazonaws.services.glue.model.GetDatabasesResult;
import io.airlift.log.Logger;
import io.trino.plugin.hive.metastore.glue.GlueMetastoreApiStats;
import org.testng.annotations.Test;

import java.util.List;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.trino.plugin.hive.metastore.glue.AwsSdkUtil.getPaginatedResults;
import static java.lang.System.currentTimeMillis;
import static java.util.concurrent.TimeUnit.DAYS;

public class TestDeltaLakeCleanUpGlueMetastore
{
private static final Logger log = Logger.get(TestDeltaLakeCleanUpGlueMetastore.class);

private static final String TEST_DATABASE_NAME_PREFIX = "test_";

@Test
public void cleanupOrphanedDatabases()
{
AWSGlueAsync glueClient = AWSGlueAsyncClientBuilder.defaultClient();
long creationTimeMillisThreshold = currentTimeMillis() - DAYS.toMillis(1);
List<String> orphanedDatabases = getPaginatedResults(
glueClient::getDatabases,
new GetDatabasesRequest(),
GetDatabasesRequest::setNextToken,
GetDatabasesResult::getNextToken,
new GlueMetastoreApiStats())
.map(GetDatabasesResult::getDatabaseList)
.flatMap(List::stream)
.filter(glueDatabase -> glueDatabase.getName().startsWith(TEST_DATABASE_NAME_PREFIX) &&
glueDatabase.getCreateTime().getTime() <= creationTimeMillisThreshold)
.map(com.amazonaws.services.glue.model.Database::getName)
.collect(toImmutableList());

if (!orphanedDatabases.isEmpty()) {
log.info("Found %s %s* databases that look orphaned, removing", orphanedDatabases.size(), TEST_DATABASE_NAME_PREFIX);
orphanedDatabases.forEach(database -> {
try {
log.info("Deleting %s database", database);
glueClient.deleteDatabase(new DeleteDatabaseRequest()
.withName(database));
}
catch (EntityNotFoundException e) {
log.info("Database [%s] not found, could be removed by other cleanup process", database);
}
catch (RuntimeException e) {
log.warn(e, "Failed to remove database [%s]", database);
}
});
}
}
}

0 comments on commit 2ccf3ad

Please sign in to comment.