Search in sources :

Example 61 with RateLimiter

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);
}
Also used : RateLimiter(com.google.common.util.concurrent.RateLimiter)

Example 62 with RateLimiter

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);
}
Also used : File(org.apache.cassandra.io.util.File) RateLimiter(com.google.common.util.concurrent.RateLimiter)

Example 63 with RateLimiter

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());
}
Also used : RateLimiter(com.google.common.util.concurrent.RateLimiter) Test(org.junit.Test)

Example 64 with RateLimiter

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();
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) RateLimiter(com.google.common.util.concurrent.RateLimiter) Callable(java.util.concurrent.Callable) SuppressFBWarnings(alluxio.annotation.SuppressFBWarnings)

Example 65 with RateLimiter

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();
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) RateLimiter(com.google.common.util.concurrent.RateLimiter)

Aggregations

RateLimiter (com.google.common.util.concurrent.RateLimiter)74 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)22 BigInteger (java.math.BigInteger)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)14 ThreadPoolTaskExecutor (org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)14 Test (org.junit.Test)13 ParameterException (com.beust.jcommander.ParameterException)12 ArrayList (java.util.ArrayList)12 ExecutorService (java.util.concurrent.ExecutorService)11 IOException (java.io.IOException)10 ApplicationContext (org.springframework.context.ApplicationContext)10 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)10 Random (java.util.Random)9 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)9 Service (org.fisco.bcos.channel.client.Service)9 Web3j (org.fisco.bcos.web3j.protocol.Web3j)9 ChannelEthereumService (org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService)9 TransactionReceipt (org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt)9 List (java.util.List)8 Credentials (org.fisco.bcos.web3j.crypto.Credentials)8