ReadWriteDiskValidator implements DiskValidator { @Override public void checkStatus(File dir) throws DiskErrorException { ReadWriteDiskValidatorMetrics metric = ReadWriteDiskValidatorMetrics.getMetric(dir.toString()); Path tmpFile = null; try { if (!dir.isDirectory()) { metric.diskCheckFailed(); throw new DiskErrorException(dir + " is not a directory!"); } DiskChecker.checkDir(dir); tmpFile = Files.createTempFile(dir.toPath(), "test", "tmp"); byte[] inputBytes = new byte[16]; RANDOM.nextBytes(inputBytes); long startTime = System.nanoTime(); Files.write(tmpFile, inputBytes); long writeLatency = TimeUnit.MICROSECONDS.convert( System.nanoTime() - startTime, TimeUnit.NANOSECONDS); metric.addWriteFileLatency(writeLatency); startTime = System.nanoTime(); byte[] outputBytes = Files.readAllBytes(tmpFile); long readLatency = TimeUnit.MICROSECONDS.convert( System.nanoTime() - startTime, TimeUnit.NANOSECONDS); metric.addReadFileLatency(readLatency); if (!Arrays.equals(inputBytes, outputBytes)) { metric.diskCheckFailed(); throw new DiskErrorException("Data in file has been corrupted."); } } catch (IOException e) { metric.diskCheckFailed(); throw new DiskErrorException("Disk Check failed!", e); } finally { if (tmpFile != null) { try { Files.delete(tmpFile); } catch (IOException e) { metric.diskCheckFailed(); throw new DiskErrorException("File deletion failed!", e); } } } }  @Override void checkStatus(File dir); static final String NAME; }
@Test public void testReadWriteDiskValidator() throws DiskErrorException, InterruptedException { int count = 100; File testDir = new File(System.getProperty("test.build.data")); ReadWriteDiskValidator readWriteDiskValidator = (ReadWriteDiskValidator) DiskValidatorFactory.getInstance( ReadWriteDiskValidator.NAME); for (int i = 0; i < count; i++) { readWriteDiskValidator.checkStatus(testDir); } ReadWriteDiskValidatorMetrics metric = ReadWriteDiskValidatorMetrics.getMetric(testDir.toString()); Assert.assertEquals("The count number of estimator in MutableQuantiles" + "metrics of file read is not right", metric.getFileReadQuantiles()[0].getEstimator().getCount(), count); Assert.assertEquals("The count number of estimator in MutableQuantiles" + "metrics of file write is not right", metric.getFileWriteQuantiles()[0].getEstimator().getCount(), count); MetricsSource source = ms.getSource( ReadWriteDiskValidatorMetrics.sourceName(testDir.toString())); MetricsCollectorImpl collector = new MetricsCollectorImpl(); source.getMetrics(collector, true); MetricsRecords.assertMetric(collector.getRecords().get(0), "FailureCount", 0); MetricsRecords.assertMetric(collector.getRecords().get(0), "LastFailureTime", (long)0); MetricsRecords.assertMetricNotNull(collector.getRecords().get(0), "WriteLatency3600sNumOps"); MetricsRecords.assertMetricNotNull(collector.getRecords().get(0), "WriteLatency3600s50thPercentileLatencyMicros"); MetricsRecords.assertMetricNotNull(collector.getRecords().get(0), "WriteLatency86400sNumOps"); MetricsRecords.assertMetricNotNull(collector.getRecords().get(0), "WriteLatency864000sNumOps"); MetricsRecords.assertMetricNotNull(collector.getRecords().get(0), "ReadLatency3600sNumOps"); MetricsRecords.assertMetricNotNull(collector.getRecords().get(0), "ReadLatency3600s50thPercentileLatencyMicros"); MetricsRecords.assertMetricNotNull(collector.getRecords().get(0), "ReadLatency86400sNumOps"); MetricsRecords.assertMetricNotNull(collector.getRecords().get(0), "ReadLatency864000sNumOps"); }
@Test public void testCheckFailures() throws Throwable { ReadWriteDiskValidator readWriteDiskValidator = (ReadWriteDiskValidator) DiskValidatorFactory.getInstance( ReadWriteDiskValidator.NAME); File testDir = Files.createTempDirectory( Paths.get(System.getProperty("test.build.data")), "test").toFile(); try { Shell.execCommand(Shell.getSetPermissionCommand("000", false, testDir.getAbsolutePath())); } catch (Exception e){ testDir.delete(); throw e; } try { readWriteDiskValidator.checkStatus(testDir); fail("Disk check should fail."); } catch (DiskErrorException e) { assertEquals("Disk Check failed!", e.getMessage()); } MetricsSource source = ms.getSource( ReadWriteDiskValidatorMetrics.sourceName(testDir.toString())); MetricsCollectorImpl collector = new MetricsCollectorImpl(); source.getMetrics(collector, true); try { readWriteDiskValidator.checkStatus(testDir); fail("Disk check should fail."); } catch (DiskErrorException e) { assertEquals("Disk Check failed!", e.getMessage()); } source.getMetrics(collector, true); MetricsRecords.assertMetric(collector.getRecords().get(0), "FailureCount", 1); Long lastFailureTime1 = (Long) MetricsRecords.getMetricValueByName( collector.getRecords().get(0), "LastFailureTime"); MetricsRecords.assertMetric(collector.getRecords().get(1), "FailureCount", 2); Long lastFailureTime2 = (Long) MetricsRecords.getMetricValueByName( collector.getRecords().get(1), "LastFailureTime"); assertTrue("The first failure time should be less than the second one", lastFailureTime1 < lastFailureTime2); testDir.delete(); }