Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions hadoop-hdds/common/src/main/resources/ozone-default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2354,7 +2354,7 @@
</property>
<property>
<name>ozone.recon.sql.db.driver</name>
<value>org.sqlite.JDBC</value>
<value>org.apache.derby.jdbc.EmbeddedDriver</value>
<tag>OZONE, RECON</tag>
<description>
Database driver class name available on the
Expand All @@ -2363,7 +2363,7 @@
</property>
<property>
<name>ozone.recon.sql.db.jdbc.url</name>
<value>jdbc:sqlite:${ozone.recon.db.dir}/ozone_recon_sqlite.db</value>
<value>jdbc:derby:${ozone.recon.db.dir}/ozone_recon_derby.db</value>
<tag>OZONE, RECON</tag>
<description>
Ozone Recon SQL database jdbc url.
Expand Down
6 changes: 3 additions & 3 deletions hadoop-ozone/recon-codegen/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
<artifactId>hadoop-ozone-common</artifactId>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.25.2</version>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.14.2.0</version>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@
*/
package org.hadoop.ozone.recon.codegen;

import static org.hadoop.ozone.recon.codegen.SqlDbUtils.DERBY_DRIVER_CLASS;
import static org.hadoop.ozone.recon.codegen.SqlDbUtils.createNewDerbyDatabase;

import java.io.File;
import java.nio.file.Paths;
import java.sql.SQLException;
import java.util.Set;

import javax.sql.DataSource;

