modules/common/drift/src/main/java/org/rhq/common/drift/FileEntry.java
| 16
modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManager.java
| 8
modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManagerImpl.java
| 47 -
modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetectionSchedule.java
| 1
modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java
| 332 ++++++----
modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftManager.java
| 12
6 files changed, 245 insertions(+), 171 deletions(-)
New commits:
commit 2e75028c77cc24a2b097e82385d4c7dfb9b774d5
Author: Jay Shaughnessy <jshaughn(a)redhat.com>
Date: Tue Dec 13 17:17:29 2011 -0500
[Bug 756100 - RFE: use timestamp and file size during drift detection scans]
More work on using timestamp (and filesize) info to avoid SHA digest
generation. Handle situations where the current changeset is supplied
by the server, due to pinning or agent sync. In these situations work
to replace the non-timestamped changesets with timestamped versions as
soon as possible.
diff --git a/modules/common/drift/src/main/java/org/rhq/common/drift/FileEntry.java
b/modules/common/drift/src/main/java/org/rhq/common/drift/FileEntry.java
index e1766a3..564e9c5 100644
--- a/modules/common/drift/src/main/java/org/rhq/common/drift/FileEntry.java
+++ b/modules/common/drift/src/main/java/org/rhq/common/drift/FileEntry.java
@@ -29,7 +29,7 @@ import java.util.Date;
import org.rhq.core.domain.drift.DriftCategory;
import org.rhq.core.util.file.FileUtil;
-public class FileEntry implements Serializable {
+public class FileEntry implements Serializable, Comparable<FileEntry> {
private static final long serialVersionUID = 1L;
@@ -117,9 +117,23 @@ public class FileEntry implements Serializable {
return size;
}
+ public void setLastModified(Long lastModified) {
+ this.lastModified = lastModified;
+ }
+
+ public void setSize(Long size) {
+ this.size = size;
+ }
+
@Override
public String toString() {
return getClass().getSimpleName() + "[newSHA: " + newSHA + ",
oldSHA: " + oldSHA + ", file: " + file
+ ", type: " + type.code() + ", lastModified: " + new
Date(lastModified) + ", size: " + size + "]";
}
+
+ // Support Sets and Ordering by deferring to a String compare on the file path
+ @Override
+ public int compareTo(FileEntry o) {
+ return this.file.compareTo(o.getFile());
+ }
}
diff --git
a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java
b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java
index 10d2006..869e376 100644
---
a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java
+++
b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java
@@ -184,117 +184,152 @@ public class DriftDetector implements Runnable {
log.debug("Generating drift change set for " + schedule);
- File currentSnapshot = changeSetMgr.findChangeSet(schedule.getResourceId(),
schedule.getDriftDefinition()
+ boolean isPinned = schedule.getDriftDefinition().isPinned();
+ final File basedir = new File(basedir(schedule.getResourceId(),
schedule.getDriftDefinition()));
+
+ File currentFullSnapshot = changeSetMgr.findChangeSet(schedule.getResourceId(),
schedule.getDriftDefinition()
.getName(), COVERAGE);
- File snapshotFile = currentSnapshot;
- if (schedule.getDriftDefinition().isPinned()) {
- snapshotFile = new File(snapshotFile.getParentFile(), FILE_SNAPSHOT_PINNED);
+ // unless pinned use the current full snapshot file for the definition, otherwise
use the pinned snapshot
+ File snapshotFile = isPinned ? new File(currentFullSnapshot.getParentFile(),
FILE_SNAPSHOT_PINNED)
+ : currentFullSnapshot;
+
+ // get a Set of all files in the detection, consider them initially new files,
and we'll knock the
+ // list down as we go. As we build up FileEntries in memory this Set will
shrink. It's marginally
+ // less memory than if we had both in memory at the same time.
+ final Set<File> newFiles = new HashSet<File>(1000);
+
+ // If the basedir is still valid we need to do a directory tree scan to look for
newly added files
+ if (basedir.isDirectory()) {
+ DriftDefinition driftDef = schedule.getDriftDefinition();
+ List<Filter> includes = driftDef.getIncludes();
+ List<Filter> excludes = driftDef.getExcludes();
+
+ for (File dir : getScanDirectories(basedir, includes)) {
+ forEachFile(dir, new FilterFileVisitor(basedir, includes, excludes, new
FileVisitor() {
+ @Override
+ public void visit(File file) {
+ if (file.canRead()) {
+ newFiles.add(file);
+ } else if (log.isDebugEnabled()) {
+ log.debug("Skipping " + file.getPath() + " as
new file since it is not readable.");
+ }
+ }
+ }));
+ }
}
- final File basedir = new File(basedir(schedule.getResourceId(),
schedule.getDriftDefinition()));
- final Set<File> processedFiles = new HashSet<File>();
- final List<FileEntry> snapshotEntries = new LinkedList<FileEntry>();
- final List<FileEntry> deltaEntries = new LinkedList<FileEntry>();
+ final List<FileEntry> unchangedEntries = new
LinkedList<FileEntry>();
+ final List<FileEntry> removedEntries = new LinkedList<FileEntry>();
+ final List<FileEntry> cantReadEntries = new LinkedList<FileEntry>();
+ final List<FileEntry> changedEntries = new LinkedList<FileEntry>();
+ final List<FileEntry> changedPinnedEntries = isPinned ? new
LinkedList<FileEntry>() : null;
- ChangeSetReader coverageReader = null;
+ ChangeSetReader snapshotReader = null;
int newVersion;
+ boolean updateSnapshot = false;
try {
- coverageReader = changeSetMgr.getChangeSetReader(snapshotFile);
+ snapshotReader = changeSetMgr.getChangeSetReader(snapshotFile);
if (!basedir.exists()) {
log.warn("The base directory [" + basedir.getAbsolutePath() +
"] for " + schedule + " does not exist.");
}
- if (schedule.getDriftDefinition().isPinned()) {
- ChangeSetReader snapshotReader = null;
+ if (isPinned) {
+ // If pinned we compare against the pinned snapshot but we need to know
the current snapshot version,
+ // get it from the current full snapshot.
+ ChangeSetReader currentFullSnapshotReader = null;
try {
- snapshotReader = changeSetMgr.getChangeSetReader(currentSnapshot);
- newVersion = snapshotReader.getHeaders().getVersion() + 1;
+ currentFullSnapshotReader =
changeSetMgr.getChangeSetReader(currentFullSnapshot);
+ newVersion = currentFullSnapshotReader.getHeaders().getVersion() +
1;
} finally {
- snapshotReader.close();
+ currentFullSnapshotReader.close();
}
} else {
- newVersion = coverageReader.getHeaders().getVersion() + 1;
+ newVersion = snapshotReader.getHeaders().getVersion() + 1;
}
// First look for files that have either been modified or deleted
- scanForModifiedOrDeletedFiles(schedule, basedir, processedFiles,
snapshotEntries, deltaEntries,
- coverageReader);
+ updateSnapshot = scanSnapshotFiles(schedule, basedir, snapshotReader,
newFiles, unchangedEntries,
+ removedEntries, cantReadEntries, changedEntries, changedPinnedEntries);
+
} finally {
- coverageReader.close();
+ snapshotReader.close();
}
- // If the basedir is still valid we need to do a directory tree scan to look for
newly added files
- if (basedir.isDirectory()) {
- DriftDefinition driftDef = schedule.getDriftDefinition();
- List<Filter> includes = driftDef.getIncludes();
- List<Filter> excludes = driftDef.getExcludes();
+ // if necessary, re-write the pinned snapshot file because we've updated
timestamp/filesize info, which
+ // on subsequent detection runs will help us avoid SHA generation. It must
maintain the same entries.
+ if (isPinned && updateSnapshot) {
+ changedPinnedEntries.addAll(unchangedEntries);
- for (File dir : getScanDirectories(basedir, includes)) {
- forEachFile(dir, new FilterFileVisitor(basedir, includes, excludes, new
FileVisitor() {
- @Override
- public void visit(File file) {
- try {
- if (processedFiles.contains(file)) {
- return;
- }
+ backupAndDeleteCurrentSnapshot(snapshotFile);
+ updatePinnedSnapshot(schedule, snapshotFile, changedPinnedEntries);
+ }
- if (!file.canRead()) {
- if (log.isDebugEnabled()) {
- log.debug("Skipping " + file.getPath() +
" since it is not readable.");
- }
- return;
- }
+ final List<FileEntry> snapshotEntries = new
LinkedList<FileEntry>(unchangedEntries);
+ snapshotEntries.addAll(changedEntries);
- if (log.isInfoEnabled()) {
- log.info("Detected added file for " + schedule
+ " --> " + file.getAbsolutePath());
- }
+ final List<FileEntry> deltaEntries = new
LinkedList<FileEntry>(changedEntries);
+ deltaEntries.addAll(removedEntries);
+ deltaEntries.addAll(cantReadEntries);
- FileEntry newEntry = addedFileEntry(relativePath(basedir,
file), sha256(file), file
- .lastModified(), file.length());
- deltaEntries.add(newEntry);
- snapshotEntries.add(newEntry);
- } catch (IOException e) {
- log.error("An error occurred while generating a drift
change set for " + schedule + ": "
- + e.getMessage());
- throw new DriftDetectionException("An error occurred
while generating a drift change set",
- e);
- }
- }
- }));
+ for (File file : newFiles) {
+ try {
+ if (log.isInfoEnabled()) {
+ log.info("Detected added file for " + schedule + "
--> " + file.getAbsolutePath());
+ }
+
+ FileEntry newEntry = addedFileEntry(relativePath(basedir, file),
sha256(file), file.lastModified(),
+ file.length());
+
+ deltaEntries.add(newEntry);
+ snapshotEntries.add(newEntry);
+
+ } catch (IOException e) {
+ log.error("An error occurred while generating a drift change set for
" + schedule + ": "
+ + e.getMessage());
+ throw new DriftDetectionException("An error occurred while
generating a drift change set", e);
}
}
if (deltaEntries.isEmpty()) {
- summary.setNewSnapshot(currentSnapshot);
-
- // If nothing has changed, there is no need to add/update any files
- // unless the definition is pinned in which case we need to reset
- // the current snapshot to match the pinned snapshot. Note though
- // that we increment the snapshot version in order to let the server
- // know about the state change.
- // if no timestamp must re-gen the files.
- if (schedule.getDriftDefinition().isPinned() && newVersion > 1
- && !isPreviousChangeSetEmpty(schedule.getResourceId(),
schedule.getDriftDefinition())) {
- currentSnapshot.delete();
- File newSnapshot = updateCurrentSnapshot(schedule, snapshotEntries,
newVersion);
-
- updateDeltaSnapshot(summary, schedule, deltaEntries, newVersion,
currentSnapshot, newSnapshot);
- // TODO report back to the server that we are back in compliance
+ File newSnapshot = currentFullSnapshot;
+
+ if (!isPinned) {
+ // If unpinned and there is no detected drift then we generally don't
need to add/update any files.
+ // But, if we have timestamp/filesize updates then we want to replace the
current snapshot with
+ // the updated entries, so we can avoid SHA generation on subsequent
runs.
+ if (updateSnapshot) {
+ currentFullSnapshot.delete();
+ newSnapshot = updateCurrentSnapshot(schedule, snapshotEntries,
newVersion - 1);
+ }
+ } else {
+ // If pinned and returning to compliance (meaning no drift now but the
previous snapshot did have drift)
+ // then we need to reset the current snapshot to match the pinned
snapshot. Note though that we
+ // increment the snapshot version in order to let the server know about
the state change.
+ if (newVersion > 1
+ && !isPreviousChangeSetEmpty(schedule.getResourceId(),
schedule.getDriftDefinition())) {
+ currentFullSnapshot.delete();
+ newSnapshot = updateCurrentSnapshot(schedule, snapshotEntries,
newVersion);
+
+ updateDeltaSnapshot(summary, schedule, deltaEntries, newVersion,
currentFullSnapshot, newSnapshot);
+ }
}
+
+ summary.setNewSnapshot(newSnapshot);
+
} else {
- if (schedule.getDriftDefinition().isPinned() && newVersion > 1
- && isSameAsPreviousChangeSet(deltaEntries, currentSnapshot)) {
- // if we are still out of compliance just report, we report a
- // repeat change set to indicate no changes but also still out
- // of compliance.
+ // if there is drift, but we're pinned and the drift is the same as the
previous detection, just
+ // mark it as a repeat to indicate that we're out of compliance but not
in any new way.
+ if (isPinned && newVersion > 1 &&
isSameAsPreviousChangeSet(deltaEntries, currentFullSnapshot)) {
summary.setVersion(newVersion - 1);
summary.setRepeat(true);
+
return;
}
- File oldSnapshot = backupAndDeleteCurrentSnapshot(currentSnapshot);
+ // otherwise, generate a new current snapshot, and a snapshot delta
reflecting the latest drift
+ File oldSnapshot = backupAndDeleteCurrentSnapshot(currentFullSnapshot);
File newSnapshot = updateCurrentSnapshot(schedule, snapshotEntries,
newVersion);
updateDeltaSnapshot(summary, schedule, deltaEntries, newVersion, oldSnapshot,
newSnapshot);
@@ -321,55 +356,88 @@ public class DriftDetector implements Runnable {
return directories;
}
- private void scanForModifiedOrDeletedFiles(DriftDetectionSchedule schedule, File
basedir, Set<File> processedFiles,
- List<FileEntry> snapshotEntries, List<FileEntry> deltaEntries,
ChangeSetReader coverageReader)
+ /**
+ * Process the entries for the snapshotReader. Each entry will be placed in one of
the various Lists depending
+ * on what bucket it fall into.
+ * @return true if unchangedEntries (meaning no drift) had timestamp/filesize info
updated, in which case the
+ * snapshot should be re-written to disk even if there was no drift.
+ * @throws IOException
+ */
+ private boolean scanSnapshotFiles(DriftDetectionSchedule schedule, File basedir,
ChangeSetReader snapshotReader,
+ Set<File> newFiles, List<FileEntry> unchangedEntries,
List<FileEntry> removedEntries,
+ List<FileEntry> cantReadEntries, List<FileEntry> modifiedEntries,
List<FileEntry> changedPinnedEntries)
throws IOException {
- for (FileEntry entry : coverageReader) {
+ boolean result = false;
+
+ for (FileEntry entry : snapshotReader) {
File file = new File(basedir, entry.getFile());
+ newFiles.remove(file);
+
if (!file.exists()) {
// The file has been deleted since the last scan
if (log.isDebugEnabled()) {
log.debug("Detected deleted file for " + schedule + "
--> " + file.getAbsolutePath());
}
- deltaEntries.add(removedFileEntry(entry.getFile(), entry.getNewSHA()));
+ removedEntries.add(removedFileEntry(entry.getFile(),
entry.getNewSHA()));
+
+ if (null != changedPinnedEntries) {
+ changedPinnedEntries.add(entry);
+ }
+
+ continue;
+
} else if (!file.canRead()) {
- processedFiles.add(file);
if (log.isDebugEnabled()) {
log.debug(file.getPath() + " is no longer readable. Treating it
as a deleted file.");
}
- deltaEntries.add(removedFileEntry(entry.getFile(), entry.getNewSHA()));
+ cantReadEntries.add(removedFileEntry(entry.getFile(),
entry.getNewSHA()));
+
+ if (null != changedPinnedEntries) {
+ changedPinnedEntries.add(entry);
+ }
+
+ continue;
+
} else {
- processedFiles.add(file);
- // if we do not have the last modification time and the file size, then
we
- // have to compare SHAs. We can wind up without a timestamp of a file
size
- // when the agent is restarted and the data directory is purged. The
server
- // sends snapshots down to the agent and since the server does not store
the
- // file modification time or size, the fields will get initialized to
-1.
- if (entry.getLastModified() == -1 || entry.getSize() == -1) {
- String currentSHA = sha256(file);
- if (!entry.getNewSHA().equals(currentSHA)) {
- FileEntry modifiedEntry = changedFileEntry(entry.getFile(),
entry.getNewSHA(), currentSHA, file
- .lastModified(), file.length());
- deltaEntries.add(modifiedEntry);
- snapshotEntries.add(modifiedEntry);
- }
- } else if (entry.getLastModified() != -1 && entry.getSize() !=
-1
- && (entry.getLastModified() != file.lastModified() ||
entry.getSize() != file.length())) {
- if (log.isDebugEnabled()) {
- log.debug("Detected modified file for " + schedule +
" --> " + file.getAbsolutePath());
- }
- String currentSHA = sha256(file);
+ String currentSHA = null;
+ boolean isModified = false;
+
+ // perform a SHA comparison if we are unable to compare size and
lastModified or if the
+ // size or lastModified test fails. We may not have size of lastModified
values for the
+ // entry when the current snapshot was provided by the server, either due
to a synch or
+ // pinning scenario. The server does not store that information and will
provide -1 for defaults.
+ if (entry.getLastModified() == -1 || entry.getSize() == -1
+ || entry.getLastModified() != file.lastModified() || entry.getSize()
!= file.length()) {
+
+ currentSHA = sha256(file);
+ isModified = !entry.getNewSHA().equals(currentSHA);
+ }
+
+ if (isModified) {
FileEntry modifiedEntry = changedFileEntry(entry.getFile(),
entry.getNewSHA(), currentSHA, file
.lastModified(), file.length());
- deltaEntries.add(modifiedEntry);
- snapshotEntries.add(modifiedEntry);
+ modifiedEntries.add(modifiedEntry);
+
+ if (null != changedPinnedEntries) {
+ changedPinnedEntries.add(entry);
+ }
+
} else {
- // The file has not changed
- snapshotEntries.add(entry);
+ if (-1 == entry.getLastModified()) {
+ entry.setLastModified(file.lastModified());
+ result = true;
+ }
+ if (-1 == entry.getSize()) {
+ entry.setSize(file.length());
+ result = true;
+ }
+ unchangedEntries.add(entry);
}
}
}
+
+ return result;
}
@SuppressWarnings("unused")
@@ -449,6 +517,26 @@ public class DriftDetector implements Runnable {
}
}
+ private File updatePinnedSnapshot(DriftDetectionSchedule schedule, File
pinnedSnapshot,
+ List<FileEntry> snapshotEntries) throws IOException {
+
+ ChangeSetWriter newSnapshotWriter = null;
+
+ try {
+ Headers snapshotHeaders = createHeaders(schedule, COVERAGE, 0);
+ newSnapshotWriter = changeSetMgr.getChangeSetWriter(pinnedSnapshot,
snapshotHeaders);
+
+ for (FileEntry entry : snapshotEntries) {
+ newSnapshotWriter.write(entry);
+ }
+
+ return pinnedSnapshot;
+
+ } finally {
+ newSnapshotWriter.close();
+ }
+ }
+
private boolean isSameAsPreviousChangeSet(List<FileEntry> entries, File
currentSnapsotFile) throws IOException {
HashMap<String, FileEntry> entriesMap = new HashMap<String,
FileEntry>();
for (FileEntry e : entries) {
commit bf599134469592012e7ee69a00354cc815e7db8e
Author: Jay Shaughnessy <jshaughn(a)redhat.com>
Date: Tue Dec 13 16:22:13 2011 -0500
Make sure the drift def id is also in the copy so it can be applied to
generated changeset headers.
diff --git
a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetectionSchedule.java
b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetectionSchedule.java
index 8ddfc03..60fb35b 100644
---
a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetectionSchedule.java
+++
b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetectionSchedule.java
@@ -43,6 +43,7 @@ public class DriftDetectionSchedule implements
Comparable<DriftDetectionSchedule
public DriftDetectionSchedule copy() {
DriftDetectionSchedule copy = new DriftDetectionSchedule(resourceId, new
DriftDefinition(driftDef
.getConfiguration().deepCopyWithoutProxies()));
+ copy.driftDef.setId(driftDef.getId());
copy.nextScan = nextScan;
return copy;
}
commit 54ccb75ffb746b3e167f8fdfb2fba3cc70dc13e2
Author: Jay Shaughnessy <jshaughn(a)redhat.com>
Date: Mon Dec 12 15:53:14 2011 -0500
Ensure we have both the timestamp and the filesize before we skip using
a SHA test.
diff --git
a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java
b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java
index 11a2d85..10d2006 100644
---
a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java
+++
b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java
@@ -346,7 +346,7 @@ public class DriftDetector implements Runnable {
// when the agent is restarted and the data directory is purged. The
server
// sends snapshots down to the agent and since the server does not store
the
// file modification time or size, the fields will get initialized to
-1.
- if (entry.getLastModified() == -1 && entry.getSize() == -1) {
+ if (entry.getLastModified() == -1 || entry.getSize() == -1) {
String currentSHA = sha256(file);
if (!entry.getNewSHA().equals(currentSHA)) {
FileEntry modifiedEntry = changedFileEntry(entry.getFile(),
entry.getNewSHA(), currentSHA, file
commit 610d9447060444806f5fd27c29a5f118195636c0
Author: Jay Shaughnessy <jshaughn(a)redhat.com>
Date: Mon Dec 12 15:51:46 2011 -0500
Just some cleanup
- remove unused/obsolete methods from ChangeSetManager and the impl
- move some commonly referenced filenames into constants
diff --git
a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManager.java
b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManager.java
index 1fc9fe9..5117ab1 100644
---
a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManager.java
+++
b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManager.java
@@ -26,7 +26,6 @@ import org.rhq.common.drift.ChangeSetReader;
import org.rhq.common.drift.ChangeSetWriter;
import org.rhq.common.drift.Headers;
import org.rhq.core.domain.drift.DriftChangeSetCategory;
-import org.rhq.core.domain.drift.DriftDefinition;
public interface ChangeSetManager {
@@ -66,11 +65,4 @@ public interface ChangeSetManager {
ChangeSetWriter getChangeSetWriter(int resourceId, Headers headers) throws
IOException;
ChangeSetWriter getChangeSetWriter(File changeSetFile, Headers headers) throws
IOException;
-
- ChangeSetWriter getChangeSetWriterForUpdate(int resourceId, Headers headers) throws
IOException;
-
- void updateChangeSet(int resourceId, Headers headers) throws IOException;
-
- void addFileToChangeSet(int resourceId, DriftDefinition driftDefinition, File file);
-
}
diff --git
a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManagerImpl.java
b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManagerImpl.java
index 132a7ca..97e92d3 100644
---
a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManagerImpl.java
+++
b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/ChangeSetManagerImpl.java
@@ -21,11 +21,7 @@ package org.rhq.core.pc.drift;
import static org.rhq.core.domain.drift.DriftChangeSetCategory.COVERAGE;
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
import java.io.IOException;
import org.rhq.common.drift.ChangeSetReader;
@@ -34,8 +30,6 @@ import org.rhq.common.drift.ChangeSetWriter;
import org.rhq.common.drift.ChangeSetWriterImpl;
import org.rhq.common.drift.Headers;
import org.rhq.core.domain.drift.DriftChangeSetCategory;
-import org.rhq.core.domain.drift.DriftDefinition;
-import org.rhq.core.util.stream.StreamUtil;
public class ChangeSetManagerImpl implements ChangeSetManager {
@@ -51,13 +45,13 @@ public class ChangeSetManagerImpl implements ChangeSetManager {
if (changeSetDir == null || !changeSetDir.exists()) {
return false;
}
- return new File(changeSetDir, "changeset.txt").exists();
+ return new File(changeSetDir, DriftDetector.FILE_CHANGESET_FULL).exists();
}
@Override
public File findChangeSet(int resourceId, String driftDefinitionName) throws
IOException {
File changeSetDir = findChangeSetDir(resourceId, driftDefinitionName);
- File changeSetFile = new File(changeSetDir, "changeset.txt");
+ File changeSetFile = new File(changeSetDir, DriftDetector.FILE_CHANGESET_FULL);
if (changeSetFile.exists()) {
return changeSetFile;
@@ -76,9 +70,9 @@ public class ChangeSetManagerImpl implements ChangeSetManager {
switch (type) {
case COVERAGE:
- return new File(changeSetDir, "changeset.txt");
+ return new File(changeSetDir, DriftDetector.FILE_CHANGESET_FULL);
case DRIFT:
- return new File(changeSetDir, "drift-changeset.txt");
+ return new File(changeSetDir, DriftDetector.FILE_CHANGESET_DELTA);
default:
throw new IllegalArgumentException(type + " is not a recognized,
supported change set type.");
}
@@ -111,9 +105,9 @@ public class ChangeSetManagerImpl implements ChangeSetManager {
File changeSet;
if (headers.getType() == COVERAGE) {
- changeSet = new File(changeSetDir, "changeset.txt");
+ changeSet = new File(changeSetDir, DriftDetector.FILE_CHANGESET_FULL);
} else {
- changeSet = new File(changeSetDir, "drift-changeset.txt");
+ changeSet = new File(changeSetDir, DriftDetector.FILE_CHANGESET_DELTA);
}
return new ChangeSetWriterImpl(changeSet, headers);
}
@@ -123,35 +117,6 @@ public class ChangeSetManagerImpl implements ChangeSetManager {
return new ChangeSetWriterImpl(changeSetFile, headers);
}
- @Override
- public ChangeSetWriter getChangeSetWriterForUpdate(int resourceId, Headers headers)
throws IOException {
- File resourceDir = new File(changeSetsDir, Integer.toString(resourceId));
- File changeSetDir = new File(resourceDir, headers.getDriftDefinitionName());
-
- return new ChangeSetWriterImpl(new File(changeSetDir,
"changeset.working"), headers);
- }
-
- @Override
- public void updateChangeSet(int resourceId, Headers headers) throws IOException {
- File resourceDir = new File(changeSetsDir, Integer.toString(resourceId));
- File changeSetDir = new File(resourceDir, headers.getDriftDefinitionName());
- File newChangeSet = new File(changeSetDir, "changeset.working");
- File changeSet = new File(changeSetDir, "changeset.txt");
-
- if (!newChangeSet.exists()) {
- return;
- }
-
- changeSet.delete();
-
- StreamUtil.copy(new BufferedInputStream(new FileInputStream(newChangeSet)), new
BufferedOutputStream(
- new FileOutputStream(changeSet)));
- }
-
- @Override
- public void addFileToChangeSet(int resourceId, DriftDefinition driftDefinition, File
file) {
- }
-
private File findChangeSetDir(int resourceId, String driftDefinitionName) {
File resourceDir = new File(changeSetsDir, Integer.toString(resourceId));
if (!resourceDir.exists()) {
diff --git
a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java
b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java
index 848ae93..11a2d85 100644
---
a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java
+++
b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftDetector.java
@@ -19,8 +19,26 @@
package org.rhq.core.pc.drift;
+import static org.rhq.common.drift.FileEntry.addedFileEntry;
+import static org.rhq.common.drift.FileEntry.changedFileEntry;
+import static org.rhq.common.drift.FileEntry.removedFileEntry;
+import static org.rhq.core.domain.drift.DriftChangeSetCategory.COVERAGE;
+import static org.rhq.core.domain.drift.DriftChangeSetCategory.DRIFT;
+import static org.rhq.core.util.file.FileUtil.copyFile;
+import static org.rhq.core.util.file.FileUtil.forEachFile;
+
+import java.io.File;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+
import org.rhq.common.drift.ChangeSetReader;
import org.rhq.common.drift.ChangeSetWriter;
import org.rhq.common.drift.FileEntry;
@@ -31,20 +49,13 @@ import org.rhq.core.domain.drift.Filter;
import org.rhq.core.util.MessageDigestGenerator;
import org.rhq.core.util.file.FileVisitor;
-import java.io.File;
-import java.io.FilenameFilter;
-import java.io.IOException;
-import java.util.*;
-
-import static org.rhq.common.drift.FileEntry.*;
-import static org.rhq.core.domain.drift.DriftChangeSetCategory.COVERAGE;
-import static org.rhq.core.domain.drift.DriftChangeSetCategory.DRIFT;
-import static org.rhq.core.util.file.FileUtil.copyFile;
-import static org.rhq.core.util.file.FileUtil.forEachFile;
-
public class DriftDetector implements Runnable {
private Log log = LogFactory.getLog(DriftDetector.class);
+ static final String FILE_CHANGESET_FULL = "changeset.txt";
+ static final String FILE_CHANGESET_DELTA = "drift-changeset.txt";
+ static final String FILE_SNAPSHOT_PINNED = "snapshot.pinned";
+
private ScheduleQueue scheduleQueue;
private ChangeSetManager changeSetMgr;
@@ -178,7 +189,7 @@ public class DriftDetector implements Runnable {
File snapshotFile = currentSnapshot;
if (schedule.getDriftDefinition().isPinned()) {
- snapshotFile = new File(snapshotFile.getParentFile(),
"snapshot.pinned");
+ snapshotFile = new File(snapshotFile.getParentFile(), FILE_SNAPSHOT_PINNED);
}
final File basedir = new File(basedir(schedule.getResourceId(),
schedule.getDriftDefinition()));
@@ -240,14 +251,16 @@ public class DriftDetector implements Runnable {
log.info("Detected added file for " + schedule
+ " --> " + file.getAbsolutePath());
}
- FileEntry newEntry = addedFileEntry(relativePath(basedir, file),
sha256(file),
- file.lastModified(), file.length());
- deltaEntries.add(newEntry);
- snapshotEntries.add(newEntry);
- } catch (IOException e) {
- log.error("An error occurred while generating a drift change
set for " + schedule + ": "
- + e.getMessage());
- throw new DriftDetectionException("An error occurred while
generating a drift change set", e);
+ FileEntry newEntry = addedFileEntry(relativePath(basedir,
file), sha256(file), file
+ .lastModified(), file.length());
+ deltaEntries.add(newEntry);
+ snapshotEntries.add(newEntry);
+ } catch (IOException e) {
+ log.error("An error occurred while generating a drift
change set for " + schedule + ": "
+ + e.getMessage());
+ throw new DriftDetectionException("An error occurred
while generating a drift change set",
+ e);
+ }
}
}));
}
@@ -261,6 +274,7 @@ public class DriftDetector implements Runnable {
// the current snapshot to match the pinned snapshot. Note though
// that we increment the snapshot version in order to let the server
// know about the state change.
+ // if no timestamp must re-gen the files.
if (schedule.getDriftDefinition().isPinned() && newVersion > 1
&& !isPreviousChangeSetEmpty(schedule.getResourceId(),
schedule.getDriftDefinition())) {
currentSnapshot.delete();
@@ -335,19 +349,19 @@ public class DriftDetector implements Runnable {
if (entry.getLastModified() == -1 && entry.getSize() == -1) {
String currentSHA = sha256(file);
if (!entry.getNewSHA().equals(currentSHA)) {
- FileEntry modifiedEntry = changedFileEntry(entry.getFile(),
entry.getNewSHA(), currentSHA,
- file.lastModified(), file.length());
+ FileEntry modifiedEntry = changedFileEntry(entry.getFile(),
entry.getNewSHA(), currentSHA, file
+ .lastModified(), file.length());
deltaEntries.add(modifiedEntry);
snapshotEntries.add(modifiedEntry);
}
- } else if (entry.getLastModified() != -1 && entry.getSize() != -1
&&
- (entry.getLastModified() != file.lastModified() || entry.getSize() !=
file.length())) {
+ } else if (entry.getLastModified() != -1 && entry.getSize() !=
-1
+ && (entry.getLastModified() != file.lastModified() ||
entry.getSize() != file.length())) {
if (log.isDebugEnabled()) {
log.debug("Detected modified file for " + schedule +
" --> " + file.getAbsolutePath());
}
String currentSHA = sha256(file);
- FileEntry modifiedEntry = changedFileEntry(entry.getFile(),
entry.getNewSHA(), currentSHA,
- file.lastModified(), file.length());
+ FileEntry modifiedEntry = changedFileEntry(entry.getFile(),
entry.getNewSHA(), currentSHA, file
+ .lastModified(), file.length());
deltaEntries.add(modifiedEntry);
snapshotEntries.add(modifiedEntry);
} else {
@@ -443,7 +457,7 @@ public class DriftDetector implements Runnable {
ChangeSetReader reader = null;
try {
- File deltaChangeSet = new File(currentSnapsotFile.getParentFile(),
"drift-changeset.txt");
+ File deltaChangeSet = new File(currentSnapsotFile.getParentFile(),
FILE_CHANGESET_DELTA);
reader = changeSetMgr.getChangeSetReader(deltaChangeSet);
int numEntries = 0;
@@ -511,7 +525,7 @@ public class DriftDetector implements Runnable {
writer = null;
}
if (schedule.getDriftDefinition().isPinned()) {
- copyFile(snapshot, new File(snapshot.getParentFile(),
"snapshot.pinned"));
+ copyFile(snapshot, new File(snapshot.getParentFile(),
FILE_SNAPSHOT_PINNED));
}
summary.setNewSnapshot(snapshot);
} finally {
diff --git
a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftManager.java
b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftManager.java
index 1182407..386ad73 100644
--- a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftManager.java
+++ b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/drift/DriftManager.java
@@ -201,7 +201,7 @@ public class DriftManager extends AgentService implements
DriftAgentService, Dri
// change set.
DriftSnapshot pinnedSnapshot =
driftServer.getSnapshot(driftDefinition.getId(), 0, 0);
Headers pinnedHeaders = createHeaders(resource.getId(),
driftDefinition);
- File pinnedSnapshotFile = new File(currentSnapshotFile.getParent(),
"snapshot.pinned");
+ File pinnedSnapshotFile = new File(currentSnapshotFile.getParent(),
DriftDetector.FILE_SNAPSHOT_PINNED);
log.info("Preparing to write pinned snapshot to disk for "
+ toString(resource.getId(), driftDefinition));
writeSnapshotToFile(pinnedSnapshot, pinnedSnapshotFile, pinnedHeaders);
@@ -212,7 +212,7 @@ public class DriftManager extends AgentService implements
DriftAgentService, Dri
// drift that has already been reported to the server.
DriftSnapshot deltaSnapshot =
driftServer.getSnapshot(driftDefinition.getId(), snapshot
.getVersion(), snapshot.getVersion());
- File deltaFile = new File(currentSnapshotFile.getParentFile(),
"drift-changeset.txt");
+ File deltaFile = new File(currentSnapshotFile.getParentFile(),
DriftDetector.FILE_CHANGESET_DELTA);
Headers deltaHeaders = createHeaders(resource.getId(),
driftDefinition);
deltaHeaders.setVersion(snapshot.getVersion());
deltaHeaders.setType(DRIFT);
@@ -546,7 +546,7 @@ public class DriftManager extends AgentService implements
DriftAgentService, Dri
public void run() {
File currentSnapshot =
changeSetMgr.findChangeSet(schedule.getResourceId(), schedule
.getDriftDefinition().getName(), COVERAGE);
- File pinnedSnapshot = new File(currentSnapshot.getParentFile(),
"snapshot.pinned");
+ File pinnedSnapshot = new File(currentSnapshot.getParentFile(),
DriftDetector.FILE_SNAPSHOT_PINNED);
pinnedSnapshot.delete();
if (log.isDebugEnabled()) {
@@ -561,7 +561,7 @@ public class DriftManager extends AgentService implements
DriftAgentService, Dri
@Override
public void updateDriftDetection(int resourceId, DriftDefinition driftDef,
DriftSnapshot driftSnapshot) {
File currentSnapshot = changeSetMgr.findChangeSet(resourceId, driftDef.getName(),
COVERAGE);
- File pinnedSnapshot = new File(currentSnapshot.getParentFile(),
"snapshot.pinned");
+ File pinnedSnapshot = new File(currentSnapshot.getParentFile(),
DriftDetector.FILE_SNAPSHOT_PINNED);
Headers headers = createHeaders(resourceId, driftDef);
try {
@@ -777,8 +777,8 @@ public class DriftManager extends AgentService implements
DriftAgentService, Dri
writer.write(addedFileEntry(drift.getPath(),
drift.getNewDriftFile().getHashId(), -1L, -1L));
break;
case FILE_CHANGED:
- writer.write(changedFileEntry(drift.getPath(),
drift.getOldDriftFile().getHashId(),
- drift.getNewDriftFile().getHashId(), -1L, -1L));
+ writer.write(changedFileEntry(drift.getPath(),
drift.getOldDriftFile().getHashId(), drift
+ .getNewDriftFile().getHashId(), -1L, -1L));
break;
default: // FILE_REMOVED
writer.write(removedFileEntry(drift.getPath(),
drift.getOldDriftFile().getHashId()));