Skip to content
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

Add Snapshot-read-only isolation support #79

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
}
};
}

Original file line number Diff line number Diff line change
@@ -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;
@@ -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() {
Original file line number Diff line number Diff line change
@@ -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;