Skip to content

Commit

Permalink
[enhance](cloud)support SHOW-TRANSACTION
Browse files Browse the repository at this point in the history
  • Loading branch information
cjj2010 committed Oct 12, 2024
1 parent 8fde3f4 commit a8504d6
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 60 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 @@ -1704,8 +1705,8 @@ public List<List<String>> getSingleTranInfo(long dbId, long txnId) throws Analys
if (txnState == null) {
throw new AnalysisException("transaction with id " + txnId + " does not exist");
}
TxnUtil.checkAuth(dbId, txnState);
infos.add(TxnUtil.getTxnStateInfo(txnState, Lists.newArrayList()));
TransactionUtil.checkAuth(dbId, txnState);
infos.add(TransactionUtil.getTxnStateInfo(txnState, Lists.newArrayList()));
return infos;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@

package org.apache.doris.cloud.transaction;

import org.apache.doris.catalog.Database;
import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.Table;
import org.apache.doris.cloud.proto.Cloud.RLTaskTxnCommitAttachmentPB;
import org.apache.doris.cloud.proto.Cloud.RoutineLoadProgressPB;
import org.apache.doris.cloud.proto.Cloud.TxnCommitAttachmentPB;
Expand All @@ -31,19 +28,12 @@
import org.apache.doris.cloud.proto.Cloud.TxnInfoPB;
import org.apache.doris.cloud.proto.Cloud.TxnSourceTypePB;
import org.apache.doris.cloud.proto.Cloud.UniqueIdPB;
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.load.EtlStatus;
import org.apache.doris.load.FailMsg;
import org.apache.doris.load.loadv2.JobState;
import org.apache.doris.load.loadv2.LoadJobFinalOperation;
import org.apache.doris.load.routineload.KafkaProgress;
import org.apache.doris.load.routineload.RLTaskTxnCommitAttachment;
import org.apache.doris.mysql.privilege.PrivPredicate;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.thrift.TEtlState;
import org.apache.doris.thrift.TUniqueId;
import org.apache.doris.transaction.TransactionState;
Expand All @@ -58,7 +48,6 @@

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

public class TxnUtil {
private static final Logger LOG = LogManager.getLogger(TxnUtil.class);
Expand Down Expand Up @@ -378,45 +367,4 @@ public static TransactionState transactionStateFromPb(TxnInfoPB txnInfo) {
return transactionState;
}

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 @@ -33,7 +33,6 @@
import org.apache.doris.catalog.Tablet;
import org.apache.doris.catalog.TabletInvertedIndex;
import org.apache.doris.catalog.TabletMeta;
import org.apache.doris.cloud.transaction.TxnUtil;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.Config;
import org.apache.doris.common.DuplicatedRequestException;
Expand Down Expand Up @@ -245,7 +244,7 @@ public List<List<String>> getTxnStateInfoList(boolean running, int limit) {
.sorted(TransactionState.TXN_ID_COMPARATOR)
.limit(limit)
.forEach(t -> {
infos.add(TxnUtil.getTxnStateInfo(t, Lists.newArrayList()));
infos.add(TransactionUtil.getTxnStateInfo(t, Lists.newArrayList()));
});
} finally {
readUnlock();
Expand Down Expand Up @@ -281,7 +280,7 @@ public List<List<String>> getTxnStateInfoList(TransactionStatus status) {
.filter(transactionState -> (transactionState.getTransactionStatus() == status))
.sorted(TransactionState.TXN_ID_COMPARATOR)
.forEach(t -> {
infos.add(TxnUtil.getTxnStateInfo(t, Lists.newArrayList()));
infos.add(TransactionUtil.getTxnStateInfo(t, Lists.newArrayList()));
});
} finally {
readUnlock();
Expand All @@ -301,7 +300,7 @@ public List<List<String>> getTxnStateInfoList(String labelRegex) {
.filter(transactionState -> (transactionState.getLabel().matches(labelRegex)))
.sorted(TransactionState.TXN_ID_COMPARATOR)
.forEach(t -> {
infos.add(TxnUtil.getTxnStateInfo(t, Lists.newArrayList()));
infos.add(TransactionUtil.getTxnStateInfo(t, Lists.newArrayList()));
});
} finally {
readUnlock();
Expand Down Expand Up @@ -2046,8 +2045,8 @@ public List<List<String>> getSingleTranInfo(long dbId, long txnId) throws Analys
if (txnState == null) {
throw new AnalysisException("transaction with id " + txnId + " does not exist");
}
TxnUtil.checkAuth(dbId, txnState);
infos.add(TxnUtil.getTxnStateInfo(txnState, Lists.newArrayList()));
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());
}
}
}
}
}
}

0 comments on commit a8504d6

Please sign in to comment.