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

Commit

Permalink
Bug 1136751 - drift detection interval for individual drift definitio…
Browse files Browse the repository at this point in the history
…n is *always* ignored and value from agent config is used

Fix issues with previous commit.

Prevent the DriftDetector from running in infinite loop:
* when the queue is full of disabled schedules
* when a previous snapshot file exists

(cherry picked from commit 487cc7c)
Signed-off-by: Thomas Segismont <tsegismo@redhat.com>
  • Loading branch information
tsegismont committed Oct 10, 2014
1 parent dfee834 commit 5467553
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* RHQ Management Platform
* Copyright (C) 2005-2014 Red Hat, Inc.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* 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.core.pc.drift;

/**
* Comparator for a {@link java.util.PriorityQueue} of {@link DriftDetectionSchedule} instances. Disabled schedules go
* at the end of the queue, enabled schedules are ordered by nextScan property.
*
* @author Thomas Segismont
*/
class DriftDetectionScheduleQueueComparator implements java.util.Comparator<DriftDetectionSchedule> {
@Override
public int compare(DriftDetectionSchedule schedule1, DriftDetectionSchedule schedule2) {
boolean enabled1 = schedule1.getDriftDefinition().isEnabled();
boolean enabled2 = schedule2.getDriftDefinition().isEnabled();
int diff = (enabled1 == enabled2) ? 0 : (enabled2 ? 1 : -1);
if (diff == 0) {
long nextScan1 = schedule1.getNextScan();
long nextScan2 = schedule2.getNextScan();
diff = (nextScan1 == nextScan2) ? 0 : (nextScan2 > nextScan1 ? 1 : -1);
}
return diff;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ public DriftDetector(ScheduleQueue scheduleQueue, ChangeSetManager changeSetMgr,
public void run() {
log.debug("Starting drift detection...");
long startTime = System.currentTimeMillis();
boolean updateSchedule = true;
DriftDetectionSchedule schedule;
while((schedule = scheduleQueue.getNextSchedule()) != null) {
boolean updateSchedule = true;
try {
if (log.isDebugEnabled()) {
log.debug("Fetching next schedule from " + scheduleQueue);
Expand All @@ -102,14 +102,13 @@ public void run() {
log.debug("Skipping " + schedule + " because the drift definition is disabled.");
}
updateSchedule = false;
continue;
break; // We're done, rest of the queue is disabled only
}

if (previousSnapshotExists(schedule)) {
if (log.isDebugEnabled()) {
log.debug("Skipping " + schedule + " because server has not yet acked previous change set");
}
updateSchedule = false;
continue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
/*
* RHQ Management Platform
* Copyright (C) 2005-2014 Red Hat, Inc.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* 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.core.pc.drift;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.rhq.core.domain.drift.DriftDefinition;
import org.rhq.core.domain.drift.DriftDefinitionComparator;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ScheduleQueueImpl implements ScheduleQueue {
private static final Log log = LogFactory.getLog(ScheduleQueueImpl.class);


private static final Runnable NO_OP = new Runnable() {
@Override
Expand All @@ -25,15 +52,8 @@ public void run() {

private Runnable deactivationTask;

private Log log = LogFactory.getLog(ScheduleQueueImpl.class);

public ScheduleQueueImpl() {
queue = new PriorityQueue<DriftDetectionSchedule>(10, new Comparator<DriftDetectionSchedule>() {
@Override
public int compare(DriftDetectionSchedule driftDetectionSchedule, DriftDetectionSchedule driftDetectionSchedule2) {
return driftDetectionSchedule.getNextScan() > driftDetectionSchedule2.getNextScan() ? 1 : -1;
}
});
queue = new PriorityQueue<DriftDetectionSchedule>(10, new DriftDetectionScheduleQueueComparator());
}

@Override
Expand All @@ -51,10 +71,6 @@ public DriftDetectionSchedule getNextSchedule() {
}
}

private boolean isActiveSchedule(int resourceId, DriftDefinition driftDef) {
return isActiveSchedule(resourceId, driftDef.getName());
}

private boolean isActiveSchedule(int resourceId, String defName) {
try {
lock.readLock().lock();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* RHQ Management Platform
* Copyright (C) 2005-2014 Red Hat, Inc.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* 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.core.pc.drift;

import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Random;

import org.testng.annotations.Test;

import org.rhq.core.domain.configuration.Configuration;
import org.rhq.core.domain.drift.DriftDefinition;

public class DriftDetectionScheduleQueueComparatorTest {
private static final int DISABLED_COUNT = 100;
private static final int TOTAL = DISABLED_COUNT * 20;

@Test
public void testPriority() {
Collection<DriftDetectionSchedule> sampleSchedules = createSampleSchedules();

PriorityQueue<DriftDetectionSchedule> priorityQueue = new PriorityQueue<DriftDetectionSchedule>(
sampleSchedules.size(), new DriftDetectionScheduleQueueComparator());
priorityQueue.addAll(sampleSchedules);

DriftDetectionSchedule previousSchedule = priorityQueue.poll();
assertNotNull(previousSchedule);

DriftDetectionSchedule schedule;
boolean foundDisabled = !previousSchedule.getDriftDefinition().isEnabled();
while ((schedule = priorityQueue.poll()) != null) {
boolean enabled = schedule.getDriftDefinition().isEnabled();
if (foundDisabled && enabled) {
fail("All disabled schedule should be at the end of queue");
}
foundDisabled = !enabled;
if (!foundDisabled) {
assertTrue(schedule.getNextScan() < previousSchedule.getNextScan(), "getNextScan priority failure");
}
previousSchedule = schedule;
}
}

private static Collection<DriftDetectionSchedule> createSampleSchedules() {
Random random = new Random();
List<DriftDetectionSchedule> schedules = new ArrayList<DriftDetectionSchedule>(TOTAL);
for (int i = 0; i < TOTAL; i++) {
schedules.add(newScheduleInstance(random.nextLong(), i > DISABLED_COUNT));
}
Collections.shuffle(schedules);
return schedules;
}

private static DriftDetectionSchedule newScheduleInstance(long interval, boolean enabled) {
DriftDefinition definition = new DriftDefinition(new Configuration());
definition.setEnabled(enabled);
definition.setInterval(interval);
DriftDetectionSchedule driftDetectionSchedule = new DriftDetectionSchedule(-1, definition);
driftDetectionSchedule.updateShedule();
return driftDetectionSchedule;
}

}

0 comments on commit 5467553

Please sign in to comment.