rdblue commented on a change in pull request #1823: URL: https://github.com/apache/iceberg/pull/1823#discussion_r549521566
########## File path: aws/src/integration/java/org/apache/iceberg/aws/glue/DynamoLockManagerTest.java ########## @@ -0,0 +1,199 @@ +/* + * 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 java.util.UUID; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import org.apache.iceberg.AssertHelpers; +import org.apache.iceberg.aws.AwsClientFactories; +import org.apache.iceberg.aws.AwsProperties; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.Mockito; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import software.amazon.awssdk.services.dynamodb.DynamoDbClient; +import software.amazon.awssdk.services.dynamodb.model.AttributeValue; +import software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest; +import software.amazon.awssdk.services.dynamodb.model.DeleteTableRequest; +import software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest; +import software.amazon.awssdk.services.dynamodb.model.GetItemRequest; +import software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException; + +public class DynamoLockManagerTest { + + private static final Logger LOG = LoggerFactory.getLogger(LockManagerTest.class); + + private static final String DATABASE = "database"; + + private static String lockTableName; + private static DynamoDbClient dynamo; + + private LockManager lockManager; + private String tableName; + + @BeforeClass + public static void beforeClass() { + lockTableName = genTableName(); + dynamo = AwsClientFactories.defaultFactory().dynamo(); + } + + @Before + public void before() { + lockManager = new DynamoLockManager(dynamo, lockTableName); + tableName = genTableName(); + } + + @AfterClass + public static void afterClass() { + dynamo.deleteTable(DeleteTableRequest.builder().tableName(lockTableName).build()); + } + + @Test + public void testTableCreation() { + try { + dynamo.describeTable(DescribeTableRequest.builder() + .tableName(lockTableName) + .build()); + } catch (Exception e) { + LOG.error("encountered failure", e); + Assert.fail("Should not throw exception when describing DynamoDB lock table"); + } + } + + @Test + public void testLock_singleProcess() { + boolean succeed = lockManager.tryLock(tableId(tableName), + AwsProperties.LOCK_EXPIRE_MS_DEFAULT); + Assert.assertTrue(succeed); + Map<String, AttributeValue> key = Maps.newHashMap(); + key.put("lockId", AttributeValue.builder().s(tableId(tableName)).build()); + try { + dynamo.getItem(GetItemRequest.builder() + .tableName(lockTableName) + .key(key) + .build()); + } catch (Exception e) { + LOG.error("encountered failure", e); + Assert.fail("Should not throw exception when getting lock after insert"); + } + } + + @Test + public void testLock_multiProcess() { + List<Boolean> results = IntStream.range(0, 100).parallel() + .mapToObj(i -> lockManager.tryLock(tableId(tableName), Review comment: I think this should create different lock managers to simulate multiple processes, not use the same one. ---------------------------------------------------------------- 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: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
