Browse Source

Fix write audit log error will cause the request failed (#15868)

3.2.2-release-bak
Wenjun Ruan 7 months ago committed by GitHub
parent
commit
a8bc23748d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 14
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/OperatorLogAspect.java
  2. 1
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/OperatorUtils.java
  3. 13
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/operator/AuditOperator.java
  4. 26
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/operator/BaseAuditOperator.java
  5. 10
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AuditService.java
  6. 8
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AuditServiceImpl.java

14
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/OperatorLogAspect.java

@ -18,9 +18,11 @@
package org.apache.dolphinscheduler.api.audit; package org.apache.dolphinscheduler.api.audit;
import org.apache.dolphinscheduler.api.audit.operator.AuditOperator; import org.apache.dolphinscheduler.api.audit.operator.AuditOperator;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.service.bean.SpringApplicationContext; import org.apache.dolphinscheduler.service.bean.SpringApplicationContext;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Map;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -54,8 +56,18 @@ public class OperatorLogAspect {
log.warn("Operation is null of method: {}", method.getName()); log.warn("Operation is null of method: {}", method.getName());
return point.proceed(); return point.proceed();
} }
long beginTime = System.currentTimeMillis();
Map<String, Object> paramsMap = OperatorUtils.getParamsMap(point, signature);
Result<?> result = (Result<?>) point.proceed();
try {
AuditOperator operator = SpringApplicationContext.getBean(operatorLog.auditType().getOperatorClass()); AuditOperator operator = SpringApplicationContext.getBean(operatorLog.auditType().getOperatorClass());
return operator.recordAudit(point, operation.description(), operatorLog.auditType()); long latency = System.currentTimeMillis() - beginTime;
operator.recordAudit(paramsMap, result, latency, operation, operatorLog);
} catch (Throwable throwable) {
log.error("Record audit log error", throwable);
}
return result;
} }
} }

1
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/OperatorUtils.java

@ -66,7 +66,6 @@ public class OperatorUtils {
auditLog.setOperationType(auditType.getAuditOperationType().getName()); auditLog.setOperationType(auditType.getAuditOperationType().getName());
auditLog.setDescription(apiDescription); auditLog.setDescription(apiDescription);
auditLog.setCreateTime(new Date()); auditLog.setCreateTime(new Date());
auditLogList.add(auditLog);
return auditLogList; return auditLogList;
} }

13
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/operator/AuditOperator.java

@ -17,11 +17,18 @@
package org.apache.dolphinscheduler.api.audit.operator; package org.apache.dolphinscheduler.api.audit.operator;
import org.apache.dolphinscheduler.api.audit.enums.AuditType; import org.apache.dolphinscheduler.api.audit.OperatorLog;
import org.apache.dolphinscheduler.api.utils.Result;
import org.aspectj.lang.ProceedingJoinPoint; import java.util.Map;
import io.swagger.v3.oas.annotations.Operation;
public interface AuditOperator { public interface AuditOperator {
Object recordAudit(ProceedingJoinPoint point, String describe, AuditType auditType) throws Throwable; void recordAudit(Map<String, Object> paramsMap,
Result<?> result,
long latency,
Operation operation,
OperatorLog operatorLog) throws Throwable;
} }

26
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/operator/BaseAuditOperator.java

