Search in sources :

Example 1 with Weigher

use of com.google.common.cache.Weigher in project jackrabbit-oak by apache.

the class CacheTest method testRefresh.

@Test
public void testRefresh() throws ExecutionException {
    CacheLIRS<Integer, String> cache = new CacheLIRS.Builder<Integer, String>().maximumWeight(100).weigher(new Weigher<Integer, String>() {

        @Override
        public int weigh(Integer key, String value) {
            return key + value.length();
        }
    }).build(new CacheLoader<Integer, String>() {

        @Override
        public String load(Integer key) throws Exception {
            if (key < 0 || key >= 100) {
                throw new Exception("Out of range");
            }
            return "n" + key;
        }

        @Override
        public ListenableFuture<String> reload(Integer key, String oldValue) {
            assertTrue(oldValue != null);
            SettableFuture<String> f = SettableFuture.create();
            f.set(oldValue);
            return f;
        }
    });
    assertEquals("n1", cache.get(1));
    cache.refresh(1);
    cache.refresh(2);
    try {
        cache.get(-1);
        fail();
    } catch (Exception e) {
    // expected
    }
    // expected to log a warning, but not fail
    cache.refresh(-1);
}
Also used : SettableFuture(com.google.common.util.concurrent.SettableFuture) Weigher(com.google.common.cache.Weigher) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 2 with Weigher

use of com.google.common.cache.Weigher 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)

Example 3 with Weigher

use of com.google.common.cache.Weigher in project gerrit by GerritCodeReview.

the class CacheModule method bindWeigher.

<K, V> Provider<Weigher<K, V>> bindWeigher(CacheProvider<K, V> m, Class<? extends Weigher<K, V>> impl) {
    Type weigherType = Types.newParameterizedType(Weigher.class, m.keyType().getType(), m.valueType().getType());
    @SuppressWarnings("unchecked") Key<Weigher<K, V>> key = (Key<Weigher<K, V>>) Key.get(weigherType, Names.named(m.name));
    bind(key).to(impl).in(Scopes.SINGLETON);
    return getProvider(key);
}
Also used : Type(java.lang.reflect.Type) Weigher(com.google.common.cache.Weigher) Key(com.google.inject.Key)

Aggregations

Weigher (com.google.common.cache.Weigher)3 ExecutionException (java.util.concurrent.ExecutionException)2 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 SettableFuture (com.google.common.util.concurrent.SettableFuture)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 Key (com.google.inject.Key)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 Type (java.lang.reflect.Type)1 ExecutorService (java.util.concurrent.ExecutorService)1 Test (org.junit.Test)1