use of com.google.common.util.concurrent.ListeningExecutorService in project bazel by bazelbuild.
the class AndroidResourceMerger method mergeData.
/** Merges all secondary resources with the primary resources. */
static MergedAndroidData mergeData(final ParsedAndroidData primary, final Path primaryManifest, final List<? extends SerializedAndroidData> direct, final List<? extends SerializedAndroidData> transitive, final Path resourcesOut, final Path assetsOut, @Nullable final PngCruncher cruncher, final VariantType type, @Nullable final Path symbolsOut, @Nullable AndroidResourceClassWriter rclassWriter) throws MergingException {
Stopwatch timer = Stopwatch.createStarted();
final ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(15));
try (Closeable closeable = ExecutorServiceCloser.createWith(executorService)) {
AndroidDataMerger merger = AndroidDataMerger.createWithPathDeduplictor(executorService);
UnwrittenMergedAndroidData merged = merger.loadAndMerge(transitive, direct, primary, primaryManifest, type != VariantType.LIBRARY);
logger.fine(String.format("merge finished in %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
timer.reset().start();
if (symbolsOut != null) {
AndroidDataSerializer serializer = AndroidDataSerializer.create();
merged.serializeTo(serializer);
serializer.flushTo(symbolsOut);
logger.fine(String.format("serialize merge finished in %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
timer.reset().start();
}
if (rclassWriter != null) {
merged.writeResourceClass(rclassWriter);
logger.fine(String.format("write classes finished in %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
timer.reset().start();
}
AndroidDataWriter writer = AndroidDataWriter.createWith(resourcesOut.getParent(), resourcesOut, assetsOut, cruncher, executorService);
return merged.write(writer);
} catch (IOException e) {
throw MergingException.wrapException(e).build();
} finally {
logger.fine(String.format("write merge finished in %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
}
}
use of com.google.common.util.concurrent.ListeningExecutorService in project buck by facebook.
the class MoreSuppliersTest method weakMemoizeShouldRunDelegateOnlyOnceOnConcurrentAccess.
@Test
public void weakMemoizeShouldRunDelegateOnlyOnceOnConcurrentAccess() throws Exception {
final int numFetchers = 10;
final Semaphore semaphore = new Semaphore(0);
class TestDelegate implements Supplier<Object> {
private int timesCalled = 0;
public int getTimesCalled() {
return timesCalled;
}
@Override
public Object get() {
try {
// Wait for all the fetch threads to be ready.
semaphore.acquire(numFetchers);
// Give other threads a chance to catch up.
Thread.sleep(50);
timesCalled++;
return new Object();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
semaphore.release(numFetchers);
}
}
}
TestDelegate delegate = new TestDelegate();
final Supplier<Object> supplier = MoreSuppliers.weakMemoize(delegate);
ExecutorService threadPool = Executors.newFixedThreadPool(numFetchers);
try {
ListeningExecutorService executor = MoreExecutors.listeningDecorator(threadPool);
class Fetcher implements Callable<Object> {
@Override
public Object call() {
// Signal that this particular fetcher is ready.
semaphore.release();
return supplier.get();
}
}
ImmutableList.Builder<Callable<Object>> fetcherBuilder = ImmutableList.builder();
for (int i = 0; i < numFetchers; i++) {
fetcherBuilder.add(new Fetcher());
}
@SuppressWarnings("unchecked") List<ListenableFuture<Object>> futures = (List<ListenableFuture<Object>>) (List<?>) executor.invokeAll(fetcherBuilder.build());
// Wait for all fetchers to finish.
List<Object> results = Futures.allAsList(futures).get();
Assert.assertEquals("should only have been called once", 1, delegate.getTimesCalled());
Assert.assertThat("all result items are the same", ImmutableSet.copyOf(results), Matchers.hasSize(1));
Preconditions.checkState(threadPool.shutdownNow().isEmpty(), "All jobs should have completed");
Preconditions.checkState(threadPool.awaitTermination(10, TimeUnit.SECONDS), "Thread pool should terminate in a reasonable amount of time");
} finally {
// In case exceptions were thrown, attempt to shut down the thread pool.
threadPool.shutdownNow();
threadPool.awaitTermination(10, TimeUnit.SECONDS);
}
}
use of com.google.common.util.concurrent.ListeningExecutorService in project buck by facebook.
the class AsyncCloseableTest method testCloseAsync.
@Test
public void testCloseAsync() throws Exception {
ListeningExecutorService directExecutor = MoreExecutors.newDirectExecutorService();
final AtomicBoolean didClose = new AtomicBoolean(false);
try (AsyncCloseable asyncCloseable = new AsyncCloseable(directExecutor)) {
asyncCloseable.closeAsync(() -> didClose.set(true));
}
assertThat(didClose.get(), Matchers.is(true));
}
use of com.google.common.util.concurrent.ListeningExecutorService in project buck by facebook.
the class ResourcePoolTest method exceptionOnResourceUsageWithRetireHandling.
@Test
public void exceptionOnResourceUsageWithRetireHandling() throws Exception {
try (Fixture f = new Fixture(/* maxResources */
1, ResourcePool.ResourceUsageErrorPolicy.RETIRE)) {
ListeningExecutorService executorService = MoreExecutors.newDirectExecutorService();
List<ListenableFuture<TestResource>> results = Stream.of(0, 1, 2).map(i -> f.getPool().scheduleOperationWithResource(r -> {
if (i == 1) {
throw new TestException();
}
return r;
}, executorService)).collect(Collectors.toList());
Futures.successfulAsList(results).get();
assertThat(f.getCreatedResources().get(), equalTo(f.getMaxResources() + 1));
// First request gets the first resource (id == 0), second request errors out causing the
// resource to be retired and the third request gets a new resource (id == 1).
assertThat(results.get(0).get().getTestResourceId(), equalTo(0));
assertThat(results.get(2).get().getTestResourceId(), equalTo(1));
expectedException.expectCause(Matchers.instanceOf(TestException.class));
results.get(1).get();
}
}
use of com.google.common.util.concurrent.ListeningExecutorService in project ddf by codice.
the class MetadataConfigurationParser method buildEntityDescriptor.
private void buildEntityDescriptor(String entityDescription) throws IOException {
EntityDescriptor entityDescriptor = null;
entityDescription = entityDescription.trim();
if (entityDescription.startsWith(HTTPS) || entityDescription.startsWith(HTTP)) {
if (entityDescription.startsWith(HTTP)) {
LOGGER.warn("Retrieving metadata via HTTP instead of HTTPS. The metadata configuration is unsafe!!!");
}
PropertyResolver propertyResolver = new PropertyResolver(entityDescription);
HttpTransport httpTransport = new NetHttpTransport();
HttpRequest httpRequest = httpTransport.createRequestFactory().buildGetRequest(new GenericUrl(propertyResolver.getResolvedString()));
httpRequest.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()).setBackOffRequired(HttpBackOffUnsuccessfulResponseHandler.BackOffRequired.ALWAYS));
httpRequest.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(new ExponentialBackOff()));
ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
ListenableFuture<HttpResponse> httpResponseFuture = service.submit(httpRequest::execute);
Futures.addCallback(httpResponseFuture, new FutureCallback<HttpResponse>() {
@Override
public void onSuccess(HttpResponse httpResponse) {
if (httpResponse != null) {
try {
String parsedResponse = httpResponse.parseAsString();
buildEntityDescriptor(parsedResponse);
} catch (IOException e) {
LOGGER.info("Unable to parse metadata from: {}", httpResponse.getRequest().getUrl().toString(), e);
}
}
}
@Override
public void onFailure(Throwable throwable) {
LOGGER.info("Unable to retrieve metadata.", throwable);
}
});
service.shutdown();
} else if (entityDescription.startsWith(FILE + System.getProperty("ddf.home"))) {
String pathStr = StringUtils.substringAfter(entityDescription, FILE);
Path path = Paths.get(pathStr);
if (Files.isReadable(path)) {
try (InputStream fileInputStream = Files.newInputStream(path)) {
entityDescriptor = readEntityDescriptor(new InputStreamReader(fileInputStream, "UTF-8"));
}
}
} else if (entityDescription.startsWith("<") && entityDescription.endsWith(">")) {
entityDescriptor = readEntityDescriptor(new StringReader(entityDescription));
} else {
LOGGER.info("Skipping unknown metadata configuration value: {}", entityDescription);
}
if (entityDescriptor != null) {
entityDescriptorMap.put(entityDescriptor.getEntityID(), entityDescriptor);
if (updateCallback != null) {
updateCallback.accept(entityDescriptor);
}
}
}
Aggregations