Skip to content

Commit

Permalink
Add Snapshot-read-only isolation support
Browse files Browse the repository at this point in the history
You can read more about this type of isolation here https://ydb.tech/docs/en/concepts/transactions#modes
  • Loading branch information
jorki02 committed Jun 27, 2024
1 parent 56fd568 commit 8b22e65
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ private TxControl<?> getTxControl() {
case ONLINE_CONSISTENT_READ_ONLY -> TxControl.onlineRo().setAllowInconsistentReads(false);
case ONLINE_INCONSISTENT_READ_ONLY -> TxControl.onlineRo().setAllowInconsistentReads(true);
case STALE_CONSISTENT_READ_ONLY -> TxControl.staleRo();
case SNAPSHOT -> {
TxControl<?> txControl = (txId != null ? TxControl.id(txId) : TxControl.snapshotRo());
yield txControl.setCommitTx(false);
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import tech.ydb.yoj.databind.schema.Column;
import tech.ydb.yoj.databind.schema.ObjectSchema;
import tech.ydb.yoj.repository.db.EntitySchema;
import tech.ydb.yoj.repository.db.IsolationLevel;
import tech.ydb.yoj.repository.db.Repository;
import tech.ydb.yoj.repository.db.RepositoryTransaction;
import tech.ydb.yoj.repository.db.Tx;
Expand Down Expand Up @@ -304,6 +305,30 @@ public void transactionLevel() {
checkSession(sessionManager, firstSession);
}

@Test
public void snapshotTransactionLevel() {
Project expected1 = new Project(new Project.Id("SP1"), "snapshot1");
Project expected2 = new Project(new Project.Id("SP2"), "snapshot2");

db.tx(() -> db.projects().save(expected1));
db.tx(() -> db.projects().save(expected2));

Project actual1 = db.tx(() -> db.projects().find(expected1.getId()));
assertThat(actual1).isEqualTo(expected1);
Project actual2 = db.readOnly().run(() -> db.projects().find(expected2.getId()));
assertThat(actual2).isEqualTo(expected2);

db.readOnly()
.withStatementIsolationLevel(IsolationLevel.SNAPSHOT)
.run(() -> {
Project actualSnapshot1 = db.projects().find(expected1.getId());
assertThat(actualSnapshot1).isEqualTo(expected1);

Project actualSnapshot2 = db.projects().find(expected2.getId());
assertThat(actualSnapshot2).isEqualTo(expected2);
});
}

@SneakyThrows
@Test
public void truncated() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ public enum IsolationLevel {
* An <em>almost</em> recent consistent state of the database. Read only.
* This level is faster then {@code ONLINE_CONSISTENT_READ_ONLY}, but may return stale data.
*/
STALE_CONSISTENT_READ_ONLY("SC");
STALE_CONSISTENT_READ_ONLY("SC"),

/**
* All the read operations within a transaction access the database snapshot. Read only.
* All the data reads are consistent. The snapshot is taken when the transaction begins,
* meaning the transaction sees all changes committed before it began.
*/
SNAPSHOT("SP");

@Getter
private final String txIdSuffix;
Expand Down

0 comments on commit 8b22e65

Please sign in to comment.