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

pl.allegro.tech.hermes.infrastructure.zookeeper.counter.SharedCounter Maven / Gradle / Ivy

There is a newer version: 2.7.0
Show newest version
package pl.allegro.tech.hermes.infrastructure.zookeeper.counter;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.atomic.DistributedAtomicLong;
import org.apache.curator.retry.ExponentialBackoffRetry;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

public class SharedCounter {

    private final LoadingCache distributedAtomicLongs;

    public SharedCounter(CuratorFramework curatorClient, Duration expireAfter,
                         Duration distributedLoaderBackoff, int distributedLoaderRetries) {
        distributedAtomicLongs = CacheBuilder.newBuilder()
                .expireAfterAccess(expireAfter.toHours(), TimeUnit.HOURS)
                .build(new DistributedAtomicLongLoader(
                                curatorClient,
                                new ExponentialBackoffRetry((int) distributedLoaderBackoff.toMillis(), distributedLoaderRetries))
                );
    }

    public boolean increment(String path, long count) {
        try {
            return distributedAtomicLongs.get(path).add(count).succeeded();
        } catch (Exception e) {
            throw new ZookeeperCounterException(path, e);
        }
    }

    public long getValue(String path) {
        try {
            return distributedAtomicLongs.get(path).get().preValue();
        } catch (Exception e) {
            throw new ZookeeperCounterException(path, e);
        }
    }

    private static final class DistributedAtomicLongLoader extends CacheLoader {

        private final CuratorFramework client;

        private final RetryPolicy retryPolicy;

        DistributedAtomicLongLoader(CuratorFramework client, RetryPolicy retryPolicy) {
            this.client = client;
            this.retryPolicy = retryPolicy;
        }

        @Override
        public DistributedAtomicLong load(String key) throws Exception {
            return new DistributedAtomicLong(client, key, retryPolicy);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy