-
Notifications
You must be signed in to change notification settings - Fork 588
HDDS-11465. Introducing Schema Versioning for Recon to Handle Fresh Installs and Upgrades. #7213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
2573e03
HDDS-11465. Introducing Schema Versioning for Recon Derby table to Ha…
ArafatKhan2198 3e64282
Covered a few edge cases of upgrades and fresh install
ArafatKhan2198 a4895e0
Made changes for latest discussion
ArafatKhan2198 3128cd9
Removed the old changes
ArafatKhan2198 a57f922
New things
ArafatKhan2198 b80f014
New changes
ArafatKhan2198 ea5946d
Fixed a bug
ArafatKhan2198 27f1538
Finalised the new approach
ArafatKhan2198 e4b4b27
Added log comments for testing
ArafatKhan2198 262a93d
Added an initial version to Feature enum
ArafatKhan2198 929d453
Added tests for ReconLayoutVersionManager
ArafatKhan2198 bf68424
Added more tests for Table definition
ArafatKhan2198 855ad92
Added missing licence certs
ArafatKhan2198 6ea93a9
TestCase updation
ArafatKhan2198 d95a1a7
Replaced the occurance of Schema Layout Version to Software Layout Ve…
ArafatKhan2198 29fe85b
Refactored Recon layout feature upgrade framework to support annotati…
ArafatKhan2198 287cafa
Added a new test case
ArafatKhan2198 81e62f0
Made final changes
ArafatKhan2198 0afebff
Final changes for review
ArafatKhan2198 c538a78
Removed the occurance of AUTO_FINALIZE in comments and made changes t…
ArafatKhan2198 4127ab5
Updated recon context for failed upgrades and made other review changes
ArafatKhan2198 3609b74
Fixed the error handling review comments
ArafatKhan2198 defd313
Fixed a failing uT
ArafatKhan2198 f75c952
Fixed stack strace problem
ArafatKhan2198 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...con-codegen/src/main/java/org/hadoop/ozone/recon/schema/SchemaVersionTableDefinition.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * 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 | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * 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.schema; | ||
|
|
||
| import com.google.inject.Inject; | ||
| import com.google.inject.Singleton; | ||
| import org.jooq.DSLContext; | ||
| import org.jooq.impl.DSL; | ||
| import org.jooq.impl.SQLDataType; | ||
|
|
||
| import javax.sql.DataSource; | ||
| import java.sql.Connection; | ||
| import java.sql.SQLException; | ||
|
|
||
| import static org.hadoop.ozone.recon.codegen.SqlDbUtils.TABLE_EXISTS_CHECK; | ||
|
|
||
| /** | ||
| * Class for managing the schema of the SchemaVersion table. | ||
| */ | ||
| @Singleton | ||
| public class SchemaVersionTableDefinition implements ReconSchemaDefinition { | ||
|
|
||
| public static final String SCHEMA_VERSION_TABLE_NAME = "RECON_SCHEMA_VERSION"; | ||
| private final DataSource dataSource; | ||
| private DSLContext dslContext; | ||
|
|
||
| @Inject | ||
| public SchemaVersionTableDefinition(DataSource dataSource) { | ||
| this.dataSource = dataSource; | ||
| } | ||
|
|
||
| @Override | ||
| public void initializeSchema() throws SQLException { | ||
| Connection conn = dataSource.getConnection(); | ||
| dslContext = DSL.using(conn); | ||
|
|
||
| if (!TABLE_EXISTS_CHECK.test(conn, SCHEMA_VERSION_TABLE_NAME)) { | ||
| createSchemaVersionTable(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Create the Schema Version table. | ||
| */ | ||
| private void createSchemaVersionTable() throws SQLException { | ||
| dslContext.createTableIfNotExists(SCHEMA_VERSION_TABLE_NAME) | ||
| .column("version_number", SQLDataType.INTEGER.nullable(false)) | ||
| .column("applied_on", SQLDataType.TIMESTAMP.defaultValue(DSL.currentTimestamp())) | ||
| .execute(); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...one/recon/src/main/java/org/apache/hadoop/ozone/recon/ReconSchemaVersionTableManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /* | ||
| * 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 | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * 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.apache.hadoop.ozone.recon; | ||
|
|
||
| import com.google.inject.Inject; | ||
| import org.jooq.DSLContext; | ||
| import org.jooq.impl.DSL; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import javax.sql.DataSource; | ||
| import java.sql.SQLException; | ||
|
|
||
| import static org.jooq.impl.DSL.name; | ||
|
|
||
| /** | ||
| * Manager for handling the Recon Schema Version table. | ||
| * This class provides methods to get and update the current schema version. | ||
| */ | ||
| public class ReconSchemaVersionTableManager { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(ReconSchemaVersionTableManager.class); | ||
| public static final String RECON_SCHEMA_VERSION_TABLE_NAME = "RECON_SCHEMA_VERSION"; | ||
| private final DSLContext dslContext; | ||
| private final DataSource dataSource; | ||
|
|
||
| @Inject | ||
| public ReconSchemaVersionTableManager(DataSource dataSource) throws SQLException { | ||
| this.dataSource = dataSource; | ||
| this.dslContext = DSL.using(dataSource.getConnection()); | ||
| } | ||
|
|
||
| /** | ||
| * Get the current schema version from the RECON_SCHEMA_VERSION table. | ||
| * If the table is empty, or if it does not exist, it will return 0. | ||
| * @return The current schema version. | ||
| */ | ||
| public int getCurrentSchemaVersion() throws SQLException { | ||
| try { | ||
| return dslContext.select(DSL.field(name("version_number"))) | ||
| .from(DSL.table(RECON_SCHEMA_VERSION_TABLE_NAME)) | ||
| .fetchOptional() | ||
| .map(record -> record.get( | ||
| DSL.field(name("version_number"), Integer.class))) | ||
| .orElse(-1); // Return -1 if no version is found | ||
| } catch (Exception e) { | ||
| LOG.error("Failed to fetch the current schema version.", e); | ||
| throw new SQLException("Unable to read schema version from the table.", e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Update the schema version in the RECON_SCHEMA_VERSION table after all tables are upgraded. | ||
| * | ||
| * @param newVersion The new version to set. | ||
| */ | ||
| public void updateSchemaVersion(int newVersion) throws SQLException { | ||
| try { | ||
| boolean recordExists = dslContext.fetchExists(dslContext.selectOne() | ||
| .from(DSL.table(RECON_SCHEMA_VERSION_TABLE_NAME))); | ||
|
|
||
| if (recordExists) { | ||
| // Update the existing schema version record | ||
| dslContext.update(DSL.table(RECON_SCHEMA_VERSION_TABLE_NAME)) | ||
| .set(DSL.field(name("version_number")), newVersion) | ||
| .set(DSL.field(name("applied_on")), DSL.currentTimestamp()) | ||
| .execute(); | ||
| LOG.info("Updated schema version to '{}'.", newVersion); | ||
| } else { | ||
| // Insert a new schema version record | ||
| dslContext.insertInto(DSL.table(RECON_SCHEMA_VERSION_TABLE_NAME)) | ||
| .columns(DSL.field(name("version_number")), | ||
| DSL.field(name("applied_on"))) | ||
| .values(newVersion, DSL.currentTimestamp()) | ||
| .execute(); | ||
| LOG.info("Inserted new schema version '{}'.", newVersion); | ||
| } | ||
| } catch (Exception e) { | ||
| LOG.error("Failed to update schema version to '{}'.", newVersion, e); | ||
sumitagrawl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new SQLException("Unable to update schema version in the table.", e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Provides the data source used by this manager. | ||
| * @return The DataSource instance. | ||
| */ | ||
| public DataSource getDataSource() { | ||
| return dataSource; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...p-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/upgrade/ReconLayoutFeature.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* | ||
| * 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 | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * 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.apache.hadoop.ozone.recon.upgrade; | ||
|
|
||
| import org.reflections.Reflections; | ||
|
|
||
| import java.util.EnumMap; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Enum representing Recon layout features with their version, description, | ||
| * and associated upgrade action to be executed during an upgrade. | ||
| */ | ||
| public enum ReconLayoutFeature { | ||
| // Represents the starting point for Recon's layout versioning system. | ||
| INITIAL_VERSION(0, "Recon Layout Versioning Introduction"); | ||
|
|
||
| private final int version; | ||
| private final String description; | ||
| private final EnumMap<ReconUpgradeAction.UpgradeActionType, ReconUpgradeAction> actions = | ||
| new EnumMap<>(ReconUpgradeAction.UpgradeActionType.class); | ||
|
|
||
| ReconLayoutFeature(final int version, String description) { | ||
| this.version = version; | ||
| this.description = description; | ||
| } | ||
|
|
||
| public int getVersion() { | ||
| return version; | ||
| } | ||
|
|
||
| public String getDescription() { | ||
| return description; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the upgrade action for the specified {@link ReconUpgradeAction.UpgradeActionType}. | ||
| * | ||
| * @param type The type of the upgrade action (e.g., FINALIZE). | ||
| * @return An {@link Optional} containing the upgrade action if present. | ||
| */ | ||
| public Optional<ReconUpgradeAction> getAction(ReconUpgradeAction.UpgradeActionType type) { | ||
| return Optional.ofNullable(actions.get(type)); | ||
| } | ||
|
|
||
| /** | ||
| * Associates a given upgrade action with a specific upgrade phase for this feature. | ||
| * | ||
| * @param type The phase/type of the upgrade action. | ||
| * @param action The upgrade action to associate with this feature. | ||
| */ | ||
| public void addAction(ReconUpgradeAction.UpgradeActionType type, ReconUpgradeAction action) { | ||
| actions.put(type, action); | ||
| } | ||
|
|
||
| /** | ||
| * Scans the classpath for all classes annotated with {@link UpgradeActionRecon} | ||
| * and registers their upgrade actions for the corresponding feature and phase. | ||
| * This method dynamically loads and registers all upgrade actions based on their | ||
| * annotations. | ||
| */ | ||
| public static void registerUpgradeActions() { | ||
| Reflections reflections = new Reflections("org.apache.hadoop.ozone.recon.upgrade"); | ||
| Set<Class<?>> actionClasses = reflections.getTypesAnnotatedWith(UpgradeActionRecon.class); | ||
|
|
||
| for (Class<?> actionClass : actionClasses) { | ||
| try { | ||
| ReconUpgradeAction action = (ReconUpgradeAction) actionClass.getDeclaredConstructor().newInstance(); | ||
| UpgradeActionRecon annotation = actionClass.getAnnotation(UpgradeActionRecon.class); | ||
| annotation.feature().addAction(annotation.type(), action); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Failed to register upgrade action: " + actionClass.getSimpleName(), e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns the list of all layout feature values. | ||
| * | ||
| * @return An array of all {@link ReconLayoutFeature} values. | ||
| */ | ||
| public static ReconLayoutFeature[] getValues() { | ||
| return ReconLayoutFeature.values(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.