use of java.util.concurrent.Semaphore in project elasticsearch by elastic.
the class IndexShardTests method testAsyncFsync.
public void testAsyncFsync() throws InterruptedException, IOException {
IndexShard shard = newStartedShard();
Semaphore semaphore = new Semaphore(Integer.MAX_VALUE);
Thread[] thread = new Thread[randomIntBetween(3, 5)];
CountDownLatch latch = new CountDownLatch(thread.length);
for (int i = 0; i < thread.length; i++) {
thread[i] = new Thread() {
@Override
public void run() {
try {
latch.countDown();
latch.await();
for (int i = 0; i < 10000; i++) {
semaphore.acquire();
shard.sync(TranslogTests.randomTranslogLocation(), (ex) -> semaphore.release());
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
};
thread[i].start();
}
for (int i = 0; i < thread.length; i++) {
thread[i].join();
}
assertTrue(semaphore.tryAcquire(Integer.MAX_VALUE, 10, TimeUnit.SECONDS));
closeShards(shard);
}
use of java.util.concurrent.Semaphore in project tomcat by apache.
the class SemaphoreValve method startInternal.
/**
* Start this component and implement the requirements
* of {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
*
* @exception LifecycleException if this component detects a fatal error
* that prevents this component from being used
*/
@Override
protected synchronized void startInternal() throws LifecycleException {
semaphore = new Semaphore(concurrency, fairness);
setState(LifecycleState.STARTING);
}
use of java.util.concurrent.Semaphore in project zookeeper by apache.
the class FLENewEpochTest method setUp.
@Before
public void setUp() throws Exception {
count = 3;
peers = new HashMap<Long, QuorumServer>(count);
threads = new ArrayList<LEThread>(count);
tmpdir = new File[count];
port = new int[count];
round = new int[3];
round[0] = 0;
round[1] = 0;
round[2] = 0;
start0 = new Semaphore(0);
finish0 = new Semaphore(0);
finish3 = new Semaphore(0);
}
use of java.util.concurrent.Semaphore in project zookeeper by apache.
the class FLERestartTest method setUp.
@Before
public void setUp() throws Exception {
count = 3;
peers = new HashMap<Long, QuorumServer>(count);
restartThreads = new ArrayList<FLERestartThread>(count);
tmpdir = new File[count];
port = new int[count];
finish = new Semaphore(0);
}
use of java.util.concurrent.Semaphore in project elasticsearch by elastic.
the class AsyncIOProcessorTests method testRandomFail.
public void testRandomFail() throws InterruptedException {
AtomicInteger received = new AtomicInteger(0);
AtomicInteger failed = new AtomicInteger(0);
AtomicInteger actualFailed = new AtomicInteger(0);
AsyncIOProcessor<Object> processor = new AsyncIOProcessor<Object>(logger, scaledRandomIntBetween(1, 2024)) {
@Override
protected void write(List<Tuple<Object, Consumer<Exception>>> candidates) throws IOException {
received.addAndGet(candidates.size());
if (randomBoolean()) {
failed.addAndGet(candidates.size());
if (randomBoolean()) {
throw new IOException();
} else {
throw new RuntimeException();
}
}
}
};
Semaphore semaphore = new Semaphore(Integer.MAX_VALUE);
final int count = randomIntBetween(1000, 20000);
Thread[] thread = new Thread[randomIntBetween(3, 10)];
CountDownLatch latch = new CountDownLatch(thread.length);
for (int i = 0; i < thread.length; i++) {
thread[i] = new Thread() {
@Override
public void run() {
try {
latch.countDown();
latch.await();
for (int i = 0; i < count; i++) {
semaphore.acquire();
processor.put(new Object(), (ex) -> {
if (ex != null) {
actualFailed.incrementAndGet();
}
semaphore.release();
});
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
;
};
thread[i].start();
}
for (int i = 0; i < thread.length; i++) {
thread[i].join();
}
assertTrue(semaphore.tryAcquire(Integer.MAX_VALUE, 10, TimeUnit.SECONDS));
assertEquals(count * thread.length, received.get());
assertEquals(actualFailed.get(), failed.get());
}
Aggregations