All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scalar.db.common.ActiveTransactionManagedDistributedTransactionManager Maven / Gradle / Ivy

Go to download

A universal transaction manager that achieves database-agnostic transactions and distributed transactions that span multiple databases

There is a newer version: 3.14.0
Show newest version
package com.scalar.db.common;

import com.scalar.db.api.Delete;
import com.scalar.db.api.DistributedTransaction;
import com.scalar.db.api.Get;
import com.scalar.db.api.Mutation;
import com.scalar.db.api.Put;
import com.scalar.db.api.Result;
import com.scalar.db.api.Scan;
import com.scalar.db.config.DatabaseConfig;
import com.scalar.db.exception.transaction.AbortException;
import com.scalar.db.exception.transaction.CommitException;
import com.scalar.db.exception.transaction.CrudException;
import com.scalar.db.exception.transaction.RollbackException;
import com.scalar.db.exception.transaction.TransactionException;
import com.scalar.db.exception.transaction.TransactionNotFoundException;
import com.scalar.db.exception.transaction.UnknownTransactionStatusException;
import com.scalar.db.util.ActiveExpiringMap;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.List;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class ActiveTransactionManagedDistributedTransactionManager
    extends AbstractDistributedTransactionManager {

  private static final long TRANSACTION_EXPIRATION_INTERVAL_MILLIS = 1000;

  private static final Logger logger =
      LoggerFactory.getLogger(ActiveTransactionManagedDistributedTransactionManager.class);

  private final ActiveExpiringMap activeTransactions;

  public ActiveTransactionManagedDistributedTransactionManager(DatabaseConfig config) {
    super(config);
    activeTransactions =
        new ActiveExpiringMap<>(
            config.getActiveTransactionManagementExpirationTimeMillis(),
            TRANSACTION_EXPIRATION_INTERVAL_MILLIS,
            (id, t) -> {
              logger.warn("the transaction is expired. transaction ID: {}", id);
              try {
                t.rollback();
              } catch (Exception e) {
                logger.warn("rollback failed. transaction ID: {}", id, e);
              }
            });
  }

  private void add(ActiveTransaction transaction) throws TransactionException {
    if (activeTransactions.putIfAbsent(transaction.getId(), transaction).isPresent()) {
      transaction.rollback();
      throw new TransactionException("The transaction already exists", transaction.getId());
    }
  }

  private void remove(String transactionId) {
    activeTransactions.remove(transactionId);
  }

  @Override
  public DistributedTransaction resume(String txId) throws TransactionNotFoundException {
    return activeTransactions
        .get(txId)
        .orElseThrow(
            () ->
                new TransactionNotFoundException(
                    "A transaction associated with the specified transaction ID is not found. "
                        + "It might have been expired",
                    txId));
  }

  @Override
  protected DistributedTransaction decorate(DistributedTransaction transaction)
      throws TransactionException {
    return new ActiveTransaction(super.decorate(transaction));
  }

  private class ActiveTransaction extends AbstractDistributedTransaction
      implements DecoratedDistributedTransaction {

    private final DistributedTransaction transaction;

    @SuppressFBWarnings("EI_EXPOSE_REP2")
    private ActiveTransaction(DistributedTransaction transaction) throws TransactionException {
      this.transaction = transaction;
      add(this);
    }

    @Override
    public String getId() {
      return transaction.getId();
    }

    @Override
    public synchronized Optional get(Get get) throws CrudException {
      return transaction.get(get);
    }

    @Override
    public synchronized List scan(Scan scan) throws CrudException {
      return transaction.scan(scan);
    }

    @Override
    public synchronized void put(Put put) throws CrudException {
      transaction.put(put);
    }

    @Override
    public synchronized void put(List puts) throws CrudException {
      transaction.put(puts);
    }

    @Override
    public synchronized void delete(Delete delete) throws CrudException {
      transaction.delete(delete);
    }

    @Override
    public synchronized void delete(List deletes) throws CrudException {
      transaction.delete(deletes);
    }

    @Override
    public synchronized void mutate(List mutations) throws CrudException {
      transaction.mutate(mutations);
    }

    @Override
    public synchronized void commit() throws CommitException, UnknownTransactionStatusException {
      transaction.commit();
      remove(getId());
    }

    @Override
    public synchronized void rollback() throws RollbackException {
      try {
        transaction.rollback();
      } finally {
        remove(getId());
      }
    }

    @Override
    public void abort() throws AbortException {
      try {
        transaction.abort();
      } finally {
        remove(getId());
      }
    }

    @Override
    public DistributedTransaction getOriginalTransaction() {
      if (transaction instanceof DecoratedDistributedTransaction) {
        return ((DecoratedDistributedTransaction) transaction).getOriginalTransaction();
      }
      return transaction;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy