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";
}));
}
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();
});
}
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());
}
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());
}
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());
}
Aggregations