Search in sources :

Example 16 with TransactionState

use of com.scalar.db.api.TransactionState in project scalardb by scalar-labs.

the class TwoPhaseConsensusCommitSpecificIntegrationTestBase method getState_forSuccessfulTransaction_ShouldReturnCommittedState.

@Test
public void getState_forSuccessfulTransaction_ShouldReturnCommittedState() throws TransactionException {
    // Arrange
    TwoPhaseConsensusCommit transaction = manager.start();
    transaction.get(prepareGet(0, 0, TABLE_1));
    transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
    transaction.prepare();
    transaction.commit();
    // Act
    TransactionState state = manager.getState(transaction.getId());
    // Assert
    assertThat(state).isEqualTo(TransactionState.COMMITTED);
}
Also used : TransactionState(com.scalar.db.api.TransactionState) Test(org.junit.jupiter.api.Test)

Example 17 with TransactionState

use of com.scalar.db.api.TransactionState in project scalardb by scalar-labs.

the class TwoPhaseConsensusCommitSpecificIntegrationTestBase method populatePreparedRecordAndCoordinatorStateRecord.

private void populatePreparedRecordAndCoordinatorStateRecord(String table, TransactionState recordState, long preparedAt, TransactionState coordinatorState) throws ExecutionException, CoordinatorException {
    Key partitionKey = new Key(new IntValue(ACCOUNT_ID, 0));
    Key clusteringKey = new Key(new IntValue(ACCOUNT_TYPE, 0));
    Put put = new Put(partitionKey, clusteringKey).forNamespace(namespace).forTable(table).withValue(new IntValue(BALANCE, INITIAL_BALANCE)).withValue(Attribute.toIdValue(ANY_ID_2)).withValue(Attribute.toStateValue(recordState)).withValue(Attribute.toVersionValue(2)).withValue(Attribute.toPreparedAtValue(preparedAt)).withValue(Attribute.toBeforeIdValue(ANY_ID_1)).withValue(Attribute.toBeforeStateValue(TransactionState.COMMITTED)).withValue(Attribute.toBeforeVersionValue(1)).withValue(Attribute.toBeforePreparedAtValue(1)).withValue(Attribute.toBeforeCommittedAtValue(1));
    storage.put(put);
    if (coordinatorState == null) {
        return;
    }
    State state = new State(ANY_ID_2, coordinatorState);
    coordinator.putState(state);
}
Also used : State(com.scalar.db.transaction.consensuscommit.Coordinator.State) TransactionState(com.scalar.db.api.TransactionState) IntValue(com.scalar.db.io.IntValue) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put)

Example 18 with TransactionState

use of com.scalar.db.api.TransactionState in project scalardb by scalar-labs.

the class CommitHandler method commitState.

public void commitState(Snapshot snapshot) throws CommitException, UnknownTransactionStatusException {
    String id = snapshot.getId();
    try {
        commitState(snapshot.getId());
    } catch (CoordinatorException e) {
        TransactionState state = abort(id);
        if (state.equals(TransactionState.ABORTED)) {
            rollbackRecords(snapshot);
            throw new CommitException("committing state in coordinator failed. the transaction is aborted", e);
        }
    }
    LOGGER.debug("transaction {} is committed successfully at {}", id, System.currentTimeMillis());
}
Also used : TransactionState(com.scalar.db.api.TransactionState) CommitException(com.scalar.db.exception.transaction.CommitException)

Example 19 with TransactionState

use of com.scalar.db.api.TransactionState in project scalardb by scalar-labs.

the class TransactionResultTest method getTransactionState_ResultGiven_ShouldReturnCorrectState.

@Test
public void getTransactionState_ResultGiven_ShouldReturnCorrectState() {
    // Arrange
    TransactionResult result = prepareResult();
    // Act
    TransactionState state = result.getState();
    // Assert
    assertThat(state).isEqualTo(TransactionState.COMMITTED);
}
Also used : TransactionState(com.scalar.db.api.TransactionState) Test(org.junit.jupiter.api.Test)

Example 20 with TransactionState

use of com.scalar.db.api.TransactionState in project scalardb by scalar-labs.

the class ConsensusCommitManagerTest method check_StateReturned_ReturnTheState.

@Test
public void check_StateReturned_ReturnTheState() throws CoordinatorException {
    // Arrange
    TransactionState expected = TransactionState.COMMITTED;
    when(coordinator.getState(ANY_TX_ID)).thenReturn(Optional.of(new State(ANY_TX_ID, expected)));
    // Act
    TransactionState actual = manager.getState(ANY_TX_ID);
    // Assert
    assertThat(actual).isEqualTo(expected);
}
Also used : TransactionState(com.scalar.db.api.TransactionState) TransactionState(com.scalar.db.api.TransactionState) State(com.scalar.db.transaction.consensuscommit.Coordinator.State) Test(org.junit.jupiter.api.Test)

Aggregations

TransactionState (com.scalar.db.api.TransactionState)37 Test (org.junit.jupiter.api.Test)16 Test (org.junit.Test)12 Put (com.scalar.db.api.Put)4 Key (com.scalar.db.io.Key)4 State (com.scalar.db.transaction.consensuscommit.Coordinator.State)4 GrpcTransaction (com.scalar.db.transaction.rpc.GrpcTransaction)3 GrpcTwoPhaseCommitTransaction (com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction)3 IntValue (com.scalar.db.io.IntValue)2 CommitException (com.scalar.db.exception.transaction.CommitException)1