use of com.evolveum.midpoint.util.Holder in project midpoint by Evolveum.
the class SqlAuditServiceImpl method cleanupAuditMaxAge.
private void cleanupAuditMaxAge(CleanupPolicyType policy, OperationResult parentResult) {
if (policy.getMaxAge() == null) {
return;
}
final String operation = "deletingMaxAge";
SqlPerformanceMonitorImpl pm = getPerformanceMonitor();
long opHandle = pm.registerOperationStart(OP_CLEANUP_AUDIT_MAX_AGE, AuditEventRecord.class);
int attempt = 1;
Duration duration = policy.getMaxAge();
if (duration.getSign() > 0) {
duration = duration.negate();
}
Date minValue = new Date();
duration.addTo(minValue);
checkTemporaryTablesSupport();
long start = System.currentTimeMillis();
boolean first = true;
Holder<Integer> totalCountHolder = new Holder<>(0);
try {
while (true) {
try {
LOGGER.info("{} audit cleanup, deleting up to {} (duration '{}'), batch size {}{}.", first ? "Starting" : "Continuing with ", minValue, duration, CLEANUP_AUDIT_BATCH_SIZE, first ? "" : ", up to now deleted " + totalCountHolder.getValue() + " entries");
first = false;
int count;
do {
// the following method may restart due to concurrency
// (or any other) problem - in any iteration
long batchStart = System.currentTimeMillis();
LOGGER.debug("Starting audit cleanup batch, deleting up to {} (duration '{}')," + " batch size {}, up to now deleted {} entries.", minValue, duration, CLEANUP_AUDIT_BATCH_SIZE, totalCountHolder.getValue());
count = batchDeletionAttempt((session, tempTable) -> selectRecordsByMaxAge(session, tempTable, minValue), totalCountHolder, batchStart, parentResult);
} while (count > 0);
return;
} catch (RuntimeException ex) {
attempt = baseHelper.logOperationAttempt(null, operation, attempt, ex, parentResult);
pm.registerOperationNewAttempt(opHandle, attempt);
}
}
} finally {
pm.registerOperationFinish(opHandle, attempt);
LOGGER.info("Audit cleanup based on age finished; deleted {} entries in {} seconds.", totalCountHolder.getValue(), (System.currentTimeMillis() - start) / 1000L);
}
}
use of com.evolveum.midpoint.util.Holder in project midpoint by Evolveum.
the class SqlAuditServiceImpl method cleanupAuditMaxRecords.
private void cleanupAuditMaxRecords(CleanupPolicyType policy, OperationResult parentResult) {
if (policy.getMaxRecords() == null) {
return;
}
final String operation = "deletingMaxRecords";
SqlPerformanceMonitorImpl pm = getPerformanceMonitor();
long opHandle = pm.registerOperationStart(OP_CLEANUP_AUDIT_MAX_RECORDS, AuditEventRecord.class);
int attempt = 1;
int recordsToKeep = policy.getMaxRecords();
checkTemporaryTablesSupport();
long start = System.currentTimeMillis();
boolean first = true;
Holder<Integer> totalCountHolder = new Holder<>(0);
try {
while (true) {
try {
LOGGER.info("{} audit cleanup, keeping at most {} records, batch size {}{}.", first ? "Starting" : "Continuing with ", recordsToKeep, CLEANUP_AUDIT_BATCH_SIZE, first ? "" : ", up to now deleted " + totalCountHolder.getValue() + " entries");
first = false;
int count;
do {
// the following method may restart due to concurrency
// (or any other) problem - in any iteration
long batchStart = System.currentTimeMillis();
LOGGER.debug("Starting audit cleanup batch, keeping at most {} records," + " batch size {}, up to now deleted {} entries.", recordsToKeep, CLEANUP_AUDIT_BATCH_SIZE, totalCountHolder.getValue());
count = batchDeletionAttempt((session, tempTable) -> selectRecordsByNumberToKeep(session, tempTable, recordsToKeep), totalCountHolder, batchStart, parentResult);
} while (count > 0);
return;
} catch (RuntimeException ex) {
attempt = baseHelper.logOperationAttempt(null, operation, attempt, ex, parentResult);
pm.registerOperationNewAttempt(opHandle, attempt);
}
}
} finally {
pm.registerOperationFinish(opHandle, attempt);
LOGGER.info("Audit cleanup based on record count finished; deleted {} entries in {} seconds.", totalCountHolder.getValue(), (System.currentTimeMillis() - start) / 1000L);
}
}
use of com.evolveum.midpoint.util.Holder in project midpoint by Evolveum.
the class IterativeActivityRun method getWorkBucket.
private WorkBucketType getWorkBucket(boolean initialRun, OperationResult result) throws ActivityRunException {
RunningTask task = taskRun.getRunningTask();
Holder<BucketProgressOverviewType> bucketProgressHolder = new Holder<>();
WorkBucketType bucket;
try {
GetBucketOperationOptions options = GetBucketOperationOptionsBuilder.anOptions().withDistributionDefinition(activity.getDefinition().getDistributionDefinition()).withFreeBucketWaitTime(FREE_BUCKET_WAIT_TIME).withCanRun(task::canRun).withExecuteInitialWait(initialRun).withImplicitSegmentationResolver(this).withIsScavenger(isScavenger(task)).withBucketProgressConsumer(bucketProgressHolder).build();
bucket = beans.bucketingManager.getWorkBucket(bucketingSituation.coordinatorTaskOid, bucketingSituation.workerTaskOid, activity.getPath(), options, getLiveBucketManagementStatistics(), result);
// We want to have the most current state of the running task.
task.refresh(result);
} catch (InterruptedException e) {
LOGGER.trace("InterruptedExecution in getWorkBucket for {}", task);
if (!task.canRun()) {
return null;
} else {
LoggingUtils.logUnexpectedException(LOGGER, "Unexpected InterruptedException in {}", e, task);
throw new SystemException("Unexpected InterruptedException: " + e.getMessage(), e);
}
} catch (Throwable t) {
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't allocate a work bucket for task {}", t, task);
throw new SystemException("Couldn't allocate a work bucket for task: " + t.getMessage(), t);
}
announceNumberOfBuckets(bucketProgressHolder.getValue(), result);
return bucket;
}
use of com.evolveum.midpoint.util.Holder in project midpoint by Evolveum.
the class ResourceObjectReferenceResolver method resolve.
/**
* Resolves a {@link ResourceObjectReferenceType}.
*
* @param useRawDefinition If true, object class definition is used (instead of object type definition).
* This is to avoid endless recursion when resolving the base context for object type.
*/
@Nullable
PrismObject<ShadowType> resolve(@NotNull ProvisioningContext ctx, @NotNull ResourceObjectReferenceType resourceObjectReference, boolean useRawDefinition, @NotNull String desc, @NotNull OperationResult result) throws ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
ObjectReferenceType shadowRef = resourceObjectReference.getShadowRef();
if (shadowRef != null && shadowRef.getOid() != null) {
if (resourceObjectReference.getResolutionFrequency() == null || resourceObjectReference.getResolutionFrequency() == ResourceObjectReferenceResolutionFrequencyType.ONCE) {
PrismObject<ShadowType> shadow = repositoryService.getObject(ShadowType.class, shadowRef.getOid(), null, result);
shadowsFacade.applyDefinition(shadow, ctx.getTask(), result);
return shadow;
}
} else if (resourceObjectReference.getResolutionFrequency() == ResourceObjectReferenceResolutionFrequencyType.NEVER) {
throw new ObjectNotFoundException("No shadowRef OID in " + desc + " and resolution frequency set to NEVER");
}
argCheck(resourceObjectReference.getObjectClass() != null, "No object class name in object reference in %s", desc);
QName objectClassName = QNameUtil.qualifyIfNeeded(resourceObjectReference.getObjectClass(), MidPointConstants.NS_RI);
ProvisioningContext subCtx = useRawDefinition ? ctx.spawnForObjectClassWithRawDefinition(objectClassName) : ctx.spawnForObjectClass(objectClassName);
subCtx.assertDefinition();
ObjectQuery refQuery = prismContext.getQueryConverter().createObjectQuery(ShadowType.class, resourceObjectReference.getFilter());
// No variables. At least not now. We expect that mostly constants will be used here.
VariablesMap variables = new VariablesMap();
ObjectQuery evaluatedRefQuery = ExpressionUtil.evaluateQueryExpressions(refQuery, variables, MiscSchemaUtil.getExpressionProfile(), expressionFactory, prismContext, desc, ctx.getTask(), result);
ObjectFilter baseFilter = ObjectQueryUtil.createResourceAndObjectClassFilter(ctx.getResource().getOid(), objectClassName, prismContext);
ObjectFilter filter = prismContext.queryFactory().createAnd(baseFilter, evaluatedRefQuery.getFilter());
ObjectQuery query = prismContext.queryFactory().createQuery(filter);
// TODO: implement "repo" search strategies, don't forget to apply definitions
Holder<PrismObject<ShadowType>> shadowHolder = new Holder<>();
ResultHandler<ShadowType> handler = (shadow, objResult) -> {
if (shadowHolder.getValue() != null) {
throw new IllegalStateException("More than one search results for " + desc);
}
shadowHolder.setValue(shadow);
return true;
};
shadowsFacade.searchObjectsIterative(subCtx, query, null, handler, result);
return shadowHolder.getValue();
}
use of com.evolveum.midpoint.util.Holder in project midpoint by Evolveum.
the class GetBucketOperation method reclaimWronglyAllocatedBuckets.
/**
* For each allocated work bucket we check if it is allocated to existing and non-closed child task.
*
* TODO use generalized transaction to obtain really current worker task state
* and then reclaim also from suspended workers
*/
private void reclaimWronglyAllocatedBuckets(OperationResult result) throws SchemaException, ObjectNotFoundException, ObjectAlreadyExistsException {
Set<String> liveWorkers = getLiveWorkers(result);
Holder<Integer> reclaimingHolder = new Holder<>(0);
plainRepositoryService.modifyObjectDynamically(TaskType.class, coordinatorTaskOid, null, task -> {
Collection<ItemDelta<?, ?>> modifications = new ArrayList<>();
ItemPath statePath = ActivityStateUtil.getStateItemPath(task.getActivityState(), activityPath);
List<WorkBucketType> buckets = getBuckets(task.getActivityState(), activityPath);
int reclaiming = 0;
for (WorkBucketType bucket : buckets) {
if (bucket.getState() == WorkBucketStateType.DELEGATED) {
String workerOid = getWorkerOid(bucket);
if (!liveWorkers.contains(workerOid)) {
LOGGER.info("Will try to reclaim wrongly allocated work bucket {} from worker task {}", bucket, workerOid);
modifications.addAll(bucketStateChangeDeltas(statePath, bucket, WorkBucketStateType.READY, null));
reclaiming++;
}
}
}
reclaimingHolder.setValue(reclaiming);
return modifications;
}, null, result);
if (reclaimingHolder.getValue() > 0) {
LOGGER.info("Reclaimed {} buckets in {}", reclaimingHolder.getValue(), coordinatorTaskOid);
} else {
LOGGER.debug("Reclaimed no buckets in {}", coordinatorTaskOid);
}
}
Aggregations