Skip to content

Commit 7fb4269

Browse files
feeblefakiejnmt
andauthored
Backport to branch(3) : Fix to put state even in read-only transactions (#187)
Co-authored-by: Jun Nemoto <[email protected]>
1 parent a806e1d commit 7fb4269

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

ledger/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ dependencies {
5454
implementation group: 'com.github.everit-org.json-schema', name: 'org.everit.json.schema', version: '1.14.4'
5555

5656
testImplementation group: 'com.scalar-labs', name: 'scalardb-schema-loader', version: "${scalarDbVersion}"
57+
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: "${junitVersion}"
5758

5859
// for Error Prone
5960
errorprone "com.google.errorprone:error_prone_core:${errorproneVersion}"

ledger/src/main/java/com/scalar/dl/ledger/database/scalardb/ScalarTamperEvidentAssetLedger.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,17 @@ public List<AssetProof> commit() {
133133
if (snapshot.hasWriteSet()) {
134134
puts = assetComposer.compose(snapshot, request);
135135

136-
if (config.isTxStateManagementEnabled()) {
137-
stateManager.putCommit(transaction, transaction.getId());
138-
}
139-
140136
transaction.put(puts);
141137

142138
if (!config.isDirectAssetAccessEnabled()) {
143139
metadata.put(snapshot.getWriteSet().values());
144140
}
145141
}
146142

143+
if (config.isTxStateManagementEnabled()) {
144+
stateManager.putCommit(transaction, transaction.getId());
145+
}
146+
147147
transaction.commit();
148148
} catch (CrudConflictException e) {
149149
throw new ConflictException(

ledger/src/test/java/com/scalar/dl/ledger/database/scalardb/ScalarTamperEvidentAssetLedgerTest.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
import java.util.Optional;
5252
import org.junit.jupiter.api.BeforeEach;
5353
import org.junit.jupiter.api.Test;
54+
import org.junit.jupiter.params.ParameterizedTest;
55+
import org.junit.jupiter.params.provider.ValueSource;
5456
import org.mockito.ArgumentCaptor;
5557
import org.mockito.Mock;
5658
import org.mockito.MockitoAnnotations;
@@ -466,6 +468,7 @@ public void commit_EmptySnapshotGiven_ShouldDoNothing()
466468
CrudException {
467469
// Arrange
468470
when(config.isDirectAssetAccessEnabled()).thenReturn(false);
471+
when(config.isTxStateManagementEnabled()).thenReturn(false);
469472

470473
// Act
471474
ledger.commit();
@@ -482,6 +485,7 @@ public void commit_EmptySnapshotGivenAndDirectAssetAccessEnabled_ShouldDoNothing
482485
CrudException {
483486
// Arrange
484487
when(config.isDirectAssetAccessEnabled()).thenReturn(true);
488+
when(config.isTxStateManagementEnabled()).thenReturn(false);
485489

486490
// Act
487491
ledger.commit();
@@ -741,12 +745,14 @@ public void commit_CommitExceptionThrownInCommit_ShouldThrowDatabaseException()
741745
assertThat(proofs).containsOnly(proof);
742746
}
743747

744-
@Test
745-
public void commit_TxStateManagementEnabled_ShouldPutWithStateManager()
748+
@ParameterizedTest
749+
@ValueSource(booleans = {true, false})
750+
public void commit_WriteTransactionGiven_ShouldPutWithStateManagerAccordingToConfig(
751+
boolean txStateManagementEnabled)
746752
throws CrudException, CommitException,
747753
com.scalar.db.exception.transaction.UnknownTransactionStatusException {
748754
// Arrange
749-
when(config.isTxStateManagementEnabled()).thenReturn(true);
755+
when(config.isTxStateManagementEnabled()).thenReturn(txStateManagementEnabled);
750756
snapshot.put(ANY_ID, asset);
751757
snapshot.put(ANY_ID, ANY_DATA);
752758
doNothing().when(transaction).put(any(List.class));
@@ -758,32 +764,40 @@ public void commit_TxStateManagementEnabled_ShouldPutWithStateManager()
758764
ledger.commit();
759765

760766
// Assert
761-
verify(stateManager).putCommit(transaction, ANY_NONCE);
767+
if (txStateManagementEnabled) {
768+
verify(stateManager).putCommit(transaction, ANY_NONCE);
769+
} else {
770+
verify(stateManager, never()).putCommit(any(DistributedTransaction.class), anyString());
771+
}
762772
verify(transaction).put(any(List.class));
763773
verify(transaction).put(any(Put.class));
764774
verify(transaction).commit();
765775
}
766776

767-
@Test
768-
public void commit_TxStateManagementDisabled_ShouldNotPutWithStateManager()
777+
@ParameterizedTest
778+
@ValueSource(booleans = {true, false})
779+
public void commit_ReadOnlyTransactionGiven_ShouldPutWithStateManagerAccordingToConfig(
780+
boolean txStateManagementEnabled)
769781
throws CrudException, CommitException,
770782
com.scalar.db.exception.transaction.UnknownTransactionStatusException {
771783
// Arrange
772-
when(config.isTxStateManagementEnabled()).thenReturn(false);
773-
snapshot.put(ANY_ID, asset);
774-
snapshot.put(ANY_ID, ANY_DATA);
775-
doNothing().when(transaction).put(any(List.class));
776-
doNothing().when(transaction).put(any(Put.class));
784+
when(config.isTxStateManagementEnabled()).thenReturn(txStateManagementEnabled);
785+
when(config.isDirectAssetAccessEnabled()).thenReturn(false);
777786
when(transaction.getId()).thenReturn(ANY_NONCE);
778787
doNothing().when(stateManager).putCommit(any(DistributedTransaction.class), anyString());
788+
snapshot.put(ANY_ID, asset);
779789

780790
// Act
781791
ledger.commit();
782792

783793
// Assert
784-
verify(stateManager, never()).putCommit(transaction, ANY_NONCE);
785-
verify(transaction).put(any(List.class));
786-
verify(transaction).put(any(Put.class));
794+
if (txStateManagementEnabled) {
795+
verify(stateManager).putCommit(transaction, ANY_NONCE);
796+
} else {
797+
verify(stateManager, never()).putCommit(any(DistributedTransaction.class), anyString());
798+
}
799+
verify(transaction, never()).put(any(List.class));
800+
verify(transaction, never()).put(any(Put.class));
787801
verify(transaction).commit();
788802
}
789803

0 commit comments

Comments
 (0)