Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Commit

Permalink
Bug 1115725 - Operation history continues to grow unbound with no eas…
Browse files Browse the repository at this point in the history
…y way to purge old history

Fix an issue on Oracle, no ID conversion needed, Hibernate does it

Also, some code cleanup

Cherry picked from 584e516
  • Loading branch information
tsegismont committed Jul 24, 2014
1 parent 0a4c817 commit c466c9e
Showing 1 changed file with 67 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* RHQ Management Platform
* Copyright (C) 2005-2013 Red Hat, Inc.
* Copyright (C) 2005-2014 Red Hat, Inc.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
Expand All @@ -16,6 +16,7 @@
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/

package org.rhq.enterprise.server.operation;

import static javax.ejb.TransactionAttributeType.NEVER;
Expand All @@ -35,6 +36,7 @@
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -387,7 +389,9 @@ public GroupOperationSchedule scheduleGroupOperation(Subject subject, int compat
Date next = scheduler.scheduleJob(jobDetail, trigger);
GroupOperationSchedule newSchedule = getGroupOperationSchedule(subject, jobDetail);

LOG.debug("Scheduled group operation [" + newSchedule + "] - next fire time is [" + next + "]");
if (LOG.isDebugEnabled()) {
LOG.debug("Scheduled group operation [" + newSchedule + "] - next fire time is [" + next + "]");
}

return newSchedule;
}
Expand Down Expand Up @@ -434,8 +438,6 @@ public void unscheduleResourceOperation(Subject subject, String jobId, int resou
} catch (Exception e) {
throw new UnscheduleException(e);
}

return;
}

@Override
Expand Down Expand Up @@ -482,34 +484,33 @@ public void unscheduleGroupOperation(Subject subject, String jobId, int resource
} catch (Exception e) {
throw new UnscheduleException(e);
}

return;
}

@Override
public void deleteOperationScheduleEntity(ScheduleJobId jobId) {
try {
OperationScheduleEntity doomed = findOperationScheduleEntity(jobId);
if (doomed != null) {
LOG.debug("Deleting schedule entity: " + jobId);
if (LOG.isDebugEnabled()) {
LOG.debug("Deleting schedule entity: " + jobId);
}
entityManager.remove(doomed);
} else {
LOG.info("Asked to delete unknown schedule - ignoring: " + jobId);
}
} catch (NoResultException nre) {
LOG.info("Asked to delete unknown schedule - ignoring: " + jobId);
}

return;
}

@Override
public void updateOperationScheduleEntity(ScheduleJobId jobId, long nextFireTime) {
// sched will be managed - just setting the property is enough for it to be committed
OperationScheduleEntity sched = findOperationScheduleEntity(jobId);
sched.setNextFireTime(nextFireTime);
LOG.debug("Scheduled job has a new next-fire-time: " + sched);
return;
if (LOG.isDebugEnabled()) {
LOG.debug("Scheduled job has a new next-fire-time: " + sched);
}
}

@Override
Expand Down Expand Up @@ -720,7 +721,7 @@ public GroupOperationSchedule getGroupOperationSchedule(Subject subject, JobDeta
if (!executionOrder.contains(memberResource)) {
executionOrder.add(memberResource);
}
} else {
} else if (LOG.isDebugEnabled()) {
LOG.debug("Resource [" + resourceId
+ "] looks like it was deleted and is no longer a member of group [" + group
+ "] - ignoring it");
Expand Down Expand Up @@ -1068,8 +1069,6 @@ public void cancelOperationHistory(Subject subject, int historyId, boolean ignor
} else {
cancelResourceOperation(subject, (ResourceOperationHistory) doomedHistory, ignoreAgentErrors);
}

return;
}

