rdblue commented on a change in pull request #1823: URL: https://github.com/apache/iceberg/pull/1823#discussion_r549518160
########## 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), + AwsProperties.LOCK_EXPIRE_MS_DEFAULT)) + .collect(Collectors.toList()); + Assert.assertEquals(1, results.stream().filter(s -> s).count()); + } + + @Test + public void testWaitLock_singleProcess() throws Exception { + boolean succeed = lockManager.tryLock(tableId(tableName), + AwsProperties.LOCK_EXPIRE_MS_DEFAULT, + AwsProperties.LOCK_WAIT_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 testWaitLock_multiProcess_allSucceed() { + List<Boolean> results = IntStream.range(0, 100).parallel() + .mapToObj(i -> { + try { + return lockManager.tryLock(tableId(tableName), 10, 100000); + } catch (InterruptedException e) { + LOG.error("lock acquire interrupted", e); + return false; + } + }) + .collect(Collectors.toList()); + Assert.assertEquals(100, results.stream().filter(s -> s).count()); + } + + @Test + public void testWaitLock_multiProcess_allFail() { + List<Boolean> results = IntStream.range(0, 100).parallel() + .mapToObj(i -> { + try { + return lockManager.tryLock(tableId(tableName), 1000000, 10); + } catch (InterruptedException e) { + LOG.error("lock acquire interrupted", e); + return false; + } + }) + .collect(Collectors.toList()); + Assert.assertEquals(1, results.stream().filter(s -> s).count()); + } + + + @Test + public void testUnlock() { + 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()); + dynamo.deleteItem(DeleteItemRequest.builder() + .tableName(lockTableName) + .key(key) + .build()); + succeed = lockManager.tryLock(tableId(tableName), + AwsProperties.LOCK_EXPIRE_MS_DEFAULT); + Assert.assertTrue(succeed); + } + + @Test + public void testTableCreationFailure() { + DynamoDbClient dynamo2 = Mockito.mock(DynamoDbClient.class); + Mockito.doThrow(ResourceNotFoundException.class).when(dynamo2) + .describeTable(Mockito.any(DescribeTableRequest.class)); + AssertHelpers.assertThrows("should fail to initialize the lock manager", + IllegalStateException.class, + "Cannot find Dynamo table", + () -> new DynamoLockManager(dynamo2, lockTableName)); + } + + private static String tableId(String tableName) { + return String.format("%s.%s", DATABASE, tableName); + } + + private static String genTableName() { + return UUID.randomUUID().toString().replace("-", ""); Review comment: Why use `replace` here? Isn't it better if the value is easily identifiable as a UUID? ---------------------------------------------------------------- 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