HIVE-29668: Add -rebuildIndexes utility to reconstruct backend Metast… - #6636
HIVE-29668: Add -rebuildIndexes utility to reconstruct backend Metast…#6636soumyakanti3578 wants to merge 2 commits into
Conversation
e0821f6 to
d42f9cf
Compare
|
nrg4878
left a comment
There was a problem hiding this comment.
I wasnt expecting that we will be checking in rebuild-indexes scripts as well. But I do not have a problem per se as long as these get updated when new indices are added.
|
|
||
| @Category(MetastoreUnitTest.class) | ||
| @RunWith(Parameterized.class) | ||
| public class TestRebuildIndexesScriptConsistency { |
There was a problem hiding this comment.
can we add a test that fails if we add a new index to the schema file that is not found in the rebuild-indexes file ? So that way developers will remember to update these files as well?
There was a problem hiding this comment.
Yes, that is exactly what's being tested in rebuildScriptMatchesInitScript()
Pattern pattern = PATTERNS.get(dbType);
Set<String> initIndexes = extractIndexNames(schemaScript, pattern);
Set<String> rebuildIndexes = extractIndexNames(rebuildScript, pattern);
Set<String> missing = new HashSet<>(initIndexes);
missing.removeAll(rebuildIndexes);
Set<String> extra = new HashSet<>(rebuildIndexes);
extra.removeAll(initIndexes);
assertTrue("Indexes in init script missing from rebuild script: " + missing, missing.isEmpty());
assertTrue("Indexes in rebuild script not found in init script: " + extra, extra.isEmpty());
and it tests extra indexes in the rebuild scripts too, so devs can remember to remove them if we ever remove an index from the schema file.
There was a problem hiding this comment.
Pull request overview
Adds a new -rebuildIndexes operation to the standalone metastore SchemaTool to (re)create HMS backend indexes via DB-specific SQL scripts, with accompanying tests to validate both execution and script/index consistency across supported DBs.
Changes:
- Adds
-rebuildIndexescommand-line option and wires it to a newSchemaToolTaskRebuildIndexes. - Introduces per-DB
rebuild-indexes.<db>.sqlscripts (postgres/mysql/oracle/mssql) for dropping/recreating indexes. - Adds tests to execute the new task and to ensure rebuild scripts stay consistent with init schema index definitions.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/tools/schematool/TestSchemaToolForMetastore.java | Adds an integration-style test invoking the new -rebuildIndexes task. |
| standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/tools/schematool/TestRebuildIndexesScriptConsistency.java | New test validating rebuild scripts’ index sets match init schema scripts for supported DBs. |
| standalone-metastore/metastore-server/src/main/sql/postgres/rebuild-indexes.postgres.sql | Adds PostgreSQL index rebuild script. |
| standalone-metastore/metastore-server/src/main/sql/mysql/rebuild-indexes.mysql.sql | Adds MySQL/MariaDB index rebuild script using ALTER TABLE for drop+add. |
| standalone-metastore/metastore-server/src/main/sql/oracle/rebuild-indexes.oracle.sql | Adds Oracle index rebuild script. |
| standalone-metastore/metastore-server/src/main/sql/mssql/rebuild-indexes.mssql.sql | Adds MSSQL index rebuild script using DROP_EXISTING = ON. |
| standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/tools/schematool/SchemaToolTaskRebuildIndexes.java | New SchemaTool task that locates and executes the rebuild script (or logs it in dry-run). |
| standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/tools/schematool/SchemaToolCommandLine.java | Registers the new rebuildIndexes CLI option and fixes the Postgres constant reference. |
| standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/tools/schematool/MetastoreSchemaTool.java | Routes -rebuildIndexes to the new task implementation. |
| standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/tools/schematool/HiveSchemaHelper.java | Renames the Postgres DB type constant to DB_POSTGRES and updates parser selection. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| -- See the License for the specific language governing permissions and | ||
|
|
There was a problem hiding this comment.
nit: incomplete license info.
There was a problem hiding this comment.
Ack, will update in the next commit.
| public static final String DB_MSSQL = "mssql"; | ||
| public static final String DB_MYSQL = "mysql"; | ||
| public static final String DB_POSTGRACE = "postgres"; | ||
| public static final String DB_POSTGRES = "postgres"; | ||
| public static final String DB_ORACLE = "oracle"; |
There was a problem hiding this comment.
I don't think this change is very risky, but let me know what others think.
| LOG.info("Dry run: would execute {}", script.getAbsolutePath()); | ||
| LOG.info(new String(Files.readAllBytes(script.toPath()))); | ||
| } catch (IOException e) { |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 10 out of 10 changed files in this pull request and generated no new comments.
Suppressed comments (3)
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/tools/schematool/HiveSchemaHelper.java:46
- Renaming/removing the public constant
DB_POSTGRACEis a source/binary compatibility break for any downstream code that references it. Consider keeping it as a deprecated alias toDB_POSTGRESto preserve compatibility while still fixing the typo.
public static final String DB_DERBY = "derby";
public static final String DB_HIVE = "hive";
public static final String DB_MSSQL = "mssql";
public static final String DB_MYSQL = "mysql";
public static final String DB_POSTGRES = "postgres";
public static final String DB_ORACLE = "oracle";
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/tools/schematool/SchemaToolTaskRebuildIndexes.java:54
new String(Files.readAllBytes(...))uses the platform default charset, which can make dry-run output vary by environment. Prefer reading with an explicit charset.
LOG.info("Dry run: would execute {}", script.getAbsolutePath());
LOG.info(new String(Files.readAllBytes(script.toPath())));
} catch (IOException e) {
standalone-metastore/metastore-server/src/main/sql/oracle/rebuild-indexes.oracle.sql:20
- As written, this Oracle rebuild script will fail immediately if any index is already missing (no
DROP INDEX IF EXISTS), which undermines-rebuildIndexesas an unattended recovery utility. Consider making each DROP resilient (e.g., PL/SQL block that ignores ORA-01418) so the tool can recreate missing indexes without manual edits.
-- Oracle has no DROP INDEX IF EXISTS; this script uses plain DROP INDEX.
-- If an index is already missing, comment out its DROP line before running.
-- Update this file whenever a new upgrade script adds an index.
saihemanth-cloudera
left a comment
There was a problem hiding this comment.
For databases with native support, should we prefer REINDEX, ALTER INDEX ... REBUILD, or engine-specific online rebuild options rather drop/create semantics?
| -- Oracle has no DROP INDEX IF EXISTS; this script uses plain DROP INDEX. | ||
| -- If an index is already missing, comment out its DROP line before running. | ||
| -- Update this file whenever a new upgrade script adds an index. |
There was a problem hiding this comment.
If the utility is run because an index is missing, Oracle fails on the first missing index and never reaches the create. The comment telling users to manually edit the script defeats the purpose of -rebuildIndexes. Can we use use PL/SQL blocks that ignore only “index does not exist”?
There was a problem hiding this comment.
Yes, PL/SQL should work for this. I wanted to go with the simpler approach for now given that this feature would be used rarely, and maintainability of a PL/SQL script is higher. I will update the script with PL/SQL.
| -- Update this file whenever a new upgrade script adds an index. | ||
|
|
||
| DROP INDEX IF EXISTS "NOTIFICATION_LOG_EVENT_ID"; | ||
| CREATE UNIQUE INDEX "NOTIFICATION_LOG_EVENT_ID" ON "NOTIFICATION_LOG" USING btree ("EVENT_ID"); |
There was a problem hiding this comment.
hive-schema-4.3.0.postgres.sql defines important uniqueness objects like UNIQUE_DATABASE, UNIQUETABLE, UNIQUEPARTITION, and UNIQUE_TYPE. This script only covers CREATE INDEX objects, so it cannot fully reconstruct the backend indexing/constraint state?
The new consistency test also misses this because it only extracts CREATE INDEX names from the init script.
|
|
||
| @Category(MetastoreUnitTest.class) | ||
| @RunWith(Parameterized.class) | ||
| public class TestRebuildIndexesScriptConsistency { |
There was a problem hiding this comment.
This would pass if a rebuild script recreated an index with the same name but wrong table, wrong columns, wrong order, wrong uniqueness, or wrong access method.
I think the right way to test this is that, it should parse and compare at least index name, table, column list/order, and uniqueness; for PostgreSQL it also needs constraint-backed indexes. What do you think?
There was a problem hiding this comment.
Yes but it would also make it more complex, and we would have to rely more on regex. Moreover, the pattern will be different for all DBs so even more regex!
Since this feature would be rarely used, and addition/deletion of indexes to the schema file is not super common and restricted to developers, we can probably rely on these tests?
What you are suggesting is probably the right thing to do but since we are relying on regex, it will make the code more error-prone. It would probably have been easier to do with the other approach #6545.
Do let me know what you think
| -- See the License for the specific language governing permissions and | ||
|
|
There was a problem hiding this comment.
nit: incomplete license info.
From what I understand, if an index was dropped accidentally, these statements won't rebuild it. |



…ore indexes
What changes were proposed in this pull request?
rebuildIndexesoption toschemaToolWhy are the changes needed?
Gives users the ability to drop and recreate indexes easily through schematool.
Does this PR introduce any user-facing change?
Yes, adds a new option to schematool.
How was this patch tested?