Skip to content

Commit

Permalink
repo-sqale: rudimentary audit insert without infinite loop on exception
Browse files Browse the repository at this point in the history
  • Loading branch information
virgo47 committed Aug 27, 2021
1 parent add5a09 commit da32e67
Showing 1 changed file with 59 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@

import java.time.Instant;
import java.util.*;
import java.util.Map.Entry;
import java.util.function.BiFunction;
import javax.annotation.PreDestroy;
import javax.xml.datatype.Duration;

import com.querydsl.sql.ColumnMetadata;
import com.google.common.base.Strings;
import com.querydsl.sql.SQLQuery;
import com.querydsl.sql.dml.DefaultMapper;
import com.querydsl.sql.dml.SQLInsertClause;
Expand Down Expand Up @@ -94,41 +93,41 @@ public SqlRepoContext getSqlRepoContext() {
}

@Override
public void audit(AuditEventRecord record, Task task, OperationResult result) {
public void audit(AuditEventRecord record, Task task, OperationResult parentResult) {
Objects.requireNonNull(record, "Audit event record must not be null.");
Objects.requireNonNull(task, "Task must not be null.");

long opHandle = registerOperationStart(OP_AUDIT);
while (true) {
try {
auditAttempt(record);
return;
} catch (RuntimeException ex) {
// TODO
// attempt = baseHelper.logOperationAttempt(null, OP_AUDIT, attempt, ex, result);
// pm.registerOperationNewAttempt(opHandle, attempt);
} finally {
registerOperationFinish(opHandle, 1);
}
OperationResult operationResult = parentResult.createSubresult(OP_NAME_PREFIX + OP_AUDIT);

try {
auditExecute(record);
} catch (RuntimeException e) {
throw handledGeneralException(e, operationResult);
} catch (Throwable t) {
operationResult.recordFatalError(t);
throw t;
} finally {
operationResult.computeStatusIfUnknown();
}
}

private void auditAttempt(AuditEventRecord record) {
private void auditExecute(AuditEventRecord record) {
long opHandle = registerOperationStart(OP_AUDIT);
try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) {
try {
long recordId = insertAuditEventRecord(jdbcSession, record);

Collection<MAuditDelta> deltas =
insertAuditDeltas(jdbcSession, recordId, record.getDeltas());
insertChangedItemPaths(jdbcSession, recordId, deltas);

insertProperties(jdbcSession, recordId, record.getProperties());
insertReferences(jdbcSession, recordId, record.getReferences());
insertResourceOids(jdbcSession, recordId, record.getResourceOids());
jdbcSession.commit();
} catch (RuntimeException ex) {
jdbcSession.handleGeneralException(ex, null);
}
long recordId = insertAuditEventRecord(jdbcSession, record);

/* TODO
Collection<MAuditDelta> deltas =
insertAuditDeltas(jdbcSession, recordId, record.getDeltas());
insertChangedItemPaths(jdbcSession, recordId, deltas);
insertProperties(jdbcSession, recordId, record.getProperties());
insertReferences(jdbcSession, recordId, record.getReferences());
insertResourceOids(jdbcSession, recordId, record.getResourceOids());
*/
jdbcSession.commit();
} finally {
registerOperationFinish(opHandle, 1);
}
}

Expand All @@ -144,6 +143,7 @@ private Long insertAuditEventRecord(
MAuditEventRecord aerBean = aerMapping.toRowObject(record);
SQLInsertClause insert = jdbcSession.newInsert(aer).populate(aerBean);

/* TODO or ext?
Map<String, ColumnMetadata> customColumns = aerMapping.getExtensionColumns();
for (Entry<String, String> property : record.getCustomColumnProperty().entrySet()) {
String propertyName = property.getKey();
Expand All @@ -154,6 +154,7 @@ private Long insertAuditEventRecord(
// Like insert.set, but that one is too parameter-type-safe for our generic usage here.
insert.columns(aer.getPath(propertyName)).values(property.getValue());
}
*/

return insert.executeWithKey(aer.id);
}
Expand Down Expand Up @@ -712,4 +713,32 @@ private long registerOperationStart(String kind) {
private void registerOperationFinish(long opHandle, int attempt) {
performanceMonitor.registerOperationFinish(opHandle, attempt);
}

// TODO copied from SqaleRepositoryService, will we create some SqaleRepoBase?

/**
* Handles exception outside of transaction - this does not handle transactional problems.
* Returns {@link SystemException}, call with `throw` keyword.
*/
private SystemException handledGeneralException(
@NotNull Throwable ex, OperationResult operationResult) {
// TODO reconsider this whole mechanism including isFatalException decision
LOGGER.error("General checked exception occurred.", ex);
recordException(ex, operationResult,
sqlRepoContext.getJdbcRepositoryConfiguration().isFatalException(ex));

return ex instanceof SystemException
? (SystemException) ex
: new SystemException(ex.getMessage(), ex);
}

private void recordException(
@NotNull Throwable ex, OperationResult operationResult, boolean fatal) {
String message = Strings.isNullOrEmpty(ex.getMessage()) ? "null" : ex.getMessage();

// non-fatal errors will NOT be put into OperationResult, not to confuse the user
if (operationResult != null && fatal) {
operationResult.recordFatalError(message, ex);
}
}
}

0 comments on commit da32e67

Please sign in to comment.