Search in sources :

Example 21 with Cache

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

the class CachesCollection method parse.

@Override
public CacheResource parse(ConfigResource parent, IdString id) throws AuthException, ResourceNotFoundException, PermissionBackendException {
    permissionBackend.user(self).check(GlobalPermission.VIEW_CACHES);
    String cacheName = id.get();
    String pluginName = "gerrit";
    int i = cacheName.lastIndexOf('-');
    if (i != -1) {
        pluginName = cacheName.substring(0, i);
        cacheName = cacheName.length() > i + 1 ? cacheName.substring(i + 1) : "";
    }
    Provider<Cache<?, ?>> cacheProvider = cacheMap.byPlugin(pluginName).get(cacheName);
    if (cacheProvider == null) {
        throw new ResourceNotFoundException(id);
    }
    return new CacheResource(pluginName, cacheName, cacheProvider);
}
Also used : IdString(com.google.gerrit.extensions.restapi.IdString) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) Cache(com.google.common.cache.Cache)

Example 22 with Cache

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

the class NoteDbModule method configure.

@Override
public void configure() {
    factory(ChangeUpdate.Factory.class);
    factory(ChangeDraftUpdate.Factory.class);
    factory(DeleteCommentRewriter.Factory.class);
    factory(DraftCommentNotes.Factory.class);
    factory(RobotCommentUpdate.Factory.class);
    factory(RobotCommentNotes.Factory.class);
    factory(NoteDbUpdateManager.Factory.class);
    if (!useTestBindings) {
        install(ChangeNotesCache.module());
        if (cfg.getBoolean("noteDb", null, "testRebuilderWrapper", false)) {
            // Yes, another variety of test bindings with a different way of
            // configuring it.
            bind(ChangeRebuilder.class).to(TestChangeRebuilderWrapper.class);
        } else {
            bind(ChangeRebuilder.class).to(ChangeRebuilderImpl.class);
        }
    } else {
        bind(ChangeRebuilder.class).toInstance(new ChangeRebuilder(null) {

            @Override
            public Result rebuild(ReviewDb db, Change.Id changeId) {
                return null;
            }

            @Override
            public Result rebuildEvenIfReadOnly(ReviewDb db, Id changeId) {
                return null;
            }

            @Override
            public Result rebuild(NoteDbUpdateManager manager, ChangeBundle bundle) {
                return null;
            }

            @Override
            public NoteDbUpdateManager stage(ReviewDb db, Change.Id changeId) {
                return null;
            }

            @Override
            public Result execute(ReviewDb db, Change.Id changeId, NoteDbUpdateManager manager) {
                return null;
            }

            @Override
            public void buildUpdates(NoteDbUpdateManager manager, ChangeBundle bundle) {
            // Do nothing.
            }

            @Override
            public void rebuildReviewDb(ReviewDb db, Project.NameKey project, Id changeId) {
            // Do nothing.
            }
        });
        bind(new TypeLiteral<Cache<ChangeNotesCache.Key, ChangeNotesState>>() {
        }).annotatedWith(Names.named(ChangeNotesCache.CACHE_NAME)).toInstance(CacheBuilder.newBuilder().<ChangeNotesCache.Key, ChangeNotesState>build());
    }
}
Also used : Change(com.google.gerrit.reviewdb.client.Change) Result(com.google.gerrit.server.notedb.NoteDbUpdateManager.Result) Project(com.google.gerrit.reviewdb.client.Project) Id(com.google.gerrit.reviewdb.client.Change.Id) Id(com.google.gerrit.reviewdb.client.Change.Id) ChangeRebuilder(com.google.gerrit.server.notedb.rebuild.ChangeRebuilder) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) Cache(com.google.common.cache.Cache)

Example 23 with Cache

use of com.google.common.cache.Cache in project bookkeeper by apache.

the class TestFileInfoBackingCache method testForDeadlocks.

/**
 * Of course this can't prove they don't exist, but
 * try to shake them out none the less.
 */
