Uploaded image for project: 'Apache Avro'
  1. Apache Avro
  2. AVRO-2636

GenericData defaultValueCache caches mutable ByteBuffers

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • None
    • None
    • java
    • None

    Description

      It appears that for default value for Byte type (and Decimal logical type if it uses underlying Bytes type) value rendered with getDefaultValue is cached. This leads to bugs when you read the same value (for example if converted with DecimalConversion). For single thread environment workaround would be to reset ByteBuffer after read, but in concurrent environment we should not cache mutable objects.

       

      @Test(expected=NumberFormatException.class)
      public void testReuse() {
          Conversions.DecimalConversion decimalConversion =
                  new Conversions.DecimalConversion();
      
          LogicalType logicalDecimal =
                  LogicalTypes.decimal(38, 9);
      
          ByteBuffer defaultValue =
                  decimalConversion.toBytes(
                          BigDecimal.valueOf(42L).setScale(9),
                          null,
                          logicalDecimal);
      
          Schema schema = SchemaBuilder
                  .record("test")
                  .fields()
                  .name("decimal")
                  .type(logicalDecimal.addToSchema(SchemaBuilder.builder().bytesType()))
                  .withDefault(defaultValue)
                  .endRecord();
      
          BigDecimal firstRead = decimalConversion
                  .fromBytes(
                          (ByteBuffer) GenericData.get().getDefaultValue(schema.getField("decimal")),
                          null,
                          logicalDecimal);
      
          BigDecimal secondRead = decimalConversion
                  .fromBytes(
                          (ByteBuffer) GenericData.get().getDefaultValue(schema.getField("decimal")),
                          null,
                          logicalDecimal);
      
      }
      

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              rednikotin Valentin Nikotin
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated: