Search in sources :

Example 6 with MetricsException

use of org.apache.hadoop.metrics2.MetricsException in project hadoop by apache.

the class AbstractPatternFilter method init.

@Override
public void init(SubsetConfiguration conf) {
    String patternString = conf.getString(INCLUDE_KEY);
    if (patternString != null && !patternString.isEmpty()) {
        setIncludePattern(compile(patternString));
    }
    patternString = conf.getString(EXCLUDE_KEY);
    if (patternString != null && !patternString.isEmpty()) {
        setExcludePattern(compile(patternString));
    }
    String[] patternStrings = conf.getStringArray(INCLUDE_TAGS_KEY);
    if (patternStrings != null && patternStrings.length != 0) {
        for (String pstr : patternStrings) {
            Matcher matcher = tagPattern.matcher(pstr);
            if (!matcher.matches()) {
                throw new MetricsException("Illegal tag pattern: " + pstr);
            }
            setIncludeTagPattern(matcher.group(1), compile(matcher.group(2)));
        }
    }
    patternStrings = conf.getStringArray(EXCLUDE_TAGS_KEY);
    if (patternStrings != null && patternStrings.length != 0) {
        for (String pstr : patternStrings) {
            Matcher matcher = tagPattern.matcher(pstr);
            if (!matcher.matches()) {
                throw new MetricsException("Illegal tag pattern: " + pstr);
            }
            setExcludeTagPattern(matcher.group(1), compile(matcher.group(2)));
        }
    }
}
Also used : Matcher(com.google.re2j.Matcher) MetricsException(org.apache.hadoop.metrics2.MetricsException)

Example 7 with MetricsException

use of org.apache.hadoop.metrics2.MetricsException in project hadoop by apache.

the class TestRollingFileSystemSink method testGetRollInterval.

/**
   * Test whether the roll interval is correctly calculated from the
   * configuration settings.
   */
@Test
public void testGetRollInterval() {
    doTestGetRollInterval(1, new String[] { "m", "min", "minute", "minutes" }, 60 * 1000L);
    doTestGetRollInterval(1, new String[] { "h", "hr", "hour", "hours" }, 60 * 60 * 1000L);
    doTestGetRollInterval(1, new String[] { "d", "day", "days" }, 24 * 60 * 60 * 1000L);
    ConfigBuilder builder = new ConfigBuilder();
    SubsetConfiguration conf = builder.add("sink.roll-interval", "1").subset("sink");
    // We can reuse the same sink evry time because we're setting the same
    // property every time.
    RollingFileSystemSink sink = new RollingFileSystemSink();
    sink.init(conf);
    assertEquals(3600000L, sink.getRollInterval());
    for (char c : "abcefgijklnopqrtuvwxyz".toCharArray()) {
        builder = new ConfigBuilder();
        conf = builder.add("sink.roll-interval", "90 " + c).subset("sink");
        try {
            sink.init(conf);
            sink.getRollInterval();
            fail("Allowed flush interval with bad units: " + c);
        } catch (MetricsException ex) {
        // Expected
        }
    }
}
Also used : ConfigBuilder(org.apache.hadoop.metrics2.impl.ConfigBuilder) MetricsException(org.apache.hadoop.metrics2.MetricsException) SubsetConfiguration(org.apache.commons.configuration2.SubsetConfiguration) Test(org.junit.Test)

Example 8 with MetricsException

use of org.apache.hadoop.metrics2.MetricsException in project hadoop by apache.

the class TestRollingFileSystemSinkWithHdfs method testFailedClose.

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

Example 9 with MetricsException

use of org.apache.hadoop.metrics2.MetricsException in project hbase by apache.

the class DynamicMetricsRegistry method tag.

/**
   * Add a tag to the metrics
   * @param info  metadata of the tag
   * @param value of the tag
   * @param override existing tag if true
   * @return the registry (for keep adding tags etc.)
   */
public DynamicMetricsRegistry tag(MetricsInfo info, String value, boolean override) {
    MetricsTag tag = Interns.tag(info, value);
    if (!override) {
        MetricsTag existing = tagsMap.putIfAbsent(info.name(), tag);
        if (existing != null) {
            throw new MetricsException("Tag " + info.name() + " already exists!");
        }
        return this;
    }
    tagsMap.put(info.name(), tag);
    return this;
}
Also used : MetricsException(org.apache.hadoop.metrics2.MetricsException) MetricsTag(org.apache.hadoop.metrics2.MetricsTag)

Example 10 with MetricsException

use of org.apache.hadoop.metrics2.MetricsException in project hadoop by apache.

the class RollingFileSystemSink method getRollInterval.

/**
   * Extract the roll interval from the configuration and return it in
   * milliseconds.
   *
   * @return the roll interval in millis
   */
@VisibleForTesting
protected long getRollInterval() {
    String rollInterval = properties.getString(ROLL_INTERVAL_KEY, DEFAULT_ROLL_INTERVAL);
    Pattern pattern = Pattern.compile("^\\s*(\\d+)\\s*([A-Za-z]*)\\s*$");
    Matcher match = pattern.matcher(rollInterval);
    long millis;
    if (match.matches()) {
        String flushUnit = match.group(2);
        int rollIntervalInt;
        try {
            rollIntervalInt = Integer.parseInt(match.group(1));
        } catch (NumberFormatException ex) {
            throw new MetricsException("Unrecognized flush interval: " + rollInterval + ". Must be a number followed by an optional " + "unit. The unit must be one of: minute, hour, day", ex);
        }
        if ("".equals(flushUnit)) {
            millis = TimeUnit.HOURS.toMillis(rollIntervalInt);
        } else {
            switch(flushUnit.toLowerCase()) {
                case "m":
                case "min":
                case "minute":
                case "minutes":
                    millis = TimeUnit.MINUTES.toMillis(rollIntervalInt);
                    break;
                case "h":
                case "hr":
                case "hour":
                case "hours":
                    millis = TimeUnit.HOURS.toMillis(rollIntervalInt);
                    break;
                case "d":
                case "day":
                case "days":
                    millis = TimeUnit.DAYS.toMillis(rollIntervalInt);
                    break;
                default:
                    throw new MetricsException("Unrecognized unit for flush interval: " + flushUnit + ". Must be one of: minute, hour, day");
            }
        }
    } else {
        throw new MetricsException("Unrecognized flush interval: " + rollInterval + ". Must be a number followed by an optional unit." + " The unit must be one of: minute, hour, day");
    }
    if (millis < 60000) {
        throw new MetricsException("The flush interval property must be " + "at least 1 minute. Value was " + rollInterval);
    }
    return millis;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) MetricsException(org.apache.hadoop.metrics2.MetricsException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

MetricsException (org.apache.hadoop.metrics2.MetricsException)16 IOException (java.io.IOException)5 AbstractMetric (org.apache.hadoop.metrics2.AbstractMetric)3 MetricsTag (org.apache.hadoop.metrics2.MetricsTag)3 Test (org.junit.Test)3 ExecutionException (java.util.concurrent.ExecutionException)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Matcher (com.google.re2j.Matcher)1 Annotation (java.lang.annotation.Annotation)1 URISyntaxException (java.net.URISyntaxException)1 Instant (java.time.Instant)1 LocalDateTime (java.time.LocalDateTime)1 Collection (java.util.Collection)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 Timer (java.util.Timer)1