rdblue commented on a change in pull request #1823: URL: https://github.com/apache/iceberg/pull/1823#discussion_r549519934
########## File path: aws/src/main/java/org/apache/iceberg/aws/glue/DynamoLockManager.java ########## @@ -0,0 +1,213 @@ +/* + * 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.List; +import java.util.Map; +import org.apache.iceberg.aws.AwsClientFactories; +import org.apache.iceberg.aws.AwsProperties; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.apache.iceberg.util.Tasks; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import software.amazon.awssdk.services.dynamodb.DynamoDbClient; +import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition; +import software.amazon.awssdk.services.dynamodb.model.AttributeValue; +import software.amazon.awssdk.services.dynamodb.model.BillingMode; +import software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException; +import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest; +import software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest; +import software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest; +import software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse; +import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement; +import software.amazon.awssdk.services.dynamodb.model.KeyType; +import software.amazon.awssdk.services.dynamodb.model.PutItemRequest; +import software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException; +import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType; +import software.amazon.awssdk.services.dynamodb.model.TableStatus; + +/** + * DynamoDB implementation for the lock manager. + */ +class DynamoLockManager implements LockManager { + + private static final Logger LOG = LoggerFactory.getLogger(DynamoLockManager.class); + + private static final String LOCK_TABLE_COL_LOCK_ID = "lockId"; + private static final String LOCK_TABLE_COL_EXPIRE_TS_MILLIS = "expireTimestampMillis"; + private static final int LOCK_TABLE_CREATION_WAIT_ATTEMPTS_MAX = 5; + + private static final List<KeySchemaElement> LOCK_TABLE_SCHEMA = Lists.newArrayList( + KeySchemaElement.builder() + .attributeName(LOCK_TABLE_COL_LOCK_ID) + .keyType(KeyType.HASH) + .build() + ); + + private static final List<AttributeDefinition> LOCK_TABLE_COL_DEFINITIONS = Lists.newArrayList( + AttributeDefinition.builder() + .attributeName(LOCK_TABLE_COL_LOCK_ID) + .attributeType(ScalarAttributeType.S) + .build() + ); + + private DynamoDbClient dynamo; + private String lockTableName; + + /** + * constructor for dynamic initialization, {@link #initialize(Map)} must be called later. + */ + DynamoLockManager() { + } + + /** + * All arg constructor, used for testing purpose + * @param dynamo dynamo client + * @param lockTableName lock table name + */ + DynamoLockManager(DynamoDbClient dynamo, String lockTableName) { + this.dynamo = dynamo; + this.lockTableName = lockTableName; + ensureLockTableExistsOrCreate(); + } + + private void ensureLockTableExistsOrCreate() { + ensureTableExistsOrCreate(lockTableName, LOCK_TABLE_SCHEMA, LOCK_TABLE_COL_DEFINITIONS); + } + + private void ensureTableExistsOrCreate(String tableName, List<KeySchemaElement> schema, + List<AttributeDefinition> definitions) { + if (tableExists(tableName)) { + return; + } + + LOG.info("Dynamo lock table {} not found, trying to create", tableName); + dynamo.createTable(CreateTableRequest.builder() + .tableName(tableName) + .keySchema(schema) + .attributeDefinitions(definitions) + .billingMode(BillingMode.PAY_PER_REQUEST) + .build()); + + Tasks.foreach(tableName) + .retry(LOCK_TABLE_CREATION_WAIT_ATTEMPTS_MAX) + .throwFailureWhenFinished() + .run(this::checkTableActive); + } + + private boolean tableExists(String tableName) { + try { + dynamo.describeTable(DescribeTableRequest.builder() + .tableName(tableName) + .build()); + return true; + } catch (ResourceNotFoundException e) { + return false; + } + } + + private void checkTableActive(String tableName) { + try { + DescribeTableResponse response = dynamo.describeTable(DescribeTableRequest.builder() + .tableName(tableName) + .build()); + TableStatus currentStatus = response.table().tableStatus(); + if (!currentStatus.equals(TableStatus.ACTIVE)) { + throw new IllegalStateException(String.format("Dynamo table %s is not active, current status: %s", + tableName, currentStatus)); + } + } catch (ResourceNotFoundException e) { + throw new IllegalStateException(String.format("Cannot find Dynamo table %s", tableName)); + } + } + + @Override + public void initialize(Map<String, String> properties) { + this.dynamo = AwsClientFactories.from(properties).dynamo(); + this.lockTableName = properties.get(AwsProperties.LOCK_DYNAMO_TABLE); + Preconditions.checkNotNull(lockTableName, "tableName must not be null"); Review comment: It is better to avoid including class, method, or variable names in messages because the user often won't be familiar with this code's variable names. Instead, use "Table name" or "Lock table name" because that's what the user sets. ---------------------------------------------------------------- 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