Search in sources :

Example 1 with DexingKey

use of com.google.devtools.build.android.dexer.Dexing.DexingKey in project bazel by bazelbuild.

the class DexBuilder method runPersistentWorker.

/**
   * Implements a persistent worker process for use with Bazel (see {@code WorkerSpawnStrategy}).
   */
private static void runPersistentWorker() throws IOException {
    ExecutorService executor = newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    Cache<DexingKey, byte[]> dexCache = CacheBuilder.newBuilder().maximumWeight(Math.min(Runtime.getRuntime().maxMemory() - 25 * ONE_MEG, 200 * ONE_MEG)).weigher(new Weigher<DexingKey, byte[]>() {

        @Override
        public int weigh(DexingKey key, byte[] value) {
            return key.classfileContent().length + value.length;
        }
    }).build();
    try {
        while (true) {
            WorkRequest request = WorkRequest.parseDelimitedFrom(System.in);
            if (request == null) {
                return;
            }
            // Redirect dx's output so we can return it in response
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PrintStream ps = new PrintStream(baos, /*autoFlush*/
            true);
            DxConsole.out = DxConsole.err = ps;
            // Make sure that we exit nonzero in case uncaught errors occur during processRequest.
            int exitCode = 1;
            try {
                processRequest(executor, dexCache, request.getArgumentsList());
                // success!
                exitCode = 0;
            } catch (Exception e) {
                // Deliberate catch-all so we can capture a stack trace.
                // TODO(bazel-team): Consider canceling any outstanding futures created for this request
                e.printStackTrace(ps);
            } catch (Error e) {
                e.printStackTrace();
                // try capturing the error, may fail if out of memory
                e.printStackTrace(ps);
                // rethrow to kill the worker
                throw e;
            } finally {
                // Try sending a response no matter what
                String output;
                try {
                    output = baos.toString();
                } catch (Throwable t) {
                    // most likely out of memory, so log with minimal memory needs
                    t.printStackTrace();
                    output = "check worker log for exceptions";
                }
                WorkResponse.newBuilder().setOutput(output).setExitCode(exitCode).build().writeDelimitedTo(System.out);
                System.out.flush();
            }
        }
    } finally {
        executor.shutdown();
    }
}
Also used : WorkRequest(com.google.devtools.build.lib.worker.WorkerProtocol.WorkRequest) PrintStream(java.io.PrintStream) Weigher(com.google.common.cache.Weigher) ExecutorService(java.util.concurrent.ExecutorService) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DexingKey(com.google.devtools.build.android.dexer.Dexing.DexingKey) IOException(java.io.IOException) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

Weigher (com.google.common.cache.Weigher)1 DexingKey (com.google.devtools.build.android.dexer.Dexing.DexingKey)1 WorkRequest (com.google.devtools.build.lib.worker.WorkerProtocol.WorkRequest)1 OptionsParsingException (com.google.devtools.common.options.OptionsParsingException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1