-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add cleanup mechanism for Iceberg Glue tests #11903
Closed
+205
−8
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
plugin/trino-iceberg/src/test/java/io/trino/plugin/iceberg/catalog/glue/TestGlueCleanup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* 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.iceberg.catalog.glue; | ||
|
||
import com.amazonaws.services.glue.AWSGlueAsync; | ||
import com.amazonaws.services.glue.AWSGlueAsyncClientBuilder; | ||
import com.amazonaws.services.glue.model.Database; | ||
import com.amazonaws.services.glue.model.DatabaseInput; | ||
import com.amazonaws.services.glue.model.DeleteDatabaseRequest; | ||
import com.amazonaws.services.glue.model.GetDatabaseRequest; | ||
import com.amazonaws.services.glue.model.GetDatabasesRequest; | ||
import com.amazonaws.services.glue.model.GetDatabasesResult; | ||
import com.amazonaws.services.glue.model.UpdateDatabaseRequest; | ||
import com.google.common.collect.ImmutableMap; | ||
import io.airlift.log.Logger; | ||
import io.trino.plugin.hive.metastore.glue.GlueMetastoreApiStats; | ||
import org.testng.annotations.Test; | ||
|
||
import java.time.Duration; | ||
import java.util.Collection; | ||
import java.util.Date; | ||
|
||
import static io.trino.plugin.hive.metastore.glue.AwsSdkUtil.getPaginatedResults; | ||
|
||
/** | ||
* Test class with methods to clean up existing Glue tables and schemas from test which were not finished properly. | ||
*/ | ||
public class TestGlueCleanup | ||
{ | ||
// All tests creating Glue schemas should add this key/value pair to the schema properties. | ||
public static final String GLUE_SCHEMA_PROPERTY_KEY = "ci_iceberg_integration_test"; | ||
public static final String GLUE_SCHEMA_PROPERTY_VALUE = "true"; | ||
|
||
private static final Logger LOG = Logger.get(TestGlueCleanup.class); | ||
|
||
public static void setSchemaFlag(String schemaName) | ||
{ | ||
AWSGlueAsync glueClient = AWSGlueAsyncClientBuilder.defaultClient(); | ||
Database database = glueClient.getDatabase(new GetDatabaseRequest().withName(schemaName)).getDatabase(); | ||
ImmutableMap.Builder<String, String> schemaParameters = ImmutableMap.builder(); | ||
if (database.getParameters() != null) { | ||
schemaParameters.putAll(database.getParameters()); | ||
} | ||
schemaParameters.put(GLUE_SCHEMA_PROPERTY_KEY, GLUE_SCHEMA_PROPERTY_VALUE); | ||
|
||
DatabaseInput databaseInput = new DatabaseInput() | ||
.withName(schemaName) | ||
.withDescription(database.getDescription()) | ||
.withLocationUri(database.getLocationUri()) | ||
.withParameters(schemaParameters.buildOrThrow()) | ||
.withCreateTableDefaultPermissions(database.getCreateTableDefaultPermissions()) | ||
.withTargetDatabase(database.getTargetDatabase()); | ||
glueClient.updateDatabase(new UpdateDatabaseRequest() | ||
.withName(schemaName) | ||
.withDatabaseInput(databaseInput)); | ||
} | ||
|
||
@Test | ||
public void cleanupOrphanedSchemas() | ||
{ | ||
AWSGlueAsync glueClient = AWSGlueAsyncClientBuilder.defaultClient(); | ||
long createdAtCutoff = System.currentTimeMillis() - Duration.ofDays(1).toMillis(); | ||
getPaginatedResults( | ||
glueClient::getDatabases, | ||
new GetDatabasesRequest(), | ||
GetDatabasesRequest::setNextToken, | ||
GetDatabasesResult::getNextToken, | ||
new GlueMetastoreApiStats()) | ||
.map(GetDatabasesResult::getDatabaseList) | ||
.flatMap(Collection::stream) | ||
.filter(database -> database.getParameters() != null && GLUE_SCHEMA_PROPERTY_VALUE.equals(database.getParameters().get(GLUE_SCHEMA_PROPERTY_KEY))) | ||
.filter(database -> database.getCreateTime().before(new Date(createdAtCutoff))) | ||
.forEach(database -> { | ||
LOG.warn("Deleting old Glue database " + database); | ||
glueClient.deleteDatabase(new DeleteDatabaseRequest().withName(database.getName())); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find the property-based approach clever and neat.
I am concerned however that some new tests will forget to set it. Also, it's less obvious than a name. Simple persons like me will notice a name, but won't notice the propery.
I would rather have it based on schema name only.
It is not a hard ask to require each test schema name to begin with
test_
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about that, but was worried about deleting schemas that might be used for other things. I could see there being
test_*
schemas that are supposed to be long-lived, but I don't know what else might be in the CI's AWS account.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CI must run on an isolated account.
tests may erroneously delete something, so you won't let tests run where you keep anything precious.
i agree
ci_
orci_test_
would be a better prefix, but we have already existing cases of usingtest_
and am not worried about enforcing this in a generic fashion.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I agree that you shouldn't run tests anywhere with precious databases, but when writing the tests I ran them against my personal AWS account, so I guess I wrote this with the same level of care as I would treat that account.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use your company's testing account when working locally. this will allow the test code to be simpler, and your personal AWS account to be safe. Still, removing
test_
schemas isn't "very destructive" even in a personal account.