Uploaded image for project: 'Hadoop Common'
  1. Hadoop Common
  2. HADOOP-16897

Sort fields in ReflectionUtils.java

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • 3.3.0
    • 3.3.0
    • test, util
    • None
    • Patch

    Description

      The tests in org.apache.hadoop.metrics2.impl.TestMetricsSystemImpl#testInitFirstVerifyCallBacks and org.apache.hadoop.metrics2.impl.TestMetricsSystemImpl#testInitFirstVerifyStopInvokedImmediately can fail.

      java.lang.AssertionError:

      Element 0 for metrics expected:<MetricCounterLong{info=MetricsInfoImpl

      {name=C1, description=C1 desc}

      , value=1}>

      but was:<MetricGaugeLong{info=MetricsInfoImpl

      {name=G1, description=G1 desc}

      , value=2}>

      at org.junit.Assert.fail(Assert.java:88)

      at org.junit.Assert.failNotEquals(Assert.java:834)

      at org.junit.Assert.assertEquals(Assert.java:118)

      at org.apache.hadoop.test.MoreAsserts.assertEquals(MoreAsserts.java:60)

      at org.apache.hadoop.metrics2.impl.TestMetricsSystemImpl.checkMetricsRecords(TestMetricsSystemImpl.java:439)

      at org.apache.hadoop.metrics2.impl.TestMetricsSystemImpl.testInitFirstVerifyCallBacks(TestMetricsSystemImpl.java:178)

       

      The root cause of this failure can be analyzed in the following stack trace:

      java.lang.Class.getDeclaredFields(Class.java:1916)
      org.apache.hadoop.util.ReflectionUtils.getDeclaredFieldsIncludingInherited(ReflectionUtils.java:353)
      org.apache.hadoop.metrics2.lib.MetricsSourceBuilder.<init>(MetricsSourceBuilder.java:68)
      org.apache.hadoop.metrics2.lib.MetricsAnnotations.newSourceBuilder(MetricsAnnotations.java:43)
      org.apache.hadoop.metrics2.impl.MetricsSystemImpl.register(MetricsSystemImpl.java:223)
      org.apache.hadoop.metrics2.impl.TestMetricsSystemImpl.testInitFirstVerifyCallBacks(TestMetricsSystemImpl.java:156)

      The specification about getDeclaredFields() says that "the elements in the returned array are not sorted and are not in any particular order". The documentation is here for your reference: https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getDeclaredFields--
      And the behaviour might be different for different JVM versions or vendors

       

      The fix is to sort the fields returned by getDeclaredFields() so that the non-deterministic behaviour can be eliminated completely. In this way, the test becomes more stable and it will not suffer from the failure above any more.

       

       

       

       

      Attachments

        Issue Links

          Activity

            People

              cpugputpu cpugputpu
              cpugputpu cpugputpu
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: