Search in sources :

Example 1 with Cache

use of com.baidu.hugegraph.backend.cache.Cache in project incubator-hugegraph by apache.

the class CacheTest method testGetOrFetch.

@Test
public void testGetOrFetch() {
    Cache<Id, Object> cache = newCache();
    Id id = IdGenerator.of("1");
    Assert.assertNull(cache.get(id));
    Assert.assertEquals("value-1", cache.getOrFetch(id, key -> {
        return "value-1";
    }));
    cache.update(id, "value-2");
    Assert.assertEquals("value-2", cache.getOrFetch(id, key -> {
        return "value-1";
    }));
}
Also used : Blob(com.baidu.hugegraph.util.Blob) Date(java.util.Date) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) RamCache(com.baidu.hugegraph.backend.cache.RamCache) HashMap(java.util.HashMap) Test(org.junit.Test) UUID(java.util.UUID) LevelCache(com.baidu.hugegraph.backend.cache.LevelCache) LinkedHashMap(java.util.LinkedHashMap) Mockito(org.mockito.Mockito) Bytes(com.baidu.hugegraph.util.Bytes) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) HugeGraph(com.baidu.hugegraph.HugeGraph) IdGenerator(com.baidu.hugegraph.backend.id.IdGenerator) Map(java.util.Map) After(org.junit.After) Id(com.baidu.hugegraph.backend.id.Id) Whitebox(com.baidu.hugegraph.testutil.Whitebox) Cache(com.baidu.hugegraph.backend.cache.Cache) Assert(com.baidu.hugegraph.testutil.Assert) OffheapCache(com.baidu.hugegraph.backend.cache.OffheapCache) Before(org.junit.Before) Id(com.baidu.hugegraph.backend.id.Id) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 2 with Cache

use of com.baidu.hugegraph.backend.cache.Cache in project incubator-hugegraph by apache.

the class GraphManager method addMetrics.

private void addMetrics(HugeConfig config) {
    final MetricManager metric = MetricManager.INSTANCE;
    // Force to add server reporter
    ServerReporter reporter = ServerReporter.instance(metric.getRegistry());
    reporter.start(60L, TimeUnit.SECONDS);
    // Add metrics for MAX_WRITE_THREADS
    int maxWriteThreads = config.get(ServerOptions.MAX_WRITE_THREADS);
    MetricsUtil.registerGauge(RestServer.class, "max-write-threads", () -> {
        return maxWriteThreads;
    });
    // Add metrics for caches
    @SuppressWarnings({ "rawtypes", "unchecked" }) Map<String, Cache<?, ?>> caches = (Map) CacheManager.instance().caches();
    registerCacheMetrics(caches);
    final AtomicInteger lastCachesSize = new AtomicInteger(caches.size());
    MetricsUtil.registerGauge(Cache.class, "instances", () -> {
        int count = caches.size();
        if (count != lastCachesSize.get()) {
            // Update if caches changed (effect in the next report period)
            registerCacheMetrics(caches);
            lastCachesSize.set(count);
        }
        return count;
    });
    // Add metrics for task
    MetricsUtil.registerGauge(TaskManager.class, "workers", () -> {
        return TaskManager.instance().workerPoolSize();
    });
    MetricsUtil.registerGauge(TaskManager.class, "pending-tasks", () -> {
        return TaskManager.instance().pendingTasks();
    });
}
Also used : MetricManager(org.apache.tinkerpop.gremlin.server.util.MetricManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerReporter(com.baidu.hugegraph.metrics.ServerReporter) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Cache(com.baidu.hugegraph.backend.cache.Cache)

Example 3 with Cache

use of com.baidu.hugegraph.backend.cache.Cache in project incubator-hugegraph by apache.

the class CacheManagerTest method testCachePutGetWithCapacity.

@Test
public void testCachePutGetWithCapacity() {
    final String name = "test-cache";
    final int capacity = 12345;
    CacheManager manager = CacheManager.instance();
    Mockito.when(this.mockCaches.containsKey(name)).thenReturn(false);
    @SuppressWarnings("rawtypes") final Cache[] cache = new Cache[1];
    Mockito.when(this.mockCaches.putIfAbsent(Mockito.anyString(), Mockito.any())).thenAnswer(i -> cache[0] = (Cache<?, ?>) i.getArguments()[1]);
    Mockito.when(this.mockCaches.get(name)).thenAnswer(i -> cache[0]);
    Cache<Id, Object> cache1 = manager.cache(name, capacity);
    Assert.assertNotNull(cache1);
    Assert.assertEquals(capacity, cache1.capacity());
    Mockito.verify(this.mockCaches).putIfAbsent(name, cache1);
    Mockito.when(this.mockCaches.containsKey(name)).thenReturn(true);
    Mockito.when(this.mockCaches.get(name)).thenReturn(cache1);
    Cache<Id, Object> cache2 = manager.cache(name, capacity);
    Assert.assertEquals(capacity, cache2.capacity());
    Assert.assertSame(cache1, cache2);
    Assert.assertSame(cache1, manager.cache(name));
    Assert.assertSame(cache1, manager.cache(name, 0));
    Assert.assertSame(cache1, manager.cache(name, 1));
    Assert.assertSame(cache1, manager.cache(name, capacity));
    Assert.assertSame(cache1, manager.cache(name, capacity + 10));
    Mockito.verify(this.mockCaches, Mockito.atMost(1)).putIfAbsent(Mockito.anyString(), Mockito.any());
}
Also used : CacheManager(com.baidu.hugegraph.backend.cache.CacheManager) Id(com.baidu.hugegraph.backend.id.Id) RamCache(com.baidu.hugegraph.backend.cache.RamCache) Cache(com.baidu.hugegraph.backend.cache.Cache) OffheapCache(com.baidu.hugegraph.backend.cache.OffheapCache) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 4 with Cache

use of com.baidu.hugegraph.backend.cache.Cache in project incubator-hugegraph by apache.

the class CacheManagerTest method testCacheList.

@Test
public void testCacheList() {
    CacheManager manager = CacheManager.instance();
    Cache<Id, Object> cache1 = Mockito.mock(RamCache.class);
    Cache<Id, Object> cache2 = Mockito.mock(OffheapCache.class);
    Mockito.when(this.mockCaches.get("cache-1")).thenReturn(cache1);
    Mockito.when(this.mockCaches.get("cache-2")).thenReturn(cache2);
    Mockito.when(this.mockCaches.size()).thenReturn(2);
    Map<String, Cache<Id, Object>> caches = manager.caches();
    Assert.assertEquals(2, caches.size());
    Assert.assertSame(cache1, caches.get("cache-1"));
    Assert.assertSame(cache2, caches.get("cache-2"));
    Assert.assertArrayEquals(this.mockCaches.values().toArray(), caches.values().toArray());
}
Also used : CacheManager(com.baidu.hugegraph.backend.cache.CacheManager) Id(com.baidu.hugegraph.backend.id.Id) RamCache(com.baidu.hugegraph.backend.cache.RamCache) Cache(com.baidu.hugegraph.backend.cache.Cache) OffheapCache(com.baidu.hugegraph.backend.cache.OffheapCache) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 5 with Cache

use of com.baidu.hugegraph.backend.cache.Cache in project incubator-hugegraph by apache.

the class CacheManagerTest method testCacheGetPut.

@Test
public void testCacheGetPut() {
    final String name = "test-cache";
    CacheManager manager = CacheManager.instance();
    Mockito.when(this.mockCaches.containsKey(name)).thenReturn(false);
    @SuppressWarnings("rawtypes") final Cache[] cache = new Cache[1];
    Mockito.when(this.mockCaches.putIfAbsent(Mockito.anyString(), Mockito.any())).thenAnswer(i -> cache[0] = (Cache<?, ?>) i.getArguments()[1]);
    Mockito.when(this.mockCaches.get(name)).thenAnswer(i -> cache[0]);
    Cache<Id, Object> cache1 = manager.cache(name);
    Assert.assertNotNull(cache1);
    Mockito.verify(this.mockCaches).putIfAbsent(name, cache1);
    Mockito.when(this.mockCaches.containsKey(name)).thenReturn(true);
    Mockito.when(this.mockCaches.get(name)).thenReturn(cache1);
    Cache<Id, Object> cache2 = manager.cache(name);
    Assert.assertSame(cache1, cache2);
    Mockito.verify(this.mockCaches, Mockito.atMost(1)).putIfAbsent(Mockito.anyString(), Mockito.any());
}
Also used : CacheManager(com.baidu.hugegraph.backend.cache.CacheManager) Id(com.baidu.hugegraph.backend.id.Id) RamCache(com.baidu.hugegraph.backend.cache.RamCache) Cache(com.baidu.hugegraph.backend.cache.Cache) OffheapCache(com.baidu.hugegraph.backend.cache.OffheapCache) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Aggregations

Cache (com.baidu.hugegraph.backend.cache.Cache)5 OffheapCache (com.baidu.hugegraph.backend.cache.OffheapCache)4 RamCache (com.baidu.hugegraph.backend.cache.RamCache)4 Id (com.baidu.hugegraph.backend.id.Id)4 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)4 Test (org.junit.Test)4 CacheManager (com.baidu.hugegraph.backend.cache.CacheManager)3 Map (java.util.Map)2 HugeGraph (com.baidu.hugegraph.HugeGraph)1 LevelCache (com.baidu.hugegraph.backend.cache.LevelCache)1 IdGenerator (com.baidu.hugegraph.backend.id.IdGenerator)1 ServerReporter (com.baidu.hugegraph.metrics.ServerReporter)1 Assert (com.baidu.hugegraph.testutil.Assert)1 Whitebox (com.baidu.hugegraph.testutil.Whitebox)1 Blob (com.baidu.hugegraph.util.Blob)1 Bytes (com.baidu.hugegraph.util.Bytes)1 ImmutableList (com.google.common.collect.ImmutableList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1