HypervisorUtils { public static void checkVolumeFileForActivity(final String filePath, int timeoutSeconds, long inactiveThresholdMilliseconds, long minimumFileSize) throws IOException { 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; int intervalSeconds = 1; while (true) { BasicFileAttributes attrs = Files.readAttributes(file.toPath(), BasicFileAttributes.class); long modifyIdle = System.currentTimeMillis() - attrs.lastModifiedTime().toMillis(); 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 (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, int timeoutSeconds, long inactiveThresholdMilliseconds, long minimumFileSize); static final Logger s_logger; }
@Test public void checkVolumeFileForActivitySmallFileTest() throws IOException { System.out.print("Testing don't block on newly created clones - "); String filePath = "./testsmallfileinactive"; int timeoutSeconds = 5; long thresholdMilliseconds = 2000; File file = new File(filePath); long startTime = setupcheckVolumeFileForActivityFile(file, 0); HypervisorUtils.checkVolumeFileForActivity(filePath, timeoutSeconds, thresholdMilliseconds, _minFileSize); long endTime = System.currentTimeMillis(); Assert.assertEquals(startTime, endTime, 1000L); System.out.println("pass"); file.delete(); }
@Test public void checkVolumeFileForActivityTest() throws IOException { System.out.print("Testing block on modified files - "); String filePath = "./testfileinactive"; int timeoutSeconds = 5; long thresholdMilliseconds = 2000; File file = new File(filePath); long startTime = setupcheckVolumeFileForActivityFile(file, _minFileSize); HypervisorUtils.checkVolumeFileForActivity(filePath, timeoutSeconds, thresholdMilliseconds, _minFileSize); 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(); }
@Test(expected=CloudRuntimeException.class) public void checkVolumeFileForActivityTimeoutTest() throws IOException { System.out.print("Testing timeout of blocking on modified files - "); String filePath = "./testfileinactive"; int timeoutSeconds = 3; long thresholdMilliseconds = 5000; File file = new File(filePath); setupcheckVolumeFileForActivityFile(file, _minFileSize); try { HypervisorUtils.checkVolumeFileForActivity(filePath, timeoutSeconds, thresholdMilliseconds, _minFileSize); } catch (CloudRuntimeException ex) { System.out.println("pass"); throw ex; } finally { file.delete(); } System.out.println("Fail"); }