[Cherry-Pick][improvement](stmt) Add fuzzy matching of label in show transaction (#30725)

* Add fuzzy matching of label in show transaction

* fix
This commit is contained in:
wudi
2024-02-01 23:04:06 +08:00
committed by GitHub
parent 9100fba47e
commit 318bd3f9de
5 changed files with 85 additions and 2 deletions

View File

@ -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'");
}
}

View File

@ -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();

View File

@ -297,6 +297,28 @@ 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;
}
private void getTxnStateInfo(TransactionState txnState, List<String> info) {
info.add(String.valueOf(txnState.getTransactionId()));
info.add(txnState.getLabel());

View File

@ -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()) {