forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[refactor](hms)Refactor HiveTransactionMgr, Refactor the logic of rea…
…ding hive acid tb.
- Loading branch information
Showing
12 changed files
with
390 additions
and
176 deletions.
There are no files selected for viewing
This file contains 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
74 changes: 74 additions & 0 deletions
74
fe/fe-core/src/main/java/org/apache/doris/catalog/QueryCallBackMgr.java
This file contains 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,74 @@ | ||
// 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.doris.catalog; | ||
|
||
import org.apache.doris.thrift.TUniqueId; | ||
|
||
import com.google.common.collect.Maps; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* This class is suitable for queries, especially for releasing resources after the query is completed. | ||
* The resources generally need to be held from the plan to the end of the query. | ||
* | ||
* For example, when reading a Hive ACID table, you should first acquire a lock from the HMS. | ||
* Releasing the lock on the HMS is divided into two parts: | ||
* 1. The plan fails. | ||
* 2. The plan is sent to the BE (backend), and the BE finishes reading. | ||
*/ | ||
public class QueryCallBackMgr { | ||
//query id => plan fail callback func. | ||
// In the same query, different plan nodes may have different callback functions, so handle a List<Runnable>. | ||
private Map<TUniqueId, List<Runnable>> planFailCallBackMap = Maps.newConcurrentMap(); | ||
//query id => query end callback func .(plan success). | ||
private Map<TUniqueId, List<Runnable>> queryEndCallBackMap = Maps.newConcurrentMap(); | ||
|
||
public void registerPlanFailFunc(TUniqueId queryId, Runnable callback) { | ||
planFailCallBackMap.computeIfAbsent(queryId, | ||
k -> Collections.synchronizedList(new ArrayList<>())).add(callback); | ||
} | ||
|
||
public void registerQueryEndFunc(TUniqueId queryId, Runnable callback) { | ||
queryEndCallBackMap.computeIfAbsent(queryId, | ||
k -> Collections.synchronizedList(new ArrayList<>())).add(callback); | ||
} | ||
|
||
public void planFailCallback(TUniqueId queryId) { | ||
List<Runnable> callbacks = planFailCallBackMap.remove(queryId); | ||
if (callbacks != null) { | ||
for (Runnable callback : callbacks) { | ||
callback.run(); | ||
} | ||
} | ||
queryEndCallBackMap.remove(queryId); | ||
} | ||
|
||
public void queryEndCallback(TUniqueId queryId) { | ||
List<Runnable> callbacks = queryEndCallBackMap.remove(queryId); | ||
if (callbacks != null) { | ||
for (Runnable callback : callbacks) { | ||
callback.run(); | ||
} | ||
} | ||
planFailCallBackMap.remove(queryId); | ||
} | ||
} |
This file contains 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
57 changes: 57 additions & 0 deletions
57
fe/fe-core/src/main/java/org/apache/doris/datasource/hive/HiveAcidTransaction.java
This file contains 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,57 @@ | ||
// 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.doris.datasource.hive; | ||
|
||
import org.apache.doris.analysis.TableName; | ||
import org.apache.doris.common.util.DebugUtil; | ||
import org.apache.doris.thrift.TUniqueId; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* HiveAcidTransaction is used to maintain the information of a transaction table during the query process. | ||
* <p> | ||
* Each HiveAcidTransaction is bound to a specific scanNode during the plan process. | ||
* A plan may contain multiple HiveAcidTransaction, such as join. | ||
*/ | ||
public class HiveAcidTransaction { | ||
private final TUniqueId queryId; | ||
private final long hmsTxnId; | ||
private final String user; | ||
private final HMSExternalTable hiveTable; | ||
|
||
private Map<String, String> txnValidIds = null; | ||
|
||
public HiveAcidTransaction(TUniqueId queryId, long hmsTxnId, String user, HMSExternalTable hiveTable) { | ||
this.queryId = queryId; | ||
this.hmsTxnId = hmsTxnId; | ||
this.user = user; | ||
this.hiveTable = hiveTable; | ||
} | ||
|
||
public Map<String, String> getValidWriteIds(HMSCachedClient client, List<String> partitionNames) { | ||
if (txnValidIds == null) { | ||
TableName tableName = new TableName(hiveTable.getCatalog().getName(), hiveTable.getDbName(), | ||
hiveTable.getName()); | ||
client.acquireSharedLock(DebugUtil.printId(queryId), hmsTxnId, user, tableName, partitionNames, 5000); | ||
txnValidIds = client.getValidWriteIds(tableName.getDb() + "." + tableName.getTbl(), hmsTxnId); | ||
} | ||
return txnValidIds; | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
fe/fe-core/src/main/java/org/apache/doris/datasource/hive/HiveAcidTransactionMgr.java
This file contains 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,94 @@ | ||
// 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.doris.datasource.hive; | ||
|
||
import org.apache.doris.catalog.Env; | ||
import org.apache.doris.thrift.TUniqueId; | ||
|
||
import com.google.common.collect.Maps; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* HiveAcidTransactionMgr is responsible for starting and automatically committing HMS transactions. | ||
* HiveAcidTransactionMgr should be at the catalog level. | ||
* <p> | ||
* beginQueryTransaction() starts the transaction and return HiveAcidTransaction. | ||
* HiveAcidTransaction.getValidWriteIds() requests table locks. | ||
* The scanNode needs to call beginQueryTransaction to obtain a HiveAcidTransaction. | ||
* The scanNode calls the getValidWriteIds method from HiveAcidTransaction to retrieve transaction information. | ||
* <p> | ||
* Example expected behavior as follows: | ||
* SQL1: catalog1.db.a JOIN catalog1.db.b | ||
* scanNode1 (catalog1.db.a): beginQueryTransaction() | ||
* scanNode2 (catalog1.db.b): beginQueryTransaction() | ||
* Only one transaction will be started on HMS, table 'a' needs one lock, table 'b' needs one lock. | ||
* <p> | ||
* SQL2: catalog1.db.a JOIN catalog1.db.a | ||
* scanNode1 (catalog1.db.a): beginQueryTransaction() | ||
* scanNode2 (catalog1.db.a): beginQueryTransaction() | ||
* Only one transaction will be started on HMS, table 'a' needs two locks (because locking needs to consider | ||
* the partitioning of the query, after partition pruning). | ||
* <p> | ||
* SQL3: catalog1.db.a JOIN catalog2.db.a | ||
* scanNode1 (catalog1.db.a): beginQueryTransaction() | ||
* scanNode2 (catalog2.db.a): beginQueryTransaction() | ||
* For two different catalogs, even if they end up connecting to the same HMS, we still consider table 'a' | ||
* as two different tables. So two transactions will be started on HMS, and table 'a' needs two locks. | ||
*/ | ||
public class HiveAcidTransactionMgr { | ||
private static final Logger LOG = LogManager.getLogger(HiveAcidTransactionMgr.class); | ||
// doris query id => hms transaction id. | ||
private Map<TUniqueId, Long> queryIdToHmsTxnIdMap = Maps.newConcurrentMap(); | ||
|
||
public HiveAcidTransaction beginQueryTransaction(TUniqueId queryId, String user, | ||
HMSExternalCatalog catalog, HMSExternalTable hiveTable) { | ||
long hmsTxnId = queryIdToHmsTxnIdMap.computeIfAbsent( | ||
queryId, | ||
k -> { | ||
long newHmsTxnId = catalog.getClient().openTxn(user); | ||
|
||
// After the plan fails or the query ends, we need to commit the transaction to release the locks | ||
// that were acquired during the plan execution. | ||
Env.getCurrentQueryCallBackMgr().registerPlanFailFunc(queryId, () -> { | ||
commitQueryTransaction(queryId, newHmsTxnId, catalog); | ||
}); | ||
|
||
Env.getCurrentQueryCallBackMgr().registerQueryEndFunc(queryId, () -> { | ||
commitQueryTransaction(queryId, newHmsTxnId, catalog); | ||
}); | ||
|
||
return newHmsTxnId; | ||
} | ||
); | ||
return new HiveAcidTransaction(queryId, hmsTxnId, user, hiveTable); | ||
} | ||
|
||
private void commitQueryTransaction(TUniqueId queryId, long hmsTxnId, HMSExternalCatalog catalog) { | ||
queryIdToHmsTxnIdMap.remove(queryId); | ||
try { | ||
catalog.getClient().commitTxn(hmsTxnId); | ||
} catch (Exception e) { | ||
//The transaction may still exist on HMS. After a while, HMS will clear the transaction. | ||
LOG.warn("catalog ={},queryId = {},hmsTxnId = {},commit hms transaction failed", | ||
catalog.getName(), queryId, hmsTxnId, e); | ||
} | ||
} | ||
} |
89 changes: 0 additions & 89 deletions
89
fe/fe-core/src/main/java/org/apache/doris/datasource/hive/HiveTransaction.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.