From 34b8faa25e30db052400c54975d626fc1632df09 Mon Sep 17 00:00:00 2001 From: Zhangmei Li Date: Thu, 3 Jan 2019 20:14:01 +0800 Subject: [PATCH] fix bug: primitive system id reservation not work fix #314 Change-Id: Ibced7753ab75a4921e951775ecca3b974c5b8f12 --- .../java/com/baidu/hugegraph/HugeGraph.java | 20 ++++++++++--------- .../store/AbstractBackendStoreProvider.java | 19 +++++++++++++++--- .../backend/store/BackendStoreProvider.java | 3 +++ .../backend/store/BackendStoreSystemInfo.java | 9 +++++---- .../backend/tx/SchemaIndexTransaction.java | 4 ++-- .../baidu/hugegraph/schema/IndexLabel.java | 3 +-- .../baidu/hugegraph/task/TaskScheduler.java | 17 +++------------- .../java/com/baidu/hugegraph/util/Events.java | 1 + .../com/baidu/hugegraph/cmd/InitStore.java | 6 +++--- 9 files changed, 45 insertions(+), 37 deletions(-) diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java index c225431b2b..25640bbf9b 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java @@ -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; @@ -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; @@ -184,7 +182,9 @@ public EventHub indexEventHub() { return this.indexEventHub; } - @SuppressWarnings("UnstableApiUsage") + /** + * @SuppressWarnings("UnstableApiUsage") + */ public RateLimiter rateLimiter() { return this.rateLimiter; } @@ -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 @@ -218,6 +220,8 @@ public void clearBackend() { this.loadSystemStore().close(); this.loadSchemaStore().close(); } + + LOG.info("Graph '{}' has been cleared", this.name); } @Override @@ -225,7 +229,9 @@ 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() { @@ -237,10 +243,6 @@ private void waitUntilAllTasksCompleted() { } } - private void initBackendStoreSystemInfo() { - new BackendStoreSystemInfo(this).init(); - } - private SchemaTransaction openSchemaTransaction() throws HugeException { this.checkGraphNotClosed(); try { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/AbstractBackendStoreProvider.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/AbstractBackendStoreProvider.java index ab4dc82ea9..ede53a9fa8 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/AbstractBackendStoreProvider.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/AbstractBackendStoreProvider.java @@ -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; @@ -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"); @@ -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); } @@ -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 @@ -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 @@ -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 diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/BackendStoreProvider.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/BackendStoreProvider.java index c1b6cb1a1a..3ec888fcd1 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/BackendStoreProvider.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/BackendStoreProvider.java @@ -19,6 +19,7 @@ package com.baidu.hugegraph.backend.store; +import com.baidu.hugegraph.HugeGraph; import com.baidu.hugegraph.event.EventListener; public interface BackendStoreProvider { @@ -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); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/BackendStoreSystemInfo.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/BackendStoreSystemInfo.java index 816e28315a..0b409a73fe 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/BackendStoreSystemInfo.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/BackendStoreSystemInfo.java @@ -47,6 +47,11 @@ 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() @@ -54,10 +59,6 @@ public void init() { .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 info() { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/SchemaIndexTransaction.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/SchemaIndexTransaction.java index 75c81bf8c1..ab39556f54 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/SchemaIndexTransaction.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/SchemaIndexTransaction.java @@ -19,6 +19,7 @@ package com.baidu.hugegraph.backend.tx; +import java.util.Collections; import java.util.Iterator; import com.baidu.hugegraph.HugeGraph; @@ -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 { @@ -101,7 +101,7 @@ private Iterator queryByName(ConditionQuery query) { idQuery.query(index.elementIds()); } if (idQuery.ids().isEmpty()) { - return ImmutableList.of().iterator(); + return Collections.emptyIterator(); } assert idQuery.ids().size() == 1 : idQuery.ids(); return super.query(idQuery); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/IndexLabel.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/IndexLabel.java index cc30819870..965862648c 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/IndexLabel.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/IndexLabel.java @@ -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; diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/task/TaskScheduler.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/task/TaskScheduler.java index 6110e381ce..750b1161f5 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/task/TaskScheduler.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/task/TaskScheduler.java @@ -118,18 +118,12 @@ private TaskTransaction tx() { } private EventListener listenChanges() { - // Listen store event: "store.init", "store.truncate" - Set storeEvents = ImmutableSet.of(Events.STORE_INIT, - Events.STORE_TRUNCATE); + // Listen store event: "store.inited" + Set 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; @@ -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 void restoreTasks() { // Restore 'RESTORING', 'RUNNING' and 'QUEUED' tasks in order. for (TaskStatus status : TaskStatus.PENDING_STATUSES) { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/util/Events.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/util/Events.java index 0e594e5fef..70bbd0469c 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/util/Events.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/util/Events.java @@ -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"; } diff --git a/hugegraph-dist/src/main/java/com/baidu/hugegraph/cmd/InitStore.java b/hugegraph-dist/src/main/java/com/baidu/hugegraph/cmd/InitStore.java index 23da927b15..022822a18c 100644 --- a/hugegraph-dist/src/main/java/com/baidu/hugegraph/cmd/InitStore.java +++ b/hugegraph-dist/src/main/java/com/baidu/hugegraph/cmd/InitStore.java @@ -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); }