Skip to content

Commit

Permalink
HIVE-28416: Another process is rebuilding the materialized view is th…
Browse files Browse the repository at this point in the history
…rown for subsequent MV rebuilds when the current MV rebuild is aborted for the same MV. (Dayakar M, reviewed by Krisztian Kasa, Zsolt Miskolczi, Zoltan Ratkai, Dmitriy Fingerman)
  • Loading branch information
mdayakar authored Oct 16, 2024
1 parent b552630 commit b4dd5cf
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4696,4 +4696,42 @@ public void testMaterializedViewEnableRewriteNonBlocking() throws Exception {
txnMgr.rollbackTxn();
driver.run("drop materialized view mv_tab_acid");
}

@Test
public void testMaterializedViewRebuildLockForSameMV() throws Exception {
driver.run("drop materialized view if exists mv_tab_acid");
dropTable(new String[] { "tab_acid" });

driver.run("create table if not exists tab_acid (a int, b int) partitioned by (p string) " +
"stored as orc TBLPROPERTIES ('transactional'='true')");
driver.run("insert into tab_acid partition(p) (a,b,p) values(1,2,'foo'),(3,4,'bar')");

driver.run("create materialized view mv_tab_acid partitioned on (p) " +
"stored as orc TBLPROPERTIES ('transactional'='true') as select a, p from tab_acid where b > 1");

driver.run("insert into tab_acid partition(p) (a,b,p) values(1,2,'foo'),(3,4,'bar')");

// execute MV Rebuild SQL for mv_tab_acid table
driver.compileAndRespond("alter materialized view mv_tab_acid rebuild");
driver.lockAndRespond();
// Corresponding MV Rebuild lock entry should be present in MATERIALIZATION_REBUILD_LOCKS table
Assert.assertEquals("One lock should be there as MV Rebuild is in-progress", 1, TestTxnDbUtil.countQueryAgent(conf,
"select count(*) from MATERIALIZATION_REBUILD_LOCKS where MRL_TBL_NAME='mv_tab_acid'"));

// rollback the transaction
txnMgr.rollbackTxn();
// As transaction is rollback there should not be any MV Rebuild lock entry for mv_tab_acid table.
Assert.assertEquals("There should not be any MV Rebuild lock as Txn is rollback.", 0,
TestTxnDbUtil.countQueryAgent(conf,
"select count(*) from MATERIALIZATION_REBUILD_LOCKS where MRL_TBL_NAME='mv_tab_acid'"));

// re-execute MV Rebuild SQL for mv_tab_acid table
driver.compileAndRespond("alter materialized view mv_tab_acid rebuild");
// Should be able to execute MV Rebuild and corresponding MV Rebuild lock entry should be present for mv_tab_acid table.
Assert.assertEquals("One lock should be there as MV Rebuild is re-triggered", 1, TestTxnDbUtil.countQueryAgent(conf,
"select count(*) from MATERIALIZATION_REBUILD_LOCKS where MRL_TBL_NAME='mv_tab_acid'"));

// cleanup
driver.run("drop materialized view mv_tab_acid");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ public Integer execute(MultiDataSourceJdbcResource jdbcResource) throws MetaExce
prefix.append("DELETE FROM \"HIVE_LOCKS\" WHERE ");
TxnUtils.buildQueryWithINClause(conf, queries, prefix, suffix, txnids, "\"HL_TXNID\"", false, false);

// add delete mv rebuild locks queries to query list
prefix.setLength(0);
suffix.setLength(0);
prefix.append("DELETE FROM \"MATERIALIZATION_REBUILD_LOCKS\" WHERE ");
TxnUtils.buildQueryWithINClause(conf, queries, prefix, suffix, txnids, "\"MRL_TXN_ID\"", false, false);


//If this abort is for REPL_CREATED TXN initiated outside the replication flow, then clean the corresponding entry
//from REPL_TXN_MAP and mark that database as replication incompatible.
if (!isReplReplayed) {
Expand Down

0 comments on commit b4dd5cf

Please sign in to comment.