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

io.shardingsphere.opentracing.sampling.SamplingService Maven / Gradle / Ivy

There is a newer version: 3.1.0
Show newest version
/*
 * Copyright 2016-2018 shardingsphere.io.
 * 

* 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 io.shardingsphere.opentracing.sampling; import lombok.AccessLevel; import lombok.NoArgsConstructor; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.TimeUnit; /** * Sampling control service. * * @author chenqingyang */ @NoArgsConstructor(access = AccessLevel.PRIVATE) public final class SamplingService { private static final SamplingService INSTANCE = new SamplingService(); private int sampleNumPM; private volatile boolean on; private volatile AtomicInteger samplingCount; private volatile ScheduledFuture scheduledFuture; /** * Get sampling service instance. * * @return sampling service instance */ public static SamplingService getInstance() { return INSTANCE; } /** * sampling service init. * * @param sampleNumPM sampling num in one minutes */ public void init(final int sampleNumPM) { this.sampleNumPM = sampleNumPM; if (scheduledFuture != null) { scheduledFuture.cancel(true); } if (this.sampleNumPM > 0) { on = true; this.resetSamplingFactor(); ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() { private final AtomicInteger threadIndex = new AtomicInteger(0); @Override public Thread newThread(final Runnable r) { Thread thread = new Thread(r); thread.setName("Sharding-opentracing-sampling" + threadIndex.incrementAndGet()); thread.setDaemon(true); return thread; } }); scheduledFuture = service.scheduleAtFixedRate(new Runnable() { @Override public void run() { resetSamplingFactor(); } }, 0, 1, TimeUnit.MINUTES); } } /** * Is sampling allowed. * * @return true, if sampling mechanism is on. */ public boolean trySampling() { return !on || samplingCount.get() <= sampleNumPM; } /** * sampling count. */ public void samplingAdd() { if (on) { samplingCount.getAndIncrement(); } } private void resetSamplingFactor() { samplingCount = new AtomicInteger(0); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy