Skip to content

Commit

Permalink
[enhance](branch-3.0)support show-transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
cjj2010 committed Oct 14, 2024
1 parent 969f2c8 commit 08f9da3
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
import org.apache.doris.transaction.TransactionState.LoadJobSourceType;
import org.apache.doris.transaction.TransactionState.TxnCoordinator;
import org.apache.doris.transaction.TransactionStatus;
import org.apache.doris.transaction.TransactionUtil;
import org.apache.doris.transaction.TxnCommitAttachment;
import org.apache.doris.transaction.TxnStateCallbackFactory;
import org.apache.doris.transaction.TxnStateChangeCallback;
Expand Down Expand Up @@ -1507,7 +1508,14 @@ public TransactionStatus getLabelState(long dbId, String label) throws AnalysisE

@Override
public Long getTransactionId(Long dbId, String label) throws AnalysisException {
throw new AnalysisException(NOT_SUPPORTED_MSG);
try {
TransactionStatus labelState = getLabelState(dbId, label);
List<TransactionStatus> statusList = Lists.newArrayList(labelState);
return getTransactionIdByLabel(dbId, label, statusList);
} catch (UserException e) {
LOG.warn("Get transaction id by label " + label + " failed", e);
return null;
}
}

@Override
Expand Down Expand Up @@ -1688,7 +1696,14 @@ public List<List<String>> getDbTransInfoByLabelMatch(long dbId, String label) th

