-
Notifications
You must be signed in to change notification settings - Fork 3
Backport to branch(3.11) : Fix to put state even in read-only transactions #188
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -51,6 +51,8 @@ | |||||||||||
| import java.util.Optional; | ||||||||||||
| import org.junit.jupiter.api.BeforeEach; | ||||||||||||
| import org.junit.jupiter.api.Test; | ||||||||||||
| import org.junit.jupiter.params.ParameterizedTest; | ||||||||||||
| import org.junit.jupiter.params.provider.ValueSource; | ||||||||||||
| import org.mockito.ArgumentCaptor; | ||||||||||||
| import org.mockito.Mock; | ||||||||||||
| import org.mockito.MockitoAnnotations; | ||||||||||||
|
|
@@ -466,6 +468,7 @@ public void commit_EmptySnapshotGiven_ShouldDoNothing() | |||||||||||
| CrudException { | ||||||||||||
| // Arrange | ||||||||||||
| when(config.isDirectAssetAccessEnabled()).thenReturn(false); | ||||||||||||
| when(config.isTxStateManagementEnabled()).thenReturn(false); | ||||||||||||
|
|
||||||||||||
| // Act | ||||||||||||
| ledger.commit(); | ||||||||||||
|
|
@@ -482,6 +485,7 @@ public void commit_EmptySnapshotGivenAndDirectAssetAccessEnabled_ShouldDoNothing | |||||||||||
| CrudException { | ||||||||||||
| // Arrange | ||||||||||||
| when(config.isDirectAssetAccessEnabled()).thenReturn(true); | ||||||||||||
| when(config.isTxStateManagementEnabled()).thenReturn(false); | ||||||||||||
|
|
||||||||||||
| // Act | ||||||||||||
| ledger.commit(); | ||||||||||||
|
|
@@ -741,12 +745,14 @@ public void commit_CommitExceptionThrownInCommit_ShouldThrowDatabaseException() | |||||||||||
| assertThat(proofs).containsOnly(proof); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| @Test | ||||||||||||
| public void commit_TxStateManagementEnabled_ShouldPutWithStateManager() | ||||||||||||
| @ParameterizedTest | ||||||||||||
| @ValueSource(booleans = {true, false}) | ||||||||||||
| public void commit_WriteTransactionGiven_ShouldPutWithStateManagerAccordingToConfig( | ||||||||||||
| boolean txStateManagementEnabled) | ||||||||||||
| throws CrudException, CommitException, | ||||||||||||
| com.scalar.db.exception.transaction.UnknownTransactionStatusException { | ||||||||||||
| // Arrange | ||||||||||||
| when(config.isTxStateManagementEnabled()).thenReturn(true); | ||||||||||||
| when(config.isTxStateManagementEnabled()).thenReturn(txStateManagementEnabled); | ||||||||||||
| snapshot.put(ANY_ID, asset); | ||||||||||||
| snapshot.put(ANY_ID, ANY_DATA); | ||||||||||||
| doNothing().when(transaction).put(any(List.class)); | ||||||||||||
|
|
@@ -758,32 +764,40 @@ public void commit_TxStateManagementEnabled_ShouldPutWithStateManager() | |||||||||||
| ledger.commit(); | ||||||||||||
|
|
||||||||||||
| // Assert | ||||||||||||
| verify(stateManager).putCommit(transaction, ANY_NONCE); | ||||||||||||
| if (txStateManagementEnabled) { | ||||||||||||
| verify(stateManager).putCommit(transaction, ANY_NONCE); | ||||||||||||
| } else { | ||||||||||||
| verify(stateManager, never()).putCommit(any(DistributedTransaction.class), anyString()); | ||||||||||||
| } | ||||||||||||
| verify(transaction).put(any(List.class)); | ||||||||||||
| verify(transaction).put(any(Put.class)); | ||||||||||||
| verify(transaction).commit(); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| @Test | ||||||||||||
| public void commit_TxStateManagementDisabled_ShouldNotPutWithStateManager() | ||||||||||||
| @ParameterizedTest | ||||||||||||
| @ValueSource(booleans = {true, false}) | ||||||||||||
| public void commit_ReadOnlyTransactionGiven_ShouldPutWithStateManagerAccordingToConfig( | ||||||||||||
| boolean txStateManagementEnabled) | ||||||||||||
| throws CrudException, CommitException, | ||||||||||||
| com.scalar.db.exception.transaction.UnknownTransactionStatusException { | ||||||||||||
| // Arrange | ||||||||||||
| when(config.isTxStateManagementEnabled()).thenReturn(false); | ||||||||||||
| snapshot.put(ANY_ID, asset); | ||||||||||||
| snapshot.put(ANY_ID, ANY_DATA); | ||||||||||||
| doNothing().when(transaction).put(any(List.class)); | ||||||||||||
| doNothing().when(transaction).put(any(Put.class)); | ||||||||||||
| when(config.isTxStateManagementEnabled()).thenReturn(txStateManagementEnabled); | ||||||||||||
| when(config.isDirectAssetAccessEnabled()).thenReturn(false); | ||||||||||||
| when(transaction.getId()).thenReturn(ANY_NONCE); | ||||||||||||
| doNothing().when(stateManager).putCommit(any(DistributedTransaction.class), anyString()); | ||||||||||||
| snapshot.put(ANY_ID, asset); | ||||||||||||
|
|
||||||||||||
| // Act | ||||||||||||
| ledger.commit(); | ||||||||||||
|
|
||||||||||||
| // Assert | ||||||||||||
| verify(stateManager, never()).putCommit(transaction, ANY_NONCE); | ||||||||||||
| verify(transaction).put(any(List.class)); | ||||||||||||
| verify(transaction).put(any(Put.class)); | ||||||||||||
| if (txStateManagementEnabled) { | ||||||||||||
| verify(stateManager).putCommit(transaction, ANY_NONCE); | ||||||||||||
| } else { | ||||||||||||
| verify(stateManager, never()).putCommit(any(DistributedTransaction.class), anyString()); | ||||||||||||
| } | ||||||||||||
| verify(transaction, never()).put(any(List.class)); | ||||||||||||
| verify(transaction, never()).put(any(Put.class)); | ||||||||||||
|
Comment on lines
+799
to
+800
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The assertions
Suggested change
|
||||||||||||
| verify(transaction).commit(); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
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.
Moving the
transaction.putcall outside theifblock could lead to unexpected behavior ifconfig.isTxStateManagementEnabled()is true butsnapshot.hasWriteSet()is false. In such cases, the transaction state would be updated without any actual data being written, potentially causing inconsistencies. This should be reverted to ensure the transaction state is only updated when there are writes to the snapshot.