Skip to content
This repository was archived by the owner on Jul 10, 2024. It is now read-only.

Commit 70198cd

Browse files
committed
SUBMARINE-1168. Add database initialization metadata with flyway
1 parent efb4e7c commit 70198cd

7 files changed

Lines changed: 611 additions & 0 deletions

File tree

submarine-commons/commons-utils/src/main/java/org/apache/submarine/commons/utils/SubmarineConfVars.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ public enum ConfVars {
5151
CLUSTER_HEARTBEAT_INTERVAL("cluster.heartbeat.interval", 3000),
5252
CLUSTER_HEARTBEAT_TIMEOUT("cluster.heartbeat.timeout", 9000),
5353

54+
SUBMARINE_METADATA_INIT("submarine.metadata.init", false),
55+
SUBMARINE_METADATA_VALIDATE("submarine.metadata.validate", true),
56+
SUBMARINE_METADATA_VERSION("submarine.metadata.version", "0.7.0"),
57+
SUBMARINE_METADATA_LOCATION("submarine.metadata.location", "classpath:db/migration"),
58+
5459
JDBC_DRIVERCLASSNAME("jdbc.driverClassName", "com.mysql.jdbc.Driver"),
5560
JDBC_URL("jdbc.url", "jdbc:mysql://127.0.0.1:3306/submarine" +
5661
"?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&allowMultiQueries=true&" +

submarine-server/server-core/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,20 @@
438438
</exclusion>
439439
</exclusions>
440440
</dependency>
441+
442+
<dependency>
443+
<groupId>org.flywaydb</groupId>
444+
<artifactId>flyway-core</artifactId>
445+
<version>7.15.0</version>
446+
</dependency>
447+
448+
<dependency>
449+
<groupId>com.h2database</groupId>
450+
<artifactId>h2</artifactId>
451+
<version>1.4.194</version>
452+
<scope>test</scope>
453+
</dependency>
454+
441455
</dependencies>
442456

443457
<build>

submarine-server/server-core/src/main/java/org/apache/submarine/server/SubmarineServer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.submarine.server;
2020

2121
import org.apache.log4j.PropertyConfigurator;
22+
import org.apache.submarine.server.database.initialization.DatabaseMetadataService;
2223
import org.apache.submarine.server.database.utils.HibernateUtil;
2324
import org.apache.submarine.server.rest.provider.YamlEntityProvider;
2425
import org.apache.submarine.server.workbench.websocket.NotebookServer;
@@ -118,6 +119,9 @@ protected void configure() {
118119
// Cluster Server is useless for submarine now. Shield it to improve performance.
119120
// setupClusterServer();
120121

122+
// init database metadata
123+
new DatabaseMetadataService().initDatabaseMetadata();
124+
121125
startServer();
122126
}
123127

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.submarine.server.database.initialization;
19+
20+
import org.apache.submarine.commons.utils.SubmarineConfVars;
21+
import org.apache.submarine.commons.utils.SubmarineConfiguration;
22+
import org.flywaydb.core.Flyway;
23+
import org.flywaydb.core.api.configuration.FluentConfiguration;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
26+
27+
public class DatabaseMetadataService {
28+
29+
private static final Logger LOG = LoggerFactory.getLogger(DatabaseMetadataService.class);
30+
31+
/**
32+
* init database metadata by flyway
33+
*/
34+
public void initDatabaseMetadata() {
35+
SubmarineConfiguration conf = SubmarineConfiguration.getInstance();
36+
if (!conf.getBoolean(SubmarineConfVars.ConfVars.SUBMARINE_METADATA_INIT)) {
37+
LOG.info("Skip submarine metadata initialization. If you want to init database metadata, " +
38+
"you can set `submarine.metadata.init` or SUBMARINE_METADATA_INIT env to true.");
39+
return;
40+
}
41+
42+
// get database connection conf
43+
String jdbcUrl = conf.getJdbcUrl();
44+
String jdbcUserName = conf.getJdbcUserName();
45+
String jdbcPassword = conf.getJdbcPassword();
46+
47+
// flyway config
48+
FluentConfiguration fluentConfiguration = new FluentConfiguration()
49+
.dataSource(jdbcUrl, jdbcUserName, jdbcPassword);
50+
51+
// sql files location
52+
fluentConfiguration.locations(conf.getString(SubmarineConfVars.ConfVars.SUBMARINE_METADATA_LOCATION));
53+
// schema metadata history table, control schema version. version id is default release version
54+
fluentConfiguration.table("submarine_schema_history");
55+
// if schema metadata history table is missed,try to create it
56+
fluentConfiguration.baselineOnMigrate(true);
57+
// Whether to automatically call validate or not when running migrate.
58+
// If encounter script tuning, we can set this value to false
59+
fluentConfiguration.validateOnMigrate(
60+
conf.getBoolean(SubmarineConfVars.ConfVars.SUBMARINE_METADATA_VALIDATE));
61+
// basic version, we may start it from 0.7.0
62+
// We can replace this value if we want to update from an intermediate version in some cases
63+
fluentConfiguration.baselineVersion(
64+
conf.getString(SubmarineConfVars.ConfVars.SUBMARINE_METADATA_VERSION));
65+
66+
Flyway flyway = fluentConfiguration.load();
67+
try {
68+
flyway.migrate();
69+
} catch (Exception e) {
70+
LOG.warn("Error during database initialization. You may need to manually initialize " +
71+
"the actual metadata and data.", e);
72+
}
73+
}
74+
75+
}

0 commit comments

Comments
 (0)