Search in sources :

Example 6 with MyMetrics1

use of org.apache.hadoop.metrics2.sink.RollingFileSystemSinkTestBase.MyMetrics1 in project hadoop by apache.

the class TestRollingFileSystemSinkWithHdfs method testFailedWrite.

/**
   * Test that writing to HDFS fails when HDFS is unavailable.
   *
   * @throws IOException thrown when reading or writing log files
   */
@Test
public void testFailedWrite() throws IOException {
    final String path = "hdfs://" + cluster.getNameNode().getHostAndPort() + "/tmp";
    MetricsSystem ms = initMetricsSystem(path, false, false);
    new MyMetrics1().registerWith(ms);
    shutdownHdfs();
    MockSink.errored = false;
    // publish the metrics
    ms.publishMetricsNow();
    assertTrue("No exception was generated while writing metrics " + "even though HDFS was unavailable", MockSink.errored);
    try {
        ms.stop();
    } finally {
        ms.shutdown();
    }
}
Also used : MyMetrics1(org.apache.hadoop.metrics2.sink.RollingFileSystemSinkTestBase.MyMetrics1) MetricsSystem(org.apache.hadoop.metrics2.MetricsSystem) Test(org.junit.Test)

Example 7 with MyMetrics1

use of org.apache.hadoop.metrics2.sink.RollingFileSystemSinkTestBase.MyMetrics1 in project hadoop by apache.

the class TestFileSink method testFileSink.

@Test(timeout = 6000)
public void testFileSink() throws IOException {
    outFile = getTestTempFile("test-file-sink-", ".out");
    final String outPath = outFile.getAbsolutePath();
    // NB: specify large period to avoid multiple metrics snapshotting: 
    new ConfigBuilder().add("*.period", 10000).add("test.sink.mysink0.class", FileSink.class.getName()).add("test.sink.mysink0.filename", outPath).add("test.sink.mysink0.context", "test1").save(TestMetricsConfig.getTestFilename("hadoop-metrics2-test"));
    MetricsSystemImpl ms = new MetricsSystemImpl("test");
    ms.start();
    final MyMetrics1 mm1 = new MyMetrics1().registerWith(ms);
    new MyMetrics2().registerWith(ms);
    mm1.testMetric1.incr();
    mm1.testMetric2.incr(2);
    // publish the metrics
    ms.publishMetricsNow();
    ms.stop();
    ms.shutdown();
    InputStream is = null;
    ByteArrayOutputStream baos = null;
    String outFileContent = null;
    try {
        is = new FileInputStream(outFile);
        baos = new ByteArrayOutputStream((int) outFile.length());
        IOUtils.copyBytes(is, baos, 1024, true);
        outFileContent = new String(baos.toByteArray(), "UTF-8");
    } finally {
        IOUtils.cleanup(null, baos, is);
    }
    // Check the out file content. Should be something like the following:
    //1360244820087 test1.testRecord1: Context=test1, testTag1=testTagValue1, testTag2=testTagValue2, Hostname=myhost, testMetric1=1, testMetric2=2
    //1360244820089 test1.testRecord2: Context=test1, testTag22=testTagValue22, Hostname=myhost
    // Note that in the below expression we allow tags and metrics to go in arbitrary order.  
    Pattern expectedContentPattern = Pattern.compile(// line #1:
    "^\\d+\\s+test1.testRecord1:\\s+Context=test1,\\s+" + "(testTag1=testTagValue1,\\s+testTag2=testTagValue2|testTag2=testTagValue2,\\s+testTag1=testTagValue1)," + "\\s+Hostname=.*,\\s+(testMetric1=1,\\s+testMetric2=2|testMetric2=2,\\s+testMetric1=1)" + // line #2:
    "$[\\n\\r]*^\\d+\\s+test1.testRecord2:\\s+Context=test1," + "\\s+testTag22=testTagValue22,\\s+Hostname=.*$[\\n\\r]*", Pattern.MULTILINE);
    assertTrue(expectedContentPattern.matcher(outFileContent).matches());
}
Also used : Pattern(java.util.regex.Pattern) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ConfigBuilder(org.apache.hadoop.metrics2.impl.ConfigBuilder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MetricsSystemImpl(org.apache.hadoop.metrics2.impl.MetricsSystemImpl) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 8 with MyMetrics1

use of org.apache.hadoop.metrics2.sink.RollingFileSystemSinkTestBase.MyMetrics1 in project hadoop by apache.

the class TestRollingFileSystemSinkWithLocal method testSilentFailedWrite.

/**
   * Test that writing fails silently when the directory is not writable.
   */
@Test
public void testSilentFailedWrite() {
    String path = methodDir.getAbsolutePath();
    MetricsSystem ms = initMetricsSystem(path, true, false);
    new MyMetrics1().registerWith(ms);
    methodDir.setWritable(false);
    MockSink.errored = false;
    try {
        // publish the metrics
        ms.publishMetricsNow();
        assertFalse("An exception was generated while writing metrics " + "when the target directory was not writable, even though the " + "sink is set to ignore errors", MockSink.errored);
        ms.stop();
        ms.shutdown();
    } finally {
        // Make sure the dir is writable again so we can delete it at the end
        methodDir.setWritable(true);
    }
}
Also used : MetricsSystem(org.apache.hadoop.metrics2.MetricsSystem) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)8 MetricsSystem (org.apache.hadoop.metrics2.MetricsSystem)7 MyMetrics1 (org.apache.hadoop.metrics2.sink.RollingFileSystemSinkTestBase.MyMetrics1)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 Calendar (java.util.Calendar)1 Pattern (java.util.regex.Pattern)1 Configuration (org.apache.hadoop.conf.Configuration)1 FileStatus (org.apache.hadoop.fs.FileStatus)1 FileSystem (org.apache.hadoop.fs.FileSystem)1 Path (org.apache.hadoop.fs.Path)1 MetricsException (org.apache.hadoop.metrics2.MetricsException)1 ConfigBuilder (org.apache.hadoop.metrics2.impl.ConfigBuilder)1 MetricsSystemImpl (org.apache.hadoop.metrics2.impl.MetricsSystemImpl)1