mythrocks commented on a change in pull request #807:
URL: https://github.com/apache/hive/pull/807#discussion_r434898293



##########
File path: serde/src/java/org/apache/hadoop/hive/serde2/avro/InstanceCache.java
##########
@@ -51,20 +51,19 @@ public Instance retrieve(SeedObject hv) throws 
AvroSerdeException {
    * Retrieve (or create if it doesn't exist) the correct Instance for this
    * SeedObject using 'seenSchemas' to resolve circular references
    */
-  public synchronized Instance retrieve(SeedObject hv,
-      Set<SeedObject> seenSchemas) throws AvroSerdeException {
+  public Instance retrieve(SeedObject hv, Set<SeedObject> seenSchemas)
+    throws AvroSerdeException {
     if(LOG.isDebugEnabled()) LOG.debug("Checking for hv: " + hv.toString());
 
     if(cache.containsKey(hv)) {

Review comment:
       Ok, I think I have it.
   
   
[`InstanceCache::retrieve()`](https://github.com/apache/hive/blob/f37c5de6c32b9395d1b34fa3c02ed06d1bfbf6eb/serde/src/java/org/apache/hadoop/hive/serde2/avro/InstanceCache.java#L66)
 modifies the cache here:
   ```java
       Instance instance = makeInstance(hv, seenSchemas);
       cache.put(hv, instance);
       return instance;
   ```
   We would like to use `computeIfAbsent()` instead:
   ```java
   cache.computeIfAbsent(hv, ()->makeInstance(hv, seenSchemas));
   ```
   If `makeInstance()` ends up modifying `cache`, (e.g. by calling 
`retrieve()`), that would cause undefined behaviour in`computeIfAbsent()`.
   
   
[`SchemaToTypeInfo`](https://github.com/apache/hive/blob/f37c5de6c32b9395d1b34fa3c02ed06d1bfbf6eb/serde/src/java/org/apache/hadoop/hive/serde2/avro/SchemaToTypeInfo.java#L116)
 implements `InstanceCache::makeInstance()` as follows:
   ```java
     static InstanceCache<Schema, TypeInfo> typeInfoCache = new 
InstanceCache<Schema, TypeInfo>() {
                                     @Override
                                     protected TypeInfo makeInstance(Schema s,
                                         Set<Schema> seenSchemas)
                                         throws AvroSerdeException {
                                       return generateTypeInfoWorker(s, 
seenSchemas); // <---- HERE!
                                     }
                                   };
   ```
   
   Please note that 
[`SchemaToTypeInfo::generateTypeInfo()`](https://github.com/apache/hive/blob/f37c5de6c32b9395d1b34fa3c02ed06d1bfbf6eb/serde/src/java/org/apache/hadoop/hive/serde2/avro/SchemaToTypeInfo.java#L186)
 happens to call `InstanceCache::retrieve()`:
   ```java
     public static TypeInfo generateTypeInfo(Schema schema,
         Set<Schema> seenSchemas) throws AvroSerdeException {
       // ...
       return typeInfoCache.retrieve(schema, seenSchemas);
     }
   ```
   Hence, the recursive call. :/




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to