rdblue commented on a change in pull request #1633: URL: https://github.com/apache/iceberg/pull/1633#discussion_r525648235
########## File path: aws/src/main/java/org/apache/iceberg/aws/glue/GlueTableOperations.java ########## @@ -0,0 +1,198 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.iceberg.aws.glue; + +import java.util.Locale; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import org.apache.iceberg.BaseMetastoreTableOperations; +import org.apache.iceberg.TableMetadata; +import org.apache.iceberg.aws.AwsProperties; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.exceptions.AlreadyExistsException; +import org.apache.iceberg.exceptions.CommitFailedException; +import org.apache.iceberg.exceptions.NoSuchTableException; +import org.apache.iceberg.io.FileIO; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import software.amazon.awssdk.core.exception.SdkException; +import software.amazon.awssdk.services.glue.GlueClient; +import software.amazon.awssdk.services.glue.model.ConcurrentModificationException; +import software.amazon.awssdk.services.glue.model.CreateTableRequest; +import software.amazon.awssdk.services.glue.model.EntityNotFoundException; +import software.amazon.awssdk.services.glue.model.GetTableRequest; +import software.amazon.awssdk.services.glue.model.GetTableResponse; +import software.amazon.awssdk.services.glue.model.Table; +import software.amazon.awssdk.services.glue.model.TableInput; +import software.amazon.awssdk.services.glue.model.UpdateTableRequest; + +class GlueTableOperations extends BaseMetastoreTableOperations { + + private static final Logger LOG = LoggerFactory.getLogger(GlueTableOperations.class); + + // same as org.apache.hadoop.hive.metastore.TableType.EXTERNAL_TABLE + // more details: https://docs.aws.amazon.com/glue/latest/webapi/API_TableInput.html + private static final String GLUE_EXTERNAL_TABLE_TYPE = "EXTERNAL_TABLE"; + + private final GlueClient glue; + private final AwsProperties awsProperties; + private final String databaseName; + private final String tableName; + private final String fullTableName; + private final FileIO fileIO; + + GlueTableOperations(GlueClient glue, String catalogName, AwsProperties awsProperties, + FileIO fileIO, TableIdentifier tableIdentifier) { + this.glue = glue; + this.awsProperties = awsProperties; + this.databaseName = IcebergToGlueConverter.getDatabaseName(tableIdentifier); + this.tableName = IcebergToGlueConverter.getTableName(tableIdentifier); + this.fullTableName = String.format("%s.%s.%s", catalogName, databaseName, tableName); + this.fileIO = fileIO; + } + + @Override + public FileIO io() { + return fileIO; + } + + @Override + protected String tableName() { + return fullTableName; + } + + @Override + protected void doRefresh() { + String metadataLocation = null; + Optional<Table> tableOptional = getGlueTable(); + if (tableOptional.isPresent()) { + Table table = tableOptional.get(); + GlueToIcebergConverter.validateTable(table, tableName()); + metadataLocation = table.parameters().get(METADATA_LOCATION_PROP); + } else { + if (currentMetadataLocation() != null) { + throw new NoSuchTableException("Cannot find Glue table %s after refresh, " + + "maybe another process deleted it or revoked your access permission", tableName()); + } + } + + refreshFromMetadataLocation(metadataLocation); + } + + @Override + protected void doCommit(TableMetadata base, TableMetadata metadata) { + String newMetadataLocation = writeNewMetadata(metadata, currentVersion() + 1); + boolean exceptionThrown = true; + boolean isUpdate = false; + Table glueTable = null; + try { + Optional<Table> glueTableOptional = getGlueTable(); + if (glueTableOptional.isPresent()) { + glueTable = glueTableOptional.get(); + isUpdate = true; + // If we try to create the table but the metadata location is already set, then we had a concurrent commit + if (base == null && glueTable.parameters().get(METADATA_LOCATION_PROP) != null) { + throw new AlreadyExistsException("Cannot commit because table %s already exists in Glue", tableName()); + } + } + + checkMetadataLocation(isUpdate, glueTable, base); + Map<String, String> properties = prepareProperties(isUpdate, glueTable, newMetadataLocation); + persistGlueTable(isUpdate, properties); + exceptionThrown = false; + } catch (CommitFailedException | AlreadyExistsException e) { + throw e; Review comment: I don't think these exceptions are caught by other blocks, so you should be able to just remove this. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org