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

fix schema label: addIndexLabel/removeIndexLabel race condition #1807

Merged
merged 1 commit into from
Apr 21, 2022
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 @@ -983,7 +983,7 @@ public VertexLabel readVertexLabel(HugeGraph graph,
vertexLabel.properties(readIds(HugeKeys.PROPERTIES));
vertexLabel.primaryKeys(readIds(HugeKeys.PRIMARY_KEYS));
vertexLabel.nullableKeys(readIds(HugeKeys.NULLABLE_KEYS));
vertexLabel.indexLabels(readIds(HugeKeys.INDEX_LABELS));
vertexLabel.addIndexLabels(readIds(HugeKeys.INDEX_LABELS));
vertexLabel.enableLabelIndex(readBool(HugeKeys.ENABLE_LABEL_INDEX));
vertexLabel.status(readEnum(HugeKeys.STATUS, SchemaStatus.class));
vertexLabel.ttl(readLong(HugeKeys.TTL));
Expand Down Expand Up @@ -1024,7 +1024,7 @@ public EdgeLabel readEdgeLabel(HugeGraph graph,
edgeLabel.properties(readIds(HugeKeys.PROPERTIES));
edgeLabel.sortKeys(readIds(HugeKeys.SORT_KEYS));
edgeLabel.nullableKeys(readIds(HugeKeys.NULLABLE_KEYS));
edgeLabel.indexLabels(readIds(HugeKeys.INDEX_LABELS));
edgeLabel.addIndexLabels(readIds(HugeKeys.INDEX_LABELS));
edgeLabel.enableLabelIndex(readBool(HugeKeys.ENABLE_LABEL_INDEX));
edgeLabel.status(readEnum(HugeKeys.STATUS, SchemaStatus.class));
edgeLabel.ttl(readLong(HugeKeys.TTL));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ public VertexLabel readVertexLabel(HugeGraph graph,
vertexLabel.properties(this.toIdArray(properties));
vertexLabel.primaryKeys(this.toIdArray(primaryKeys));
vertexLabel.nullableKeys(this.toIdArray(nullableKeys));
vertexLabel.indexLabels(this.toIdArray(indexLabels));
vertexLabel.addIndexLabels(this.toIdArray(indexLabels));
vertexLabel.status(status);
vertexLabel.ttl(ttl.longValue());
vertexLabel.ttlStartTime(this.toId(ttlStartTime));
Expand Down Expand Up @@ -530,7 +530,7 @@ public EdgeLabel readEdgeLabel(HugeGraph graph, BackendEntry backendEntry) {
edgeLabel.properties(this.toIdArray(properties));
edgeLabel.sortKeys(this.toIdArray(sortKeys));
edgeLabel.nullableKeys(this.toIdArray(nullableKeys));
edgeLabel.indexLabels(this.toIdArray(indexLabels));
edgeLabel.addIndexLabels(this.toIdArray(indexLabels));
edgeLabel.status(status);
edgeLabel.ttl(ttl.longValue());
edgeLabel.ttlStartTime(this.toId(ttlStartTime));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ public VertexLabel readVertexLabel(HugeGraph graph,
vertexLabel.properties(readIds(properties));
vertexLabel.primaryKeys(readIds(primaryKeys));
vertexLabel.nullableKeys(readIds(nullableKeys));
vertexLabel.indexLabels(readIds(indexLabels));
vertexLabel.addIndexLabels(readIds(indexLabels));
vertexLabel.enableLabelIndex(JsonUtil.fromJson(enableLabelIndex,
Boolean.class));
readUserdata(vertexLabel, entry);
Expand Down Expand Up @@ -643,7 +643,7 @@ public EdgeLabel readEdgeLabel(HugeGraph graph,
edgeLabel.properties(readIds(properties));
edgeLabel.sortKeys(readIds(sortKeys));
edgeLabel.nullableKeys(readIds(nullablekeys));
edgeLabel.indexLabels(readIds(indexLabels));
edgeLabel.addIndexLabels(readIds(indexLabels));
edgeLabel.enableLabelIndex(JsonUtil.fromJson(enableLabelIndex,
Boolean.class));
readUserdata(edgeLabel, entry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,40 @@ public void addIndexLabel(SchemaLabel schemaLabel, IndexLabel indexLabel) {
if (schemaLabel.equals(VertexLabel.OLAP_VL)) {
return;
}
schemaLabel.indexLabel(indexLabel.id());
this.updateSchema(schemaLabel);

// FIXME: move schemaLabel update into updateSchema() lock block instead
synchronized (schemaLabel) {
schemaLabel.addIndexLabel(indexLabel.id());
this.updateSchema(schemaLabel);
}
}

@Watched(prefix = "schema")
public void removeIndexLabelFromBaseLabel(IndexLabel indexLabel) {
HugeType baseType = indexLabel.baseType();
Id baseValue = indexLabel.baseValue();
SchemaLabel schemaLabel;
if (baseType == HugeType.VERTEX_LABEL) {
schemaLabel = this.getVertexLabel(baseValue);
} else {
assert baseType == HugeType.EDGE_LABEL;
schemaLabel = this.getEdgeLabel(baseValue);
}

if (schemaLabel == null) {
LOG.info("The base label '{}' of index label '{}' " +
"may be deleted before", baseValue, indexLabel);
return;
}
if (schemaLabel.equals(VertexLabel.OLAP_VL)) {
return;
}

// FIXME: move schemaLabel update into updateSchema() lock block instead
synchronized (schemaLabel) {
schemaLabel.removeIndexLabel(indexLabel.id());
this.updateSchema(schemaLabel);
}
}

@Watched(prefix = "schema")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ protected static void removeIndexLabel(HugeGraphParams graph, Id id) {
// Set index label to "deleting" status
schemaTx.updateSchemaStatus(indexLabel, SchemaStatus.DELETING);
try {
// Remove label from indexLabels of vertex or edge label
removeIndexLabelFromBaseLabel(schemaTx, indexLabel);
// Remove indexLabel from indexLabels of vertex/edge label
schemaTx.removeIndexLabelFromBaseLabel(indexLabel);

// Remove index data
// TODO: use event to replace direct call
graphTx.removeIndex(indexLabel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
import com.baidu.hugegraph.backend.id.IdGenerator;
import com.baidu.hugegraph.backend.tx.SchemaTransaction;
import com.baidu.hugegraph.job.SysJob;
import com.baidu.hugegraph.schema.IndexLabel;
import com.baidu.hugegraph.schema.SchemaElement;
import com.baidu.hugegraph.schema.SchemaLabel;
import com.baidu.hugegraph.schema.VertexLabel;
import com.baidu.hugegraph.type.HugeType;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.Log;
Expand Down Expand Up @@ -65,25 +62,6 @@ public static String formatTaskName(HugeType type, Id id, String name) {
return String.join(SPLITOR, type.toString(), id.asString(), name);
}

protected static void removeIndexLabelFromBaseLabel(SchemaTransaction tx,
IndexLabel label) {
HugeType baseType = label.baseType();
Id baseValue = label.baseValue();
SchemaLabel schemaLabel;
if (baseType == HugeType.VERTEX_LABEL) {
if (VertexLabel.OLAP_VL.id().equals(baseValue)) {
return;
}
schemaLabel = tx.getVertexLabel(baseValue);
} else {
assert baseType == HugeType.EDGE_LABEL;
schemaLabel = tx.getEdgeLabel(baseValue);
}
assert schemaLabel != null;
schemaLabel.removeIndexLabel(label.id());
updateSchema(tx, schemaLabel);
}

/**
* Use reflection to call SchemaTransaction.removeSchema(),
* which is protected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ public Id indexField() {
return this.indexFields.get(0);
}

public SchemaLabel baseElement() {
return getElement(this.graph, this.baseType, this.baseValue);
public SchemaLabel baseLabel() {
return getBaseLabel(this.graph, this.baseType, this.baseValue);
}

public boolean hasSameContent(IndexLabel other) {
Expand Down Expand Up @@ -210,8 +210,9 @@ public static IndexLabel label(HugeGraph graph, Id id) {
return graph.indexLabel(id);
}

public static SchemaLabel getElement(HugeGraph graph,
HugeType baseType, Object baseValue) {
public static SchemaLabel getBaseLabel(HugeGraph graph,
HugeType baseType,
Object baseValue) {
E.checkNotNull(baseType, "base type", "index label");
E.checkNotNull(baseValue, "base value", "index label");
E.checkArgument(baseValue instanceof String || baseValue instanceof Id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ public Set<Id> indexLabels() {
return Collections.unmodifiableSet(this.indexLabels);
}

public void indexLabel(Id id) {
public void addIndexLabel(Id id) {
this.indexLabels.add(id);
}

public void indexLabels(Id... ids) {
public void addIndexLabels(Id... ids) {
this.indexLabels.addAll(Arrays.asList(ids));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ public IndexLabelBuilder(SchemaTransaction transaction,
super(transaction, graph);
E.checkNotNull(copy, "copy");
// Get base element from self graph
SchemaLabel schemaLabel = IndexLabel.getElement(graph, copy.baseType(),
copy.baseValue());
SchemaLabel schemaLabel = IndexLabel.getBaseLabel(graph,
copy.baseType(),
copy.baseValue());
this.id = null;
this.name = copy.name();
this.baseType = copy.baseType();
Expand All @@ -110,7 +111,7 @@ public IndexLabel build() {
this.checkFields4Range();
IndexLabel indexLabel = new IndexLabel(graph, id, this.name);
indexLabel.baseType(this.baseType);
SchemaLabel schemaLabel = this.loadElement();
SchemaLabel schemaLabel = this.loadBaseLabel();
indexLabel.baseValue(schemaLabel.id());
indexLabel.indexType(this.indexType);
for (String field : this.indexFields) {
Expand All @@ -137,7 +138,7 @@ private boolean hasSameProperties(IndexLabel existedIndexLabel) {
return false;
}

SchemaLabel schemaLabel = this.loadElement();
SchemaLabel schemaLabel = this.loadBaseLabel();
if (!schemaLabel.id().equals(existedIndexLabel.baseValue())) {
return false;
}
Expand Down Expand Up @@ -200,7 +201,7 @@ public SchemaElement.TaskWithSchema createWithTask() {
IdGenerator.ZERO);
}

SchemaLabel schemaLabel = this.loadElement();
SchemaLabel schemaLabel = this.loadBaseLabel();

/*
* If new index label is prefix of existed index label, or has
Expand Down Expand Up @@ -286,7 +287,7 @@ public IndexLabel append() {
this.checkStableVars();
Userdata.check(this.userdata, Action.APPEND);
indexLabel.userdata(this.userdata);
SchemaLabel schemaLabel = indexLabel.baseElement();
SchemaLabel schemaLabel = indexLabel.baseLabel();
this.graph().addIndexLabel(schemaLabel, indexLabel);
return indexLabel;
}
Expand All @@ -302,7 +303,7 @@ public IndexLabel eliminate() {
Userdata.check(this.userdata, Action.ELIMINATE);

indexLabel.removeUserdata(this.userdata);
SchemaLabel schemaLabel = indexLabel.baseElement();
SchemaLabel schemaLabel = indexLabel.baseLabel();
this.graph().addIndexLabel(schemaLabel, indexLabel);
return indexLabel;
}
Expand Down Expand Up @@ -454,9 +455,9 @@ private void checkIndexType() {
}
}

private SchemaLabel loadElement() {
return IndexLabel.getElement(this.graph(),
this.baseType, this.baseValue);
private SchemaLabel loadBaseLabel() {
return IndexLabel.getBaseLabel(this.graph(),
this.baseType, this.baseValue);
}

private void checkFields(Set<Id> propertyIds) {
Expand Down Expand Up @@ -619,7 +620,6 @@ private Set<Id> removeSubIndex(SchemaLabel schemaLabel) {
}
Set<Id> tasks = InsertionOrderUtil.newSet();
for (Id id : overrideIndexLabelIds) {
schemaLabel.removeIndexLabel(id);
Id task = this.graph().removeIndexLabel(id);
E.checkNotNull(task, "remove sub index label task");
tasks.add(task);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public boolean hasTtl() {
if (this.indexLabel.system()) {
return false;
}
return this.indexLabel.baseElement().ttl() > 0L;
return this.indexLabel.baseLabel().ttl() > 0L;
}

public long ttl() {
Expand Down