This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch branch-2.1
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-2.1 by this push:
new 77cef64e687 Add fuzzy matching of label in show transaction (#30510)
77cef64e687 is described below
commit 77cef64e68756f2ea2f0dc70842a52d5424562d3
Author: wudi <[email protected]>
AuthorDate: Tue Jan 30 17:02:50 2024 +0800
Add fuzzy matching of label in show transaction (#30510)
---
.../apache/doris/analysis/ShowTransactionStmt.java | 15 +++++++-
.../java/org/apache/doris/qe/ShowExecutor.java | 3 ++
.../doris/transaction/DatabaseTransactionMgr.java | 44 ++++++++++++++++++++++
.../doris/transaction/GlobalTransactionMgr.java | 5 +++
.../query_p0/show/test_show_transaction.groovy | 42 +++++++++++++++++++++
5 files changed, 107 insertions(+), 2 deletions(-)
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowTransactionStmt.java
b/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowTransactionStmt.java
index b88922298b2..f34c0e5e336 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowTransactionStmt.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowTransactionStmt.java
@@ -42,6 +42,7 @@ public class ShowTransactionStmt extends ShowStmt {
private long txnId = -1;
private String label = "";
private TransactionStatus status = TransactionStatus.UNKNOWN;
+ private boolean labelMatch = false;
public ShowTransactionStmt(String dbName, Expr whereClause) {
this.dbName = dbName;
@@ -64,6 +65,10 @@ public class ShowTransactionStmt extends ShowStmt {
return status;
}
+ public boolean labelMatch() {
+ return labelMatch;
+ }
+
@Override
public void analyze(Analyzer analyzer) throws AnalysisException,
UserException {
super.analyze(analyzer);
@@ -95,7 +100,7 @@ public class ShowTransactionStmt extends ShowStmt {
valid = false;
break CHECK;
}
- } else {
+ } else if (!(whereClause instanceof LikePredicate)) {
valid = false;
break CHECK;
}
@@ -123,10 +128,16 @@ public class ShowTransactionStmt extends ShowStmt {
} else {
valid = false;
}
+
+ if (whereClause instanceof LikePredicate &&
leftKey.equalsIgnoreCase("label")) {
+ //Only supports label like matching
+ labelMatch = true;
+ label = label.replaceAll("%", ".*");
+ }
}
if (!valid) {
- throw new AnalysisException("Where clause should looks like one of
them: id = 123 or label = 'label' "
+ throw new AnalysisException("Where clause should looks like one of
them: id = 123 or label =/like 'label' "
+ "or status =
'prepare/precommitted/committed/visible/aborted'");
}
}
diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java
b/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java
index dbe0a27f931..c6cb9dcb055 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java
@@ -2250,6 +2250,9 @@ public class ShowExecutor {
if (status != TransactionStatus.UNKNOWN) {
resultSet = new ShowResultSet(showStmt.getMetaData(),
transactionMgr.getDbTransInfoByStatus(db.getId(), status));
+ } else if (showStmt.labelMatch() && !showStmt.getLabel().isEmpty()) {
+ resultSet = new ShowResultSet(showStmt.getMetaData(),
+ transactionMgr.getDbTransInfoByLabelMatch(db.getId(),
showStmt.getLabel()));
} else {
Long txnId = showStmt.getTxnId();
String label = showStmt.getLabel();
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/transaction/DatabaseTransactionMgr.java
b/fe/fe-core/src/main/java/org/apache/doris/transaction/DatabaseTransactionMgr.java
index 43233002935..e42816301fb 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/transaction/DatabaseTransactionMgr.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/transaction/DatabaseTransactionMgr.java
@@ -297,6 +297,50 @@ public class DatabaseTransactionMgr {
return infos;
}
+ public List<List<String>> getTxnStateInfoList(String labelRegex) {
+ List<List<String>> infos = Lists.newArrayList();
+ List<TransactionState> transactionStateCollection =
Lists.newArrayList();
+ readLock();
+ try {
+
transactionStateCollection.addAll(idToFinalStatusTransactionState.values());
+
transactionStateCollection.addAll(idToRunningTransactionState.values());
+ // get transaction order by txn id desc
+ transactionStateCollection.stream()
+ .filter(transactionState ->
(transactionState.getLabel().matches(labelRegex)))
+ .sorted(TransactionState.TXN_ID_COMPARATOR)
+ .forEach(t -> {
+ List<String> info = Lists.newArrayList();
+ getTxnStateInfo(t, info);
+ infos.add(info);
+ });
+ } finally {
+ readUnlock();
+ }
+ return infos;
+ }
+
+ public List<List<String>> getTxnStateInfoList(String labelRegex) {
+ List<List<String>> infos = Lists.newArrayList();
+ List<TransactionState> transactionStateCollection =
Lists.newArrayList();
+ readLock();
+ try {
+
transactionStateCollection.addAll(idToFinalStatusTransactionState.values());
+
transactionStateCollection.addAll(idToRunningTransactionState.values());
+ // get transaction order by txn id desc
+ transactionStateCollection.stream()
+ .filter(transactionState ->
(transactionState.getLabel().matches(labelRegex)))
+ .sorted(TransactionState.TXN_ID_COMPARATOR)
+ .forEach(t -> {
+ List<String> info = Lists.newArrayList();
+ getTxnStateInfo(t, info);
+ infos.add(info);
+ });
+ } finally {
+ readUnlock();
+ }
+ return infos;
+ }
+
private void getTxnStateInfo(TransactionState txnState, List<String> info)
{
info.add(String.valueOf(txnState.getTransactionId()));
info.add(txnState.getLabel());
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/transaction/GlobalTransactionMgr.java
b/fe/fe-core/src/main/java/org/apache/doris/transaction/GlobalTransactionMgr.java
index 4832d66bedb..30f1c706c50 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/transaction/GlobalTransactionMgr.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/transaction/GlobalTransactionMgr.java
@@ -565,6 +565,11 @@ public class GlobalTransactionMgr implements Writable {
return dbTransactionMgr.getTxnStateInfoList(status);
}
+ public List<List<String>> getDbTransInfoByLabelMatch(long dbId, String
label) throws AnalysisException {
+ DatabaseTransactionMgr dbTransactionMgr =
getDatabaseTransactionMgr(dbId);
+ return dbTransactionMgr.getTxnStateInfoList(label);
+ }
+
public long getTxnNumByStatus(TransactionStatus status) {
long counter = 0;
for (DatabaseTransactionMgr dbMgr :
dbIdToDatabaseTransactionMgrs.values()) {
diff --git a/regression-test/suites/query_p0/show/test_show_transaction.groovy
b/regression-test/suites/query_p0/show/test_show_transaction.groovy
new file mode 100644
index 00000000000..fa7f0ddbdc9
--- /dev/null
+++ b/regression-test/suites/query_p0/show/test_show_transaction.groovy
@@ -0,0 +1,42 @@
+// 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.
+
+suite("test_show_transaction", "p0") {
+ // define a sql table
+ def testTable = "test_show_transaction"
+
+ sql "DROP TABLE IF EXISTS ${testTable}"
+ sql """
+ CREATE TABLE IF NOT EXISTS ${testTable} (
+ `k1` INT NULL COMMENT "",
+ `k2` STRING NOT NULL COMMENT ""
+ ) ENGINE=OLAP
+ DUPLICATE KEY(`k1`)
+ DISTRIBUTED BY HASH(`k1`) BUCKETS 1
+ PROPERTIES (
+ "replication_allocation" = "tag.location.default: 1",
+ "storage_format" = "V2"
+ );
+ """
+
+ def uuid = UUID.randomUUID().toString().replaceAll("-", "");
+ sql """ INSERT INTO ${testTable} WITH LABEL
label_test_show_transaction_${uuid} VALUES(100, 'doris') """
+ def res = sql_return_maparray """ show transaction where label =
'label_test_show_transaction_${uuid}' """
+ print("show transaction result : " + res)
+ def reslike = sql_return_maparray """ show transaction where label like
'label_test_show_transaction_${uuid}%' """
+ assertTrue(res.equals(reslike))
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]