Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
Expand Up @@ -81,6 +81,34 @@ public interface DistributedTransactionManager
DistributedTransaction begin(String txId)
throws TransactionNotFoundException, TransactionException;

/**
* Begins a new transaction in read-only mode.
*
* @return {@link DistributedTransaction}
* @throws TransactionNotFoundException if the transaction fails to begin due to transient faults.
* You can retry the transaction
* @throws TransactionException if the transaction fails to begin due to transient or nontransient
* faults. You can try retrying the transaction, but you may not be able to begin the
* transaction due to nontransient faults
*/
DistributedTransaction beginReadOnly() throws TransactionNotFoundException, TransactionException;

/**
* Begins a new transaction with the specified transaction ID in read-only mode. It is users'
* responsibility to guarantee uniqueness of the ID, so it is not recommended to use this method
* unless you know exactly what you are doing.
*
* @param txId an user-provided unique transaction ID
* @return {@link DistributedTransaction}
* @throws TransactionNotFoundException if the transaction fails to begin due to transient faults.
* You can retry the transaction
* @throws TransactionException if the transaction fails to begin due to transient or nontransient
* faults. You can try retrying the transaction, but you may not be able to begin the
* transaction due to nontransient faults
*/
DistributedTransaction beginReadOnly(String txId)
throws TransactionNotFoundException, TransactionException;

/**
* Starts a new transaction. This method is an alias of {@link #begin()}.
*
Expand Down Expand Up @@ -112,6 +140,39 @@ default DistributedTransaction start(String txId)
return begin(txId);
}

/**
* Starts a new transaction in read-only mode. This method is an alias of {@link
* #beginReadOnly()}.
*
* @return {@link DistributedTransaction}
* @throws TransactionNotFoundException if the transaction fails to start due to transient faults.
* You can retry the transaction
* @throws TransactionException if the transaction fails to start due to transient or nontransient
* faults. You can try retrying the transaction, but you may not be able to start the
* transaction due to nontransient faults
*/
default DistributedTransaction startReadOnly()
throws TransactionNotFoundException, TransactionException {
return beginReadOnly();
}

/**
* Starts a new transaction with the specified transaction ID in read-only mode. This method is an
* alias of {@link #beginReadOnly(String)}.
*
* @param txId an user-provided unique transaction ID
* @return {@link DistributedTransaction}
* @throws TransactionNotFoundException if the transaction fails to start due to transient faults.
* You can retry the transaction
* @throws TransactionException if the transaction fails to start due to transient or nontransient
* faults. You can try retrying the transaction, but you may not be able to start the
* transaction due to nontransient faults
*/
default DistributedTransaction startReadOnly(String txId)
throws TransactionNotFoundException, TransactionException {
return beginReadOnly(txId);
}

/**
* Starts a new transaction with the specified {@link Isolation} level.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;
import javax.annotation.concurrent.ThreadSafe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ThreadSafe
public class ActiveTransactionManagedDistributedTransactionManager
extends DecoratedDistributedTransactionManager {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;
import javax.annotation.concurrent.ThreadSafe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ThreadSafe
public class ActiveTransactionManagedTwoPhaseCommitTransactionManager
extends DecoratedTwoPhaseCommitTransactionManager {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ public DistributedTransaction begin(String txId) throws TransactionException {
return decorateTransactionOnBeginOrStart(transactionManager.begin(txId));
}

@Override
public DistributedTransaction beginReadOnly() throws TransactionException {
return decorateTransactionOnBeginOrStart(transactionManager.beginReadOnly());
}

@Override
public DistributedTransaction beginReadOnly(String txId) throws TransactionException {
return decorateTransactionOnBeginOrStart(transactionManager.beginReadOnly(txId));
}

@Override
public DistributedTransaction start() throws TransactionException {
return decorateTransactionOnBeginOrStart(transactionManager.start());
Expand All @@ -86,6 +96,16 @@ public DistributedTransaction start(String txId) throws TransactionException {
return decorateTransactionOnBeginOrStart(transactionManager.start(txId));
}

@Override
public DistributedTransaction startReadOnly(String txId) throws TransactionException {
return decorateTransactionOnBeginOrStart(transactionManager.startReadOnly(txId));
}

@Override
public DistributedTransaction startReadOnly() throws TransactionException {
return decorateTransactionOnBeginOrStart(transactionManager.startReadOnly());
}

/** @deprecated As of release 2.4.0. Will be removed in release 4.0.0. */
@Deprecated
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.scalar.db.common;

