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

support ttl for vertex and edge #794

Merged
merged 24 commits into from
May 14, 2020
Merged

support ttl for vertex and edge #794

merged 24 commits into from
May 14, 2020

Conversation

zhoney
Copy link
Contributor

@zhoney zhoney commented Dec 25, 2019

implemented: #295

Change-Id: I16a762c5aa850f14cbd554d29fb8592db87e7619

@codecov
Copy link

codecov bot commented Dec 26, 2019

Codecov Report

Merging #794 into master will increase coverage by 0.31%.
The diff coverage is 82.44%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #794      +/-   ##
============================================
+ Coverage     69.72%   70.04%   +0.31%     
- Complexity     5126     5255     +129     
============================================
  Files           320      324       +4     
  Lines         24853    25433     +580     
  Branches       3466     3559      +93     
============================================
+ Hits          17329    17814     +485     
- Misses         5861     5916      +55     
- Partials       1663     1703      +40     
Impacted Files Coverage Δ Complexity Δ
...a/com/baidu/hugegraph/api/schema/EdgeLabelAPI.java 56.94% <0.00%> (-6.14%) 0.00 <0.00> (ø)
...com/baidu/hugegraph/api/schema/VertexLabelAPI.java 58.57% <0.00%> (-6.51%) 0.00 <0.00> (ø)
...a/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java 4.75% <0.00%> (-0.02%) 0.00 <0.00> (ø)
...e/src/main/java/com/baidu/hugegraph/HugeGraph.java 57.14% <ø> (ø) 8.00 <0.00> (ø)
...hugegraph/backend/serializer/TextBackendEntry.java 86.79% <0.00%> (-0.55%) 53.00 <0.00> (ø)
...om/baidu/hugegraph/backend/store/BackendEntry.java 74.28% <ø> (ø) 1.00 <0.00> (ø)
...baidu/hugegraph/backend/store/BackendFeatures.java 0.00% <ø> (ø) 0.00 <0.00> (ø)
...ain/java/com/baidu/hugegraph/schema/EdgeLabel.java 80.64% <ø> (ø) 16.00 <0.00> (ø)
...n/java/com/baidu/hugegraph/schema/VertexLabel.java 94.11% <ø> (ø) 10.00 <0.00> (ø)
...aidu/hugegraph/backend/store/mysql/MysqlTable.java 79.08% <23.07%> (-2.34%) 77.00 <0.00> (+1.00) ⬇️
... and 63 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6ac15dd...f4070fb. Read the comment docs.