/**
Expand Down Expand Up @@ -1123,8 +1122,6 @@ private void cancelGroupOperation(Subject subject, GroupOperationHistory doomedH
doomedHistory.setStatus(OperationRequestStatus.CANCELED); // we expect doomedHistory to be jpa attached
notifyAlertConditionCacheManager("cancelGroupOperation", doomedHistory);
}

return;
}

/**
Expand Down Expand Up @@ -1180,8 +1177,10 @@ private CancelResults cancelResourceOperation(Subject subject, ResourceOperation
// "success" message is sent with guaranteed delivery - so the crash had to occur in just the right
// split second of time for that to occur.

LOG.debug("Agent already finished the operation so it cannot be canceled. " + "agent=[" + agent
+ "], op=[" + doomedHistory + "]");
if (LOG.isDebugEnabled()) {
LOG.debug("Agent already finished the operation so it cannot be canceled. " + "agent=[" + agent
+ "], op=[" + doomedHistory + "]");
}
break;
}

Expand All @@ -1190,8 +1189,10 @@ private CancelResults cancelResourceOperation(Subject subject, ResourceOperation
// got a chance to tell its plugin to execute the operation; it just dequeued and threw the op away.
// Therefore, we can really say it was canceled.
canceled = true;
LOG.debug("Cancel successful. Agent dequeued the operation and will not invoke it. " + "agent=["
+ agent + "], op=[" + doomedHistory + "]");
if (LOG.isDebugEnabled()) {
LOG.debug("Cancel successful. Agent dequeued the operation and will not invoke it. "
+ "agent=[" + agent + "], op=[" + doomedHistory + "]");
}
break;
}

Expand All @@ -1203,8 +1204,10 @@ private CancelResults cancelResourceOperation(Subject subject, ResourceOperation
// We still flag the operation as canceled to indicate that the agent did attempt to cancel it;
// hopefully, the plugin did the right thing.
canceled = true;
LOG.debug("Agent attempted to cancel the operation - it interrupted the operation while it was running. "
+ "agent=[" + agent + "], op=[" + doomedHistory + "]");
if (LOG.isDebugEnabled()) {
LOG.debug("Agent attempted to cancel the operation - it interrupted the operation while it was running. "
+ "agent=[" + agent + "], op=[" + doomedHistory + "]");
}
break;
}

Expand All @@ -1218,8 +1221,10 @@ private CancelResults cancelResourceOperation(Subject subject, ResourceOperation
// the operation. In this case, we'll allow its state to be canceled since the most probably reason
// for this is the agent was recycled and has no idea what this operation is or was.
canceled = true;
LOG.debug("Agent does not know about the operation. Nothing to cancel. " + "agent=[" + agent
+ "], op=[" + doomedHistory + "]");
if (LOG.isDebugEnabled()) {
LOG.debug("Agent does not know about the operation. Nothing to cancel. " + "agent=[" + agent
+ "], op=[" + doomedHistory + "]");
}
break;
}

Expand Down Expand Up @@ -1285,10 +1290,9 @@ public void deleteOperationHistory(Subject subject, int historyId, boolean purge
}

deleteOperationHistory_helper(doomedHistory.getId());

return;
}

@Override
@TransactionAttribute(TransactionAttributeType.NEVER)
public int purgeOperationHistory(Date purgeBeforeTime) {
int totalPurged = 0;
Expand Down Expand Up @@ -1317,27 +1321,26 @@ public int purgeOperationHistory(Date purgeBeforeTime) {
return totalPurged;
}

@Override
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public int purgeOperationHistoryInNewTransaction(Date purgeBeforeTime, boolean isGroupPurge, int limit) {

String entity = isGroupPurge ? "GroupOperationHistory" : "ResourceOperationHistory";
String limitClause = DatabaseTypeFactory.getDefaultDatabaseType().getLimitClause(limit);
String query = "SELECT h.id FROM " + entity + " h WHERE h.createdTime < :purgeBeforeTime";
Query q = entityManager.createQuery(query);
q.setParameter("purgeBeforeTime", purgeBeforeTime.getTime());
q.setMaxResults(limit);

List rs = q.getResultList();
for (Object r : rs) {
int id = DatabaseTypeFactory.getDefaultDatabaseType().getInteger(r);
String queryString = "SELECT h.id FROM " + entity + " h WHERE h.createdTime < :purgeBeforeTime";
TypedQuery<Integer> query = entityManager.createQuery(queryString, Integer.class);
query.setParameter("purgeBeforeTime", purgeBeforeTime.getTime());
query.setMaxResults(limit);

List<Integer> idList = query.getResultList();
for (Integer id : idList) {
if (isGroupPurge) {
deleteOperationHistory(subjectManager.getOverlord(), id, true);
} else {
deleteOperationHistory_helper(id);
}
}

return rs.size();
return idList.size();
}

@SuppressWarnings("unchecked")
Expand All @@ -1361,8 +1364,6 @@ private void deleteOperationHistory_helper(int historyId) {
allConfigurationIdsToDelete.addAll(historyArgumentConfigurationIds);
allConfigurationIdsToDelete.addAll(historyResultConfigurationIds);
configurationManager.deleteConfigurations(allConfigurationIdsToDelete);

return;
}

@Override
Expand Down Expand Up @@ -1492,10 +1493,14 @@ public boolean isResourceOperationSupported(Subject subject, int resourceId) {
} catch (PermissionException e) {
// notice we caught this exception before propogating it up to the EJB layer, so
// our transaction is not rolled back
LOG.debug("isOperationSupported: User cannot control resource: " + resourceId);
if (LOG.isDebugEnabled()) {
LOG.debug("isOperationSupported: User cannot control resource: " + resourceId);
}
return false;
} catch (Exception e) {
LOG.debug("isOperationSupported: Resource does not exist: " + resourceId);
if (LOG.isDebugEnabled()) {
LOG.debug("isOperationSupported: Resource does not exist: " + resourceId);
}
return false;
}

Expand All @@ -1514,17 +1519,23 @@ public boolean isGroupOperationSupported(Subject subject, int resourceGroupId) {
return false;
}
} catch (ResourceGroupNotFoundException e) {
LOG.debug("isGroupOperationSupported: group does not exist: " + resourceGroupId);
if (LOG.isDebugEnabled()) {
LOG.debug("isGroupOperationSupported: group does not exist: " + resourceGroupId);
}
return false;
} catch (PermissionException pe) {
// notice we caught this exception before propogating it up to the EJB layer, so
// our transaction is not rolled back
LOG.debug("isGroupOperationSupported: User cannot view (and thus) control group: " + resourceGroupId);
if (LOG.isDebugEnabled()) {
LOG.debug("isGroupOperationSupported: User cannot view (and thus) control group: " + resourceGroupId);
}
return false;
}

if (!authorizationManager.hasGroupPermission(subject, Permission.CONTROL, group.getId())) {
LOG.debug("isGroupOperationSupported: User cannot control group: " + group);
if (LOG.isDebugEnabled()) {
LOG.debug("isGroupOperationSupported: User cannot control group: " + group);
}
return false;
}

Expand All @@ -1539,8 +1550,10 @@ public void checkForTimedOutOperations(Subject subject) {
LOG.debug("Begin scanning for timed out operation histories");

if (!authorizationManager.isOverlord(subject)) {
LOG.debug("Unauthorized user " + subject + " tried to execute checkForTimedOutOperations: "
+ "only the overlord may execute this system operation");
if (LOG.isDebugEnabled()) {
LOG.debug("Unauthorized user " + subject + " tried to execute checkForTimedOutOperations: "
+ "only the overlord may execute this system operation");
}
return;
}

Expand Down Expand Up @@ -1944,8 +1957,6 @@ public void checkForCompletedGroupOperation(int historyId) {
notifyAlertConditionCacheManager("checkForCompletedGroupOperation", groupHistory);
}
}

return;
}

/**
Expand Down Expand Up @@ -2075,7 +2086,9 @@ private String createJobGroupName(ResourceGroup group) {
@Override
@Nullable
public ResourceOperationHistory getLatestCompletedResourceOperation(Subject subject, int resourceId) {
LOG.debug("Getting latest completed operation for resource [" + resourceId + "]");
if (LOG.isDebugEnabled()) {
LOG.debug("Getting latest completed operation for resource [" + resourceId + "]");
}

ResourceOperationHistory result;

Expand All @@ -2095,7 +2108,9 @@ public ResourceOperationHistory getLatestCompletedResourceOperation(Subject subj
@Override
@Nullable
public ResourceOperationHistory getOldestInProgressResourceOperation(Subject subject, int resourceId) {
LOG.debug("Getting oldest in-progress operation for resource [" + resourceId + "]");
if (LOG.isDebugEnabled()) {
LOG.debug("Getting oldest in-progress operation for resource [" + resourceId + "]");
}

ResourceOperationHistory result;

Expand Down Expand Up @@ -2145,8 +2160,9 @@ public OperationDefinition getOperationDefinitionByResourceTypeAndName(int resou

private void notifyAlertConditionCacheManager(String callingMethod, OperationHistory operationHistory) {
AlertConditionCacheStats stats = alertConditionCacheManager.checkConditions(operationHistory);

LOG.debug(callingMethod + ": " + stats.toString());
if (LOG.isDebugEnabled()) {
LOG.debug(callingMethod + ": " + stats.toString());
}
}

/**
Expand Down

0 comments on commit c466c9e

Please sign in to comment.