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

com.scalar.db.sql.springdata.twopc.RemotePrepareCommitPhaseOperations Maven / Gradle / Ivy

package com.scalar.db.sql.springdata.twopc;

import static com.google.common.base.Preconditions.checkNotNull;

import com.scalar.db.sql.springdata.exception.ScalarDbNonTransientException;
import com.scalar.db.sql.springdata.exception.ScalarDbTransientException;
import com.scalar.db.sql.springdata.exception.ScalarDbUnknownTransactionStateException;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class RemotePrepareCommitPhaseOperations {
  private final Operation prepare;
  @Nullable private final Operation validate;
  private final Operation commit;
  private final Operation rollback;

  @FunctionalInterface
  public interface Operation {
    /**
     * Execute a remote prepare/commit operation
     *
     * @param transactionId a transaction ID
     * @throws ScalarDbUnknownTransactionStateException if the operation fails due to a
     *     non-transient cause with an unknown final status. The exception contains {@code
     *     transactionId}. Note that the final 2PC transaction status is unknown. Whether the
     *     transaction is actually committed or not needs to be decided by the application side
     *     (e.g. check if the target record is expectedly updated)
     * @throws ScalarDbNonTransientException if the operation fails due to a non-transient cause.
     *     The exception contains {@code transactionId}
     * @throws ScalarDbTransientException if the operation fails due to a transient cause. The
     *     exception contains {@code transactionId}
     */
    void execute(String transactionId);
  }

  private RemotePrepareCommitPhaseOperations(
      @Nonnull Operation prepare,
      @Nullable Operation validate,
      @Nonnull Operation commit,
      @Nonnull Operation rollback) {
    this.prepare = checkNotNull(prepare);
    this.validate = validate;
    this.commit = checkNotNull(commit);
    this.rollback = checkNotNull(rollback);
  }

  @Nonnull
  public static RemotePrepareCommitPhaseOperations createSerializable(
      @Nonnull Operation prepare,
      @Nonnull Operation validate,
      @Nonnull Operation commit,
      @Nonnull Operation rollback) {
    return new RemotePrepareCommitPhaseOperations(prepare, validate, commit, rollback);
  }

  @Nonnull
  public static RemotePrepareCommitPhaseOperations createSnapshot(
      @Nonnull Operation prepare, @Nonnull Operation commit, @Nonnull Operation rollback) {
    return new RemotePrepareCommitPhaseOperations(prepare, null, commit, rollback);
  }

  void prepare(String transactionId) {
    prepare.execute(transactionId);
  }

  void validate(String transactionId) {
    if (validate != null) {
      validate.execute(transactionId);
    }
  }

  void commit(String transactionId) {
    commit.execute(transactionId);
  }

  void rollback(String transactionId) {
    rollback.execute(transactionId);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy