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

com.palantir.atlasdb.transaction.impl.AbstractTransactionManager Maven / Gradle / Ivy

There is a newer version: 0.1152.0
Show newest version
/*
 * (c) Copyright 2018 Palantir Technologies Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.palantir.atlasdb.transaction.impl;

import com.palantir.atlasdb.cache.TimestampCache;
import com.palantir.atlasdb.health.MetricsBasedTimelockHealthCheck;
import com.palantir.atlasdb.health.TimelockHealthCheck;
import com.palantir.atlasdb.transaction.api.TimelockServiceStatus;
import com.palantir.atlasdb.transaction.api.Transaction;
import com.palantir.atlasdb.transaction.api.TransactionFailedException;
import com.palantir.atlasdb.transaction.api.TransactionManager;
import com.palantir.atlasdb.transaction.api.TransactionTask;
import com.palantir.atlasdb.util.MetricsManager;
import com.palantir.logsafe.Preconditions;
import java.util.Optional;
import java.util.concurrent.ExecutorService;

public abstract class AbstractTransactionManager implements TransactionManager {
    final TimestampCache timestampValidationReadCache;
    private volatile boolean closed = false;

    private final TimelockHealthCheck timelockHealthCheck;

    AbstractTransactionManager(MetricsManager metricsManager, TimestampCache timestampCache) {
        this.timelockHealthCheck = new MetricsBasedTimelockHealthCheck(metricsManager);
        this.timestampValidationReadCache = timestampCache;
    }

    protected boolean shouldStopRetrying(@SuppressWarnings("unused") int numTimesFailed) {
        return false;
    }

    protected final  T runTaskThrowOnConflict(TransactionTask task, Transaction txn)
            throws E, TransactionFailedException {
        checkOpen();
        try {
            T ret = task.execute(txn);
            if (txn.isUncommitted()) {
                txn.commit();
            }
            return ret;
        } finally {
            // Make sure that anyone trying to retain a reference to this transaction
            // will not be able to use it.
            if (txn.isUncommitted()) {
                txn.abort();
            }
        }
    }

    protected final  T runTaskThrowOnConflictWithCallback(
            TransactionTask task, CallbackAwareTransaction txn, Runnable callback)
            throws E, TransactionFailedException {
        try {
            checkOpen();
            try {
                T ret = task.execute(txn);
                if (txn.isUncommitted()) {
                    txn.commitWithoutCallbacks();
                }
                return ret;
            } finally {
                // Make sure that anyone trying to retain a reference to this transaction
                // will not be able to use it.
                if (txn.isUncommitted()) {
                    txn.abort();
                }
            }
        } finally {
            callback.run();
            txn.reportExpectationsCollectedData();
            txn.runSuccessCallbacksIfDefinitivelyCommitted();
        }
    }

    @Override
    public void close() {
        this.closed = true;
    }

    /**
     * Checks that the transaction manager is open.
     *
     * @throws IllegalStateException if the transaction manager has been closed.
     */
    protected void checkOpen() {
        Preconditions.checkState(!this.closed, "Operations cannot be performed on closed TransactionManager.");
    }

    @Override
    public void clearTimestampCache() {
        timestampValidationReadCache.clear();
    }

    ExecutorService createGetRangesExecutor(int numThreads, Optional sharedExecutorSize) {
        return GetRangesExecutors.createGetRangesExecutor(
                numThreads, AbstractTransactionManager.this.getClass().getSimpleName(), sharedExecutorSize);
    }

    @Override
    public TimelockServiceStatus getTimelockServiceStatus() {
        return timelockHealthCheck.getStatus();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy