Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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,6 +24,8 @@
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;
Expand All @@ -37,9 +39,12 @@
@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,11 +53,20 @@ public SchemaVersionTableDefinition(DataSource dataSource) {

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

if (!TABLE_EXISTS_CHECK.test(conn, SCHEMA_VERSION_TABLE_NAME)) {
createSchemaVersionTable();
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 = checkForFreshInstall(conn);
createSchemaVersionTable();

if (isFreshInstall) {
// Fresh install: Set the SLV to the latest version
insertInitialSLV(conn, latestSLV);
}
}
}
}

Expand All @@ -66,4 +80,37 @@ private void createSchemaVersionTable() throws SQLException {
.execute();
}

/**
* Inserts the initial SLV into the Schema Version table.
* @param conn The database connection.
* @param slv The initial SLV value.
*/
private void insertInitialSLV(Connection conn, int slv) throws SQLException {
dslContext = DSL.using(conn);
dslContext.insertInto(DSL.table(SCHEMA_VERSION_TABLE_NAME))
.columns(DSL.field("version_number"), DSL.field("applied_on"))
.values(slv, DSL.currentTimestamp())
.execute();
LOG.info("Inserted initial SLV '{}' into SchemaVersion table.", slv);
}

/**
* Determines if this is a fresh install by checking the presence of key tables.
* @param conn The database connection.
* @return True if this is a fresh install, false otherwise.
*/
private boolean checkForFreshInstall(Connection conn) throws SQLException {
// Check for presence of key Recon tables (e.g., UNHEALTHY_CONTAINERS, RECON_TASK_STATUS, etc.)
return !TABLE_EXISTS_CHECK.test(conn, "UNHEALTHY_CONTAINERS") &&
!TABLE_EXISTS_CHECK.test(conn, "RECON_TASK_STATUS") &&
!TABLE_EXISTS_CHECK.test(conn, "CONTAINER_COUNT_BY_SIZE");
}

/**
* 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,12 +22,16 @@
import java.util.HashSet;
import java.util.Set;

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

import com.google.common.annotations.VisibleForTesting;
import com.google.inject.Inject;
import org.apache.hadoop.ozone.recon.ReconUtils;

/**
* Class used to create Recon SQL tables.
Expand All @@ -45,13 +49,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 @@ -70,10 +70,7 @@ private int determineMLV() throws SQLException {
* @return The Software Layout Version (SLV).
*/
private int determineSLV() {
return Arrays.stream(ReconLayoutFeature.values())
.mapToInt(ReconLayoutFeature::getVersion)
.max()
.orElse(0); // Default to 0 if no features are defined
return ReconLayoutFeature.determineSLV();
}

/**
Expand Down
Loading