Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update cache_type default value #1681

Merged
merged 7 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,20 @@ protected Object access(Id id) {
@Override
protected boolean write(Id id, Object value, long timeOffset) {
Value serializedValue = new Value(value);
int serializedSize = serializedValue.serializedSize();
int serializedSize;
try {
serializedSize = serializedValue.serializedSize();
} catch (Throwable e) {
// May can't cache value that failed to serialize, like 0x00 byte
LOG.warn("Can't cache '{}' due to {}", id, e.toString());
return false;
}
if (serializedSize > VALUE_SIZE_TO_SKIP) {
LOG.info("Skip to cache '{}' due to value size {} > limit {}",
id, serializedSize, VALUE_SIZE_TO_SKIP);
return false;
}

long expireTime = this.expire();
boolean success;
if (expireTime <= 0L) {
Expand Down Expand Up @@ -222,10 +230,15 @@ public ByteBuffer asBuffer() {
if (this.value instanceof List) {
listSize = ((List<?>) this.value).size();
}
this.svalue = BytesBuffer.allocate(64 * listSize);
this.serialize(this.value, this.svalue);
this.serializedSize = this.svalue.position();
this.svalue.forReadWritten();

BytesBuffer buffer = BytesBuffer.allocate(64 * listSize);

// May fail to serialize and throw exception here
this.serialize(this.value, buffer);

this.serializedSize = buffer.position();
buffer.forReadWritten();
this.svalue = buffer;
}
return this.svalue.asByteBuffer();
}
Expand Down Expand Up @@ -339,13 +352,16 @@ private static enum ValueType {
LIST,
VERTEX,
EDGE,
BYTES(DataType.BLOB),
BOOLEAN(DataType.BOOLEAN),
BYTE(DataType.BYTE),
BLOB(DataType.BLOB),
STRING(DataType.TEXT),
INT(DataType.INT),
LONG(DataType.LONG),
FLOAT(DataType.FLOAT),
DOUBLE(DataType.DOUBLE),
DATE(DataType.DATE);
DATE(DataType.DATE),
UUID(DataType.UUID);

private DataType dataType;

Expand Down Expand Up @@ -379,7 +395,7 @@ public static ValueType valueOf(Object object) {
return ValueType.LIST;
} else if (HugeVertex.class.isAssignableFrom(clazz)) {
return ValueType.VERTEX;
} else if (clazz == HugeEdge.class) {
} else if (HugeEdge.class.isAssignableFrom(clazz)) {
return ValueType.EDGE;
} else {
for (ValueType type : values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ public static synchronized CoreOptions instance() {
"vertex.cache_type",
"The type of vertex cache, allowed values are [l1, l2].",
allowValues("l1", "l2"),
"l1"
"l2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can also add some performance comparison results?

Copy link
Contributor Author

@lxb1111 lxb1111 Dec 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test plan:
通过配置不同的cache配置,然后启动server;准备好不同的数据集通过jmeter工具对server进行50并发的查询(ID查询点、ID查询边);观察GC变化、请求的延迟情况。

result:
1、10000w数据集情况下点、边查询从请求延迟时间、GC回收时间来看L2级缓存优于L1级缓存
2、10000w数据集情况下点、边查询L1级缓存延迟时间、GC回收时间都是随着缓存设置的递增呈现非线性递增,且变化明显
3、10000w数据集情况下点、边查询L2级缓存延迟时间随着缓存设置的递增没有明显太大变化,但是GC回收时间是整体趋势为增加的情况
image

);

public static final ConfigOption<Long> VERTEX_CACHE_CAPACITY =
Expand All @@ -546,7 +546,7 @@ public static synchronized CoreOptions instance() {
"edge.cache_type",
"The type of edge cache, allowed values are [l1, l2].",
allowValues("l1", "l2"),
"l1"
"l2"
);

public static final ConfigOption<Long> EDGE_CACHE_CAPACITY =
Expand Down
2 changes: 2 additions & 0 deletions hugegraph-dist/src/assembly/static/conf/hugegraph.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ gremlin.graph=com.baidu.hugegraph.HugeFactory
# cache config
#schema.cache_capacity=100000
# vertex-cache default is 1000w, 10min expired
vertex.cache_type=l2
#vertex.cache_capacity=10000000
#vertex.cache_expire=600
# edge-cache default is 100w, 10min expired
edge.cache_type=l2
#edge.cache_capacity=1000000
#edge.cache_expire=600

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import com.baidu.hugegraph.HugeException;
import com.baidu.hugegraph.HugeGraph;
import com.baidu.hugegraph.backend.cache.Cache;
import com.baidu.hugegraph.backend.cache.LevelCache;
Expand All @@ -41,6 +41,7 @@
import com.baidu.hugegraph.testutil.Assert;
import com.baidu.hugegraph.testutil.Whitebox;
import com.baidu.hugegraph.unit.BaseUnitTest;
import com.baidu.hugegraph.util.Blob;
import com.baidu.hugegraph.util.Bytes;

import jersey.repackaged.com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -183,25 +184,22 @@ protected HugeGraph graph() {
return this.graph;
}

@Override
@Test
public void testUpdateAndGetWithDataType() {
public void testUpdateAndGetWithInvalidDataType() {
Cache<Id, Object> cache = newCache();
Id id = IdGenerator.of("1");

Assert.assertThrows(HugeException.class, () -> {
cache.update(id, 'c');
}, e -> {
Assert.assertContains("Unsupported type of serialize value",
e.getMessage());
});
cache.update(id, 'c');
Assert.assertNull(cache.get(id));

Assert.assertThrows(HugeException.class, () -> {
cache.update(id, true);
}, e -> {
Assert.assertContains("Unsupported type of serialize value",
e.getMessage());
});
cache.update(id, new Object());
Assert.assertNull(cache.get(id));

cache.update(id, new byte[]{1});
Assert.assertNull(cache.get(id));

cache.update(id, "string");
Assert.assertEquals("string", cache.get(id));
}
}

Expand All @@ -218,6 +216,28 @@ protected Cache<Id, Object> newCache(long capacity) {
OffheapCache l2cache = (OffheapCache) super.newCache(capacity);
return new LevelCache(l1cache, l2cache);
}

@Test
@Override
public void testUpdateAndGetWithInvalidDataType() {
// LevelCache includes level-1 RamCache, which can cache any object
Cache<Id, Object> cache = newCache();
Id id = IdGenerator.of("1");

cache.update(id, 'c');
Assert.assertEquals('c', cache.get(id));

Object obj = new Object();
cache.update(id, obj);
Assert.assertEquals(obj, cache.get(id));

byte[] bytes = new byte[]{1};
cache.update(id, bytes);
Assert.assertArrayEquals(bytes, (byte[]) cache.get(id));

cache.update(id, "string");
Assert.assertEquals("string", cache.get(id));
}
}

@Test
Expand All @@ -240,15 +260,26 @@ public void testUpdateAndGetWithDataType() {
Assert.assertNull(cache.get(id));

/*
* BOOLEAN
* BYTE
* STRING
* INT
* LONG
* FLOAT
* DOUBLE
* DATE
* BYTES
* UUID
* BLOB
* LIST
*/
cache.update(id, true);
Assert.assertEquals(true, cache.get(id));
cache.update(id, false);
Assert.assertEquals(false, cache.get(id));

cache.update(id, (byte) 123);
Assert.assertEquals((byte) 123, cache.get(id));

cache.update(id, "string");
Assert.assertEquals("string", cache.get(id));

Expand All @@ -268,9 +299,13 @@ public void testUpdateAndGetWithDataType() {
cache.update(id, now);
Assert.assertEquals(now, cache.get(id));

UUID uuid = UUID.randomUUID();
cache.update(id, uuid);
Assert.assertEquals(uuid, cache.get(id));

byte[] bytes = new byte[]{1, 33, 88};
cache.update(id, bytes);
Assert.assertArrayEquals(bytes, (byte[]) cache.get(id));
cache.update(id, Blob.wrap(bytes));
Assert.assertEquals(Blob.wrap(bytes), cache.get(id));

List<Integer> list = ImmutableList.of(1, 3, 5);
cache.update(id, list);
Expand Down