Skip to content

Commit

Permalink
Add test for creating empty snapshot for table with no transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
kr565370 committed Apr 30, 2024
1 parent c69c79f commit 2a68c4c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public LatestSnapshots getLatestSnapshots() {
if (result.getCount() > 0) {
return getLatestSnapshotsFromItem(result.getItems().get(0));
} else {
return new LatestSnapshots(null, null);
return LatestSnapshots.empty();
}
}

Expand Down Expand Up @@ -203,25 +203,29 @@ private static TransactionLogSnapshot getSnapshotFromItem(Map<String, AttributeV
}

public static class LatestSnapshots {
private final Optional<TransactionLogSnapshot> filesSnapshotOpt;
private final Optional<TransactionLogSnapshot> partitionsSnapshotOpt;
private final TransactionLogSnapshot filesSnapshot;
private final TransactionLogSnapshot partitionsSnapshot;

public static LatestSnapshots empty() {
return new LatestSnapshots(null, null);
}

public LatestSnapshots(TransactionLogSnapshot filesSnapshot, TransactionLogSnapshot partitionsSnapshot) {
this.filesSnapshotOpt = Optional.ofNullable(filesSnapshot);
this.partitionsSnapshotOpt = Optional.ofNullable(partitionsSnapshot);
this.filesSnapshot = filesSnapshot;
this.partitionsSnapshot = partitionsSnapshot;
}

public Optional<TransactionLogSnapshot> getFilesSnapshot() {
return filesSnapshotOpt;
return Optional.ofNullable(filesSnapshot);
}

public Optional<TransactionLogSnapshot> getPartitionsSnapshot() {
return partitionsSnapshotOpt;
return Optional.ofNullable(partitionsSnapshot);
}

@Override
public int hashCode() {
return Objects.hash(filesSnapshotOpt, partitionsSnapshotOpt);
return Objects.hash(filesSnapshot, partitionsSnapshot);
}

@Override
Expand All @@ -233,12 +237,12 @@ public boolean equals(Object obj) {
return false;
}
LatestSnapshots other = (LatestSnapshots) obj;
return Objects.equals(filesSnapshotOpt, other.filesSnapshotOpt) && Objects.equals(partitionsSnapshotOpt, other.partitionsSnapshotOpt);
return Objects.equals(filesSnapshot, other.filesSnapshot) && Objects.equals(partitionsSnapshot, other.partitionsSnapshot);
}

@Override
public String toString() {
return "LatestSnapshots{filesSnapshotOpt=" + filesSnapshotOpt + ", partitionsSnapshotOpt=" + partitionsSnapshotOpt + "}";
return "LatestSnapshots{filesSnapshot=" + filesSnapshot + ", partitionsSnapshot=" + partitionsSnapshot + "}";
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ void shouldRetrieveLatestSnapshotsWhenNoPartitionsSnapshotsArePresent() throws E
void shouldRetrieveLatestSnapshotsWhenNoSnapshotsArePresent() throws Exception {
// Given / When / Then
assertThat(store.getLatestSnapshots()).isEqualTo(
new LatestSnapshots(null, null));
LatestSnapshots.empty());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,21 @@ void shouldSkipCreatingSnapshotsIfStateHasNotUpdatedSinceLastSnapshot() throws E
partitionsSnapshot(table, 1)));
}

@Test
void shouldNotCreateSnapshotForTableWithNoTransactions() throws Exception {
// Given
TableProperties table = createTable("test-table-id-1", "test-table-1");

// When
runSnapshotCreator(table);

// Then
assertThat(snapshotStore(table).getLatestSnapshots())
.isEqualTo(LatestSnapshots.empty());
assertThat(snapshotStore(table).getFilesSnapshots()).isEmpty();
assertThat(snapshotStore(table).getPartitionsSnapshots()).isEmpty();
}

@Test
void shouldRemoveSnapshotFilesIfDynamoTransactionFailed() throws Exception {
// Given
Expand All @@ -220,7 +235,7 @@ void shouldRemoveSnapshotFilesIfDynamoTransactionFailed() throws Exception {
assertThatThrownBy(() -> runSnapshotCreator(table, failedUpdate()))
.isInstanceOf(RuntimeException.class);
assertThat(snapshotStore(table).getLatestSnapshots())
.isEqualTo(new LatestSnapshots(null, null));
.isEqualTo(LatestSnapshots.empty());
assertThat(Files.exists(filesSnapshotPath(table, 1))).isFalse();
assertThat(Files.exists(partitionsSnapshotPath(table, 1))).isFalse();
}
Expand All @@ -247,15 +262,11 @@ private void runSnapshotCreator(
}

private StateStore createStateStoreWithInMemoryTransactionLog(TableProperties table) {
TransactionLogStore fileTransactionStore = new InMemoryTransactionLogStore();
TransactionLogStore partitionTransactionStore = new InMemoryTransactionLogStore();
fileTransactionStoreByTableId.put(table.get(TABLE_ID), fileTransactionStore);
partitionTransactionStoreByTableId.put(table.get(TABLE_ID), partitionTransactionStore);
StateStore stateStore = TransactionLogStateStore.builder()
.sleeperTable(table.getStatus())
.schema(table.getSchema())
.filesLogStore(fileTransactionStore)
.partitionsLogStore(partitionTransactionStore)
.filesLogStore(fileTransactionStoreByTableId.get(table.get(TABLE_ID)))
.partitionsLogStore(partitionTransactionStoreByTableId.get(table.get(TABLE_ID)))
.build();
stateStore.fixFileUpdateTime(DEFAULT_UPDATE_TIME);
stateStore.fixPartitionUpdateTime(DEFAULT_UPDATE_TIME);
Expand All @@ -271,6 +282,8 @@ private TableProperties createTable(String tableId, String tableName) {
tableProperties.set(TABLE_ID, tableId);
tableProperties.set(TABLE_NAME, tableName);
tableProperties.set(STATESTORE_CLASSNAME, DynamoDBTransactionLogStateStore.class.getName());
fileTransactionStoreByTableId.put(tableId, new InMemoryTransactionLogStore());
partitionTransactionStoreByTableId.put(tableId, new InMemoryTransactionLogStore());
return tableProperties;
}

Expand Down

0 comments on commit 2a68c4c

Please sign in to comment.