use of com.google.common.util.concurrent.RateLimiter in project cassandra by apache.
the class TestRateLimiter method acquire.
/**
* Acquires a single unit at the given rate. If the rate changes between calls, a new rate limiter is created
* and the old one is discarded.
*/
public void acquire(double rate) {
RateLimiter limiter = ref.get();
if (limiter == null || limiter.getRate() != rate) {
ref.compareAndSet(limiter, RateLimiter.create(rate));
limiter = ref.get();
}
limiter.acquire(1);
}
use of com.google.common.util.concurrent.RateLimiter in project cassandra by apache.
the class Keyspace method clearSnapshot.
/**
* Clear all the snapshots for a given keyspace.
*
* @param snapshotName the user supplied snapshot name. It empty or null,
* all the snapshots will be cleaned
*/
public static void clearSnapshot(String snapshotName, String keyspace) {
RateLimiter clearSnapshotRateLimiter = DatabaseDescriptor.getSnapshotRateLimiter();
List<File> tableDirectories = Directories.getKSChildDirectories(keyspace);
Directories.clearSnapshot(snapshotName, tableDirectories, clearSnapshotRateLimiter);
}
use of com.google.common.util.concurrent.RateLimiter in project cassandra by apache.
the class FileTest method testDeletes.
@Test
public void testDeletes() throws IOException {
File subdir = new File(dir, "deletes");
File file = new File(dir, "f");
subdir.tryCreateDirectory();
Assert.assertTrue(new File(subdir, "subsubdir").tryCreateDirectory());
subdir.deleteRecursive();
Assert.assertFalse(subdir.exists());
subdir.tryCreateDirectory();
file.createFileIfNotExists();
Assert.assertTrue(new File(subdir, "subsubdir").tryCreateDirectory());
long start = System.nanoTime();
RateLimiter rateLimiter = RateLimiter.create(2);
subdir.deleteRecursive(rateLimiter);
file.delete(rateLimiter);
long end = System.nanoTime();
Assert.assertTrue("" + NANOSECONDS.toMillis(end - start), SECONDS.toNanos(1) <= end - start);
Assert.assertFalse(subdir.exists());
Assert.assertFalse(file.exists());
}
use of com.google.common.util.concurrent.RateLimiter in project alluxio by Alluxio.
the class StressMasterBench method runLocal.
@Override
@SuppressFBWarnings("BC_UNCONFIRMED_CAST")
public MasterBenchTaskResult runLocal() throws Exception {
ExecutorService service = ExecutorServiceFactories.fixedThreadPool("bench-thread", mParameters.mThreads).create();
RateLimiter rateLimiter = RateLimiter.create(mParameters.mTargetThroughput);
long fileSize = FormatUtils.parseSpaceSize(mParameters.mCreateFileSize);
mFiledata = new byte[(int) Math.min(fileSize, StressConstants.WRITE_FILE_ONCE_MAX_BYTES)];
Arrays.fill(mFiledata, (byte) 0x7A);
long durationMs = FormatUtils.parseTimeSize(mParameters.mDuration);
long warmupMs = FormatUtils.parseTimeSize(mParameters.mWarmup);
long startMs = mBaseParameters.mStartMs;
if (mBaseParameters.mStartMs == BaseParameters.UNDEFINED_START_MS) {
startMs = CommonUtils.getCurrentMs() + 1000;
}
long endMs = startMs + warmupMs + durationMs;
BenchContext context = new BenchContext(rateLimiter, startMs, endMs);
List<Callable<Void>> callables = new ArrayList<>(mParameters.mThreads);
for (int i = 0; i < mParameters.mThreads; i++) {
callables.add(getBenchThread(context, i));
}
LOG.info("Starting {} bench threads", callables.size());
service.invokeAll(callables, FormatUtils.parseTimeSize(mBaseParameters.mBenchTimeout), TimeUnit.MILLISECONDS);
LOG.info("Bench threads finished");
service.shutdownNow();
service.awaitTermination(30, TimeUnit.SECONDS);
if (!mBaseParameters.mProfileAgent.isEmpty()) {
context.addAdditionalResult();
}
return context.getResult();
}
use of com.google.common.util.concurrent.RateLimiter in project micro-service by Lovnx.
the class ApiCallTask method call.
private void call() {
ExecutorService executor = Executors.newFixedThreadPool(threadNum);
final RateLimiter rateLimiter = RateLimiter.create(permitsPerSecond);
for (int i = 0; i < threadNum; i++) {
executor.execute(new ApiCallTask(rateLimiter));
}
executor.shutdown();
}
Aggregations