@Override
public List<List<String>> getSingleTranInfo(long dbId, long txnId) throws AnalysisException {
throw new AnalysisException(NOT_SUPPORTED_MSG);
List<List<String>> infos = new ArrayList<List<String>>();
TransactionState txnState = this.getTransactionState(dbId, txnId);
if (txnState == null) {
throw new AnalysisException("transaction with id " + txnId + " does not exist");
}
TransactionUtil.checkAuth(dbId, txnState);
infos.add(TransactionUtil.getTxnStateInfo(txnState, Lists.newArrayList()));
return infos;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.Config;
import org.apache.doris.common.DuplicatedRequestException;
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
import org.apache.doris.common.FeNameFormat;
import org.apache.doris.common.LabelAlreadyUsedException;
import org.apache.doris.common.LoadException;
Expand All @@ -50,12 +48,9 @@
import org.apache.doris.common.util.DebugUtil;
import org.apache.doris.common.util.InternalDatabaseUtil;
import org.apache.doris.common.util.MetaLockUtils;
import org.apache.doris.common.util.TimeUtils;
import org.apache.doris.datasource.InternalCatalog;
import org.apache.doris.event.DataChangeEvent;
import org.apache.doris.metric.MetricRepo;
import org.apache.doris.mtmv.MTMVUtil;
import org.apache.doris.mysql.privilege.PrivPredicate;
import org.apache.doris.persist.BatchRemoveTransactionsOperationV2;
import org.apache.doris.persist.CleanLabelOperationLog;
import org.apache.doris.persist.EditLog;
Expand Down Expand Up @@ -249,9 +244,7 @@ public List<List<String>> getTxnStateInfoList(boolean running, int limit) {
.sorted(TransactionState.TXN_ID_COMPARATOR)
.limit(limit)
.forEach(t -> {
List<String> info = Lists.newArrayList();
getTxnStateInfo(t, info);
infos.add(info);
infos.add(TransactionUtil.getTxnStateInfo(t, Lists.newArrayList()));
});
} finally {
readUnlock();
Expand Down Expand Up @@ -287,9 +280,7 @@ public List<List<String>> getTxnStateInfoList(TransactionStatus status) {
.filter(transactionState -> (transactionState.getTransactionStatus() == status))
.sorted(TransactionState.TXN_ID_COMPARATOR)
.forEach(t -> {
List<String> info = Lists.newArrayList();
getTxnStateInfo(t, info);
infos.add(info);
infos.add(TransactionUtil.getTxnStateInfo(t, Lists.newArrayList()));
});
} finally {
readUnlock();
Expand All @@ -309,33 +300,14 @@ public List<List<String>> getTxnStateInfoList(String labelRegex) {
.filter(transactionState -> (transactionState.getLabel().matches(labelRegex)))
.sorted(TransactionState.TXN_ID_COMPARATOR)
.forEach(t -> {
List<String> info = Lists.newArrayList();
getTxnStateInfo(t, info);
infos.add(info);
infos.add(TransactionUtil.getTxnStateInfo(t, Lists.newArrayList()));
});
} finally {
readUnlock();
}
return infos;
}

private void getTxnStateInfo(TransactionState txnState, List<String> info) {
info.add(String.valueOf(txnState.getTransactionId()));
info.add(txnState.getLabel());
info.add(txnState.getCoordinator().toString());
info.add(txnState.getTransactionStatus().name());
info.add(txnState.getSourceType().name());
info.add(TimeUtils.longToTimeString(txnState.getPrepareTime()));
info.add(TimeUtils.longToTimeString(txnState.getPreCommitTime()));
info.add(TimeUtils.longToTimeString(txnState.getCommitTime()));
info.add(TimeUtils.longToTimeString(txnState.getLastPublishVersionTime()));
info.add(TimeUtils.longToTimeString(txnState.getFinishTime()));
info.add(txnState.getReason());
info.add(String.valueOf(txnState.getErrorReplicas().size()));
info.add(String.valueOf(txnState.getCallbackId()));
info.add(String.valueOf(txnState.getTimeoutMs()));
info.add(txnState.getErrMsg());
}

public long beginTransaction(List<Long> tableIdList, String label, TUniqueId requestId,
TransactionState.TxnCoordinator coordinator, TransactionState.LoadJobSourceType sourceType,
Expand Down Expand Up @@ -2069,35 +2041,12 @@ public List<List<String>> getSingleTranInfo(long dbId, long txnId) throws Analys
List<List<String>> infos = new ArrayList<List<String>>();
readLock();
try {
Database db = Env.getCurrentInternalCatalog().getDbOrAnalysisException(dbId);
TransactionState txnState = unprotectedGetTransactionState(txnId);
if (txnState == null) {
throw new AnalysisException("transaction with id " + txnId + " does not exist");
}

if (ConnectContext.get() != null) {
// check auth
Set<Long> tblIds = txnState.getIdToTableCommitInfos().keySet();
for (Long tblId : tblIds) {
Table tbl = db.getTableNullable(tblId);
if (tbl != null) {
if (!Env.getCurrentEnv().getAccessManager()
.checkTblPriv(ConnectContext.get(), InternalCatalog.INTERNAL_CATALOG_NAME,
db.getFullName(),
tbl.getName(), PrivPredicate.SHOW)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_TABLEACCESS_DENIED_ERROR,
"SHOW TRANSACTION",
ConnectContext.get().getQualifiedUser(),
ConnectContext.get().getRemoteIP(),
db.getFullName() + ": " + tbl.getName());
}
}
}
}

List<String> info = Lists.newArrayList();
getTxnStateInfo(txnState, info);
infos.add(info);
TransactionUtil.checkAuth(dbId, txnState);
infos.add(TransactionUtil.getTxnStateInfo(txnState, Lists.newArrayList()));
} finally {
readUnlock();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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.transaction;

import org.apache.doris.catalog.Database;
import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.Table;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
import org.apache.doris.common.util.TimeUtils;
import org.apache.doris.datasource.InternalCatalog;
import org.apache.doris.mysql.privilege.PrivPredicate;
import org.apache.doris.qe.ConnectContext;

import java.util.List;
import java.util.Set;

public class TransactionUtil {

public static List<String> getTxnStateInfo(TransactionState txnState, List<String> info) {
info.add(String.valueOf(txnState.getTransactionId()));
info.add(txnState.getLabel());
info.add(txnState.getCoordinator().toString());
info.add(txnState.getTransactionStatus().name());
info.add(txnState.getSourceType().name());
info.add(TimeUtils.longToTimeString(txnState.getPrepareTime()));
info.add(TimeUtils.longToTimeString(txnState.getPreCommitTime()));
info.add(TimeUtils.longToTimeString(txnState.getCommitTime()));
info.add(TimeUtils.longToTimeString(txnState.getLastPublishVersionTime()));
info.add(TimeUtils.longToTimeString(txnState.getFinishTime()));
info.add(txnState.getReason());
info.add(String.valueOf(txnState.getErrorReplicas().size()));
info.add(String.valueOf(txnState.getCallbackId()));
info.add(String.valueOf(txnState.getTimeoutMs()));
info.add(txnState.getErrMsg());
return info;
}

public static void checkAuth(long dbId, TransactionState txnState) throws AnalysisException {
Database db = Env.getCurrentInternalCatalog().getDbOrAnalysisException(dbId);
if (ConnectContext.get() != null) {
// check auth
Set<Long> tblIds = txnState.getIdToTableCommitInfos().keySet();
for (Long tblId : tblIds) {
Table tbl = db.getTableNullable(tblId);
if (tbl != null) {
if (!Env.getCurrentEnv().getAccessManager()
.checkTblPriv(ConnectContext.get(), InternalCatalog.INTERNAL_CATALOG_NAME,
db.getFullName(),
tbl.getName(), PrivPredicate.SHOW)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_TABLEACCESS_DENIED_ERROR,
"SHOW TRANSACTION",
ConnectContext.get().getQualifiedUser(),
ConnectContext.get().getRemoteIP(),
db.getFullName() + ": " + tbl.getName());
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ excludeSuites = "000_the_start_sentinel_do_not_touch," + // keep this line as th
"test_refresh_mtmv," + // not supported yet
"test_report_version_missing," +
"test_set_partition_version," +
"test_show_transaction," + // not supported yet
"test_spark_load," +
"test_index_lowercase_fault_injection," +
"test_index_compaction_failure_injection," +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ suite("test_show_transaction", "p0") {
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}' """
assertTrue(res.size() != 0)
print("show transaction result : " + res)
def reslike = sql_return_maparray """ show transaction where label like 'label_test_show_transaction_${uuid}%' """
assertTrue(res.equals(reslike))
if (!isCloudMode()) {
def reslike = sql_return_maparray """ show transaction where label like 'label_test_show_transaction_${uuid}%' """
assertTrue(res.equals(reslike))
}
}

0 comments on commit 08f9da3

Please sign in to comment.