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

com.aerospike.kafka.connect.sink.Counter Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2016-2017 Aerospike, Inc.
 *
 * Portions may be licensed to Aerospike, Inc. under one or more contributor
 * license agreements WHICH ARE COMPATIBLE WITH THE APACHE LICENSE, VERSION 2.0.
 *
 * 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.aerospike.kafka.connect.sink;

import java.util.concurrent.atomic.AtomicInteger;

import org.apache.kafka.connect.errors.ConnectException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * Atomic counter to keep track of number of asynchronous, in-flight
 * requests
 */
class Counter {
    private static final long DEFAULT_SLEEP_INTERVAL_MS = 1;

    private static final Logger log = LoggerFactory.getLogger(Counter.class);

    private AtomicInteger counter;
    private final long sleepMs;

    public Counter() {
        this(DEFAULT_SLEEP_INTERVAL_MS);
    }

    public Counter(long sleepMs) {
        this.sleepMs = sleepMs;
        counter = new AtomicInteger(0);
    }

    public void increment() {
        counter.incrementAndGet();
    }

    public void decrement() {
        counter.decrementAndGet();
    }

    public void waitUntilZero() {
        try {
            int count;
            while ((count = counter.get()) > 0) {
                log.trace("Waiting " + sleepMs + "ms for counter to reach zero - current: " + count);
                Thread.sleep(sleepMs);
            }
        } catch (InterruptedException e) {
            throw new ConnectException("Interrupted while waiting to complete in-flight requests", e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy