Skip to content

Commit

Permalink
merge: #1145
Browse files Browse the repository at this point in the history
1145: [Backport release-8.5.0] fix: db transaction inconsistencies when deleting keys r=github-actions[bot] a=backport-action

# Description
Backport of #1139 to `release-8.5.0`.

relates to #1001 #1001

Co-authored-by: Meggle (Sebastian Bathke) <sebastian.bathke@camunda.com>
  • Loading branch information
zeebe-bors-camunda[bot] and megglos authored Apr 3, 2024
2 parents 5ecf619 + 15df726 commit 9cb7d5c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
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;
}

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));
}
}
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

0 comments on commit 9cb7d5c

Please sign in to comment.