@Test
public void testForDeadlocks() throws Exception {
    int numRunners = 20;
    int maxLedgerId = 10;
    AtomicBoolean done = new AtomicBoolean(false);
    FileInfoBackingCache cache = new FileInfoBackingCache((ledgerId, createIfNotFound) -> {
        File f = new File(baseDir, String.valueOf(ledgerId));
        f.deleteOnExit();
        return f;
    });
    Iterable<Future<Set<CachedFileInfo>>> futures = IntStream.range(0, numRunners).mapToObj((i) -> {
        Callable<Set<CachedFileInfo>> c = () -> {
            Random r = new Random();
            List<CachedFileInfo> fileInfos = new ArrayList<>();
            Set<CachedFileInfo> allFileInfos = new HashSet<>();
            while (!done.get()) {
                if (r.nextBoolean() && fileInfos.size() < 5) {
                    // take a reference
                    CachedFileInfo fi = cache.loadFileInfo(r.nextInt(maxLedgerId), masterKey);
                    Assert.assertFalse(fi.isClosed());
                    allFileInfos.add(fi);
                    fileInfos.add(fi);
                } else {
                    // release a reference
                    Collections.shuffle(fileInfos);
                    if (!fileInfos.isEmpty()) {
                        fileInfos.remove(0).release();
                    }
                }
            }
            for (CachedFileInfo fi : fileInfos) {
                Assert.assertFalse(fi.isClosed());
                fi.release();
            }
            return allFileInfos;
        };
        return executor.submit(c);
    }).collect(Collectors.toList());
    Thread.sleep(TimeUnit.SECONDS.toMillis(10));
    done.set(true);
    // ensure all threads are finished operating on cache, before checking any
    for (Future<Set<CachedFileInfo>> f : futures) {
        f.get();
    }
    for (Future<Set<CachedFileInfo>> f : futures) {
        for (CachedFileInfo fi : f.get()) {
            Assert.assertTrue(fi.isClosed());
            Assert.assertEquals(FileInfoBackingCache.DEAD_REF, fi.getRefCount());
        }
    }
    // They should be loaded fresh (i.e. this load should be only reference)
    for (int i = 0; i < maxLedgerId; i++) {
        Assert.assertEquals(1, cache.loadFileInfo(i, masterKey).getRefCount());
    }
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) IntStream(java.util.stream.IntStream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) Callable(java.util.concurrent.Callable) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Future(java.util.concurrent.Future) After(org.junit.After) ThreadFactory(java.util.concurrent.ThreadFactory) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) RemovalNotification(com.google.common.cache.RemovalNotification) LongStream(java.util.stream.LongStream) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) Collectors(java.util.stream.Collectors) File(java.io.File) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) CacheBuilder(com.google.common.cache.CacheBuilder) Cache(com.google.common.cache.Cache) Assert(org.junit.Assert) Collections(java.util.Collections) CachedFileInfo(org.apache.bookkeeper.bookie.FileInfoBackingCache.CachedFileInfo) HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CachedFileInfo(org.apache.bookkeeper.bookie.FileInfoBackingCache.CachedFileInfo) Random(java.util.Random) Future(java.util.concurrent.Future) File(java.io.File) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 24 with Cache

use of com.google.common.cache.Cache in project bookkeeper by apache.

the class TestFileInfoBackingCache method testRaceGuavaEvictAndReleaseBeforeRetain.