@ -17,6 +17,7 @@
package org.apache.dolphinscheduler.api.audit.operator; package org.apache.dolphinscheduler.api.audit.operator;
import org.apache.dolphinscheduler.api.audit.OperatorLog;
import org.apache.dolphinscheduler.api.audit.OperatorUtils; import org.apache.dolphinscheduler.api.audit.OperatorUtils;
import org.apache.dolphinscheduler.api.audit.enums.AuditType; import org.apache.dolphinscheduler.api.audit.enums.AuditType;
import org.apache.dolphinscheduler.api.service.AuditService; import org.apache.dolphinscheduler.api.service.AuditService;
@ -31,12 +32,11 @@ import java.util.Map;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import io.swagger.v3.oas.annotations.Operation;
@Service @Service
@Slf4j @Slf4j
@ -46,26 +46,27 @@ public abstract class BaseAuditOperator implements AuditOperator {
private AuditService auditService; private AuditService auditService;
@Override @Override
public Object recordAudit(ProceedingJoinPoint point, String describe, AuditType auditType) throws Throwable { public void recordAudit(Map<String, Object> paramsMap,
long beginTime = System.currentTimeMillis(); Result<?> result,
long latency,
Operation operation,
OperatorLog operatorLog) {
MethodSignature signature = (MethodSignature) point.getSignature(); AuditType auditType = operatorLog.auditType();
Map<String, Object> paramsMap = OperatorUtils.getParamsMap(point, signature);
User user = OperatorUtils.getUser(paramsMap); User user = OperatorUtils.getUser(paramsMap);
if (user == null) { if (user == null) {
log.error("user is null"); log.error("user is null");
return point.proceed(); return;
} }
List<AuditLog> auditLogList = OperatorUtils.buildAuditLogList(describe, auditType, user); List<AuditLog> auditLogList = OperatorUtils.buildAuditLogList(operation.description(), auditType, user);
setRequestParam(auditType, auditLogList, paramsMap); setRequestParam(auditType, auditLogList, paramsMap);
Result<?> result = (Result<?>) point.proceed();
if (OperatorUtils.resultFail(result)) { if (OperatorUtils.resultFail(result)) {
log.error("request fail, code {}", result.getCode()); log.error("request fail, code {}", result.getCode());
return result; return;
} }
setObjectIdentityFromReturnObject(auditType, result, auditLogList); setObjectIdentityFromReturnObject(auditType, result, auditLogList);
@ -73,10 +74,9 @@ public abstract class BaseAuditOperator implements AuditOperator {
modifyAuditOperationType(auditType, paramsMap, auditLogList); modifyAuditOperationType(auditType, paramsMap, auditLogList);
modifyAuditObjectType(auditType, paramsMap, auditLogList); modifyAuditObjectType(auditType, paramsMap, auditLogList);
long latency = System.currentTimeMillis() - beginTime; auditLogList.forEach(auditLog -> auditLog.setLatency(latency));
auditService.addAudit(auditLogList, latency); auditLogList.forEach(auditLog -> auditService.addAudit(auditLog));
return result;
} }
protected void setRequestParam(AuditType auditType, List<AuditLog> auditLogList, Map<String, Object> paramsMap) { protected void setRequestParam(AuditType auditType, List<AuditLog> auditLogList, Map<String, Object> paramsMap) {

10
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AuditService.java

@ -21,8 +21,6 @@ import org.apache.dolphinscheduler.api.dto.AuditDto;
import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.PageInfo;
import org.apache.dolphinscheduler.dao.entity.AuditLog; import org.apache.dolphinscheduler.dao.entity.AuditLog;
import java.util.List;
/** /**
* audit information service * audit information service
*/ */
@ -35,14 +33,6 @@ public interface AuditService {
*/ */
void addAudit(AuditLog auditLog); void addAudit(AuditLog auditLog);
/**
* add audit by list
*
* @param auditLogList auditLog list
* @param latency api latency milliseconds
*/
void addAudit(List<AuditLog> auditLogList, long latency);
/** /**
* query audit log list * query audit log list
* *

8
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AuditServiceImpl.java

@ -57,14 +57,6 @@ public class AuditServiceImpl extends BaseServiceImpl implements AuditService {
auditLogMapper.insert(auditLog); auditLogMapper.insert(auditLog);
} }
@Override
public void addAudit(List<AuditLog> auditLogList, long latency) {
auditLogList.forEach(auditLog -> {
auditLog.setLatency(latency);
addAudit(auditLog);
});
}
/** /**
* query audit log paging * query audit log paging
* *

Loading…
Cancel
Save