Skip to content

Commit 6f679f4

Browse files
committed
issues/1588: Incremental Janusgraph schema updates
1 parent 5839594 commit 6f679f4

File tree

16 files changed

+1284
-589
lines changed

16 files changed

+1284
-589
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ This application consists of the following modules:
99
* `strongbox-db-import` which creates a `zip` file with packed built database snapshot built from the liquibase changesets
1010

1111
## How is this project used in [strongbox](https://github.com/strongbox/strongbox) ?
12-
[strongbox](https://github.com/strongbox/strongbox) uses this project submodules as required dependencies. During the startup of [Strongbox](https://github.com/strongbox/strongbox), the application detects if an JanusGraph database already exists. If not, then the application uses an extracted empty JanusGraph database snapshot from the `strongbox-db-import` artifact. If the JanusGraph database already exists, then the application applies all missing changesets from the `strongbox-db-schema` artifact.
12+
[strongbox](https://github.com/strongbox/strongbox) uses this project submodules as required dependencies. During the startup of [Strongbox](https://github.com/strongbox/strongbox), the application detects if a JanusGraph database already exists. If one doesn't exist, it uses an extracted empty JanusGraph database snapshot from the `strongbox-db-import` artifact; if a database exists, then the application applies all missing changesets from the `strongbox-db-schema` artifact.
1313

1414
## How to build this project ?
1515
To build the code, simply execute:
1616
`mvn clean install`
1717

1818
## What's the result of the build process ?
1919
This project produces the following artifacts:
20-
* `strongbox-db-shcema-${version}.jar` : Located in `strongbox-db-schema/target`, which contains current database changesets
20+
* `strongbox-db-schema-${version}.jar` : Located in `strongbox-db-schema/target`, which contains current database changesets
2121
* `strongbox-db-import-${version}.zip` : Located in `strongbox-db-import/target`, which contains zipped fresh database snapshot built from the above changesets

strongbox-db-import/src/main/java/org/carlspring/strongbox/dbimport/EmbeddedDbServerConfiguration.java

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package org.carlspring.strongbox.dbimport;
22

33
import java.io.IOException;
4-
import java.io.InputStream;
5-
import java.nio.file.Files;
64
import java.nio.file.Path;
75
import java.nio.file.Paths;
8-
import java.nio.file.StandardCopyOption;
96

107
import org.carlspring.strongbox.db.schema.StrongboxSchema;
118
import org.janusgraph.core.JanusGraph;
@@ -19,6 +16,7 @@
1916
import org.strongbox.db.server.JanusGraphProperties;
2017
import org.strongbox.db.server.JanusGraphServer;
2118
import org.strongbox.db.server.JanusGraphWithEmbeddedCassandra;
19+
import org.strongbox.util.ConfigurationUtils;
2220

2321
/**
2422
* @author Przemyslaw Fusik
@@ -37,20 +35,6 @@ JanusGraphServer embeddedDbServer(CassandraEmbeddedConfiguration cassandraConfig
3735
return new JanusGraphWithEmbeddedCassandra(cassandraConfiguration, janusGraphConfiguration);
3836
}
3937

40-
private void extractConfigurationFile(String rootFolder,
41-
String configFileName)
42-
throws IOException
43-
{
44-
try (InputStream in = JanusGraphWithEmbeddedCassandra.class.getResourceAsStream(String.format("/etc/conf/%s", configFileName)))
45-
{
46-
Path configRoot = Paths.get(rootFolder).resolve("etc").resolve("conf");
47-
Files.createDirectories(configRoot);
48-
Files.copy(in,
49-
configRoot.resolve(configFileName),
50-
StandardCopyOption.REPLACE_EXISTING);
51-
}
52-
}
53-
5438
@Bean
5539
JanusGraph janusGraph(JanusGraphServer server)
5640
throws Exception
@@ -62,7 +46,7 @@ JanusGraph janusGraph(JanusGraphServer server)
6246
JanusGraphProperties dbImportJanusGraphProperties(@Value("${strongbox.dbimport.root}") String dbImportRoot)
6347
throws IOException
6448
{
65-
extractConfigurationFile(dbImportRoot, "janusgraph-cassandra.properties");
49+
ConfigurationUtils.extractConfigurationFile(dbImportRoot, "janusgraph-cassandra.properties");
6650
Path configFilePath = Paths.get(dbImportRoot).resolve("etc").resolve("conf").resolve("janusgraph-cassandra.properties");
6751

6852
return new JanusGraphProperties( String.format("file:%s", configFilePath.toAbsolutePath().toString()));
@@ -72,7 +56,7 @@ JanusGraphProperties dbImportJanusGraphProperties(@Value("${strongbox.dbimport.r
7256
CassandraEmbeddedProperties dbImportCassandraEmbeddedProperties(@Value("${strongbox.dbimport.root}") String dbImportRoot)
7357
throws IOException
7458
{
75-
extractConfigurationFile(dbImportRoot, "cassandra.yaml");
59+
ConfigurationUtils.extractConfigurationFile(dbImportRoot, "cassandra.yaml");
7660
Path rootPath = Paths.get(dbImportRoot).toAbsolutePath();
7761
Path configFilePath = rootPath.resolve("etc").resolve("conf").resolve("cassandra.yaml");
7862
Path storageRootPath = rootPath.resolve("db");

strongbox-db-schema/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
<inceptionYear>2019</inceptionYear>
1818

1919
<dependencies>
20+
<dependency>
21+
<groupId>${project.groupId}</groupId>
22+
<artifactId>strongbox-db-server</artifactId>
23+
<version>${project.version}</version>
24+
</dependency>
2025
<dependency>
2126
<groupId>org.janusgraph</groupId>
2227
<artifactId>janusgraph-core</artifactId>
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package org.carlspring.strongbox.db.schema;
2+
3+
import java.util.Map;
4+
import java.util.Map.Entry;
5+
import java.util.Set;
6+
7+
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
8+
import org.carlspring.strongbox.db.schema.migration.Changeset;
9+
import org.janusgraph.core.JanusGraph;
10+
import org.janusgraph.core.schema.JanusGraphManagement;
11+
import org.janusgraph.graphdb.database.management.ManagementSystem;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
15+
/**
16+
* @author sbespalov
17+
*/
18+
public class SchemaTemplate
19+
{
20+
private static final Logger logger = LoggerFactory.getLogger(SchemaTemplate.class);
21+
22+
private final JanusGraph jg;
23+
24+
public SchemaTemplate(JanusGraph jg)
25+
{
26+
this.jg = jg;
27+
}
28+
29+
public void applyChangeset(Changeset changeset)
30+
{
31+
logger.info(String.format("Apply changeset [%s]-[%s].", changeset.getVersion(), changeset.getName()));
32+
33+
JanusGraphManagement jgm = jg.openManagement();
34+
Set<String> compositeIndexes;
35+
Map<String, String> relationIndexes;
36+
try
37+
{
38+
changeset.applySchemaChanges(jgm);
39+
40+
logger.info(String.format("Create indexes [%s]-[%s].", changeset.getVersion(), changeset.getName()));
41+
compositeIndexes = changeset.createVertexIndexes(jgm);
42+
relationIndexes = changeset.createRelationIndexes(jgm);
43+
44+
jgm.commit();
45+
}
46+
catch (Exception e)
47+
{
48+
jgm.rollback();
49+
throw new RuntimeException(String.format("Failed to apply changeset [%s]-[%s].",
50+
changeset.getVersion(),
51+
changeset.getName()),
52+
e);
53+
}
54+
55+
GraphTraversalSource g = jg.traversal();
56+
try
57+
{
58+
changeset.applyGraphChanges(g);
59+
g.tx().commit();
60+
}
61+
catch (Exception e)
62+
{
63+
g.tx().rollback();
64+
throw new RuntimeException(String.format("Failed to apply changeset [%s]-[%s].",
65+
changeset.getVersion(),
66+
changeset.getName()),
67+
e);
68+
}
69+
70+
// Waiting index status
71+
try
72+
{
73+
for (String janusGraphIndex : compositeIndexes)
74+
{
75+
logger.info(String.format("Wait index [%s] to be registered.", janusGraphIndex));
76+
ManagementSystem.awaitGraphIndexStatus(jg, janusGraphIndex).call();
77+
}
78+
79+
for (Entry<String, String> relationIndex : relationIndexes.entrySet())
80+
{
81+
logger.info(String.format("Wait index [%s] to be registered.", relationIndex.getKey()));
82+
ManagementSystem.awaitRelationIndexStatus(jg, relationIndex.getKey(), relationIndex.getValue()).call();
83+
}
84+
}
85+
catch (InterruptedException e)
86+
{
87+
Thread.currentThread().interrupt();
88+
throw new RuntimeException(String.format("Failed to enable indexes for [%s]-[%s].",
89+
changeset.getVersion(),
90+
changeset.getName()),
91+
e);
92+
}
93+
}
94+
95+
}

0 commit comments

Comments
 (0)