@Test
public void testRaceGuavaEvictAndReleaseBeforeRetain() throws Exception {
    AtomicBoolean done = new AtomicBoolean(false);
    FileInfoBackingCache cache = new FileInfoBackingCache((ledgerId, createIfNotFound) -> {
        File f = new File(baseDir, String.valueOf(ledgerId));
        f.deleteOnExit();
        return f;
    });
    Cache<Long, CachedFileInfo> guavaCache = CacheBuilder.newBuilder().maximumSize(1).removalListener(this::guavaEvictionListener).build();
    Iterable<Future<Set<CachedFileInfo>>> futures = LongStream.range(0L, 2L).mapToObj((i) -> {
        Callable<Set<CachedFileInfo>> c = () -> {
            Set<CachedFileInfo> allFileInfos = new HashSet<>();
            while (!done.get()) {
                CachedFileInfo fi = null;
                do {
                    fi = guavaCache.get(i, () -> cache.loadFileInfo(i, masterKey));
                    allFileInfos.add(fi);
                    Thread.sleep(100);
                } while (!fi.tryRetain());
                Assert.assertFalse(fi.isClosed());
                fi.release();
            }
            return allFileInfos;
        };
        return executor.submit(c);
    }).collect(Collectors.toList());
    Thread.sleep(TimeUnit.SECONDS.toMillis(10));
    done.set(true);
    // ensure all threads are finished operating on cache, before checking any
    for (Future<Set<CachedFileInfo>> f : futures) {
        f.get();
    }
    guavaCache.invalidateAll();
    for (Future<Set<CachedFileInfo>> f : futures) {
        for (CachedFileInfo fi : f.get()) {
            Assert.assertTrue(fi.isClosed());
            Assert.assertEquals(FileInfoBackingCache.DEAD_REF, fi.getRefCount());
        }
    }
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) IntStream(java.util.stream.IntStream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) Callable(java.util.concurrent.Callable) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Future(java.util.concurrent.Future) After(org.junit.After) ThreadFactory(java.util.concurrent.ThreadFactory) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) RemovalNotification(com.google.common.cache.RemovalNotification) LongStream(java.util.stream.LongStream) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) Collectors(java.util.stream.Collectors) File(java.io.File) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) CacheBuilder(com.google.common.cache.CacheBuilder) Cache(com.google.common.cache.Cache) Assert(org.junit.Assert) Collections(java.util.Collections) CachedFileInfo(org.apache.bookkeeper.bookie.FileInfoBackingCache.CachedFileInfo) HashSet(java.util.HashSet) Set(java.util.Set) Callable(java.util.concurrent.Callable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CachedFileInfo(org.apache.bookkeeper.bookie.FileInfoBackingCache.CachedFileInfo) Future(java.util.concurrent.Future) File(java.io.File) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 25 with Cache

use of com.google.common.cache.Cache in project micrometer by micrometer-metrics.

the class CacheSample method main.

public static void main(String[] args) {
    MeterRegistry registry = SampleConfig.myMonitoringSystem();
    GuavaCacheMetrics.monitor(registry, guavaCache, "book.guava");
    // read all of Frankenstein
    HttpClient.create("www.gutenberg.org").get("/cache/epub/84/pg84.txt").flatMapMany(res -> res.addHandler(wordDecoder()).receive().asString()).delayElements(// one word per 10 ms
    Duration.ofMillis(10)).filter(word -> !word.isEmpty()).doOnNext(word -> {
        if (guavaCache.getIfPresent(word) == null)
            guavaCache.put(word, 1);
    }).blockLast();
}
Also used : IntStream(java.util.stream.IntStream) SampleConfig(io.micrometer.core.samples.utils.SampleConfig) ByteBuf(io.netty.buffer.ByteBuf) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Duration(java.time.Duration) HttpClient(reactor.ipc.netty.http.client.HttpClient) CacheBuilder(com.google.common.cache.CacheBuilder) Unpooled.wrappedBuffer(io.netty.buffer.Unpooled.wrappedBuffer) Cache(com.google.common.cache.Cache) DelimiterBasedFrameDecoder(io.netty.handler.codec.DelimiterBasedFrameDecoder) GuavaCacheMetrics(io.micrometer.core.instrument.binder.cache.GuavaCacheMetrics) MeterRegistry(io.micrometer.core.instrument.MeterRegistry)

Aggregations

Cache (com.google.common.cache.Cache)43 List (java.util.List)21 CacheBuilder (com.google.common.cache.CacheBuilder)19 Set (java.util.Set)11 Collectors (java.util.stream.Collectors)11 ArrayList (java.util.ArrayList)10 Optional (java.util.Optional)10 TimeUnit (java.util.concurrent.TimeUnit)10 MILLISECONDS (java.util.concurrent.TimeUnit.MILLISECONDS)10 Module (com.google.inject.Module)9 CachingOrcFileTailSource (com.facebook.presto.orc.cache.CachingOrcFileTailSource)8 OrcFileTailSource (com.facebook.presto.orc.cache.OrcFileTailSource)8 StorageOrcFileTailSource (com.facebook.presto.orc.cache.StorageOrcFileTailSource)8 OrcFileTail (com.facebook.presto.orc.metadata.OrcFileTail)8 ExecutorService (java.util.concurrent.ExecutorService)8 ConfigBinder.configBinder (com.facebook.airlift.configuration.ConfigBinder.configBinder)7 CachingStripeMetadataSource (com.facebook.presto.orc.CachingStripeMetadataSource)7 DwrfAwareStripeMetadataSourceFactory (com.facebook.presto.orc.DwrfAwareStripeMetadataSourceFactory)7 OrcDataSourceId (com.facebook.presto.orc.OrcDataSourceId)7 StorageStripeMetadataSource (com.facebook.presto.orc.StorageStripeMetadataSource)7