跳到内容

永久性陈旧索引不一致

在某些情况下,由于存储数据库、索引数据库或JanusGraph实例的崩溃,可能会出现永久性陈旧索引。
其中一种情况可能是从图中删除了一个顶点,但由于索引持久化期间的崩溃,索引可能永远不会更新。如果尝试删除一个已从存储数据库中移除但仍被索引的顶点,则会抛出带有消息 Vertex with id %vertexId% was removed.IllegalStateException。例如,使用 g.V().has("name", "HelloWorld").drop().iterate(); g.tx().commit(); 我们可能会收到一个异常,指出某个顶点已被移除,但其记录仍在索引中。每当我们尝试从图中删除此类顶点时,都会抛出异常。
此问题是已知的,并且在JanusGraph中修复此问题之前应视为临时限制。
目前有一个实用工具,可用于修复永久性陈旧索引。

StaleIndexRecordUtil

StaleIndexRecordUtil.classjanusgraph-core 模块中可用,旨在用作修复永久性陈旧索引条目的辅助类。

StaleIndexRecordUtil.forceRemoveVertexFromGraphIndex 可用于强制从图索引中移除任何顶点的索引记录。
下面是使用此方法的一个示例

// Let's say we want to remove non-existent vertex from a stale index. 
// We will assume the next constraints: 
// Vertex id is: `12345`
// Index name is: `nameAgeIndex`
// There are two indexed properties: `name` and `age`
// Value of the name property is: `HelloWorld`
// Value of the age property is: `123`

JanusGraph graph = JanusGraphFactory.open(configuration);

Map<String, Object> indexRecordPropertyValues = new HashMap<>();
indexRecordPropertyValues.put("name", "HelloWorld");
indexRecordPropertyValues.put("age", 123);

// After the below method is executed index entry of the vertex 12345 should be removed from the index which 
// effectively fixes permanent stale index inconsistency
StaleIndexRecordUtil.forceRemoveVertexFromGraphIndex(
    12345L, // vertex id of the index record to be removed
    indexRecordPropertyValues, // index record property values
    graph,
    "nameAgeIndex" // graph index name for which to remove the index record
);
有关此工具用法的更深入信息以及此工具附加方法的解释,请参阅 StaleIndexRecordUtil 的 JavaDoc。