jt2594838 commented on code in PR #18569:
URL: https://github.com/apache/iotdb/pull/18569#discussion_r3913077080
##########
iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/client/DispatchLogHandler.java:
##########
@@ -172,4 +186,47 @@ private void completeBatch(Batch batch) {
// removeBatch
thread.updateSafelyDeletedSearchIndex();
}
+
+ private void recordTransferAttempt(boolean success, String errorCode,
Throwable error) {
+ try {
+ recordTransferAttempt(
+ thread.getImpl().getUserDataTransferAuditHandler(),
+ batch,
+ thread.getImpl().getThisNode().getEndpoint(),
+ thread.getPeer().getEndpoint(),
+ UserDataTransferProtectionMethod.fromTlsEnabled(
+ thread.getConfig().getRpc().isEnableSSL()),
+ success,
+ errorCode,
+ error);
+ } catch (RuntimeException ignored) {
+ // Audit recording must not affect consensus replication.
+ }
+ }
+
+ static void recordTransferAttempt(
+ UserDataTransferAuditHandler auditHandler,
+ Batch batch,
+ TEndPoint source,
+ TEndPoint target,
+ UserDataTransferProtectionMethod protectionMethod,
+ boolean success,
+ String errorCode,
+ Throwable error) {
+ try {
+ if (!batch.containsUserData() || !auditHandler.isEnabled()) {
+ return;
+ }
+ auditHandler.onAttempt(
+ new UserDataTransferAuditEvent(
+ source,
+ source,
+ target,
+ protectionMethod,
+ success,
+ errorCode != null ? errorCode : error == null ? null :
error.getClass().getName()));
+ } catch (RuntimeException ignored) {
+ // Audit recording must not affect consensus replication.
+ }
Review Comment:
Leave a warn log for debugging?
##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/audit/DataNodeUserDataTransferAuditor.java:
##########
@@ -0,0 +1,139 @@
+/*
+ * 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.iotdb.db.audit;
+
+import org.apache.iotdb.common.rpc.thrift.TEndPoint;
+import org.apache.iotdb.commons.audit.UserDataTransferAuditEvent;
+import org.apache.iotdb.commons.audit.UserDataTransferProtectionMethod;
+import org.apache.iotdb.commons.conf.CommonConfig;
+import org.apache.iotdb.commons.conf.CommonDescriptor;
+import org.apache.iotdb.commons.consensus.ConsensusGroupId;
+import org.apache.iotdb.commons.consensus.DataRegionId;
+import org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNode;
+import
org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNodeType;
+import org.apache.iotdb.commons.request.IConsensusRequest;
+import org.apache.iotdb.commons.schema.table.Audit;
+import org.apache.iotdb.consensus.common.request.ByteBufferConsensusRequest;
+import org.apache.iotdb.consensus.common.request.IoTConsensusRequest;
+import org.apache.iotdb.db.queryengine.plan.planner.plan.node.write.InsertNode;
+import org.apache.iotdb.db.storageengine.StorageEngine;
+import org.apache.iotdb.db.storageengine.dataregion.DataRegion;
+import org.apache.iotdb.db.storageengine.dataregion.wal.buffer.WALEntry;
+
+import javax.annotation.Nullable;
+
+public final class DataNodeUserDataTransferAuditor {
+
+ private static final CommonConfig COMMON_CONFIG =
CommonDescriptor.getInstance().getConfig();
+
+ private DataNodeUserDataTransferAuditor() {}
+
+ public static boolean isEnabled() {
+ return COMMON_CONFIG.isEnableAuditLog();
+ }
+
+ public static void record(
+ TEndPoint initiator,
+ TEndPoint source,
+ TEndPoint target,
+ boolean success,
+ @Nullable String errorCode,
+ @Nullable Throwable error) {
+ try {
+ if (!isEnabled()) {
+ return;
+ }
+ DNAuditLogger.getInstance()
+ .recordUserDataTransferAuditLog(
+ new UserDataTransferAuditEvent(
+ initiator,
+ source,
+ target,
+ UserDataTransferProtectionMethod.fromTlsEnabled(
+ COMMON_CONFIG.isEnableInternalSSL()),
+ success,
+ errorCode != null
+ ? errorCode
+ : error == null ? null : error.getClass().getName()));
+ } catch (RuntimeException ignored) {
+ // Audit recording must not affect user data transfer.
+ }
+ }
+
+ public static boolean containsUserData(
+ ConsensusGroupId consensusGroupId, IConsensusRequest request) {
+ if (!(consensusGroupId instanceof DataRegionId)) {
+ return false;
+ }
+ final DataRegion dataRegion =
+ StorageEngine.getInstance().getDataRegion((DataRegionId)
consensusGroupId);
+ return dataRegion != null &&
containsUserData(dataRegion.getDatabaseName(), request);
+ }
+
+ public static boolean containsUserData(ConsensusGroupId consensusGroupId) {
+ if (!(consensusGroupId instanceof DataRegionId)) {
+ return false;
+ }
+ final DataRegion dataRegion =
+ StorageEngine.getInstance().getDataRegion((DataRegionId)
consensusGroupId);
+ return dataRegion != null &&
containsUserData(dataRegion.getDatabaseName());
+ }
Review Comment:
Is it reasonable to exclude schema regions?
Attributes also seem to be part of the user data.
##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/audit/DataNodeUserDataTransferAuditor.java:
##########
@@ -0,0 +1,139 @@
+/*
+ * 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.iotdb.db.audit;
+
+import org.apache.iotdb.common.rpc.thrift.TEndPoint;
+import org.apache.iotdb.commons.audit.UserDataTransferAuditEvent;
+import org.apache.iotdb.commons.audit.UserDataTransferProtectionMethod;
+import org.apache.iotdb.commons.conf.CommonConfig;
+import org.apache.iotdb.commons.conf.CommonDescriptor;
+import org.apache.iotdb.commons.consensus.ConsensusGroupId;
+import org.apache.iotdb.commons.consensus.DataRegionId;
+import org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNode;
+import
org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNodeType;
+import org.apache.iotdb.commons.request.IConsensusRequest;
+import org.apache.iotdb.commons.schema.table.Audit;
+import org.apache.iotdb.consensus.common.request.ByteBufferConsensusRequest;
+import org.apache.iotdb.consensus.common.request.IoTConsensusRequest;
+import org.apache.iotdb.db.queryengine.plan.planner.plan.node.write.InsertNode;
+import org.apache.iotdb.db.storageengine.StorageEngine;
+import org.apache.iotdb.db.storageengine.dataregion.DataRegion;
+import org.apache.iotdb.db.storageengine.dataregion.wal.buffer.WALEntry;
+
+import javax.annotation.Nullable;
+
+public final class DataNodeUserDataTransferAuditor {
+
+ private static final CommonConfig COMMON_CONFIG =
CommonDescriptor.getInstance().getConfig();
+
+ private DataNodeUserDataTransferAuditor() {}
+
+ public static boolean isEnabled() {
+ return COMMON_CONFIG.isEnableAuditLog();
+ }
+
+ public static void record(
+ TEndPoint initiator,
+ TEndPoint source,
+ TEndPoint target,
+ boolean success,
+ @Nullable String errorCode,
+ @Nullable Throwable error) {
+ try {
+ if (!isEnabled()) {
+ return;
+ }
+ DNAuditLogger.getInstance()
+ .recordUserDataTransferAuditLog(
+ new UserDataTransferAuditEvent(
+ initiator,
+ source,
+ target,
+ UserDataTransferProtectionMethod.fromTlsEnabled(
+ COMMON_CONFIG.isEnableInternalSSL()),
+ success,
+ errorCode != null
+ ? errorCode
+ : error == null ? null : error.getClass().getName()));
+ } catch (RuntimeException ignored) {
+ // Audit recording must not affect user data transfer.
+ }
+ }
+
+ public static boolean containsUserData(
+ ConsensusGroupId consensusGroupId, IConsensusRequest request) {
+ if (!(consensusGroupId instanceof DataRegionId)) {
+ return false;
+ }
+ final DataRegion dataRegion =
+ StorageEngine.getInstance().getDataRegion((DataRegionId)
consensusGroupId);
+ return dataRegion != null &&
containsUserData(dataRegion.getDatabaseName(), request);
+ }
+
+ public static boolean containsUserData(ConsensusGroupId consensusGroupId) {
+ if (!(consensusGroupId instanceof DataRegionId)) {
+ return false;
+ }
+ final DataRegion dataRegion =
+ StorageEngine.getInstance().getDataRegion((DataRegionId)
consensusGroupId);
+ return dataRegion != null &&
containsUserData(dataRegion.getDatabaseName());
+ }
+
+ static boolean containsUserData(String database) {
+ return !Audit.isAuditDatabase(database);
+ }
+
+ static boolean containsUserData(String database, IConsensusRequest request) {
+ if (!containsUserData(database)) {
+ return false;
+ }
+ try {
+ final PlanNode planNode;
+ if (request instanceof PlanNode) {
+ planNode = (PlanNode) request;
+ } else if (request instanceof IoTConsensusRequest) {
+ planNode =
WALEntry.deserializeForConsensus(request.serializeToByteBuffer().duplicate());
+ } else if (request instanceof ByteBufferConsensusRequest) {
+ planNode =
PlanNodeType.deserialize(request.serializeToByteBuffer().duplicate());
Review Comment:
Will the cost be too high for these branches?
Can we somehow store and encode the flag in IConsensusRequest?
##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/protocol/iotconsensusv2/handler/IoTConsensusV2TabletBatchEventHandler.java:
##########
@@ -84,6 +85,16 @@ public void onComplete(final
TIoTConsensusV2BatchTransferResp response) {
response.getBatchResps().stream()
.map(TIoTConsensusV2TransferResp::getStatus)
.collect(Collectors.toList());
+ final TSStatus failedStatus =
+ status.stream()
+ .filter(tsStatus -> tsStatus.getCode() !=
TSStatusCode.SUCCESS_STATUS.getStatusCode())
+ .findFirst()
+ .orElse(null);
+ connector.recordUserDataTransferAudit(
+ failedStatus == null,
+ failedStatus == null ? null : String.valueOf(failedStatus.getCode()),
+ null);
+ transferAuditRecorded = true;
Review Comment:
Not sure if it is ok to report only the first failure.
Maybe we should conclude/concat the other failures too?
##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/protocol/iotconsensusv2/handler/IoTConsensusV2TabletBatchEventHandler.java:
##########
@@ -118,6 +129,10 @@ public void onComplete(final
TIoTConsensusV2BatchTransferResp response) {
@Override
public void onError(final Exception exception) {
+ if (!transferAuditRecorded) {
+ connector.recordUserDataTransferAudit(false, null, exception);
+ transferAuditRecorded = true;
+ }
Review Comment:
The same here, not sure if we should only record the first one.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]