Skip to content

Commit

Permalink
refactor olap property (#1675)
Browse files Browse the repository at this point in the history
* refactor olap property

Change-Id: I137a42916eeee00775eafb55ee299a8a64e21671
  • Loading branch information
javeme authored Dec 7, 2021
1 parent 0200dc6 commit 9978ad1
Show file tree
Hide file tree
Showing 18 changed files with 113 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -743,25 +743,28 @@ public boolean isSchemaStore() {
return false;
}

/**
* TODO: can we remove this method since createOlapTable would register?
*/
@Override
public void createOlapTable(Id pkId) {
CassandraTable table = new CassandraTables.Olap(this.store(), pkId);
table.init(this.session());
registerTableManager(this.olapTableName(pkId), table);
}

@Override
public void checkAndRegisterOlapTable(Id pkId) {
CassandraTable table = new CassandraTables.Olap(this.store(), pkId);
public void checkAndRegisterOlapTable(Id id) {
CassandraTable table = new CassandraTables.Olap(this.store(), id);
if (!this.existsTable(table.table())) {
throw new HugeException("Not exist table '%s'", table.table());
}
registerTableManager(this.olapTableName(pkId), table);
registerTableManager(this.olapTableName(id), table);
}

@Override
public void createOlapTable(Id id) {
CassandraTable table = new CassandraTables.Olap(this.store(), id);
table.init(this.session());
registerTableManager(this.olapTableName(id), table);
}

@Override
public void clearOlapTable(Id pkId) {
String name = this.olapTableName(pkId);
public void clearOlapTable(Id id) {
String name = this.olapTableName(id);
CassandraTable table = this.table(name);
if (table == null || !this.existsTable(table.table())) {
throw new HugeException("Not exist table '%s'", name);
Expand All @@ -770,8 +773,8 @@ public void clearOlapTable(Id pkId) {
}

@Override
public void removeOlapTable(Id pkId) {
String name = this.olapTableName(pkId);
public void removeOlapTable(Id id) {
String name = this.olapTableName(id);
CassandraTable table = this.table(name);
if (table == null || !this.existsTable(table.table())) {
throw new HugeException("Not exist table '%s'", name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,13 @@ public void serverStarted(Id serverId, NodeRole serverRole) {
serverId, serverRole, this.name);
this.serverInfoManager().initServerInfo(serverId, serverRole);

LOG.info("Search olap property key for graph '{}'", this.name);
this.schemaTransaction().initAndRegisterOlapTables();
// TODO: check necessary?
LOG.info("Check olap property-key tables for graph '{}'", this.name);
for (PropertyKey pk : this.schemaTransaction().getPropertyKeys()) {
if (pk.olap()) {
this.graphTransaction().initAndRegisterOlapTable(pk.id());
}
}

LOG.info("Restoring incomplete tasks for graph '{}'...", this.name);
this.taskScheduler().restoreTasks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@

import com.baidu.hugegraph.HugeGraph;
import com.baidu.hugegraph.backend.BackendException;
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.backend.store.raft.StoreSnapshotFile;
import com.baidu.hugegraph.config.CoreOptions;
import com.baidu.hugegraph.event.EventHub;
import com.baidu.hugegraph.event.EventListener;
import com.baidu.hugegraph.util.E;
Expand Down Expand Up @@ -153,34 +151,6 @@ public void initSystemInfo(HugeGraph graph) {
LOG.debug("Graph '{}' system info has been initialized", this.graph);
}

@Override
public void createOlapTable(HugeGraph graph, Id pkId) {
String g = graph.option(CoreOptions.STORE_GRAPH);
BackendStore store = this.stores.get(g);
store.createOlapTable(pkId);
}

@Override
public void initAndRegisterOlapTable(HugeGraph graph, Id pkId) {
String g = graph.option(CoreOptions.STORE_GRAPH);
BackendStore store = this.stores.get(g);
store.checkAndRegisterOlapTable(pkId);
}

@Override
public void clearOlapTable(HugeGraph graph, Id pkId) {
String g = graph.option(CoreOptions.STORE_GRAPH);
BackendStore store = this.stores.get(g);
store.clearOlapTable(pkId);
}

@Override
public void removeOlapTable(HugeGraph graph, Id pkId) {
String g = graph.option(CoreOptions.STORE_GRAPH);
BackendStore store = this.stores.get(g);
store.removeOlapTable(pkId);
}

@Override
public void createSnapshot() {
String snapshotPrefix = StoreSnapshotFile.SNAPSHOT_DIR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package com.baidu.hugegraph.backend.store;

import com.baidu.hugegraph.HugeGraph;
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.event.EventHub;
import com.baidu.hugegraph.event.EventListener;

Expand Down Expand Up @@ -55,26 +54,6 @@ public interface BackendStoreProvider {

public void initSystemInfo(HugeGraph graph);

public default void createOlapTable(HugeGraph graph, Id pkId) {
throw new UnsupportedOperationException(
"BackendStoreProvider.createOlapTable()");
}

public default void initAndRegisterOlapTable(HugeGraph graph, Id pkId) {
throw new UnsupportedOperationException(
"BackendStoreProvider.checkAndRegisterOlapTable()");
}

public default void clearOlapTable(HugeGraph graph, Id pkId) {
throw new UnsupportedOperationException(
"BackendStoreProvider.clearOlapTable()");
}

public default void removeOlapTable(HugeGraph graph, Id pkId) {
throw new UnsupportedOperationException(
"BackendStoreProvider.removeOlapTable()");
}

public void createSnapshot();

public void resumeSnapshot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2060,4 +2060,20 @@ private <T> void traverseByLabel(SchemaLabel label,
} while (page != null);
}
}

public void createOlapPk(Id pkId) {
this.store().createOlapTable(pkId);
}

public void initAndRegisterOlapTable(Id pkId) {
this.store().checkAndRegisterOlapTable(pkId);
}

public void clearOlapPk(Id pkId) {
this.store().clearOlapTable(pkId);
}

public void removeOlapPk(Id pkId) {
this.store().removeOlapTable(pkId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
import com.baidu.hugegraph.type.HugeType;
import com.baidu.hugegraph.type.define.GraphMode;
import com.baidu.hugegraph.type.define.HugeKeys;
import com.baidu.hugegraph.type.define.WriteType;
import com.baidu.hugegraph.type.define.SchemaStatus;
import com.baidu.hugegraph.type.define.WriteType;
import com.baidu.hugegraph.util.DateUtil;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.LockUtil;
Expand Down Expand Up @@ -181,7 +181,7 @@ public void addVertexLabel(VertexLabel vertexLabel) {
@Watched(prefix = "schema")
public VertexLabel getVertexLabel(Id id) {
E.checkArgumentNotNull(id, "Vertex label id can't be null");
if (SchemaElement.OLAP_ID.equals(id)) {
if (VertexLabel.OLAP_VL.id().equals(id)) {
return VertexLabel.OLAP_VL;
}
return this.getSchema(HugeType.VERTEX_LABEL, id);
Expand All @@ -191,7 +191,7 @@ public VertexLabel getVertexLabel(Id id) {
public VertexLabel getVertexLabel(String name) {
E.checkArgumentNotNull(name, "Vertex label name can't be null");
E.checkArgument(!name.isEmpty(), "Vertex label name can't be empty");
if (SchemaElement.OLAP.equals(name)) {
if (VertexLabel.OLAP_VL.name().equals(name)) {
return VertexLabel.OLAP_VL;
}
return this.getSchema(HugeType.VERTEX_LABEL, name);
Expand Down Expand Up @@ -239,8 +239,7 @@ public void addIndexLabel(SchemaLabel schemaLabel, IndexLabel indexLabel) {
* Update index name in base-label(VL/EL)
* TODO: should wrap update base-label and create index in one tx.
*/
if (schemaLabel instanceof VertexLabel &&
schemaLabel.equals(VertexLabel.OLAP_VL)) {
if (schemaLabel.equals(VertexLabel.OLAP_VL)) {
return;
}
schemaLabel.indexLabel(indexLabel.id());
Expand Down Expand Up @@ -288,18 +287,18 @@ public void createIndexLabelForOlapPk(PropertyKey propertyKey) {
return;
}

String indexName = SchemaElement.OLAP + "_by_" + propertyKey.name();
String indexName = VertexLabel.OLAP_VL.name() + "_by_" +
propertyKey.name();
IndexLabel.Builder builder = this.graph().schema()
.indexLabel(indexName)
.onV(SchemaElement.OLAP)
.onV(VertexLabel.OLAP_VL.name())
.by(propertyKey.name());
if (propertyKey.writeType() == WriteType.OLAP_SECONDARY) {
builder.secondary();
} else {
assert propertyKey.writeType() == WriteType.OLAP_RANGE;
builder.range();
}
builder.build();
this.graph().addIndexLabel(VertexLabel.OLAP_VL, builder.build());
}

Expand All @@ -324,27 +323,6 @@ public Id removeOlapPk(PropertyKey propertyKey) {
return asyncRun(this.graph(), propertyKey, callable);
}

public void createOlapPk(Id id) {
this.store().provider().createOlapTable(this.graph(), id);
}

public void initAndRegisterOlapTables() {
for (PropertyKey pk : this.getPropertyKeys()) {
if (pk.olap()) {
this.store().provider().initAndRegisterOlapTable(this.graph(),
pk.id());
}
}
}

public void clearOlapPk(Id id) {
this.store().provider().clearOlapTable(this.graph(), id);
}

public void removeOlapPk(Id id) {
this.store().provider().removeOlapTable(this.graph(), id);
}

@Watched(prefix = "schema")
public void updateSchemaStatus(SchemaElement schema, SchemaStatus status) {
schema.status(status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public String type() {
public Object execute() {
Id olap = this.schemaId();

// Clear olap data table
this.params().graphTransaction().clearOlapPk(olap);

// Clear corresponding index data
clearIndexLabel(this.params(), olap);

// Clear olap table
this.params().schemaTransaction().clearOlapPk(olap);
return null;
}

Expand All @@ -60,9 +60,9 @@ protected static void clearIndexLabel(HugeGraphParams graph, Id id) {
}
LockUtil.Locks locks = new LockUtil.Locks(graph.name());
try {
locks.lockWrites(LockUtil.INDEX_LABEL_CLEAR, olapIndexLabel);
// Set index label to "clearing" status
schemaTx.updateSchemaStatus(indexLabel, SchemaStatus.CLEARING);
locks.lockWrites(LockUtil.INDEX_LABEL_DELETE, olapIndexLabel);
// Set index label to "rebuilding" status
schemaTx.updateSchemaStatus(indexLabel, SchemaStatus.REBUILDING);
try {
// Remove index data
graphTx.removeIndex(indexLabel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ public String type() {
public Object execute() {
SchemaTransaction schemaTx = this.params().schemaTransaction();
PropertyKey propertyKey = schemaTx.getPropertyKey(this.schemaId());
// Create olap index label schema
schemaTx.createIndexLabelForOlapPk(propertyKey);
schemaTx.createOlapPk(this.schemaId());
// Create olap data table
this.params().graphTransaction().createOlapPk(propertyKey.id());
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public String type() {
public Object execute() {
Id olap = this.schemaId();

// Remove olap data table
this.params().graphTransaction().removeOlapPk(olap);

// Remove corresponding index label and index data
Id indexLabel = findOlapIndexLabel(this.params(), olap);
if (indexLabel != null) {
removeIndexLabel(this.params(), indexLabel);
}

// Remove olap table
this.params().schemaTransaction().removeOlapPk(olap);

// Remove olap property key
SchemaTransaction schemaTx = this.params().schemaTransaction();
PropertyKey propertyKey = schemaTx.getPropertyKey(olap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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;

Expand Down Expand Up @@ -65,7 +66,7 @@ protected static void removeIndexLabelFromBaseLabel(SchemaTransaction tx,
Id baseValue = label.baseValue();
SchemaLabel schemaLabel;
if (baseType == HugeType.VERTEX_LABEL) {
if (SchemaElement.OLAP_ID.equals(baseValue)) {
if (VertexLabel.OLAP_VL.id().equals(baseValue)) {
return;
}
schemaLabel = tx.getVertexLabel(baseValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public boolean hasSameContent(IndexLabel other) {
}

public boolean olap() {
return OLAP_ID.equals(this.baseValue);
return VertexLabel.OLAP_VL.id().equals(this.baseValue);
}

public Object validValue(Object value) {
Expand All @@ -154,14 +154,6 @@ public Object validValue(Object value) {
}
}

// ABS of System index id must be below SchemaElement.MAX_PRIMITIVE_SYS_ID
private static final int VL_IL_ID = -1;
private static final int EL_IL_ID = -2;
private static final int PKN_IL_ID = -3;
private static final int VLN_IL_ID = -4;
private static final int ELN_IL_ID = -5;
private static final int ILN_IL_ID = -6;

// Label index
private static final IndexLabel VL_IL = new IndexLabel(VL_IL_ID, "~vli");
private static final IndexLabel EL_IL = new IndexLabel(EL_IL_ID, "~eli");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,19 @@ public abstract class SchemaElement implements Namifiable, Typifiable,
public static final int MAX_PRIMITIVE_SYS_ID = 32;
public static final int NEXT_PRIMITIVE_SYS_ID = 8;

// ABS of system schema id must be below MAX_PRIMITIVE_SYS_ID
protected static final int VL_IL_ID = -1;
protected static final int EL_IL_ID = -2;
protected static final int PKN_IL_ID = -3;
protected static final int VLN_IL_ID = -4;
protected static final int ELN_IL_ID = -5;
protected static final int ILN_IL_ID = -6;
protected static final int OLAP_VL_ID = -7;

public static final Id NONE_ID = IdGenerator.ZERO;

public static final String UNDEF = "~undefined";

// OLAP_ID means all of vertex label ids
public static final Id OLAP_ID = IdGenerator.of(-7);
// OLAP means all of vertex label names
public static final String OLAP = "~olap";

protected final HugeGraph graph;

private final Id id;
Expand Down
Loading

0 comments on commit 9978ad1

Please sign in to comment.