use of com.google.common.util.concurrent.Service in project cdap by caskdata.
the class ReferenceCountedSupplierTests method testGetSupplier.
/**
* Starts a few threads to perform get operation on the given {@link CacheSupplier}s. It verifies that subsequent
* 'get' operation returns the same instance as it did during the first invocation. And it also verifies that the
* service is running.
*
* @param supplierList list of {@link CacheSupplier}
* @throws Exception if an error occurs during testing
*/
private void testGetSupplier(final List<CacheSupplier> supplierList) throws Exception {
// Get one instance now, for later comparisons
final List<Service> serviceList = new ArrayList<>();
for (CacheSupplier supplier : supplierList) {
serviceList.add(supplier.get());
}
final AtomicInteger numOps = new AtomicInteger(NUM_OPS);
final Random random = new Random(System.currentTimeMillis());
// Start threads that will 'get' DummyService
ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
List<Future> futureList = new ArrayList<>();
for (int i = 0; i < NUM_THREADS; i++) {
futureList.add(executor.submit(new Runnable() {
@Override
public void run() {
// Perform NUM_OPS 'gets' of DummyService
while (numOps.decrementAndGet() > 0) {
for (int i = 0; i < supplierList.size(); i++) {
CacheSupplier supplier = supplierList.get(i);
Service newService = supplier.get();
Assert.assertTrue(newService == serviceList.get(i));
Assert.assertTrue(newService.isRunning());
}
int waitTime = random.nextInt(10);
try {
TimeUnit.MICROSECONDS.sleep(waitTime);
} catch (InterruptedException e) {
LOG.warn("Received an exception.", e);
}
}
}
}));
}
for (Future future : futureList) {
future.get(5, TimeUnit.SECONDS);
}
executor.shutdown();
executor.awaitTermination(2, TimeUnit.SECONDS);
}
use of com.google.common.util.concurrent.Service in project cdap by caskdata.
the class ReferenceCountedSupplierTests method testReleaseSupplier.
/**
* Starts a few threads to perform release operation on the given {@link CacheSupplier}s. It verifies that the
* services are still running until the last release operation is performed after which they are checked to see
* if the services have stopped.
*
* @param supplierList list of {@link CacheSupplier}
* @throws Exception if an error occurs during testing
*/
private void testReleaseSupplier(final List<CacheSupplier> supplierList) throws Exception {
final AtomicInteger numOps = new AtomicInteger(NUM_OPS);
final Random random = new Random(System.currentTimeMillis());
final List<Service> serviceList = new ArrayList<>();
for (CacheSupplier supplier : supplierList) {
serviceList.add(supplier.get());
supplier.release();
}
// Start threads that will 'release' DummyService
ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
List<Future> futureList = new ArrayList<>();
numOps.set(NUM_OPS);
for (int i = 0; i < NUM_THREADS; i++) {
futureList.add(executor.submit(new Runnable() {
@Override
public void run() {
// DummyService
while (numOps.decrementAndGet() > 0) {
for (int i = 0; i < supplierList.size(); i++) {
CacheSupplier supplier = supplierList.get(i);
supplier.release();
Assert.assertTrue(serviceList.get(i).isRunning());
}
try {
TimeUnit.MICROSECONDS.sleep(random.nextInt(10));
} catch (InterruptedException e) {
LOG.warn("Received an exception.", e);
}
}
}
}));
}
for (Future future : futureList) {
future.get(1, TimeUnit.SECONDS);
}
executor.shutdown();
executor.awaitTermination(2, TimeUnit.SECONDS);
// Verify that the DummyService is still running.
for (Service service : serviceList) {
Assert.assertTrue(service.isRunning());
}
// Since we got one instance in the beginning, we need to release it
for (CacheSupplier supplier : supplierList) {
supplier.release();
}
// Verify that the DummyService is shutdown.
for (Service service : serviceList) {
Assert.assertFalse(service.isRunning());
}
// Release it again but it should not cause any problem and the service should still be stopped.
for (CacheSupplier supplier : supplierList) {
supplier.release();
}
// Verify that the DummyService is shutdown.
for (Service service : serviceList) {
Assert.assertFalse(service.isRunning());
}
}
use of com.google.common.util.concurrent.Service in project weave by continuuity.
the class ApplicationMasterMain method main.
/**
* Starts the application master.
*/
public static void main(String[] args) throws Exception {
String zkConnect = System.getenv(EnvKeys.WEAVE_ZK_CONNECT);
File weaveSpec = new File(Constants.Files.WEAVE_SPEC);
RunId runId = RunIds.fromString(System.getenv(EnvKeys.WEAVE_RUN_ID));
ZKClientService zkClientService = ZKClientServices.delegate(ZKClients.reWatchOnExpire(ZKClients.retryOnFailure(ZKClientService.Builder.of(zkConnect).build(), RetryStrategies.fixDelay(1, TimeUnit.SECONDS))));
Configuration conf = new YarnConfiguration(new HdfsConfiguration(new Configuration()));
Service service = new ApplicationMasterService(runId, zkClientService, weaveSpec, new VersionDetectYarnAMClientFactory(conf), createAppLocation(conf));
new ApplicationMasterMain(String.format("%s/%s/kafka", zkConnect, runId.getId())).doMain(zkClientService, service);
}
use of com.google.common.util.concurrent.Service in project helios by spotify.
the class SystemTestBase method baseTeardown.
@After
public void baseTeardown() throws Exception {
for (final HeliosClient client : clients) {
client.close();
}
clients.clear();
for (final Service service : services) {
try {
service.stopAsync();
} catch (Exception e) {
log.error("Uncaught exception", e);
}
}
for (final Service service : services) {
try {
service.awaitTerminated();
} catch (Exception e) {
log.error("Service failed", e);
}
}
services.clear();
// Clean up docker
try (final DockerClient dockerClient = getNewDockerClient()) {
final List<Container> containers = dockerClient.listContainers();
for (final Container container : containers) {
for (final String name : container.names()) {
if (name.contains(testTag)) {
try {
dockerClient.killContainer(container.id());
} catch (DockerException e) {
e.printStackTrace();
}
break;
}
}
}
} catch (Exception e) {
log.error("Docker client exception", e);
}
if (zk != null) {
zk.close();
}
listThreads();
}
use of com.google.common.util.concurrent.Service in project torodb by torodb.
the class StampedeBootstrapTest method testCreateStampedeService_run.
@Test
@Ignore
public void testCreateStampedeService_run() {
Service stampedeService = StampedeBootstrap.createStampedeService(new TestBootstrapModule(config, Clock.systemUTC()));
stampedeService.startAsync();
stampedeService.awaitRunning();
stampedeService.stopAsync();
stampedeService.awaitTerminated();
}
Aggregations