Skip to content
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
20 changes: 11 additions & 9 deletions hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import com.baidu.hugegraph.backend.serializer.SerializerFactory;
import com.baidu.hugegraph.backend.store.BackendProviderFactory;
import com.baidu.hugegraph.backend.store.BackendStore;
import com.baidu.hugegraph.backend.store.BackendStoreSystemInfo;
import com.baidu.hugegraph.backend.store.BackendStoreProvider;
import com.baidu.hugegraph.backend.tx.GraphTransaction;
import com.baidu.hugegraph.backend.tx.SchemaTransaction;
Expand Down Expand Up @@ -102,7 +101,6 @@ public class HugeGraph implements GremlinGraph {

private final EventHub schemaEventHub;
private final EventHub indexEventHub;
@SuppressWarnings("UnstableApiUsage")
private final RateLimiter rateLimiter;
private final TaskManager taskManager;

Expand Down Expand Up @@ -184,7 +182,9 @@ public EventHub indexEventHub() {
return this.indexEventHub;
}

@SuppressWarnings("UnstableApiUsage")
/**
* @SuppressWarnings("UnstableApiUsage")
*/
public RateLimiter rateLimiter() {
return this.rateLimiter;
}
Expand All @@ -196,12 +196,14 @@ public void initBackend() {
this.loadGraphStore().open(this.configuration);
try {
this.storeProvider.init();
this.initBackendStoreSystemInfo();
this.storeProvider.initSystemInfo(this);
} finally {
this.loadGraphStore().close();
this.loadSystemStore().close();
this.loadSchemaStore().close();
}

LOG.info("Graph '{}' has been initialized", this.name);
}

@Override
Expand All @@ -218,14 +220,18 @@ public void clearBackend() {
this.loadSystemStore().close();
this.loadSchemaStore().close();
}

LOG.info("Graph '{}' has been cleared", this.name);
}

@Override
public void truncateBackend() {
this.waitUntilAllTasksCompleted();

this.storeProvider.truncate();
this.initBackendStoreSystemInfo();
this.storeProvider.initSystemInfo(this);

LOG.info("Graph '{}' has been truncated", this.name);
}

private void waitUntilAllTasksCompleted() {
Expand All @@ -237,10 +243,6 @@ private void waitUntilAllTasksCompleted() {
}
}

private void initBackendStoreSystemInfo() {
new BackendStoreSystemInfo(this).init();
}

