com.scalar.db.transaction.consensuscommit.CommitHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scalardb Show documentation
Show all versions of scalardb Show documentation
A universal transaction manager that achieves database-agnostic transactions and distributed transactions that span multiple databases
package com.scalar.db.transaction.consensuscommit;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.collect.ImmutableList;
import com.scalar.db.api.DistributedStorage;
import com.scalar.db.api.TransactionState;
import com.scalar.db.exception.storage.ExecutionException;
import com.scalar.db.exception.storage.NoMutationException;
import com.scalar.db.exception.storage.RetriableExecutionException;
import com.scalar.db.exception.transaction.CommitConflictException;
import com.scalar.db.exception.transaction.CommitException;
import com.scalar.db.exception.transaction.PreparationConflictException;
import com.scalar.db.exception.transaction.PreparationException;
import com.scalar.db.exception.transaction.UnknownTransactionStatusException;
import com.scalar.db.exception.transaction.ValidationConflictException;
import com.scalar.db.exception.transaction.ValidationException;
import com.scalar.db.transaction.consensuscommit.ParallelExecutor.ParallelExecutorTask;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import javax.annotation.concurrent.ThreadSafe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ThreadSafe
public class CommitHandler {
private static final Logger logger = LoggerFactory.getLogger(CommitHandler.class);
private final DistributedStorage storage;
private final Coordinator coordinator;
private final TransactionTableMetadataManager tableMetadataManager;
private final ParallelExecutor parallelExecutor;
@SuppressFBWarnings("EI_EXPOSE_REP2")
public CommitHandler(
DistributedStorage storage,
Coordinator coordinator,
TransactionTableMetadataManager tableMetadataManager,
ParallelExecutor parallelExecutor) {
this.storage = checkNotNull(storage);
this.coordinator = checkNotNull(coordinator);
this.tableMetadataManager = checkNotNull(tableMetadataManager);
this.parallelExecutor = checkNotNull(parallelExecutor);
}
public void commit(Snapshot snapshot) throws CommitException, UnknownTransactionStatusException {
try {
prepare(snapshot);
} catch (PreparationException e) {
abortState(snapshot.getId());
rollbackRecords(snapshot);
if (e instanceof PreparationConflictException) {
throw new CommitConflictException(e.getMessage(), e, e.getTransactionId().orElse(null));
}
throw new CommitException(e.getMessage(), e, e.getTransactionId().orElse(null));
}
try {
validate(snapshot);
} catch (ValidationException e) {
abortState(snapshot.getId());
rollbackRecords(snapshot);
if (e instanceof ValidationConflictException) {
throw new CommitConflictException(e.getMessage(), e, e.getTransactionId().orElse(null));
}
throw new CommitException(e.getMessage(), e, e.getTransactionId().orElse(null));
}
commitState(snapshot);
commitRecords(snapshot);
}
public void prepare(Snapshot snapshot) throws PreparationException {
try {
prepareRecords(snapshot);
} catch (NoMutationException e) {
throw new PreparationConflictException("preparing record exists", e, snapshot.getId());
} catch (RetriableExecutionException e) {
throw new PreparationConflictException(
"conflict happened when preparing records", e, snapshot.getId());
} catch (ExecutionException e) {
throw new PreparationException("preparing records failed", e, snapshot.getId());
}
}
private void prepareRecords(Snapshot snapshot)
throws ExecutionException, PreparationConflictException {
PrepareMutationComposer composer =
new PrepareMutationComposer(snapshot.getId(), tableMetadataManager);
snapshot.to(composer);
PartitionedMutations mutations = new PartitionedMutations(composer.get());
ImmutableList orderedKeys = mutations.getOrderedKeys();
List tasks = new ArrayList<>(orderedKeys.size());
for (PartitionedMutations.Key key : orderedKeys) {
tasks.add(() -> storage.mutate(mutations.get(key)));
}
parallelExecutor.prepare(tasks, snapshot.getId());
}
public void validate(Snapshot snapshot) throws ValidationException {
try {
// validation is executed when SERIALIZABLE with EXTRA_READ strategy is chosen.
snapshot.toSerializableWithExtraRead(storage);
} catch (ExecutionException e) {
throw new ValidationException("validation failed", e, snapshot.getId());
}
}
public void commitState(Snapshot snapshot)
throws CommitException, UnknownTransactionStatusException {
String id = snapshot.getId();
try {
Coordinator.State state = new Coordinator.State(id, TransactionState.COMMITTED);
coordinator.putState(state);
logger.debug(
"transaction {} is committed successfully at {}", id, System.currentTimeMillis());
} catch (CoordinatorConflictException e) {
try {
Optional s = coordinator.getState(id);
if (s.isPresent()) {
TransactionState state = s.get().getState();
if (state.equals(TransactionState.ABORTED)) {
rollbackRecords(snapshot);
throw new CommitException(
"committing state in coordinator failed. the transaction is aborted", e, id);
}
} else {
throw new UnknownTransactionStatusException(
"committing state failed with NoMutationException but the coordinator status doesn't exist",
e,
id);
}
} catch (CoordinatorException e1) {
throw new UnknownTransactionStatusException("can't get the state", e1, id);
}
} catch (CoordinatorException e) {
throw new UnknownTransactionStatusException("coordinator status is unknown", e, id);
}
}
public void commitRecords(Snapshot snapshot) {
try {
CommitMutationComposer composer = new CommitMutationComposer(snapshot.getId());
snapshot.to(composer);
PartitionedMutations mutations = new PartitionedMutations(composer.get());
ImmutableList orderedKeys = mutations.getOrderedKeys();
List tasks = new ArrayList<>(orderedKeys.size());
for (PartitionedMutations.Key key : orderedKeys) {
tasks.add(() -> storage.mutate(mutations.get(key)));
}
parallelExecutor.commitRecords(tasks, snapshot.getId());
} catch (Exception e) {
logger.warn("committing records failed. transaction ID: {}", snapshot.getId(), e);
// ignore since records are recovered lazily
}
}
public TransactionState abortState(String id) throws UnknownTransactionStatusException {
try {
Coordinator.State state = new Coordinator.State(id, TransactionState.ABORTED);
coordinator.putState(state);
return TransactionState.ABORTED;
} catch (CoordinatorConflictException e) {
try {
Optional state = coordinator.getState(id);
if (state.isPresent()) {
// successfully COMMITTED or ABORTED
return state.get().getState();
}
throw new UnknownTransactionStatusException(
"aborting state failed with NoMutationException but the coordinator status doesn't exist",
e,
id);
} catch (CoordinatorException e1) {
throw new UnknownTransactionStatusException("can't get the state", e1, id);
}
} catch (CoordinatorException e) {
throw new UnknownTransactionStatusException("coordinator status is unknown", e, id);
}
}
public void rollbackRecords(Snapshot snapshot) {
logger.debug("rollback from snapshot for {}", snapshot.getId());
try {
RollbackMutationComposer composer =
new RollbackMutationComposer(snapshot.getId(), storage, tableMetadataManager);
snapshot.to(composer);
PartitionedMutations mutations = new PartitionedMutations(composer.get());
ImmutableList orderedKeys = mutations.getOrderedKeys();
List tasks = new ArrayList<>(orderedKeys.size());
for (PartitionedMutations.Key key : orderedKeys) {
tasks.add(() -> storage.mutate(mutations.get(key)));
}
parallelExecutor.rollbackRecords(tasks, snapshot.getId());
} catch (Exception e) {
logger.warn("rolling back records failed. transaction ID: {}", snapshot.getId(), e);
// ignore since records are recovered lazily
}
}
}