Skip to content

Commit

Permalink
Move GuavaSharedCache to its own module under storage/cache
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Roldan <groldan@boundlessgeo.com>
  • Loading branch information
Gabriel Roldan committed Nov 29, 2018
1 parent 48ba103 commit 9b9b68e
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 210 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ public long getMaximumSize() {
@Override
public double getMaximumSizeMB() {
long maxCacheSizeBytes = currentMaxCacheSize;
return maxCacheSizeBytes / (1024 * 1024);
return maxCacheSizeBytes / (1024d * 1024d);
}

@Override
Expand All @@ -417,7 +417,7 @@ public void setMaximumSize(long maxSizeBytes) throws IllegalArgumentException {
checkArgument(maxSizeBytes >= 0 && maxSizeBytes <= absoluteMaximumSize,
"Cache max size must be between 0 and %s, got %s", absoluteMaximumSize,
maxSizeBytes);
SharedCache cache = SharedCache.build(maxSizeBytes);
SharedCache cache = SharedCache.NO_CACHE;
SharedCache old = _SHARED_CACHE;
_SHARED_CACHE = cache;
if (old != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,10 @@ public void setEncoder(ObjectSerializingFactory encoder) {
* @throws IllegalArgumentException if {@code maxCacheSizeBytes} is lower than zero, with zero
* meaning no caching at all.
*/
public static SharedCache build(final long maxCacheSizeBytes) {
return SharedCache.build(L1_CACHE_SIZE, maxCacheSizeBytes);
}
// public static SharedCache build(final long maxCacheSizeBytes) {
// return SharedCache.build(L1_CACHE_SIZE, maxCacheSizeBytes);
// }

@VisibleForTesting
static SharedCache build(int L1capacity, long maxCacheSizeBytes) {
if (0L == maxCacheSizeBytes) {
return NO_CACHE;
}
return new GuavaSharedCache(L1capacity, maxCacheSizeBytes);
}

default boolean contains(CacheKey id) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.spy;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -32,7 +31,7 @@
import org.locationtech.geogig.storage.ObjectStore;
import org.locationtech.geogig.storage.memory.HeapObjectStore;

public class SharedCacheTest {
public abstract class SharedCacheTest {

private final long maxCacheSizeBytes = 1024 * 1024;

Expand All @@ -48,38 +47,44 @@ public class SharedCacheTest {
private RevTree obj;

public @Before void before() {
cache = spy(SharedCache.build(maxCacheSizeBytes));
repo1Id = new CacheIdentifier(1);
repo2Id = new CacheIdentifier(1000);
store = new HeapObjectStore();
store.open();
obj = RevObjectTestSupport.INSTANCE.createFeaturesTree(store, "f-", 10);
}

protected abstract SharedCache createCache(int l1CacheSize, long maxCacheSizeBytes);

public @After void after() {
store.close();
if (cache != null) {
cache.dispose();
}
}

public @Test void testBuildPrecondition() {
ex.expect(IllegalArgumentException.class);
SharedCache.build(-1L);
createCache(10, -1);
}

public @Test void testCacheDisabled() {
SharedCache cache = SharedCache.build(0L);
cache = createCache(0, 0L);
CacheKey k1 = repo1Id.create(obj.getId());
cache.put(k1, obj);
assertFalse(cache.contains(k1));
assertEquals(0L, cache.sizeBytes());
}

public @Test void testPutIfAbsent() {
cache = createCache(10, maxCacheSizeBytes);
CacheKey k1 = repo1Id.create(obj.getId());
assertNotNull(cache.put(k1, obj));
assertNull(cache.put(k1, obj));
}

public @Test void testKeyPrefix() {
cache = createCache(10, maxCacheSizeBytes);
CacheKey k1 = repo1Id.create(obj.getId());
CacheKey k2 = repo2Id.create(obj.getId());

Expand All @@ -89,6 +94,7 @@ public class SharedCacheTest {
}

public @Test void testGetIfPresentImmediately() {
cache = createCache(10, maxCacheSizeBytes);
CacheKey k1 = repo1Id.create(obj.getId());
assertNull(cache.getIfPresent(k1));
assertNotNull(cache.put(k1, obj));
Expand All @@ -100,6 +106,7 @@ public class SharedCacheTest {
}

public @Test void testGetIfPresentEnsureL2Cache() throws Exception {
cache = createCache(10, maxCacheSizeBytes);
CacheKey k1 = repo1Id.create(obj.getId());
assertNull(cache.getIfPresent(k1));

Expand All @@ -115,7 +122,7 @@ public class SharedCacheTest {

public @Test void testL1WriteBack() {
final int L1Capacity = 1000;
SharedCache cache = SharedCache.build(L1Capacity, maxCacheSizeBytes);
cache = createCache(L1Capacity, maxCacheSizeBytes);

List<RevObject> objects = createObjects(100);

Expand All @@ -125,10 +132,10 @@ public class SharedCacheTest {
objects.forEach((o) -> assertNotNull(cache.getIfPresent(repo1Id.create(o.getId()))));
}

@Ignore//too fragile depending on the jvm test heap
@Ignore // too fragile depending on the jvm test heap
public @Test void testInvalidateAllForPrefix() {
final int L1Capacity = 10;
SharedCache cache = SharedCache.build(L1Capacity, 32 * 1024 * 1024);
cache = createCache(L1Capacity, 32 * 1024 * 1024);

List<RevObject> objects = createObjects(100);

Expand All @@ -147,10 +154,10 @@ public class SharedCacheTest {
objects.forEach((o) -> assertNull(cache.getIfPresent(repo1Id.create(o.getId()))));
}

@Ignore//too fragile depending on the jvm test heap
@Ignore // too fragile depending on the jvm test heap
public @Test void testInvalidateAll() {
final int L1Capacity = 10;
SharedCache cache = SharedCache.build(L1Capacity, 32 * 1024 * 1024);
cache = createCache(L1Capacity, 32 * 1024 * 1024);

List<RevObject> objects = createObjects(500);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<parent>
<groupId>org.locationtech.geogig</groupId>
<artifactId>geogig-storage</artifactId>
<artifactId>geogig-storage-cache</artifactId>
<version>1.4-SNAPSHOT</version>
</parent>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.locationtech.geogig.storage.cache.caffeine;
package org.locationtech.geogig.cache.caffeine;

import static com.google.common.base.Preconditions.checkArgument;

Expand Down Expand Up @@ -134,13 +134,19 @@ public void setEncoder(ObjectSerializingFactory encoder) {

private long maxCacheSizeBytes;

private int l1Capacity;

private long maxCacheSizeBytes2;

CaffeineSharedCache() {
this.L1Cache = Caffeine.newBuilder().maximumSize(0).build();
this.L2Cache = Caffeine.newBuilder().maximumSize(0).build();
this.sizeTracker = new SizeTracker();
}

public CaffeineSharedCache(final int L1Capacity, long maxCacheSizeBytes) {
l1Capacity = L1Capacity;
maxCacheSizeBytes2 = maxCacheSizeBytes;
this.maxCacheSizeBytes = maxCacheSizeBytes;
checkArgument(L1Capacity >= 0);
checkArgument(maxCacheSizeBytes >= 0, "Cache size can't be < 0, 0 meaning no cache at all");
Expand Down Expand Up @@ -258,14 +264,13 @@ private void invalidateAll(CacheIdentifier prefix, ConcurrentMap<CacheKey, ?> ma
* added to the {@link #L2Cache}.
*/
public @Override @Nullable Future<?> put(CacheKey key, RevObject obj) {
RevObject l1val = TYPE.TREE == obj.getType()
RevObject l1val = (TYPE.TREE == obj.getType() && l1Capacity > 0)
? L1Cache.asMap().putIfAbsent(key, (RevTree) obj)
: null;
if (maxCacheSizeBytes > 0L && l1val == null) {
// add it to L2 if not already present, even if it's a RevTree and has been added to
// the L1 cache, since removal notifications happen after the fact
//return putInternal(key, obj);
insert(key, obj);
return putInternal(key, obj);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* Copyright (c) 2018 Boundless and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/edl-v10.html
*
* Contributors:
* Gabriel Roldan - initial implementation
*/
package org.locationtech.geogig.cache.caffeine;

import org.locationtech.geogig.storage.cache.SharedCache;
import org.locationtech.geogig.storage.cache.SharedCacheTest;

public class CaffeineSharedCacheTest extends SharedCacheTest {

protected @Override SharedCache createCache(int l1Capacity, long maxCacheSizeBytes) {
return new CaffeineSharedCache(l1Capacity, maxCacheSizeBytes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Contributors:
* Gabriel Roldan (Boundless) - initial implementation
*/
package org.locationtech.geogig.storage.cache.performance;
package org.locationtech.geogig.cache.performance;

import static org.locationtech.geogig.model.impl.RevObjectTestSupport.featureForceId;

Expand All @@ -30,6 +30,7 @@
import java.util.stream.IntStream;

import org.geotools.io.TableWriter;
import org.locationtech.geogig.cache.caffeine.CaffeineSharedCache;
import org.locationtech.geogig.model.Bucket;
import org.locationtech.geogig.model.Node;
import org.locationtech.geogig.model.ObjectId;
Expand All @@ -43,7 +44,6 @@
import org.locationtech.geogig.storage.cache.CacheIdentifier;
import org.locationtech.geogig.storage.cache.ObjectCache;
import org.locationtech.geogig.storage.cache.SharedCache;
import org.locationtech.geogig.storage.cache.caffeine.CaffeineSharedCache;
import org.locationtech.geogig.storage.datastream.DataStreamSerializationFactoryV2_2;
import org.locationtech.geogig.storage.datastream.LZ4SerializationFactory;
import org.locationtech.geogig.storage.datastream.v2_3.DataStreamSerializationFactoryV2_3;
Expand Down
72 changes: 72 additions & 0 deletions src/storage/cache/guava/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.locationtech.geogig</groupId>
<artifactId>geogig-storage-cache</artifactId>
<version>1.4-SNAPSHOT</version>
</parent>

<artifactId>geogig-cache-guava</artifactId>
<packaging>jar</packaging>
<name>Guava Cache implementation of SharedCache</name>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Automatic-Module-Name>geogig.storage.caffeine</Automatic-Module-Name>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
<dependency>
<groupId>org.locationtech.geogig</groupId>
<artifactId>geogig-core</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Test scope dependencies -->

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.locationtech.geogig</groupId>
<artifactId>geogig-core</artifactId>
<version>${project.version}</version>
<scope>test</scope>
<classifier>tests</classifier>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
package org.locationtech.geogig.storage.cache;
/* Copyright (c) 2018 Boundless and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/edl-v10.html
*
* Contributors:
* Gabriel Roldan - Pulled off from SharedCache.Impl as a top level class
*/
package org.locationtech.geogig.cache.guava;

import static com.google.common.base.Preconditions.checkArgument;

Expand All @@ -19,6 +28,10 @@
import org.locationtech.geogig.model.RevObject;
import org.locationtech.geogig.model.RevObject.TYPE;
import org.locationtech.geogig.model.RevTree;
import org.locationtech.geogig.storage.cache.CacheIdentifier;
import org.locationtech.geogig.storage.cache.CacheKey;
import org.locationtech.geogig.storage.cache.CacheStats;
import org.locationtech.geogig.storage.cache.SharedCache;
import org.locationtech.geogig.storage.datastream.v2_3.DataStreamSerializationFactoryV2_3;
import org.locationtech.geogig.storage.impl.ObjectSerializingFactory;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* Copyright (c) 2018 Boundless and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/edl-v10.html
*
* Contributors:
* Gabriel Roldan - initial implementation
*/
package org.locationtech.geogig.cache.guava;

import org.locationtech.geogig.storage.cache.SharedCache;
import org.locationtech.geogig.storage.cache.SharedCacheTest;

public class GuavaSharedCacheTest extends SharedCacheTest{

protected @Override SharedCache createCache(int l1Capacity, long maxCacheSizeBytes) {
return new GuavaSharedCache(l1Capacity, maxCacheSizeBytes);
}
}
21 changes: 21 additions & 0 deletions src/storage/cache/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.locationtech.geogig</groupId>
<artifactId>geogig</artifactId>
<version>1.4-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>

<artifactId>geogig-storage-cache</artifactId>
<packaging>pom</packaging>
<name>RevObject cache implementations</name>

<modules>
<module>guava</module>
<module>caffeine</module>
</modules>
</project>
Loading

0 comments on commit 9b9b68e

Please sign in to comment.