import org.apache.commons.io.FileUtils;
import org.apache.derby.jdbc.EmbeddedDataSource;
import org.apache.hadoop.util.Time;
import org.hadoop.ozone.recon.schema.ReconSchemaDefinition;
import org.jooq.codegen.GenerationTool;
import org.jooq.meta.jaxb.Configuration;
Expand All @@ -35,7 +41,6 @@
import org.jooq.meta.jaxb.Target;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sqlite.SQLiteDataSource;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
Expand All @@ -55,10 +60,11 @@ public class JooqCodeGenerator {
private static final Logger LOG =
LoggerFactory.getLogger(JooqCodeGenerator.class);

private static final String SQLITE_DB =
System.getProperty("java.io.tmpdir") + "/recon-generated-schema";
private static final String JDBC_URL = "jdbc:sqlite:" + SQLITE_DB;

private static final String DB = Paths.get(
System.getProperty("java.io.tmpdir"),
"recon-generated-schema-" + Time.monotonicNow()).toString();
Comment thread
avijayanhwx marked this conversation as resolved.
public static final String RECON_SCHEMA_NAME = "RECON";
Comment thread
avijayanhwx marked this conversation as resolved.
private static final String JDBC_URL = "jdbc:derby:" + DB;
private final Set<ReconSchemaDefinition> allDefinitions;

@Inject
Expand All @@ -82,20 +88,18 @@ private void generateSourceCode(String outputDir) throws Exception {
Configuration configuration =
new Configuration()
.withJdbc(new Jdbc()
.withDriver("org.sqlite.JDBC")
.withDriver(DERBY_DRIVER_CLASS)
.withUrl(JDBC_URL)
.withUser("sa")
.withPassword("sa"))
.withSchema(RECON_SCHEMA_NAME))
.withGenerator(new Generator()
.withDatabase(new Database()
.withName("org.jooq.meta.sqlite.SQLiteDatabase")
.withName("org.jooq.meta.derby.DerbyDatabase")
.withOutputSchemaToDefault(true)
.withIncludeTables(true)
.withIncludePrimaryKeys(true))
.withGenerate(new Generate()
.withDaos(true)
.withEmptyCatalogs(true)
.withEmptySchemas(true))
.withEmptyCatalogs(true))
.withStrategy(new Strategy().withName(
"org.hadoop.ozone.recon.codegen.TableNamingStrategy"))
.withTarget(new Target()
Expand All @@ -109,20 +113,25 @@ private void generateSourceCode(String outputDir) throws Exception {
* Provider for embedded datasource.
*/
static class LocalDataSourceProvider implements Provider<DataSource> {
private static SQLiteDataSource db;

private static EmbeddedDataSource dataSource;
static {
db = new SQLiteDataSource();
db.setUrl(JDBC_URL);
try {
createNewDerbyDatabase(JDBC_URL, RECON_SCHEMA_NAME);
} catch (Exception e) {
LOG.error("Error creating Recon Derby DB.", e);
}
dataSource = new EmbeddedDataSource();
dataSource.setDatabaseName(DB);
dataSource.setUser(RECON_SCHEMA_NAME);
Comment thread
avijayanhwx marked this conversation as resolved.
}

@Override
public DataSource get() {
return db;
return dataSource;
}

static void cleanup() {
FileUtils.deleteQuietly(new File(SQLITE_DB));
FileUtils.deleteQuietly(new File(DB));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.hadoop.ozone.recon.codegen;

import org.apache.hadoop.hdds.conf.Config;
import org.apache.hadoop.hdds.conf.ConfigGroup;
import org.apache.hadoop.hdds.conf.ConfigTag;
import org.apache.hadoop.hdds.conf.ConfigType;

/**
* The configuration class for the Recon SQL DB.
*/
@ConfigGroup(prefix = "ozone.recon.sql.db")
public class ReconSqlDbConfig {
Comment thread
avijayanhwx marked this conversation as resolved.

@Config(key = "jooq.dialect",
type = ConfigType.STRING,
defaultValue = "",
tags = { ConfigTag.STORAGE, ConfigTag.RECON, ConfigTag.OZONE },
description = "Recon internally uses Jooq to talk to its SQL DB. By " +
"default, we support Derby and Sqlite out of the box. Please refer " +
"to https://www.jooq.org/javadoc/latest/org" +
".jooq/org/jooq/SQLDialect.html to specify different dialect."
)
private String sqlDbDialect;

public String getSqlDbDialect() {
return sqlDbDialect;
}

public void setSqlDbDialect(String sqlDbDialect) {
this.sqlDbDialect = sqlDbDialect;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.hadoop.ozone.recon.codegen;

import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Constants and Helper functions for Recon SQL related stuff.
*/
public final class SqlDbUtils {

public final static String DERBY_DRIVER_CLASS =
"org.apache.derby.jdbc.EmbeddedDriver";
public final static String SQLITE_DRIVER_CLASS = "org.sqlite.JDBC";
public final static String DERBY_DISABLE_LOG_METHOD =
SqlDbUtils.class.getName() + ".disableDerbyLogFile";

private static final Logger LOG =
LoggerFactory.getLogger(SqlDbUtils.class);

private SqlDbUtils() {
}

/**
* Create new Derby Database with URL and schema name.
* @param jdbcUrl JDBC url.
* @param schemaName Schema name
* @throws ClassNotFoundException on not finding driver class.
* @throws SQLException on SQL exception.
*/
public static void createNewDerbyDatabase(String jdbcUrl, String schemaName)
throws ClassNotFoundException, SQLException {
System.setProperty("derby.stream.error.method",
DERBY_DISABLE_LOG_METHOD);
Class.forName(DERBY_DRIVER_CLASS);
try(Connection connection = DriverManager.getConnection(jdbcUrl
+ ";user=" + schemaName
+ ";create=true")) {
LOG.info("Created derby database at {}.", jdbcUrl);
}
}

/**
* Used to suppress embedded derby database logging.
* @return No-Op output stream.
*/
public static OutputStream disableDerbyLogFile(){
return new OutputStream() {
public void write(int b) throws IOException {
// Ignore all log messages
}
};
}
}
5 changes: 5 additions & 0 deletions hadoop-ozone/recon/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@
<artifactId>bonecp</artifactId>
<version>0.8.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.14.2.0</version>
</dependency>
<dependency>
Comment thread
elek marked this conversation as resolved.
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
import static org.apache.hadoop.ozone.recon.ReconServerConfigKeys.OZONE_RECON_SQL_MAX_CONNECTION_AGE;
import static org.apache.hadoop.ozone.recon.ReconServerConfigKeys.OZONE_RECON_SQL_MAX_IDLE_CONNECTION_AGE;
import static org.apache.hadoop.ozone.recon.ReconServerConfigKeys.OZONE_RECON_SQL_MAX_IDLE_CONNECTION_TEST_STMT;
import static org.hadoop.ozone.recon.codegen.SqlDbUtils.DERBY_DRIVER_CLASS;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
import org.apache.hadoop.hdds.scm.protocol.StorageContainerLocationProtocol;
import org.apache.hadoop.hdds.scm.server.OzoneStorageContainerManager;
Expand Down Expand Up @@ -62,6 +64,7 @@
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.hdds.utils.db.DBStore;
import org.apache.ratis.protocol.ClientId;
import org.hadoop.ozone.recon.codegen.ReconSqlDbConfig;
import org.hadoop.ozone.recon.schema.tables.daos.ClusterGrowthDailyDao;
import org.hadoop.ozone.recon.schema.tables.daos.ContainerHistoryDao;
import org.hadoop.ozone.recon.schema.tables.daos.FileCountBySizeDao;
Expand Down Expand Up @@ -191,7 +194,7 @@ DataSourceConfiguration getDataSourceConfiguration(
@Override
public String getDriverClass() {
return ozoneConfiguration.get(OZONE_RECON_SQL_DB_DRIVER,
"org.sqlite.JDBC");
DERBY_DRIVER_CLASS);
}

@Override
Expand Down Expand Up @@ -223,7 +226,11 @@ public long getConnectionTimeout() {

@Override
public String getSqlDialect() {
return JooqPersistenceModule.DEFAULT_DIALECT.toString();
ReconSqlDbConfig sqlDbConfig =
ozoneConfiguration.getObject(ReconSqlDbConfig.class);
return StringUtils.isNotEmpty(sqlDbConfig.getSqlDbDialect()) ?
sqlDbConfig.getSqlDbDialect():
JooqPersistenceModule.DEFAULT_DIALECT.toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
*/
package org.apache.hadoop.ozone.recon.persistence;

import static org.hadoop.ozone.recon.codegen.JooqCodeGenerator.RECON_SCHEMA_NAME;
import static org.hadoop.ozone.recon.codegen.SqlDbUtils.createNewDerbyDatabase;

import javax.sql.DataSource;

import org.apache.commons.lang3.StringUtils;
import org.apache.derby.jdbc.EmbeddedDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sqlite.SQLiteDataSource;

import com.google.inject.Inject;
Expand All @@ -31,6 +37,9 @@
*/
public class DefaultDataSourceProvider implements Provider<DataSource> {

private static final Logger LOG =
LoggerFactory.getLogger(DefaultDataSourceProvider.class);

@Inject
private DataSourceConfiguration configuration;

Expand All @@ -43,14 +52,26 @@ public class DefaultDataSourceProvider implements Provider<DataSource> {
*/
@Override
public DataSource get() {
Comment thread
avijayanhwx marked this conversation as resolved.
if (StringUtils.contains(configuration.getJdbcUrl(), "sqlite")) {
String jdbcUrl = configuration.getJdbcUrl();
LOG.info("JDBC Url for Recon : {} ", jdbcUrl);
if (StringUtils.contains(jdbcUrl, "derby")) {
EmbeddedDataSource dataSource = null;
try {
createNewDerbyDatabase(jdbcUrl, RECON_SCHEMA_NAME);
} catch (Exception e) {
LOG.error("Error creating Recon Derby DB.", e);
}
dataSource = new EmbeddedDataSource();
dataSource.setDatabaseName(jdbcUrl.split(":")[2]);
dataSource.setUser(RECON_SCHEMA_NAME);
return dataSource;
} else if (StringUtils.contains(jdbcUrl, "sqlite")) {
SQLiteDataSource ds = new SQLiteDataSource();
ds.setUrl(configuration.getJdbcUrl());
return ds;
}

BoneCPDataSource cpDataSource = new BoneCPDataSource();

cpDataSource.setDriverClass(configuration.getDriverClass());
cpDataSource.setJdbcUrl(configuration.getJdbcUrl());
cpDataSource.setUsername(configuration.getUserName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
public class JooqPersistenceModule extends AbstractModule {

private Provider<DataSourceConfiguration> configurationProvider;
public static final SQLDialect DEFAULT_DIALECT = SQLDialect.SQLITE;
public static final SQLDialect DEFAULT_DIALECT = SQLDialect.DERBY;

public JooqPersistenceModule(
Provider<DataSourceConfiguration> configurationProvider) {
Expand Down
Loading