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

com.palantir.atlasdb.stream.StandardPeriodicBackoffStrategy Maven / Gradle / Ivy

There is a newer version: 0.1193.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.stream;

import com.google.common.annotations.VisibleForTesting;
import com.palantir.logsafe.SafeArg;
import com.palantir.logsafe.logger.SafeLogger;
import com.palantir.logsafe.logger.SafeLoggerFactory;
import java.util.function.Supplier;

public class StandardPeriodicBackoffStrategy implements StreamStoreBackoffStrategy {
    private static final SafeLogger log = SafeLoggerFactory.get(StandardPeriodicBackoffStrategy.class);

    private final Supplier persistenceConfiguration;
    private final BackoffMechanism backoff;

    @VisibleForTesting
    StandardPeriodicBackoffStrategy(
            Supplier persistenceConfiguration, BackoffMechanism backoff) {
        this.persistenceConfiguration = persistenceConfiguration;
        this.backoff = backoff;
    }

    public static StandardPeriodicBackoffStrategy create(
            Supplier persistenceConfiguration) {
        return new StandardPeriodicBackoffStrategy(persistenceConfiguration, Thread::sleep);
    }

    @Override
    public void accept(long blockNumber) {
        if (persistenceConfiguration.get().writePauseDurationMillis() == 0) {
            return;
        }

        if (shouldBackoffBeforeWritingBlockNumber(blockNumber)) {
            try {
                long writePauseDurationMillis = persistenceConfiguration.get().writePauseDurationMillis();
                log.info(
                        "Invoking backoff for {} ms, because we are writing block {} of a stream",
                        SafeArg.of("blockNumber", blockNumber),
                        SafeArg.of("writePauseDurationMillis", writePauseDurationMillis));
                backoff.backoff(writePauseDurationMillis);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                // Preserving uninterruptibility, which is current behaviour.
            }
        }
    }

    private boolean shouldBackoffBeforeWritingBlockNumber(long blockNumber) {
        return blockNumber % persistenceConfiguration.get().numBlocksToWriteBeforePause() == 0;
    }

    interface BackoffMechanism {
        void backoff(long backoffAmount) throws InterruptedException;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy