JanusGraph内存版:先提交属性键再创建索引致索引无法启用
JanusGraph分事务创建属性键与索引的解决方案
在JanusGraph 1.0.0中,若分两个独立事务分别创建属性键和复合索引,索引会卡在INSTALLED状态,无法自动推进到ENABLED,导致awaitGraphIndexStatus无限等待。
解决方案
分事务操作时,需在索引创建提交后手动触发索引的**注册(REGISTER)**操作,再等待其启用,具体步骤如下:
- 第一个事务:创建属性键并提交
- 第二个事务:创建索引并提交(此时索引处于
INSTALLED状态) - 第三个事务:执行索引注册操作并提交,触发JanusGraph处理索引启用流程
- 最后等待索引转为
ENABLED状态
修改后的示例代码
@Test public void minimalTest() throws InterruptedException, ExecutionException { // 构建内存后端的JanusGraph实例 JanusGraph graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open(); graph.tx().rollback(); // 事务1:创建属性键 JanusGraphManagement mgmt = graph.openManagement(); String testProp = "testprop"; mgmt.getOrCreatePropertyKey(testProp); mgmt.commit(); // 事务2:创建复合索引 mgmt = graph.openManagement(); String testIndex = "testIndex"; JanusGraphIndex index = mgmt.buildIndex(testIndex, Vertex.class) .addKey(mgmt.getPropertyKey(testProp)) .buildCompositeIndex(); mgmt.commit(); // 事务3:注册索引,触发启用流程 mgmt = graph.openManagement(); index = mgmt.getGraphIndex(testIndex); mgmt.updateIndex(index, SchemaAction.REGISTER_INDEX).get(); mgmt.commit(); // 等待索引转为ENABLED状态 ManagementSystem.awaitGraphIndexStatus(graph, testIndex) .status(SchemaStatus.ENABLED) .call(); System.out.println("索引已成功启用,程序执行到这里"); }
原理说明
JanusGraph的索引生命周期中,INSTALLED是初始创建后的状态。当在同一事务中创建属性键和索引时,系统会自动触发后续的注册与启用流程;但分事务操作时,该自动流程不会触发,必须通过SchemaAction.REGISTER_INDEX手动通知系统处理索引激活,才能让索引状态推进到ENABLED。
内容的提问来源于stack exchange,提问作者NPC
相关产品推荐
相关产品推荐