import com.scalar.db.api.Delete;
import com.scalar.db.api.DistributedTransaction;
import com.scalar.db.api.Insert;
import com.scalar.db.api.Mutation;
import com.scalar.db.api.Put;
import com.scalar.db.api.Update;
import com.scalar.db.api.Upsert;
import com.scalar.db.common.error.CoreError;
import com.scalar.db.exception.transaction.CrudException;
import java.util.List;
import javax.annotation.concurrent.NotThreadSafe;

@NotThreadSafe
public class ReadOnlyDistributedTransaction extends DecoratedDistributedTransaction {

public ReadOnlyDistributedTransaction(DistributedTransaction transaction) {
super(transaction);
}

/** @deprecated As of release 3.13.0. Will be removed in release 5.0.0. */
@Deprecated
@Override
public void put(Put put) throws CrudException {
throw new IllegalStateException(
CoreError.MUTATION_NOT_ALLOWED_IN_READ_ONLY_TRANSACTION.buildMessage(getId()));
}

/** @deprecated As of release 3.13.0. Will be removed in release 5.0.0. */
@Deprecated
@Override
public void put(List<Put> puts) throws CrudException {
throw new IllegalStateException(
CoreError.MUTATION_NOT_ALLOWED_IN_READ_ONLY_TRANSACTION.buildMessage(getId()));
}

@Override
public void insert(Insert insert) throws CrudException {
throw new IllegalStateException(
CoreError.MUTATION_NOT_ALLOWED_IN_READ_ONLY_TRANSACTION.buildMessage(getId()));
}

@Override
public void upsert(Upsert upsert) throws CrudException {
throw new IllegalStateException(
CoreError.MUTATION_NOT_ALLOWED_IN_READ_ONLY_TRANSACTION.buildMessage(getId()));
}

@Override
public void update(Update update) throws CrudException {
throw new IllegalStateException(
CoreError.MUTATION_NOT_ALLOWED_IN_READ_ONLY_TRANSACTION.buildMessage(getId()));
}

@Override
public void delete(Delete delete) throws CrudException {
throw new IllegalStateException(
CoreError.MUTATION_NOT_ALLOWED_IN_READ_ONLY_TRANSACTION.buildMessage(getId()));
}

/** @deprecated As of release 3.13.0. Will be removed in release 5.0.0. */
@Deprecated
@Override
public void delete(List<Delete> deletes) throws CrudException {
throw new IllegalStateException(
CoreError.MUTATION_NOT_ALLOWED_IN_READ_ONLY_TRANSACTION.buildMessage(getId()));
}

@Override
public void mutate(List<? extends Mutation> mutations) throws CrudException {
throw new IllegalStateException(
CoreError.MUTATION_NOT_ALLOWED_IN_READ_ONLY_TRANSACTION.buildMessage(getId()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import com.scalar.db.exception.transaction.UnknownTransactionStatusException;
import java.util.List;
import java.util.Optional;
import javax.annotation.concurrent.ThreadSafe;

@ThreadSafe
public class StateManagedDistributedTransactionManager
extends DecoratedDistributedTransactionManager {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import com.scalar.db.exception.transaction.ValidationException;
import java.util.List;
import java.util.Optional;
import javax.annotation.concurrent.ThreadSafe;

@ThreadSafe
public class StateManagedTwoPhaseCommitTransactionManager
extends DecoratedTwoPhaseCommitTransactionManager {

Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/com/scalar/db/common/error/CoreError.java
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,12 @@ public enum CoreError implements ScalarDbError {
Category.USER_ERROR, "0209", "Number of max threads must be greater than 0", "", ""),
DATA_LOADER_INVALID_DATA_CHUNK_QUEUE_SIZE(
Category.USER_ERROR, "0210", "Data chunk queue size must be greater than 0", "", ""),
MUTATION_NOT_ALLOWED_IN_READ_ONLY_TRANSACTION(
Category.USER_ERROR,
"0211",
"Mutations are not allowed in read-only transactions. Transaction ID: %s",
"",
""),

//
// Errors for the concurrency error category
Expand Down
20 changes: 20 additions & 0 deletions core/src/main/java/com/scalar/db/service/TransactionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ public DistributedTransaction begin(String txId) throws TransactionException {
return manager.begin(txId);
}

@Override
public DistributedTransaction beginReadOnly() throws TransactionException {
return manager.beginReadOnly();
}

@Override
public DistributedTransaction beginReadOnly(String txId) throws TransactionException {
return manager.beginReadOnly(txId);
}

@Override
public DistributedTransaction start() throws TransactionException {
return manager.start();
Expand All @@ -91,6 +101,16 @@ public DistributedTransaction start(String txId) throws TransactionException {
return manager.start(txId);
}

@Override
public DistributedTransaction startReadOnly() throws TransactionException {
return manager.startReadOnly();
}

@Override
public DistributedTransaction startReadOnly(String txId) throws TransactionException {
return manager.startReadOnly(txId);
}

/** @deprecated As of release 2.4.0. Will be removed in release 4.0.0. */
@Deprecated
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class SelectStatementHandler {
* @param client {@code DynamoDbClient}
* @param metadataManager {@code TableMetadataManager}
* @param namespacePrefix a namespace prefix
* @param fetchSize the number of items to fetch in each request
*/
@SuppressFBWarnings("EI_EXPOSE_REP2")
public SelectStatementHandler(
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/com/scalar/db/storage/jdbc/JdbcAdmin.java
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ public TableMetadata getTableMetadata(String namespace, String table) throws Exe
boolean tableExists = false;

try (Connection connection = dataSource.getConnection()) {
rdbEngine.setReadOnly(connection, true);
rdbEngine.setConnectionToReadOnly(connection, true);

try (PreparedStatement preparedStatement =
connection.prepareStatement(getSelectColumnsStatement())) {
Expand Down Expand Up @@ -510,7 +510,7 @@ public TableMetadata getImportTableMetadata(
}

try (Connection connection = dataSource.getConnection()) {
rdbEngine.setReadOnly(connection, true);
rdbEngine.setConnectionToReadOnly(connection, true);

String catalogName = rdbEngine.getCatalogName(namespace);
String schemaName = rdbEngine.getSchemaName(namespace);
Expand Down Expand Up @@ -608,7 +608,7 @@ public Set<String> getNamespaceTableNames(String namespace) throws ExecutionExce
+ enclose(METADATA_COL_FULL_TABLE_NAME)
+ " LIKE ?";
try (Connection connection = dataSource.getConnection()) {
rdbEngine.setReadOnly(connection, true);
rdbEngine.setConnectionToReadOnly(connection, true);

try (PreparedStatement preparedStatement =
connection.prepareStatement(selectTablesOfNamespaceStatement)) {
Expand Down Expand Up @@ -644,7 +644,7 @@ public boolean namespaceExists(String namespace) throws ExecutionException {
+ enclose(NAMESPACE_COL_NAMESPACE_NAME)
+ " = ?";
try (Connection connection = dataSource.getConnection()) {
rdbEngine.setReadOnly(connection, true);
rdbEngine.setConnectionToReadOnly(connection, true);

try (PreparedStatement statement = connection.prepareStatement(selectQuery)) {
statement.setString(1, namespace);
Expand Down Expand Up @@ -992,7 +992,7 @@ private String encloseFullTableName(String schema, String table) {
@Override
public Set<String> getNamespaceNames() throws ExecutionException {
try (Connection connection = dataSource.getConnection()) {
rdbEngine.setReadOnly(connection, true);
rdbEngine.setConnectionToReadOnly(connection, true);

String selectQuery =
"SELECT * FROM " + encloseFullTableName(metadataSchema, NAMESPACES_TABLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public Optional<Result> get(Get get) throws ExecutionException {
Connection connection = null;
try {
connection = dataSource.getConnection();
rdbEngine.setReadOnly(connection, true);
rdbEngine.setConnectionToReadOnly(connection, true);
return jdbcService.get(get, connection);
} catch (SQLException e) {
throw new ExecutionException(
Expand All @@ -101,7 +101,7 @@ public Scanner scan(Scan scan) throws ExecutionException {
try {
connection = dataSource.getConnection();
connection.setAutoCommit(false);
rdbEngine.setReadOnly(connection, true);
rdbEngine.setConnectionToReadOnly(connection, true);
return jdbcService.getScanner(scan, connection);
} catch (SQLException e) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public RdbEngineTimeTypeStrategy<Integer, Long, Long, Long> getTimeTypeStrategy(
}

@Override
public void setReadOnly(Connection connection, boolean readOnly) {
public void setConnectionToReadOnly(Connection connection, boolean readOnly) {
// Do nothing. SQLite does not support read-only mode.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ default void throwIfDuplicatedIndexWarning(SQLWarning warning) throws SQLExcepti
// Do nothing
}

default void setReadOnly(Connection connection, boolean readOnly) throws SQLException {
default void setConnectionToReadOnly(Connection connection, boolean readOnly)
throws SQLException {
connection.setReadOnly(readOnly);
}
}
Loading