Skip to content
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

fix: db transaction inconsistencies when deleting keys #1139

Merged
2 commits merged into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public void put(final FullyQualifiedKey fullyQualifiedKey, final DbValue value)
public byte[] get(final FullyQualifiedKey fullyQualifiedKey) {
final Bytes key = fullyQualifiedKey.getKeyBytes();

if (deletedKeys.contains(key)) {
return null;
}
Comment on lines +76 to +78
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Makes sense too! Nice find!


final Bytes valueInCache = transactionCache.get(key);

if (valueInCache != null) {
Expand Down Expand Up @@ -108,6 +112,7 @@ public InMemoryDbIterator newIterator() {
@Override
public boolean contains(final FullyQualifiedKey fullyQualifiedKey) {
final Bytes keyBytes = fullyQualifiedKey.getKeyBytes();
return transactionCache.containsKey(keyBytes) || database.containsKey(keyBytes);
return !deletedKeys.contains(keyBytes)
&& (transactionCache.containsKey(keyBytes) || database.containsKey(keyBytes));
Comment on lines -111 to +116
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Nice find! Makes sense

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,49 @@ void shouldWriteAndDeleteSameKeyValuePairInTransaction() {
assertThat(oneColumnFamily.exists(oneKey)).isFalse();
}

@Test
void shouldAllowDeleteAndInsertInTransaction() throws Exception {
// given
oneKey.wrapLong(1);
oneValue.wrapLong(-1L);
twoValue.wrapLong(-2L);
oneColumnFamily.insert(oneKey, oneValue);
transactionContext.getCurrentTransaction().commit();

// when
transactionContext.runInTransaction(
() -> {
oneColumnFamily.deleteExisting(oneKey);
oneColumnFamily.insert(oneKey, twoValue);
});

// then
assertThat(oneColumnFamily.get(oneKey).getValue()).isEqualTo(twoValue.getValue());
}

@Test
void shouldNotGetByKeyIfDeletedInTransaction() throws Exception {
// given
oneKey.wrapLong(1);
oneValue.wrapLong(-1L);
twoValue.wrapLong(-2L);
oneColumnFamily.insert(oneKey, oneValue);
transactionContext.getCurrentTransaction().commit();

// when
transactionContext.runInTransaction(
() -> {
oneColumnFamily.deleteExisting(oneKey);

if (oneColumnFamily.get(oneKey) != null) {
fail("Should not be able to get deleted key.");
}
});

// then
assertThat(oneColumnFamily.get(oneKey)).isNull();
}

@Test
void shouldNotCommitOnError() {
// given
Expand Down
Loading