Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.function.BiPredicate;
import java.util.ArrayList;
import java.util.List;

import org.jooq.exception.DataAccessException;
import org.jooq.impl.DSL;
Expand Down Expand Up @@ -95,4 +98,23 @@ public void write(int b) throws IOException {
LOG.info("{} table already exists, skipping creation.", tableName);
return true;
};

/**
* Utility method to list all user-defined tables in the database.
*
* @param connection The database connection to use.
* @return A list of table names (user-defined tables only).
* @throws SQLException If there is an issue accessing the database metadata.
*/
public static List<String> listAllTables(Connection connection) throws SQLException {
List<String> tableNames = new ArrayList<>();
try (ResultSet resultSet = connection.getMetaData().getTables(null, null, null, new String[]{"TABLE"})) {
while (resultSet.next()) {
String tableName = resultSet.getString("TABLE_NAME");
tableNames.add(tableName);
}
}
LOG.debug("Found {} user-defined tables in the database: {}", tableNames.size(), tableNames);
return tableNames;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,28 @@
import org.jooq.DSLContext;
import org.jooq.impl.DSL;
import org.jooq.impl.SQLDataType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

import static org.hadoop.ozone.recon.codegen.SqlDbUtils.TABLE_EXISTS_CHECK;
import static org.hadoop.ozone.recon.codegen.SqlDbUtils.listAllTables;
import static org.jooq.impl.DSL.name;

/**
* Class for managing the schema of the SchemaVersion table.
*/
@Singleton
public class SchemaVersionTableDefinition implements ReconSchemaDefinition {

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

public static final String SCHEMA_VERSION_TABLE_NAME = "RECON_SCHEMA_VERSION";
private final DataSource dataSource;
private DSLContext dslContext;
private int latestSLV;

@Inject
public SchemaVersionTableDefinition(DataSource dataSource) {
Expand All @@ -48,22 +54,56 @@ public SchemaVersionTableDefinition(DataSource dataSource) {

@Override
public void initializeSchema() throws SQLException {
Connection conn = dataSource.getConnection();
dslContext = DSL.using(conn);
try (Connection conn = dataSource.getConnection()) {
DSLContext localDslContext = DSL.using(conn);

if (!TABLE_EXISTS_CHECK.test(conn, SCHEMA_VERSION_TABLE_NAME)) {
// If the RECON_SCHEMA_VERSION table does not exist, check for other tables
// to identify if it is a fresh install
boolean isFreshInstall = listAllTables(conn).isEmpty();
createSchemaVersionTable(localDslContext);

if (!TABLE_EXISTS_CHECK.test(conn, SCHEMA_VERSION_TABLE_NAME)) {
createSchemaVersionTable();
if (isFreshInstall) {
// Fresh install: Set the SLV to the latest version
insertInitialSLV(localDslContext, latestSLV);
}
}
}
}

/**
* Create the Schema Version table.
*
* @param dslContext The DSLContext to use for the operation.
*/
private void createSchemaVersionTable() throws SQLException {
private void createSchemaVersionTable(DSLContext dslContext) {
dslContext.createTableIfNotExists(SCHEMA_VERSION_TABLE_NAME)
.column("version_number", SQLDataType.INTEGER.nullable(false))
.column("applied_on", SQLDataType.TIMESTAMP.defaultValue(DSL.currentTimestamp()))
.execute();
}

/**
* Inserts the initial SLV into the Schema Version table.
*
* @param dslContext The DSLContext to use for the operation.
* @param slv The initial SLV value.
*/
private void insertInitialSLV(DSLContext dslContext, int slv) {
dslContext.insertInto(DSL.table(SCHEMA_VERSION_TABLE_NAME))
.columns(DSL.field(name("version_number")),
DSL.field(name("applied_on")))
.values(slv, DSL.currentTimestamp())
.execute();
LOG.info("Inserted initial SLV '{}' into SchemaVersion table.", slv);
}

/**
* Set the latest SLV.
*
* @param slv The latest Software Layout Version.
*/
public void setLatestSLV(int slv) {
this.latestSLV = slv;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import java.util.HashSet;
import java.util.Set;

import org.apache.hadoop.ozone.recon.upgrade.ReconLayoutFeature;
import org.hadoop.ozone.recon.schema.ReconSchemaDefinition;
import org.hadoop.ozone.recon.schema.SchemaVersionTableDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -45,13 +47,46 @@ public ReconSchemaManager(Set<ReconSchemaDefinition> reconSchemaDefinitions) {

@VisibleForTesting
public void createReconSchema() {
reconSchemaDefinitions.forEach(reconSchemaDefinition -> {
try {
reconSchemaDefinition.initializeSchema();
} catch (SQLException e) {
LOG.error("Error creating Recon schema {}.",
reconSchemaDefinition.getClass().getSimpleName(), e);
}
});
// Calculate the latest SLV from ReconLayoutFeature
int latestSLV = calculateLatestSLV();

try {
// Initialize the schema version table first
reconSchemaDefinitions.stream()
.filter(SchemaVersionTableDefinition.class::isInstance)
.findFirst()
.ifPresent(schemaDefinition -> {
SchemaVersionTableDefinition schemaVersionTable = (SchemaVersionTableDefinition) schemaDefinition;
schemaVersionTable.setLatestSLV(latestSLV);
try {
schemaVersionTable.initializeSchema();
} catch (SQLException e) {
LOG.error("Error initializing SchemaVersionTableDefinition.", e);
}
});

// Initialize all other tables
reconSchemaDefinitions.stream()
.filter(definition -> !(definition instanceof SchemaVersionTableDefinition))
.forEach(definition -> {
try {
definition.initializeSchema();
} catch (SQLException e) {
LOG.error("Error initializing schema: {}.", definition.getClass().getSimpleName(), e);
}
});

} catch (Exception e) {
LOG.error("Error creating Recon schema.", e);
}
}

/**
* Calculate the latest SLV by iterating over ReconLayoutFeature.
*
* @return The latest SLV.
*/
private int calculateLatestSLV() {
return ReconLayoutFeature.determineSLV();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.reflections.Reflections;

import java.util.Arrays;
import java.util.EnumMap;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -92,6 +93,17 @@ public static void registerUpgradeActions() {
}
}

/**
* Determines the Software Layout Version (SLV) based on the latest feature version.
* @return The Software Layout Version (SLV).
*/
public static int determineSLV() {
return Arrays.stream(ReconLayoutFeature.values())
.mapToInt(ReconLayoutFeature::getVersion)
.max()
.orElse(0); // Default to 0 if no features are defined
}

/**
* Returns the list of all layout feature values.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private int determineSLV() {
return Arrays.stream(ReconLayoutFeature.values())
.mapToInt(ReconLayoutFeature::getVersion)
.max()
.orElse(0); // Default to 0 if no features are defined
.orElse(0);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ protected Connection getConnection() throws SQLException {
return injector.getInstance(DataSource.class).getConnection();
}

protected DataSource getDataSource() {
return injector.getInstance(DataSource.class);
}

protected DSLContext getDslContext() {
return dslContext;
}
Expand Down
Loading
Loading