private SchemaTransaction openSchemaTransaction() throws HugeException {
this.checkGraphNotClosed();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.slf4j.Logger;

import com.baidu.hugegraph.HugeGraph;
import com.baidu.hugegraph.backend.BackendException;
import com.baidu.hugegraph.event.EventHub;
import com.baidu.hugegraph.event.EventListener;
Expand Down Expand Up @@ -79,6 +80,7 @@ public String graph() {

@Override
public void open(String graph) {
LOG.debug("Graph '{}' open StoreProvider", this.graph);
E.checkArgument(graph != null, "The graph name can't be null");
E.checkArgument(!graph.isEmpty(), "The graph name can't be empty");

Expand All @@ -90,6 +92,7 @@ public void open(String graph) {

@Override
public void close() throws BackendException {
LOG.debug("Graph '{}' close StoreProvider", this.graph);
this.checkOpened();
this.storeEventHub.notify(Events.STORE_CLOSE, this);
}
Expand All @@ -102,7 +105,7 @@ public void init() {
}
this.notifyAndWaitEvent(Events.STORE_INIT);

LOG.info("Graph '{}' has been initialized", this.graph);
LOG.debug("Graph '{}' store has been initialized", this.graph);
}

@Override
Expand All @@ -113,7 +116,7 @@ public void clear() throws BackendException {
}
this.notifyAndWaitEvent(Events.STORE_CLEAR);

LOG.info("Graph '{}' has been cleared", this.graph);
LOG.debug("Graph '{}' store has been cleared", this.graph);
}

@Override
Expand All @@ -124,7 +127,17 @@ public void truncate() {
}
this.notifyAndWaitEvent(Events.STORE_TRUNCATE);

LOG.info("Graph '{}' has been truncated", this.graph);
LOG.debug("Graph '{}' store has been truncated", this.graph);
}

@Override
public void initSystemInfo(HugeGraph graph) {
this.checkOpened();
BackendStoreSystemInfo info = new BackendStoreSystemInfo(graph);
info.init();
this.notifyAndWaitEvent(Events.STORE_INITED);

LOG.debug("Graph '{}' system info has been initialized", this.graph);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package com.baidu.hugegraph.backend.store;

import com.baidu.hugegraph.HugeGraph;
import com.baidu.hugegraph.event.EventListener;

public interface BackendStoreProvider {
Expand Down Expand Up @@ -48,6 +49,8 @@ public interface BackendStoreProvider {

public void truncate();

public void initSystemInfo(HugeGraph graph);

public void listen(EventListener listener);

public void unlisten(EventListener listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,18 @@ public BackendStoreSystemInfo(HugeGraph graph) {

public void init() {
SchemaTransaction schema = this.graph.schemaTransaction();

// Set schema counter to reserve primitive system id
schema.setNextIdLowest(HugeType.SYS_SCHEMA,
SchemaElement.MAX_PRIMITIVE_SYS_ID);

// Use property key to store backend version
String backendVersion = this.graph.backendVersion();
PropertyKey backendInfo = this.graph.schema()
.propertyKey(PK_BACKEND_INFO)
.userdata("version", backendVersion)
.build();
schema.addPropertyKey(backendInfo);

// Set schema counter to reserve primitive system id
schema.setNextIdLowest(HugeType.SYS_SCHEMA,
SchemaElement.MAX_PRIMITIVE_SYS_ID);
}

private Map<String, Object> info() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package com.baidu.hugegraph.backend.tx;

import java.util.Collections;
import java.util.Iterator;

import com.baidu.hugegraph.HugeGraph;
Expand All @@ -34,7 +35,6 @@
import com.baidu.hugegraph.type.HugeType;
import com.baidu.hugegraph.type.define.HugeKeys;
import com.baidu.hugegraph.util.E;
import com.google.common.collect.ImmutableList;

public class SchemaIndexTransaction extends AbstractTransaction {

Expand Down Expand Up @@ -101,7 +101,7 @@ private Iterator<BackendEntry> queryByName(ConditionQuery query) {
idQuery.query(index.elementIds());
}
if (idQuery.ids().isEmpty()) {
return ImmutableList.<BackendEntry>of().iterator();
return Collections.emptyIterator();
}
assert idQuery.ids().size() == 1 : idQuery.ids();
return super.query(idQuery);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@ public static IndexLabel label(HugeType type) {

public static IndexLabel label(HugeGraph graph, Id id) {
// Primitive IndexLabel first
if (id.asLong() < 0 &&
id.asLong() > -SchemaElement.NEXT_PRIMITIVE_SYS_ID) {
if (id.asLong() < 0 && id.asLong() > -NEXT_PRIMITIVE_SYS_ID) {
switch ((int) id.asLong()) {
case VL_IL_ID:
return VL_IL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,12 @@ private TaskTransaction tx() {
}

private EventListener listenChanges() {
// Listen store event: "store.init", "store.truncate"
Set<String> storeEvents = ImmutableSet.of(Events.STORE_INIT,
Events.STORE_TRUNCATE);
// Listen store event: "store.inited"
Set<String> storeEvents = ImmutableSet.of(Events.STORE_INITED);
EventListener eventListener = event -> {
/*
* Ensure schema cache has been cleared before calling initSchema()
* otherwise we would see schema from cache even if store truncated
*/
// Ensure task schema create after system info initialized
if (storeEvents.contains(event.name())) {
this.call(() -> this.tx().initSchema());
// Ensure we receive notify after the cache-schema (trick)
this.relistenChanges();
return true;
}
return false;
Expand All @@ -142,11 +136,6 @@ private void unlistenChanges() {
this.graph.loadSystemStore().provider().unlisten(this.eventListener);
}

private void relistenChanges() {
this.graph.loadSystemStore().provider().unlisten(this.eventListener);
this.graph.loadSystemStore().provider().listen(this.eventListener);
}

public <V> void restoreTasks() {
// Restore 'RESTORING', 'RUNNING' and 'QUEUED' tasks in order.
for (TaskStatus status : TaskStatus.PENDING_STATUSES) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ public final class Events {
public static final String STORE_INIT = "store.init";
public static final String STORE_CLEAR = "store.clear";
public static final String STORE_TRUNCATE = "store.truncate";
public static final String STORE_INITED = "store.inited";
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ private static void initGraph(String config) throws InterruptedException {
LOG.info("Init graph with config file: {}", config);
HugeGraph graph = HugeFactory.open(config);

BackendStoreSystemInfo backendStoreInfo = new BackendStoreSystemInfo(graph);
BackendStoreSystemInfo sysInfo = new BackendStoreSystemInfo(graph);
try {
if (backendStoreInfo.exist()) {
if (sysInfo.exist()) {
LOG.info("Skip init-store due to the backend store of '{}' " +
"had been initialized", graph.name());
backendStoreInfo.checkVersion();
sysInfo.checkVersion();
} else {
initBackend(graph);
}
Expand Down