Search in sources :

Example 1 with TransactionException

use of com.scalar.db.exception.transaction.TransactionException in project scalardb by scalar-labs.

the class DistributedTransactionServiceWithJdbcTransactionIntegrationTest method populateRecords.

private void populateRecords() throws TransactionException {
    GrpcTransaction transaction = manager.start();
    IntStream.range(0, NUM_ACCOUNTS).forEach(i -> IntStream.range(0, NUM_TYPES).forEach(j -> {
        Key partitionKey = new Key(ACCOUNT_ID, i);
        Key clusteringKey = new Key(ACCOUNT_TYPE, j);
        Put put = new Put(partitionKey, clusteringKey).forNamespace(NAMESPACE).forTable(TABLE).withValue(BALANCE, INITIAL_BALANCE);
        try {
            transaction.put(put);
        } catch (CrudException e) {
            throw new RuntimeException(e);
        }
    }));
    transaction.commit();
}
Also used : IntStream(java.util.stream.IntStream) IntValue(com.scalar.db.io.IntValue) BeforeClass(org.junit.BeforeClass) Consistency(com.scalar.db.api.Consistency) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ExecutionException(com.scalar.db.exception.storage.ExecutionException) Value(com.scalar.db.io.Value) StorageFactory(com.scalar.db.service.StorageFactory) DistributedStorageAdmin(com.scalar.db.api.DistributedStorageAdmin) TableMetadata(com.scalar.db.api.TableMetadata) CrudException(com.scalar.db.exception.transaction.CrudException) ArrayList(java.util.ArrayList) Result(com.scalar.db.api.Result) GrpcTransactionManager(com.scalar.db.transaction.rpc.GrpcTransactionManager) DataType(com.scalar.db.io.DataType) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Before(org.junit.Before) GrpcConfig(com.scalar.db.storage.rpc.GrpcConfig) Key(com.scalar.db.io.Key) AfterClass(org.junit.AfterClass) IOException(java.io.IOException) Test(org.junit.Test) Put(com.scalar.db.api.Put) Delete(com.scalar.db.api.Delete) Get(com.scalar.db.api.Get) List(java.util.List) Scan(com.scalar.db.api.Scan) Optional(java.util.Optional) TransactionException(com.scalar.db.exception.transaction.TransactionException) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) CrudException(com.scalar.db.exception.transaction.CrudException)

Example 2 with TransactionException

use of com.scalar.db.exception.transaction.TransactionException in project scalardb by scalar-labs.

the class ElectronicMoneyWithTransaction method charge.

@Override
public void charge(String id, int amount) throws TransactionException {
    // Start a transaction
    DistributedTransaction tx = manager.start();
    try {
        // Retrieve the current balance for id
        Get get = new Get(new Key(ID, id));
        Optional<Result> result = tx.get(get);
        // Calculate the balance
        int balance = amount;
        if (result.isPresent()) {
            int current = result.get().getValue(BALANCE).get().getAsInt();
            balance += current;
        }
        // Update the balance
        Put put = new Put(new Key(ID, id)).withValue(BALANCE, balance);
        tx.put(put);
        // Commit the transaction (records are automatically recovered in case of failure)
        tx.commit();
    } catch (Exception e) {
        tx.abort();
        throw e;
    }
}
Also used : DistributedTransaction(com.scalar.db.api.DistributedTransaction) Get(com.scalar.db.api.Get) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) TransactionException(com.scalar.db.exception.transaction.TransactionException) IOException(java.io.IOException) Result(com.scalar.db.api.Result)

Example 3 with TransactionException

use of com.scalar.db.exception.transaction.TransactionException in project scalardb by scalar-labs.

the class DistributedTransactionIntegrationTestBase method scan_ScanAllWithProjectionsGiven_ShouldRetrieveSpecifiedValues.

