@Test public void checkVolumeFileForActivityTest() throws IOException { System.out.print("Testing block on modified files - "); final String filePath = "./testfileinactive"; final int timeoutSeconds = 5; final long thresholdMilliseconds = 2000; final File file = new File(filePath); final long startTime = setupcheckVolumeFileForActivityFile(file, _minFileSize); HypervisorUtils.checkVolumeFileForActivity(filePath, timeoutSeconds, thresholdMilliseconds, _minFileSize); final long duration = System.currentTimeMillis() - startTime; Assert.assertFalse("Didn't block long enough, expected at least " + thresholdMilliseconds + " and got " + duration, duration < thresholdMilliseconds); System.out.println("pass"); file.delete(); }
public static void checkVolumeFileForActivity(final String filePath, final int timeoutSeconds, final long inactiveThresholdMilliseconds, final long minimumFileSize) throws IOException { final File file = new File(filePath); if (!file.exists()) { throw new CloudRuntimeException("File " + file.getAbsolutePath() + " not found"); } if (file.length() < minimumFileSize) { s_logger.debug("VM disk file too small, fresh clone? skipping modify check"); return; } int waitedSeconds = 0; final int intervalSeconds = 1; while (true) { final BasicFileAttributes attrs = Files.readAttributes(file.toPath(), BasicFileAttributes.class); final long modifyIdle = System.currentTimeMillis() - attrs.lastModifiedTime().toMillis(); final long accessIdle = System.currentTimeMillis() - attrs.lastAccessTime().toMillis(); if (modifyIdle > inactiveThresholdMilliseconds && accessIdle > inactiveThresholdMilliseconds) { s_logger.debug("File " + filePath + " has not been accessed or modified for at least " + inactiveThresholdMilliseconds + " ms"); return; } else { s_logger.debug("File was modified " + modifyIdle + "ms ago, accessed " + accessIdle + "ms ago, waiting for inactivity threshold of " + inactiveThresholdMilliseconds + "ms or timeout of " + timeoutSeconds + "s (waited " + waitedSeconds + "s)"); } try { TimeUnit.SECONDS.sleep(intervalSeconds); } catch (final InterruptedException ex) { throw new CloudRuntimeException("Interrupted while waiting for activity on " + filePath + " to subside", ex); } waitedSeconds += intervalSeconds; if (waitedSeconds >= timeoutSeconds) { throw new CloudRuntimeException("Reached timeout while waiting for activity on " + filePath + " to subside"); } } }
HypervisorUtils { public static void checkVolumeFileForActivity(final String filePath, final int timeoutSeconds, final long inactiveThresholdMilliseconds, final long minimumFileSize) throws IOException { final File file = new File(filePath); if (!file.exists()) { throw new CloudRuntimeException("File " + file.getAbsolutePath() + " not found"); } if (file.length() < minimumFileSize) { s_logger.debug("VM disk file too small, fresh clone? skipping modify check"); return; } int waitedSeconds = 0; final int intervalSeconds = 1; while (true) { final BasicFileAttributes attrs = Files.readAttributes(file.toPath(), BasicFileAttributes.class); final long modifyIdle = System.currentTimeMillis() - attrs.lastModifiedTime().toMillis(); final long accessIdle = System.currentTimeMillis() - attrs.lastAccessTime().toMillis(); if (modifyIdle > inactiveThresholdMilliseconds && accessIdle > inactiveThresholdMilliseconds) { s_logger.debug("File " + filePath + " has not been accessed or modified for at least " + inactiveThresholdMilliseconds + " ms"); return; } else { s_logger.debug("File was modified " + modifyIdle + "ms ago, accessed " + accessIdle + "ms ago, waiting for inactivity threshold of " + inactiveThresholdMilliseconds + "ms or timeout of " + timeoutSeconds + "s (waited " + waitedSeconds + "s)"); } try { TimeUnit.SECONDS.sleep(intervalSeconds); } catch (final InterruptedException ex) { throw new CloudRuntimeException("Interrupted while waiting for activity on " + filePath + " to subside", ex); } waitedSeconds += intervalSeconds; if (waitedSeconds >= timeoutSeconds) { throw new CloudRuntimeException("Reached timeout while waiting for activity on " + filePath + " to subside"); } } } }
HypervisorUtils { public static void checkVolumeFileForActivity(final String filePath, final int timeoutSeconds, final long inactiveThresholdMilliseconds, final long minimumFileSize) throws IOException { final File file = new File(filePath); if (!file.exists()) { throw new CloudRuntimeException("File " + file.getAbsolutePath() + " not found"); } if (file.length() < minimumFileSize) { s_logger.debug("VM disk file too small, fresh clone? skipping modify check"); return; } int waitedSeconds = 0; final int intervalSeconds = 1; while (true) { final BasicFileAttributes attrs = Files.readAttributes(file.toPath(), BasicFileAttributes.class); final long modifyIdle = System.currentTimeMillis() - attrs.lastModifiedTime().toMillis(); final long accessIdle = System.currentTimeMillis() - attrs.lastAccessTime().toMillis(); if (modifyIdle > inactiveThresholdMilliseconds && accessIdle > inactiveThresholdMilliseconds) { s_logger.debug("File " + filePath + " has not been accessed or modified for at least " + inactiveThresholdMilliseconds + " ms"); return; } else { s_logger.debug("File was modified " + modifyIdle + "ms ago, accessed " + accessIdle + "ms ago, waiting for inactivity threshold of " + inactiveThresholdMilliseconds + "ms or timeout of " + timeoutSeconds + "s (waited " + waitedSeconds + "s)"); } try { TimeUnit.SECONDS.sleep(intervalSeconds); } catch (final InterruptedException ex) { throw new CloudRuntimeException("Interrupted while waiting for activity on " + filePath + " to subside", ex); } waitedSeconds += intervalSeconds; if (waitedSeconds >= timeoutSeconds) { throw new CloudRuntimeException("Reached timeout while waiting for activity on " + filePath + " to subside"); } } }  }
HypervisorUtils { public static void checkVolumeFileForActivity(final String filePath, final int timeoutSeconds, final long inactiveThresholdMilliseconds, final long minimumFileSize) throws IOException { final File file = new File(filePath); if (!file.exists()) { throw new CloudRuntimeException("File " + file.getAbsolutePath() + " not found"); } if (file.length() < minimumFileSize) { s_logger.debug("VM disk file too small, fresh clone? skipping modify check"); return; } int waitedSeconds = 0; final int intervalSeconds = 1; while (true) { final BasicFileAttributes attrs = Files.readAttributes(file.toPath(), BasicFileAttributes.class); final long modifyIdle = System.currentTimeMillis() - attrs.lastModifiedTime().toMillis(); final long accessIdle = System.currentTimeMillis() - attrs.lastAccessTime().toMillis(); if (modifyIdle > inactiveThresholdMilliseconds && accessIdle > inactiveThresholdMilliseconds) { s_logger.debug("File " + filePath + " has not been accessed or modified for at least " + inactiveThresholdMilliseconds + " ms"); return; } else { s_logger.debug("File was modified " + modifyIdle + "ms ago, accessed " + accessIdle + "ms ago, waiting for inactivity threshold of " + inactiveThresholdMilliseconds + "ms or timeout of " + timeoutSeconds + "s (waited " + waitedSeconds + "s)"); } try { TimeUnit.SECONDS.sleep(intervalSeconds); } catch (final InterruptedException ex) { throw new CloudRuntimeException("Interrupted while waiting for activity on " + filePath + " to subside", ex); } waitedSeconds += intervalSeconds; if (waitedSeconds >= timeoutSeconds) { throw new CloudRuntimeException("Reached timeout while waiting for activity on " + filePath + " to subside"); } } }  static void checkVolumeFileForActivity(final String filePath, final int timeoutSeconds, final long inactiveThresholdMilliseconds, final long minimumFileSize); }
HypervisorUtils { public static void checkVolumeFileForActivity(final String filePath, final int timeoutSeconds, final long inactiveThresholdMilliseconds, final long minimumFileSize) throws IOException { final File file = new File(filePath); if (!file.exists()) { throw new CloudRuntimeException("File " + file.getAbsolutePath() + " not found"); } if (file.length() < minimumFileSize) { s_logger.debug("VM disk file too small, fresh clone? skipping modify check"); return; } int waitedSeconds = 0; final int intervalSeconds = 1; while (true) { final BasicFileAttributes attrs = Files.readAttributes(file.toPath(), BasicFileAttributes.class); final long modifyIdle = System.currentTimeMillis() - attrs.lastModifiedTime().toMillis(); final long accessIdle = System.currentTimeMillis() - attrs.lastAccessTime().toMillis(); if (modifyIdle > inactiveThresholdMilliseconds && accessIdle > inactiveThresholdMilliseconds) { s_logger.debug("File " + filePath + " has not been accessed or modified for at least " + inactiveThresholdMilliseconds + " ms"); return; } else { s_logger.debug("File was modified " + modifyIdle + "ms ago, accessed " + accessIdle + "ms ago, waiting for inactivity threshold of " + inactiveThresholdMilliseconds + "ms or timeout of " + timeoutSeconds + "s (waited " + waitedSeconds + "s)"); } try { TimeUnit.SECONDS.sleep(intervalSeconds); } catch (final InterruptedException ex) { throw new CloudRuntimeException("Interrupted while waiting for activity on " + filePath + " to subside", ex); } waitedSeconds += intervalSeconds; if (waitedSeconds >= timeoutSeconds) { throw new CloudRuntimeException("Reached timeout while waiting for activity on " + filePath + " to subside"); } } }  static void checkVolumeFileForActivity(final String filePath, final int timeoutSeconds, final long inactiveThresholdMilliseconds, final long minimumFileSize); static final Logger s_logger; }