-
Notifications
You must be signed in to change notification settings - Fork 11
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
Issue 1415 - DynamoDB store for table names & IDs #1418
Merged
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
774934b
Set up basic structure for table ID store
patchwork01 0bd717c
Fail to create a table which already exists
patchwork01 91fd75e
Generate numeric table IDs
patchwork01 0923d72
Add methods to look up tables
patchwork01 e4f0c00
Test returned table order
patchwork01 d3afb1e
Reorder tests
patchwork01 eab21f7
Declare TableAlreadyExistsException on interface
patchwork01 0f28816
Handle lookup when no table is found
patchwork01 9fb1314
Organise table ID tests into nested classes
patchwork01 0daf37b
Test listing no tables
patchwork01 ad20d84
Set up DynamoDBTableIdStore
patchwork01 c23f760
Finish test for creating sleeper table
kr565370 23f4ab6
Test for failing to add duplicate table
kr565370 4b8ab79
Tests for looking up tables
kr565370 6f75a0b
Finish tests in DynamoDBTableIdStoreIT
kr565370 313cd5c
Generate a short table ID
patchwork01 4ce88ee
Rename transaction cancellation reason variable
patchwork01 14b95cc
Rename TableIdStore to TableIndex
patchwork01 53bdfce
Rename DynamoDB table index package
patchwork01 01bf39a
Add TableIndexStack to CDK
patchwork01 5031970
Regenerate properties templates
patchwork01 cc105b6
Merge remote-tracking branch 'origin/main' into 1415-table-name-store
patchwork01 980fabe
Fix merge
patchwork01 0e6fa25
Tweak naming to distinguish Dynamo tables from Sleeper tables
patchwork01 7e11268
Test generating different IDs from same generator
patchwork01 d13bedb
Rename tableId field to tableUniqueId
patchwork01 fd1112b
Merge remote-tracking branch 'origin/main' into 1415-table-name-store
patchwork01 702ee15
Fix merge
patchwork01 070ccd4
Build table ID DynamoDB item once
patchwork01 a335074
Use consistent reads in DynamoDBTableIndex.streamAllTables
patchwork01 1d3b1c6
Merge remote-tracking branch 'origin/main' into 1415-table-name-store
patchwork01 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
81 changes: 81 additions & 0 deletions
81
java/cdk/src/main/java/sleeper/cdk/stack/TableIndexStack.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,81 @@ | ||
/* | ||
* Copyright 2022-2023 Crown Copyright | ||
* | ||
* 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 sleeper.cdk.stack; | ||
|
||
import software.amazon.awscdk.NestedStack; | ||
import software.amazon.awscdk.RemovalPolicy; | ||
import software.amazon.awscdk.services.dynamodb.Attribute; | ||
import software.amazon.awscdk.services.dynamodb.AttributeType; | ||
import software.amazon.awscdk.services.dynamodb.BillingMode; | ||
import software.amazon.awscdk.services.dynamodb.ITable; | ||
import software.amazon.awscdk.services.dynamodb.Table; | ||
import software.constructs.Construct; | ||
|
||
import sleeper.configuration.properties.instance.InstanceProperties; | ||
import sleeper.table.index.dynamodb.DynamoDBTableIndex; | ||
|
||
import static sleeper.cdk.Utils.removalPolicy; | ||
import static sleeper.configuration.properties.instance.CdkDefinedInstanceProperty.TABLE_ID_INDEX_DYNAMO_TABLENAME; | ||
import static sleeper.configuration.properties.instance.CdkDefinedInstanceProperty.TABLE_NAME_INDEX_DYNAMO_TABLENAME; | ||
import static sleeper.configuration.properties.instance.CommonProperty.ID; | ||
import static sleeper.configuration.properties.instance.CommonProperty.TABLE_INDEX_DYNAMO_POINT_IN_TIME_RECOVERY; | ||
|
||
public class TableIndexStack extends NestedStack { | ||
|
||
private final ITable indexByNameDynamoTable; | ||
private final ITable indexByIdDynamoTable; | ||
|
||
public TableIndexStack(Construct scope, String id, InstanceProperties instanceProperties) { | ||
super(scope, id); | ||
String instanceId = instanceProperties.get(ID); | ||
RemovalPolicy removalPolicy = removalPolicy(instanceProperties); | ||
|
||
indexByNameDynamoTable = Table.Builder | ||
.create(this, "IndexByNameDynamoDBTable") | ||
.tableName(String.join("-", "sleeper", instanceId, "table-index-by-name")) | ||
.removalPolicy(removalPolicy) | ||
.billingMode(BillingMode.PAY_PER_REQUEST) | ||
.partitionKey(Attribute.builder() | ||
.name(DynamoDBTableIndex.TABLE_NAME_FIELD) | ||
.type(AttributeType.STRING) | ||
.build()) | ||
.pointInTimeRecovery(instanceProperties.getBoolean(TABLE_INDEX_DYNAMO_POINT_IN_TIME_RECOVERY)) | ||
.build(); | ||
instanceProperties.set(TABLE_NAME_INDEX_DYNAMO_TABLENAME, indexByNameDynamoTable.getTableName()); | ||
|
||
indexByIdDynamoTable = Table.Builder | ||
.create(this, "IndexByIdDynamoDBTable") | ||
.tableName(String.join("-", "sleeper", instanceId, "table-index-by-id")) | ||
.removalPolicy(removalPolicy) | ||
.billingMode(BillingMode.PAY_PER_REQUEST) | ||
.partitionKey(Attribute.builder() | ||
.name(DynamoDBTableIndex.TABLE_ID_FIELD) | ||
.type(AttributeType.STRING) | ||
.build()) | ||
.pointInTimeRecovery(instanceProperties.getBoolean(TABLE_INDEX_DYNAMO_POINT_IN_TIME_RECOVERY)) | ||
.build(); | ||
instanceProperties.set(TABLE_ID_INDEX_DYNAMO_TABLENAME, indexByIdDynamoTable.getTableName()); | ||
} | ||
|
||
public ITable getIndexByNameDynamoTable() { | ||
return indexByNameDynamoTable; | ||
} | ||
|
||
public ITable getIndexByIdDynamoTable() { | ||
return indexByIdDynamoTable; | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
java/core/src/main/java/sleeper/core/table/TableAlreadyExistsException.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,24 @@ | ||
/* | ||
* Copyright 2022-2023 Crown Copyright | ||
* | ||
* 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 sleeper.core.table; | ||
|
||
public class TableAlreadyExistsException extends RuntimeException { | ||
|
||
public TableAlreadyExistsException(String tableName) { | ||
super("Table already exists: " + tableName); | ||
} | ||
} |
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,67 @@ | ||
/* | ||
* Copyright 2022-2023 Crown Copyright | ||
* | ||
* 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 sleeper.core.table; | ||
|
||
import java.util.Objects; | ||
|
||
public class TableId { | ||
|
||
private final String tableUniqueId; | ||
private final String tableName; | ||
|
||
private TableId(String tableUniqueId, String tableName) { | ||
this.tableUniqueId = tableUniqueId; | ||
this.tableName = tableName; | ||
} | ||
|
||
public static TableId uniqueIdAndName(String tableUniqueId, String tableName) { | ||
return new TableId(tableUniqueId, tableName); | ||
} | ||
|
||
public String getTableName() { | ||
return tableName; | ||
} | ||
|
||
public String getTableUniqueId() { | ||
return tableUniqueId; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (this == object) { | ||
return true; | ||
} | ||
if (object == null || getClass() != object.getClass()) { | ||
return false; | ||
} | ||
TableId tableId1 = (TableId) object; | ||
return Objects.equals(tableUniqueId, tableId1.tableUniqueId) && Objects.equals(tableName, tableId1.tableName); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(tableUniqueId, tableName); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "TableId{" + | ||
"tableUniqueId='" + tableUniqueId + '\'' + | ||
", tableName='" + tableName + '\'' + | ||
'}'; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
java/core/src/main/java/sleeper/core/table/TableIdGenerator.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,45 @@ | ||
/* | ||
* Copyright 2022-2023 Crown Copyright | ||
* | ||
* 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 sleeper.core.table; | ||
|
||
import org.apache.commons.codec.binary.Hex; | ||
|
||
import java.security.SecureRandom; | ||
import java.util.Random; | ||
|
||
public class TableIdGenerator { | ||
|
||
private final Random random; | ||
|
||
public TableIdGenerator() { | ||
random = new SecureRandom(); | ||
} | ||
|
||
private TableIdGenerator(int seed) { | ||
random = new Random(seed); | ||
} | ||
|
||
public static TableIdGenerator fromRandomSeed(int seed) { | ||
return new TableIdGenerator(seed); | ||
} | ||
|
||
public String generateString() { | ||
byte[] bytes = new byte[4]; | ||
random.nextBytes(bytes); | ||
return Hex.encodeHexString(bytes); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
java/core/src/main/java/sleeper/core/table/TableIndex.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,30 @@ | ||
/* | ||
* Copyright 2022-2023 Crown Copyright | ||
* | ||
* 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 sleeper.core.table; | ||
|
||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
|
||
public interface TableIndex { | ||
TableId createTable(String tableName) throws TableAlreadyExistsException; | ||
|
||
Stream<TableId> streamAllTables(); | ||
|
||
Optional<TableId> getTableByName(String tableName); | ||
|
||
Optional<TableId> getTableByUniqueId(String tableUniqueId); | ||
} |
57 changes: 57 additions & 0 deletions
57
java/core/src/test/java/sleeper/core/table/InMemoryTableIndex.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,57 @@ | ||
/* | ||
* Copyright 2022-2023 Crown Copyright | ||
* | ||
* 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 sleeper.core.table; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.TreeMap; | ||
import java.util.stream.Stream; | ||
|
||
public class InMemoryTableIndex implements TableIndex { | ||
|
||
private final Map<String, TableId> idByName = new TreeMap<>(); | ||
private final Map<String, TableId> nameById = new HashMap<>(); | ||
private int nextIdNumber = 1; | ||
|
||
@Override | ||
public TableId createTable(String tableName) { | ||
if (idByName.containsKey(tableName)) { | ||
throw new TableAlreadyExistsException(tableName); | ||
} | ||
TableId id = TableId.uniqueIdAndName("table-" + nextIdNumber, tableName); | ||
nextIdNumber++; | ||
idByName.put(tableName, id); | ||
nameById.put(id.getTableUniqueId(), id); | ||
return id; | ||
} | ||
|
||
@Override | ||
public Stream<TableId> streamAllTables() { | ||
return idByName.values().stream(); | ||
} | ||
|
||
@Override | ||
public Optional<TableId> getTableByName(String tableName) { | ||
return Optional.ofNullable(idByName.get(tableName)); | ||
} | ||
|
||
@Override | ||
public Optional<TableId> getTableByUniqueId(String tableUniqueId) { | ||
return Optional.ofNullable(nameById.get(tableUniqueId)); | ||
} | ||
} |
Oops, something went wrong.
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.
Is this the right name for this class, given it contains a field called
tableId
? It means thatTableIndex
has the methodOptional<TableId> getTableById(String tableId);
which looks wrong. Could call this classTableIdAndName
? Alternatively rename the fieldtableId
totableUniqueId
and then the method onTableIndex
would beOptional<TableId> getTableByUniqueId(String tableUniqueId);
?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've renamed the field, thanks.