use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestInterProcessSemaphore method testMaxPerSession.
@Test
public void testMaxPerSession() throws Exception {
final int CLIENT_QTY = 10;
final int LOOP_QTY = 100;
final Random random = new Random();
final int SESSION_MAX = random.nextInt(75) + 25;
final Timing timing = new Timing();
List<Future<Object>> futures = Lists.newArrayList();
ExecutorService service = Executors.newCachedThreadPool();
final Counter counter = new Counter();
final AtomicInteger available = new AtomicInteger(SESSION_MAX);
for (int i = 0; i < CLIENT_QTY; ++i) {
futures.add(service.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
client.start();
try {
InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, "/test", SESSION_MAX);
for (int i = 0; i < LOOP_QTY; ++i) {
long start = System.currentTimeMillis();
int thisQty;
synchronized (available) {
if ((System.currentTimeMillis() - start) > 10000) {
throw new TimeoutException();
}
while (available.get() == 0) {
available.wait(timing.forWaiting().milliseconds());
}
thisQty = (available.get() > 1) ? (random.nextInt(available.get()) + 1) : 1;
available.addAndGet(-1 * thisQty);
Assert.assertTrue(available.get() >= 0);
}
Collection<Lease> leases = semaphore.acquire(thisQty, timing.forWaiting().seconds(), TimeUnit.SECONDS);
Assert.assertNotNull(leases);
try {
synchronized (counter) {
counter.currentCount += thisQty;
if (counter.currentCount > counter.maxCount) {
counter.maxCount = counter.currentCount;
}
}
Thread.sleep(random.nextInt(25));
} finally {
synchronized (counter) {
counter.currentCount -= thisQty;
}
semaphore.returnAll(leases);
synchronized (available) {
available.addAndGet(thisQty);
available.notifyAll();
}
}
}
} finally {
client.close();
}
return null;
}
}));
}
for (Future<Object> f : futures) {
f.get();
}
synchronized (counter) {
Assert.assertTrue(counter.currentCount == 0);
Assert.assertTrue(counter.maxCount > 0);
Assert.assertTrue(counter.maxCount <= SESSION_MAX);
System.out.println(counter.maxCount);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestInterProcessSemaphore method testSimple2.
@Test
public void testSimple2() throws Exception {
final int MAX_LEASES = 3;
Timing timing = new Timing();
List<Lease> leases = Lists.newArrayList();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
client.start();
try {
for (int i = 0; i < MAX_LEASES; ++i) {
InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, "/test", MAX_LEASES);
Lease lease = semaphore.acquire(timing.forWaiting().seconds(), TimeUnit.SECONDS);
Assert.assertNotNull(lease);
leases.add(lease);
}
InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, "/test", MAX_LEASES);
Lease lease = semaphore.acquire(timing.forWaiting().seconds(), TimeUnit.SECONDS);
Assert.assertNull(lease);
leases.remove(0).close();
Assert.assertNotNull(semaphore.acquire(timing.forWaiting().seconds(), TimeUnit.SECONDS));
} finally {
for (Lease l : leases) {
CloseableUtils.closeQuietly(l);
}
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestInterProcessSemaphore method testReleaseInChunks.
@Test
public void testReleaseInChunks() throws Exception {
final Timing timing = new Timing();
final int MAX_LEASES = 11;
final int THREADS = 100;
final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
client.start();
try {
final Stepper latch = new Stepper();
final Random random = new Random();
final Counter counter = new Counter();
ExecutorService service = Executors.newCachedThreadPool();
ExecutorCompletionService<Object> completionService = new ExecutorCompletionService<Object>(service);
for (int i = 0; i < THREADS; ++i) {
completionService.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, "/test", MAX_LEASES);
Lease lease = semaphore.acquire(timing.forWaiting().seconds(), TimeUnit.SECONDS);
if (lease == null) {
throw new Exception("timed out");
}
try {
synchronized (counter) {
++counter.currentCount;
if (counter.currentCount > counter.maxCount) {
counter.maxCount = counter.currentCount;
}
counter.notifyAll();
}
latch.await();
} finally {
synchronized (counter) {
--counter.currentCount;
}
semaphore.returnLease(lease);
}
return null;
}
});
}
int remaining = THREADS;
while (remaining > 0) {
int times = Math.min(random.nextInt(5) + 1, remaining);
latch.countDown(times);
remaining -= times;
Thread.sleep(random.nextInt(100) + 1);
}
for (int i = 0; i < THREADS; ++i) {
completionService.take();
}
timing.sleepABit();
synchronized (counter) {
Assert.assertTrue(counter.currentCount == 0);
Assert.assertTrue(counter.maxCount > 0);
Assert.assertTrue(counter.maxCount <= MAX_LEASES);
System.out.println(counter.maxCount);
}
} finally {
client.close();
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestInterProcessSemaphore method testGetParticipantNodes.
@Test
public void testGetParticipantNodes() throws Exception {
final int LEASES = 3;
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
List<Lease> leases = Lists.newArrayList();
client.start();
try {
InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, "/test", LEASES);
for (int i = 0; i < LEASES; ++i) {
leases.add(semaphore.acquire());
}
Assert.assertEquals(semaphore.getParticipantNodes().size(), LEASES);
} finally {
for (Lease l : leases) {
CloseableUtils.closeQuietly(l);
}
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestInterProcessSemaphoreCluster method testKilledServerWithEnsembleProvider.
@Test
public void testKilledServerWithEnsembleProvider() throws Exception {
final int CLIENT_QTY = 10;
final Timing timing = new Timing();
final String PATH = "/foo/bar/lock";
ExecutorService executorService = Executors.newFixedThreadPool(CLIENT_QTY);
ExecutorCompletionService<Void> completionService = new ExecutorCompletionService<Void>(executorService);
TestingCluster cluster = new TestingCluster(3);
try {
cluster.start();
final AtomicReference<String> connectionString = new AtomicReference<String>(cluster.getConnectString());
final EnsembleProvider provider = new EnsembleProvider() {
@Override
public void start() throws Exception {
}
@Override
public String getConnectionString() {
return connectionString.get();
}
@Override
public void close() throws IOException {
}
};
final Semaphore acquiredSemaphore = new Semaphore(0);
final AtomicInteger acquireCount = new AtomicInteger(0);
final CountDownLatch suspendedLatch = new CountDownLatch(CLIENT_QTY);
for (int i = 0; i < CLIENT_QTY; ++i) {
completionService.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
CuratorFramework client = CuratorFrameworkFactory.builder().ensembleProvider(provider).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).retryPolicy(new ExponentialBackoffRetry(100, 3)).build();
try {
final Semaphore suspendedSemaphore = new Semaphore(0);
client.getConnectionStateListenable().addListener(new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if ((newState == ConnectionState.SUSPENDED) || (newState == ConnectionState.LOST)) {
suspendedLatch.countDown();
suspendedSemaphore.release();
}
}
});
client.start();
InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, PATH, 1);
while (!Thread.currentThread().isInterrupted()) {
Lease lease = null;
try {
lease = semaphore.acquire();
acquiredSemaphore.release();
acquireCount.incrementAndGet();
suspendedSemaphore.acquire();
} catch (Exception e) {
// just retry
} finally {
if (lease != null) {
acquireCount.decrementAndGet();
CloseableUtils.closeQuietly(lease);
}
}
}
} finally {
CloseableUtils.closeQuietly(client);
}
return null;
}
});
}
Assert.assertTrue(timing.acquireSemaphore(acquiredSemaphore));
Assert.assertEquals(1, acquireCount.get());
cluster.close();
timing.awaitLatch(suspendedLatch);
timing.forWaiting().sleepABit();
Assert.assertEquals(0, acquireCount.get());
cluster = new TestingCluster(3);
cluster.start();
connectionString.set(cluster.getConnectString());
timing.forWaiting().sleepABit();
Assert.assertTrue(timing.acquireSemaphore(acquiredSemaphore));
timing.forWaiting().sleepABit();
Assert.assertEquals(1, acquireCount.get());
} finally {
executorService.shutdown();
executorService.awaitTermination(10, TimeUnit.SECONDS);
executorService.shutdownNow();
CloseableUtils.closeQuietly(cluster);
}
}
Aggregations