Skip to content

Commit

Permalink
Do schema update in the lock block
Browse files Browse the repository at this point in the history
Change-Id: I2daad8825939b04ff9b1374fff930ac9c4173f72
  • Loading branch information
javeme committed Jun 6, 2022
1 parent c1e9ceb commit 327acae
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

import com.baidu.hugegraph.HugeGraphParams;
import com.baidu.hugegraph.backend.id.Id;
Expand Down Expand Up @@ -210,8 +211,9 @@ private static Id generateId(HugeType type, String name) {
}

@Override
protected void updateSchema(SchemaElement schema) {
super.updateSchema(schema);
protected void updateSchema(SchemaElement schema,
Consumer<SchemaElement> updateCallback) {
super.updateSchema(schema, updateCallback);

this.updateCache(schema);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.util.CloseableIterator;
Expand Down Expand Up @@ -132,7 +133,7 @@ public Id addPropertyKey(PropertyKey propertyKey) {

@Watched(prefix = "schema")
public void updatePropertyKey(PropertyKey propertyKey) {
this.updateSchema(propertyKey);
this.updateSchema(propertyKey, null);
}

@Watched(prefix = "schema")
Expand Down Expand Up @@ -192,7 +193,7 @@ public void addVertexLabel(VertexLabel vertexLabel) {

@Watched(prefix = "schema")
public void updateVertexLabel(VertexLabel vertexLabel) {
this.updateSchema(vertexLabel);
this.updateSchema(vertexLabel, null);
}

@Watched(prefix = "schema")
Expand Down Expand Up @@ -229,7 +230,7 @@ public void addEdgeLabel(EdgeLabel edgeLabel) {

@Watched(prefix = "schema")
public void updateEdgeLabel(EdgeLabel edgeLabel) {
this.updateSchema(edgeLabel);
this.updateSchema(edgeLabel, null);
}

@Watched(prefix = "schema")
Expand All @@ -255,26 +256,25 @@ public Id removeEdgeLabel(Id id) {

@Watched(prefix = "schema")
public void addIndexLabel(SchemaLabel baseLabel, IndexLabel indexLabel) {
this.addSchema(indexLabel);

/*
* Update index name in base-label(VL/EL)
* Create index and update index name in base-label(VL/EL)
* TODO: should wrap update base-label and create index in one tx.
*/
this.addSchema(indexLabel);

if (baseLabel.equals(VertexLabel.OLAP_VL)) {
return;
}

// FIXME: move schemaLabel update into updateSchema() lock block instead
synchronized (baseLabel) {
this.updateSchema(baseLabel, schema -> {
// NOTE: Do schema update in the lock block
baseLabel.addIndexLabel(indexLabel.id());
this.updateSchema(baseLabel);
}
});
}

@Watched(prefix = "schema")
public void updateIndexLabel(IndexLabel indexLabel) {
this.updateSchema(indexLabel);
this.updateSchema(indexLabel, null);
}

@Watched(prefix = "schema")
Expand All @@ -298,11 +298,10 @@ public void removeIndexLabelFromBaseLabel(IndexLabel indexLabel) {
return;
}

// FIXME: move schemaLabel update into updateSchema() lock block instead
synchronized (baseLabel) {
this.updateSchema(baseLabel, schema -> {
// NOTE: Do schema update in the lock block
baseLabel.removeIndexLabel(indexLabel.id());
this.updateSchema(baseLabel);
}
});
}

@Watched(prefix = "schema")
Expand Down Expand Up @@ -388,31 +387,34 @@ public void updateSchemaStatus(SchemaElement schema, SchemaStatus status) {
LOG.warn("Can't update schema '{}', it may be deleted", schema);
return;
}
schema.status(status);
this.updateSchema(schema);

this.updateSchema(schema, schemaToUpdate -> {
// NOTE: Do schema update in the lock block
schema.status(status);
});
}

@Watched(prefix = "schema")
public boolean existsSchemaId(HugeType type, Id id) {
return this.getSchema(type, id) != null;
}

protected void updateSchema(SchemaElement schema) {
protected void updateSchema(SchemaElement schema,
Consumer<SchemaElement> updateCallback) {
LOG.debug("SchemaTransaction update {} with id '{}'",
schema.type(), schema.id());
this.saveSchema(schema, true);
this.saveSchema(schema, true, updateCallback);
}

protected void addSchema(SchemaElement schema) {
LOG.debug("SchemaTransaction add {} with id '{}'",
schema.type(), schema.id());
setCreateTimeIfNeeded(schema);
this.saveSchema(schema, false);
this.saveSchema(schema, false, null);
}

private void saveSchema(SchemaElement schema, boolean update) {
BackendEntry entry = this.serialize(schema);

private void saveSchema(SchemaElement schema, boolean update,
Consumer<SchemaElement> updateCallback) {
// System schema just put into SystemSchemaStore in memory
if (schema.longId() < 0L) {
this.systemSchemaStore.add(schema);
Expand All @@ -424,6 +426,12 @@ private void saveSchema(SchemaElement schema, boolean update) {
locks.lockWrites(LockUtil.hugeType2Group(schema.type()),
schema.id());

if (updateCallback != null) {
// NOTE: Do schema update in the lock block
updateCallback.accept(schema);
}
BackendEntry entry = this.serialize(schema);

this.beforeWrite();

if (update) {
Expand Down

0 comments on commit 327acae

Please sign in to comment.