@Test
public void scan_ScanAllWithProjectionsGiven_ShouldRetrieveSpecifiedValues() throws TransactionException {
    // Arrange
    populateRecords();
    DistributedTransaction transaction = manager.start();
    ScanAll scanAll = prepareScanAll().withProjection(ACCOUNT_TYPE).withProjection(BALANCE);
    // Act
    List<Result> results = transaction.scan(scanAll);
    transaction.commit();
    // Assert
    List<ExpectedResult> expectedResults = new ArrayList<>();
    IntStream.range(0, NUM_ACCOUNTS).forEach(i -> IntStream.range(0, NUM_TYPES).forEach(j -> {
        ExpectedResultBuilder erBuilder = new ExpectedResultBuilder().clusteringKey(Key.ofInt(ACCOUNT_TYPE, j)).nonKeyColumns(ImmutableList.of(IntColumn.of(BALANCE, INITIAL_BALANCE)));
        expectedResults.add(erBuilder.build());
    }));
    assertResultsContainsExactlyInAnyOrder(results, expectedResults);
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) IntColumn(com.scalar.db.io.IntColumn) IntValue(com.scalar.db.io.IntValue) Arrays(java.util.Arrays) TestUtils.assertResultsAreASubsetOf(com.scalar.db.util.TestUtils.assertResultsAreASubsetOf) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ExecutionException(com.scalar.db.exception.storage.ExecutionException) Value(com.scalar.db.io.Value) TestUtils(com.scalar.db.util.TestUtils) CrudException(com.scalar.db.exception.transaction.CrudException) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) TestInstance(org.junit.jupiter.api.TestInstance) ImmutableList(com.google.common.collect.ImmutableList) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) CommitException(com.scalar.db.exception.transaction.CommitException) DataType(com.scalar.db.io.DataType) CommitConflictException(com.scalar.db.exception.transaction.CommitConflictException) Key(com.scalar.db.io.Key) Properties(java.util.Properties) ExpectedResultBuilder(com.scalar.db.util.TestUtils.ExpectedResult.ExpectedResultBuilder) TestUtils.assertResultsContainsExactlyInAnyOrder(com.scalar.db.util.TestUtils.assertResultsContainsExactlyInAnyOrder) TransactionFactory(com.scalar.db.service.TransactionFactory) Test(org.junit.jupiter.api.Test) List(java.util.List) ExpectedResult(com.scalar.db.util.TestUtils.ExpectedResult) Ordering(com.scalar.db.api.Scan.Ordering) Optional(java.util.Optional) TransactionException(com.scalar.db.exception.transaction.TransactionException) Collections(java.util.Collections) Assertions.assertThatCode(org.assertj.core.api.Assertions.assertThatCode) ExpectedResult(com.scalar.db.util.TestUtils.ExpectedResult) ArrayList(java.util.ArrayList) ExpectedResultBuilder(com.scalar.db.util.TestUtils.ExpectedResult.ExpectedResultBuilder) ExpectedResult(com.scalar.db.util.TestUtils.ExpectedResult) Test(org.junit.jupiter.api.Test)

Example 4 with TransactionException

use of com.scalar.db.exception.transaction.TransactionException in project scalardb by scalar-labs.

the class TwoPhaseCommitTransactionIntegrationTestBase method scan_ScanAllWithProjectionsGiven_ShouldRetrieveSpecifiedValues.

@Test
public void scan_ScanAllWithProjectionsGiven_ShouldRetrieveSpecifiedValues() throws TransactionException {
    // Arrange
    populateRecords();
    TwoPhaseCommitTransaction transaction = manager.start();
    ScanAll scanAll = prepareScanAll().withProjection(ACCOUNT_TYPE).withProjection(BALANCE);
    // Act
    List<Result> results = transaction.scan(scanAll);
    transaction.prepare();
    transaction.validate();
    transaction.commit();
    // Assert
    List<ExpectedResult> expectedResults = new ArrayList<>();
    IntStream.range(0, NUM_ACCOUNTS).forEach(i -> IntStream.range(0, NUM_TYPES).forEach(j -> {
        ExpectedResultBuilder erBuilder = new ExpectedResultBuilder().clusteringKey(Key.ofInt(ACCOUNT_TYPE, j)).nonKeyColumns(ImmutableList.of(IntColumn.of(BALANCE, INITIAL_BALANCE)));
        expectedResults.add(erBuilder.build());
    }));
    assertResultsContainsExactlyInAnyOrder(results, expectedResults);
    results.forEach(result -> {
        assertThat(result.contains(ACCOUNT_ID)).isFalse();
        assertThat(result.contains(SOME_COLUMN)).isFalse();
    });
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) IntColumn(com.scalar.db.io.IntColumn) IntValue(com.scalar.db.io.IntValue) Arrays(java.util.Arrays) TestUtils.assertResultsAreASubsetOf(com.scalar.db.util.TestUtils.assertResultsAreASubsetOf) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ExecutionException(com.scalar.db.exception.storage.ExecutionException) Value(com.scalar.db.io.Value) TestUtils(com.scalar.db.util.TestUtils) CrudException(com.scalar.db.exception.transaction.CrudException) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) TestInstance(org.junit.jupiter.api.TestInstance) ImmutableList(com.google.common.collect.ImmutableList) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) CommitException(com.scalar.db.exception.transaction.CommitException) DataType(com.scalar.db.io.DataType) Key(com.scalar.db.io.Key) Properties(java.util.Properties) ExpectedResultBuilder(com.scalar.db.util.TestUtils.ExpectedResult.ExpectedResultBuilder) TestUtils.assertResultsContainsExactlyInAnyOrder(com.scalar.db.util.TestUtils.assertResultsContainsExactlyInAnyOrder) TransactionFactory(com.scalar.db.service.TransactionFactory) Test(org.junit.jupiter.api.Test) List(java.util.List) ExpectedResult(com.scalar.db.util.TestUtils.ExpectedResult) Ordering(com.scalar.db.api.Scan.Ordering) PreparationConflictException(com.scalar.db.exception.transaction.PreparationConflictException) Optional(java.util.Optional) TransactionException(com.scalar.db.exception.transaction.TransactionException) Collections(java.util.Collections) Assertions.assertThatCode(org.assertj.core.api.Assertions.assertThatCode) ExpectedResult(com.scalar.db.util.TestUtils.ExpectedResult) ArrayList(java.util.ArrayList) ExpectedResultBuilder(com.scalar.db.util.TestUtils.ExpectedResult.ExpectedResultBuilder) ExpectedResult(com.scalar.db.util.TestUtils.ExpectedResult) Test(org.junit.jupiter.api.Test)

Example 5 with TransactionException

use of com.scalar.db.exception.transaction.TransactionException in project scalardb by scalar-labs.

the class TwoPhaseCommitTransactionIntegrationTestBase method populateRecords.

private void populateRecords() throws TransactionException {
    TwoPhaseCommitTransaction transaction = manager.start();
    IntStream.range(0, NUM_ACCOUNTS).forEach(i -> IntStream.range(0, NUM_TYPES).forEach(j -> {
        Key partitionKey = new Key(ACCOUNT_ID, i);
        Key clusteringKey = new Key(ACCOUNT_TYPE, j);
        Put put = new Put(partitionKey, clusteringKey).forNamespace(namespace).forTable(TABLE).withIntValue(BALANCE, INITIAL_BALANCE).withIntValue(SOME_COLUMN, i * j);
        try {
            transaction.put(put);
        } catch (CrudException e) {
            throw new RuntimeException(e);
        }
    }));
    transaction.prepare();
    transaction.validate();
    transaction.commit();
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) IntColumn(com.scalar.db.io.IntColumn) IntValue(com.scalar.db.io.IntValue) Arrays(java.util.Arrays) TestUtils.assertResultsAreASubsetOf(com.scalar.db.util.TestUtils.assertResultsAreASubsetOf) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ExecutionException(com.scalar.db.exception.storage.ExecutionException) Value(com.scalar.db.io.Value) TestUtils(com.scalar.db.util.TestUtils) CrudException(com.scalar.db.exception.transaction.CrudException) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) TestInstance(org.junit.jupiter.api.TestInstance) ImmutableList(com.google.common.collect.ImmutableList) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) CommitException(com.scalar.db.exception.transaction.CommitException) DataType(com.scalar.db.io.DataType) Key(com.scalar.db.io.Key) Properties(java.util.Properties) ExpectedResultBuilder(com.scalar.db.util.TestUtils.ExpectedResult.ExpectedResultBuilder) TestUtils.assertResultsContainsExactlyInAnyOrder(com.scalar.db.util.TestUtils.assertResultsContainsExactlyInAnyOrder) TransactionFactory(com.scalar.db.service.TransactionFactory) Test(org.junit.jupiter.api.Test) List(java.util.List) ExpectedResult(com.scalar.db.util.TestUtils.ExpectedResult) Ordering(com.scalar.db.api.Scan.Ordering) PreparationConflictException(com.scalar.db.exception.transaction.PreparationConflictException) Optional(java.util.Optional) TransactionException(com.scalar.db.exception.transaction.TransactionException) Collections(java.util.Collections) Assertions.assertThatCode(org.assertj.core.api.Assertions.assertThatCode) Key(com.scalar.db.io.Key) CrudException(com.scalar.db.exception.transaction.CrudException)

Aggregations

TransactionException (com.scalar.db.exception.transaction.TransactionException)12 Key (com.scalar.db.io.Key)9 ExecutionException (com.scalar.db.exception.storage.ExecutionException)7 DataType (com.scalar.db.io.DataType)7 IntValue (com.scalar.db.io.IntValue)7 Value (com.scalar.db.io.Value)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Optional (java.util.Optional)7 IntStream (java.util.stream.IntStream)7 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 CrudException (com.scalar.db.exception.transaction.CrudException)6 Get (com.scalar.db.api.Get)5 Put (com.scalar.db.api.Put)5 Result (com.scalar.db.api.Result)5 CommitException (com.scalar.db.exception.transaction.CommitException)5 Arrays (java.util.Arrays)5 Assertions.assertThatCode (org.assertj.core.api.Assertions.assertThatCode)5 ImmutableList (com.google.common.collect.ImmutableList)4 Ordering (com.scalar.db.api.Scan.Ordering)4