@@ -116,4 +116,8 @@ public boolean supportsNumberType() {
public boolean supportsAggregateProperty() {
return false;
}

@Override public boolean supportsTtl() {
Copy link
Contributor

Choose a reason for hiding this comment

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

wrap line after @OverRide

int ttl = ttl(entry);
if (ttl != 0) {
update.using(QueryBuilder.ttl(ttl));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

prefer add a static method setTtl(statment, entry)

ImmutableMap<HugeKeys, DataType> columns = ImmutableMap.of();
ImmutableMap<HugeKeys, DataType> columns = ImmutableMap.of(
HugeKeys.EXPIRED_TIME, DataType.decimal()
);
Copy link
Contributor

Choose a reason for hiding this comment

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

No need to store a column of data for EXPIRED_TIME if the backend supports ttl

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we can't get ttl along with existing query statement. Using extra query degrades performance

if (index == null) {
// Index is expired
return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

rather determine here whether it is expired

Copy link
Contributor Author

Choose a reason for hiding this comment

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

element_ids may have different ttl.

Copy link
Contributor

Choose a reason for hiding this comment

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

prefer to change HugeIndex struct

public static final ConfigOption<Integer> EXPIRED_DELETE_BATCH =
new ConfigOption<>(
"expired.delete_batch",
"Whether to delete expired object synchronously.",
Copy link
Contributor

Choose a reason for hiding this comment

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

update description

if (this.deleteTemplate != null) {
protected String buildDeleteTemplate(List<HugeKeys> idNames,
Long expiredTime) {
boolean cached;
Copy link
Contributor

Choose a reason for hiding this comment

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

rename to updateCache

protected String buildDeleteTemplate(List<HugeKeys> idNames) {
if (this.deleteTemplate != null) {
protected String buildDeleteTemplate(List<HugeKeys> idNames,
Long expiredTime) {
Copy link
Contributor

Choose a reason for hiding this comment

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

long

} else {
cached = false;
// Set delete with ttl to avoid mistaken deletion
idNames.add(HugeKeys.EXPIRED_TIME);
Copy link
Contributor

Choose a reason for hiding this comment

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

why EXPIRED_TIME is in idNames

Copy link
Contributor

Choose a reason for hiding this comment

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

address it

@@ -118,4 +118,8 @@ public boolean supportsNumberType() {
public boolean supportsAggregateProperty() {
return false;
}

@Override public boolean supportsTtl() {
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

@@ -45,5 +45,7 @@ palo.host=localhost

snowflake.force_string=true
schema.sync_deletion=true
expired.sync_deletion=true
expired.delete_batch=1
Copy link
Contributor

Choose a reason for hiding this comment

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

new ConfigOption<>(
"expired.sync_deletion",
"Whether to delete expired object synchronously.",
"sync_deletion",
Copy link
Contributor

Choose a reason for hiding this comment

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

task.sync_deletion

disallowEmpty(),
false
);

public static final ConfigOption<Integer> EXPIRED_DELETE_BATCH =
new ConfigOption<>(
"expired.delete_batch",
"Whether to delete expired object synchronously.",
"The batch size used to delete expired data.",
Copy link
Contributor

Choose a reason for hiding this comment

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

reuse COMMIT_BATCH?

Copy link
Contributor

Choose a reason for hiding this comment

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

address it

@@ -166,6 +171,8 @@ public void init(CassandraSessionPool.Session session) {
.put(HugeKeys.ENABLE_LABEL_INDEX, DataType.cboolean())
.put(HugeKeys.USER_DATA, TYPE_UD)
.put(HugeKeys.STATUS, DataType.tinyint())
.put(HugeKeys.TTL, DataType.cint())
Copy link
Contributor

Choose a reason for hiding this comment

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

define TYPE_TTL

@@ -294,7 +301,8 @@ public void init(CassandraSessionPool.Session session) {
HugeKeys.OTHER_VERTEX, TYPE_ID
);
ImmutableMap<HugeKeys, DataType> columns = ImmutableMap.of(
HugeKeys.PROPERTIES, DataType.map(TYPE_PK, TYPE_PROP)
HugeKeys.PROPERTIES, DataType.map(TYPE_PK, TYPE_PROP),
HugeKeys.EXPIRED_TIME, DataType.decimal()
Copy link
Contributor

Choose a reason for hiding this comment

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

define TYPE_EXPIRED_TIME as long

ImmutableMap<HugeKeys, DataType> columns = ImmutableMap.of();
ImmutableMap<HugeKeys, DataType> columns = ImmutableMap.of(
HugeKeys.EXPIRED_TIME, DataType.decimal()
);
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

if (!graph.graphTransaction().store().features().supportsTtl() &&
!query.showExpired() &&
index.expiredTime() != 0L && index.expiredTime() < now) {
GraphTransaction.asyncDeleteExpiredObject(graph, index);
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

@@ -437,5 +437,9 @@ public boolean supportsNumberType() {
public boolean supportsAggregateProperty() {
return false;
}

@Override public boolean supportsTtl() {
Copy link
Contributor

Choose a reason for hiding this comment

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

wrap line, check all "@OverRide public"

@@ -189,6 +193,10 @@ protected void updateIndex(Id ilId, HugeElement element, boolean removed) {
// Not build index for record with nullable field except unique index
List<Object> propValues = allPropValues.subList(0, firstNullField);

// Expired time
long expiredTime = element.type().isEdge() ?
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe it's better to keep compatible with Vertex

if (index == null) {
// Index is expired
return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

prefer to change HugeIndex struct

protected String buildDeleteTemplate(List<HugeKeys> idNames,
Long expiredTime) {
boolean updateCache;
if (expiredTime == null || expiredTime == 0L) {
Copy link
Contributor

Choose a reason for hiding this comment

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

have to use expiredTime == null?

Copy link
Contributor

Choose a reason for hiding this comment

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

address it

@zhoney zhoney force-pushed the ttl branch 2 times, most recently from 481d343 to be2c284 Compare March 16, 2020 07:20

public Set<IdWithExpiredTime> expiredElementIds() {
long now = DateUtil.now().getTime();
Set<IdWithExpiredTime> expired = new LinkedHashSet<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

InsertionOrderUtil.newSet(this.elementIds.size())

public Set<Id> elementIds() {
return Collections.unmodifiableSet(this.elementIds);
Set<Id> ids = new LinkedHashSet<>(this.elementIds.size());
Copy link
Contributor

Choose a reason for hiding this comment

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

InsertionOrderUtil.newSet(this.elementIds.size())
I will add method InsertionOrderUtil.newSet(int size) to common

@@ -187,7 +188,7 @@ private void unlistenChanges() {

return results;
}

@SuppressWarnings("unchecked")
Copy link
Contributor

Choose a reason for hiding this comment

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

missing empty line

if (survivedEdges.size() < edges.size()) {
edges = survivedEdges;
}
if (edges.size() <= MAX_CACHE_EDGES_PER_QUERY) {
Copy link
Contributor

Choose a reason for hiding this comment

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

don't update cache if unneeded

@@ -569,6 +585,20 @@ private IdHolder doIndexQueryBatch(IndexLabel indexLabel,
});
}

private void removeExpiredIndexIfNeeded(HugeIndex index,
boolean showExpired) {
if (graph().graphTransaction().store().features().supportsTtl() ||
Copy link
Contributor

Choose a reason for hiding this comment

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

this.store().features()

@@ -169,17 +184,34 @@ protected void parseRowColumns(Result row, BackendEntry entry,
public static class IndexTable extends HbaseTable {

private static final int INDEX_DELETE_BATCH = 1000;
protected HugeType type;
Copy link
Contributor

Choose a reason for hiding this comment

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

set final

session.put(this.table(), CF, col.name,
BinarySerializer.EMPTY_BYTES, col.value);
BytesBuffer buffer = BytesBuffer.wrap(col.name);
buffer.readIndexId(this.type());
Copy link
Contributor

Choose a reason for hiding this comment

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

add a field ttl to BackendColumn

protected String buildDeleteTemplate(List<HugeKeys> idNames,
Long expiredTime) {
boolean updateCache;
if (expiredTime == null || expiredTime == 0L) {
Copy link
Contributor

Choose a reason for hiding this comment

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

address it

} else {
cached = false;
// Set delete with ttl to avoid mistaken deletion
idNames.add(HugeKeys.EXPIRED_TIME);
Copy link
Contributor

Choose a reason for hiding this comment

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

address it

List<HugeKeys> idNames = this.idColumnName();
String template = this.buildDeleteTemplate(idNames);

List<HugeKeys> idNames = new ArrayList<>(this.idColumnName());
Copy link
Contributor

Choose a reason for hiding this comment

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

don't clone ArrayList here

@@ -101,7 +101,7 @@
* [0.50] Issue-746: Support userdata for index label
* [0.51] Issue-929: Support 5 TP RESTful API
* [0.52] Issue-781: Support range query for rest api like P.gt(18)
* [0.53] Issue-295: Support ttl for edge label
* [0.53] Issue-295: Support ttl
Copy link
Contributor

Choose a reason for hiding this comment

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

Support ttl for edge and vertex

@Override
public void eliminate(CassandraSessionPool.Session session,
CassandraBackendEntry.Row entry) {
Update append = this.buildEliminate(entry);
Copy link
Contributor

Choose a reason for hiding this comment

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

typo: append
and seems no need to set ttl for eliminate

Using usingTtl = QueryBuilder.ttl(ttl);
long ttl = entry.ttl();
if (ttl != 0L) {
int calcTtl = (int) Math.ceil((double) ttl / 1000);
Copy link
Contributor

Choose a reason for hiding this comment

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

convert (double) ttl may loss accuracy, prefer ttl / 1000d

@@ -655,6 +660,8 @@ public static void shutdown(long timeout) {
private final AtomicInteger refs;
// Flag opened of each thread
private final ThreadLocal<Boolean> opened;
// Last time check flag opened of each thread
private final ThreadLocal<Long> lastCheckOpenedTime;
Copy link
Contributor

Choose a reason for hiding this comment

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

move to Txs class

@@ -707,6 +716,7 @@ public void rollback() {

@Override
public boolean isOpen() {
this.lastCheckOpenedTime.set(DateUtil.now().getTime());
Copy link
Contributor

Choose a reason for hiding this comment

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

update in setOpened()

private Set<Id> elementIds;
private long expiredTime;
private Set<IdWithExpiredTime> elementIds;
private HugeGraph graph;
Copy link
Contributor

Choose a reason for hiding this comment

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

set final and move to the first line

@Override
public void insert(Session session, BackendEntry entry) {
long ttl = entry.ttl();
if (ttl == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

ttl <= 0L

long expiredTime = BytesBuffer.wrap(col.value).readVLong();
if (expiredTime == 0) {
long ttl = entry.ttl();
if (ttl == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

long expiredTime = buffer.readVLong();
if (expiredTime == 0) {
long ttl = entry.ttl();
if (ttl == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

if (aggregate == null && !hasOrder) {
select.append(this.orderByKeys());
}
if (!query.nolimit() || query.offset() > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

0L

@zhoney zhoney changed the title support edgeLabel ttl support ttl for vertex and edge Apr 20, 2020
IdQuery newQuery = new IdQuery(HugeType.VERTEX, query);
List<HugeVertex> vertices = new ArrayList<>();
for (Id vertexId : query.ids()) {
HugeVertex vertex = (HugeVertex) this.verticesCache.get(vertexId);
if (vertex == null) {
if (vertex == null || vertex.expired()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

invalid it if expired

@@ -716,7 +712,9 @@ public void rollback() {

@Override
public boolean isOpen() {
this.lastCheckOpenedTime.set(DateUtil.now().getTime());
if (this.transactions.get() != null) {
this.transactions.get().openTime(DateUtil.now().getTime());
Copy link
Contributor

Choose a reason for hiding this comment

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

should set in open()

@zhoney zhoney force-pushed the ttl branch 3 times, most recently from c47e73c to df01dc7 Compare April 29, 2020 16:55
zhoney added 15 commits May 13, 2020 11:16
implemented: #295

Change-Id: I16a762c5aa850f14cbd554d29fb8592db87e7619
Change-Id: Ia6cf75ac3c379594fc9300d685324dfa1a793ed0
Change-Id: I8a2a7869136908eb86f9330cbf807eb24a157717
Change-Id: I29d605db6b95aefa2926c63c70470ba7348533a5
Change-Id: I972151d5d17208a865947bec09e94bbb50b2f314
Change-Id: I29f4cfa2fca4b52e0c6a20edaa0885b51e1d2c57
Change-Id: I08e85edbe872480d6bf37019caa832c30d0d8735
Change-Id: I076ba069123ae7e9530d5a5e2f3c6054c781b7a4
Change-Id: If953f7321a424e62c73fea90969588cd8ecb8db6
Change-Id: I2b83062d46906165e9ad8dee385d39fd1c7b71fb
Change-Id: Ie8be022cd19caf53aa59ab65c12266e109d911dc
Change-Id: Ieaff6f73dac21b73696e529f0ba65163239de7b6
Change-Id: I9cc501548e4911ca03a3e1d6c94883dd1387cf80
Change-Id: Ib64d259c6f4eb67b370b4261a93c40f77fe4a6b1
Change-Id: I886df8c99e80a9514bb6076deac5dca5da177569
Change-Id: Ia00f4811b27048629497d476a25dfc7c5c60470b
Change-Id: Ib3afe8ca6f76d761ba8f0c7140af31ce6c1cbb6c
Change-Id: I06b260128c5cee6b5473bdd2bb1fef7b3828a670
Change-Id: Ideb9df5a99be3498e0c77f8c0bd6888329d6b7e5
Change-Id: Iba66cde9ae8152e47830ec6ec9034816a3cc3f7d
Change-Id: I363947c8031ae150a6e769255bc54e0d0db586cb
Change-Id: Ibab2e6034a45cbf808a5a6718c1e42704844cb74
Change-Id: I1b1832e2a55b4587e3a1521d0409765cf849f9f8
Change-Id: Id0efc2bac16fea21f41b9e348ed9d6684ee8034f
@zhoney zhoney merged commit cadc15a into master May 14, 2020
@zhoney zhoney deleted the ttl branch May 14, 2020 03:07
Comment on lines +558 to +579
<<<<<<< HEAD
=======
* Add a row record to a table with ttl for index
*/
public void put(String table, byte[] family, byte[] rowkey,
byte[] qualifier, byte[] value, long ttl) {
Put put = new Put(rowkey);
put.addColumn(family, qualifier, value);
put.setTTL(ttl);
this.batch(table, put);
}

/**
* Delete a record by rowkey and qualifier from a table
*/
public void remove(String table, byte[] family,
byte[] rowkey, byte[] qualifier) {
this.remove(table, family, rowkey, qualifier, false);
}

/**
>>>>>>> support edgeLabel ttl
Copy link
Member

Choose a reason for hiding this comment

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

Seems there is a pair of neglected conflicting code block marks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants