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

set indexlabel invalid if create or rebuild failed #1226

Merged
merged 6 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -86,6 +86,7 @@
import com.baidu.hugegraph.type.define.Action;
import com.baidu.hugegraph.type.define.HugeKeys;
import com.baidu.hugegraph.type.define.IndexType;
import com.baidu.hugegraph.type.define.SchemaStatus;
import com.baidu.hugegraph.util.CollectionUtil;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.InsertionOrderUtil;
Expand Down Expand Up @@ -369,6 +370,7 @@ public IdHolderList queryIndex(ConditionQuery query) {
private IdHolderList queryByLabel(ConditionQuery query) {
HugeType queryType = query.resultType();
IndexLabel il = IndexLabel.label(queryType);
validateIndexLabel(il);
Id label = query.condition(HugeKeys.LABEL);
assert label != null;

Expand Down Expand Up @@ -426,6 +428,9 @@ private IdHolderList queryByUserprop(ConditionQuery query) {
// Do index query
IdHolderList holders = new IdHolderList(paging);
for (MatchedIndex index : indexes) {
for (IndexLabel il : index.indexLabels()) {
validateIndexLabel(il);
}
if (paging && index.indexLabels().size() > 1) {
throw new NotSupportException("joint index query in paging");
}
Expand Down Expand Up @@ -1349,6 +1354,13 @@ private static NoIndexException noIndexException(HugeGraph graph,
name, String.join("/", mismatched));
}

private static void validateIndexLabel(IndexLabel indexLabel) {
if (indexLabel.status() == SchemaStatus.INVALID) {
throw new HugeException("Can't query by label index '%s' due to " +
"it is in invalid status", indexLabel);
}
}

private static boolean hasNullableProp(HugeElement element, Id key) {
return element.schemaLabel().nullableKeys().contains(key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,27 @@ private void rebuildIndex(SchemaLabel label, Collection<Id> indexLabelIds) {
* They have different id lead to it can't compare and optimize
*/
graphTx.commit();
if (label.type() == HugeType.VERTEX_LABEL) {
@SuppressWarnings("unchecked")
Consumer<Vertex> consumer = (Consumer<Vertex>) indexUpdater;
graphTx.traverseVerticesByLabel((VertexLabel) label,
consumer, false);
} else {
assert label.type() == HugeType.EDGE_LABEL;
@SuppressWarnings("unchecked")
Consumer<Edge> consumer = (Consumer<Edge>) indexUpdater;
graphTx.traverseEdgesByLabel((EdgeLabel) label,
consumer, false);

try {
if (label.type() == HugeType.VERTEX_LABEL) {
@SuppressWarnings("unchecked")
Consumer<Vertex> consumer = (Consumer<Vertex>) indexUpdater;
graphTx.traverseVerticesByLabel((VertexLabel) label,
consumer, false);
} else {
assert label.type() == HugeType.EDGE_LABEL;
@SuppressWarnings("unchecked")
Consumer<Edge> consumer = (Consumer<Edge>) indexUpdater;
graphTx.traverseEdgesByLabel((EdgeLabel) label,
consumer, false);
}
graphTx.commit();
} catch (Throwable e) {
for (IndexLabel il : ils) {
schemaTx.updateSchemaStatus(il, SchemaStatus.INVALID);
}
throw e;
}
graphTx.commit();

for (IndexLabel il : ils) {
schemaTx.updateSchemaStatus(il, SchemaStatus.CREATED);
Expand All @@ -158,6 +166,9 @@ private void removeIndex(Collection<Id> indexLabelIds) {
try {
locks.lockWrites(LockUtil.INDEX_LABEL_DELETE, indexLabelIds);
graphTx.removeIndex(il);
} catch (Throwable e) {
schemaTx.updateSchemaStatus(il, SchemaStatus.INVALID);
throw e;
} finally {
locks.unlock();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
import com.baidu.hugegraph.schema.EdgeLabel;
import com.baidu.hugegraph.schema.IndexLabel;
import com.baidu.hugegraph.schema.PropertyKey;
import com.baidu.hugegraph.schema.SchemaElement;
import com.baidu.hugegraph.schema.VertexLabel;
import com.baidu.hugegraph.type.HugeType;
import com.baidu.hugegraph.type.define.GraphMode;
import com.baidu.hugegraph.type.define.SchemaStatus;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.LockUtil;

Expand Down Expand Up @@ -77,6 +79,11 @@ protected <V> V lockCheckAndCreateSchema(HugeType type, String name,
}
}

protected void updateSchemaStatus(SchemaElement element,
SchemaStatus status) {
this.transaction.updateSchemaStatus(element, status);
}

protected void checkSchemaIdIfRestoringMode(HugeType type, Id id) {
if (this.transaction.graphMode() == GraphMode.RESTORING) {
E.checkArgument(id != null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,17 @@ public IndexLabel.CreatedIndexLabel createWithTask() {
// Create index label (just schema)
indexLabel.status(SchemaStatus.CREATING);
this.graph().addIndexLabel(schemaLabel, indexLabel);
try {
// Async rebuild index
Id rebuildTask = this.rebuildIndex(indexLabel, removeTasks);
E.checkNotNull(rebuildTask, "rebuild-index task");

// Async rebuild index
Id rebuildTask = this.rebuildIndex(indexLabel, removeTasks);
E.checkNotNull(rebuildTask, "rebuild-index task");

return new IndexLabel.CreatedIndexLabel(indexLabel, rebuildTask);
return new IndexLabel.CreatedIndexLabel(indexLabel,
rebuildTask);
} catch (Throwable e) {
this.updateSchemaStatus(indexLabel, SchemaStatus.INVALID);
Copy link
Contributor

Choose a reason for hiding this comment

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

also consider removeSubIndex() at line 207

throw e;
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public enum SchemaStatus implements SerialEnum {

REBUILDING(3, "rebuilding"),

DELETING(4, "deleting");
DELETING(4, "deleting"),

INVALID(5, "invalid");

private byte code = 0;
private String name = null;
Expand Down