Uploaded image for project: 'Thrift'
  1. Thrift
  2. THRIFT-4513

thrift generated code is not stable for constants

VotersWatch issueWatchersLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 0.12.0
    • Component/s: Compiler (General)
    • Labels:
      None

      Description

      In certain cases, output generated with java compiler is unstable, this prevents correct incremental generation, artifact caching, etc. 

      This is especially crucible if the goal is reproducible builds: same results for the same source inputs. 

      Here's example: 

      Let's say we have thrift definition:

      const map<string, Enum1Enum2> CONSTANT_MAP =

      { 'key1' :

      {"enum1" : Enum1.ENUM1_VALUE1, "enum2" : Enum2.ENUM2_VALUE}

      ,
      'key2' : {"enum1" : Enum1.ENUM1_VALUE2, "enum2" : Enum2.ENUM2_VALUE},
      'key3' : {"enum1" : Enum1.ENUM1_VALUE3, "enum2" : Enum2.ENUM2_VALUE},
      ....
      }

      Compiler generates code like:

      public static final java.util.Map<java.lang.String,Enum1Enum2> CONSTANT_MAP = new java.util.HashMap<java.lang.String,Enum1Enum2>();
      static {
      Enum1Enum2 tmp199 = new Enum1Enum2();
      tmp199.setEnum1(com.java.package.Enum1.ENUM1_VALUE2);
      tmp199.setEnum2(com.java.package.Enum2.ENUM2_VALUE);

      CONSTANT_MAP.put("key2", tmp199);
      Enum1Enum2 tmp200 = new Enum1Enum2();
      tmp200.setEnum1(com.java.package.Enum1.ENUM1_VALUE1);
      tmp200.setEnum2(com.java.package.Enum2.ENUM2_VALUE);

      CONSTANT_MAP.put("key1", tmp200);
      Enum1Enum2 tmp201 = new Enum1Enum2();
      tmp201.setEnum1(com.java.package.Enum1.ENUM1_VALUE3);
      tmp201.setEnum2(com.java.package.Enum2.ENUM2_VALUE);

      CONSTANT_MAP.put("key3", tmp201);

      Possible reorderings:

      1. Values are added in different order from run to run.

      2. Set methods in value construction may be called in different order.

      Note: this results in equivalent java maps, so there is no runtime issues. 

      Proposed remedy:
      When building construction of Constants from JSON, java code generator should use by-key ordering.

       

        Attachments

          Activity

            People

            • Assignee:
              jking3 James E. King III
              Reporter:
              romanoid Roman Soroka

              Dates

              • Created:
                Updated:
                Resolved:

                Issue deployment