Skip to content

Commit

Permalink
HIVE-19860 : HiveServer2 ObjectInspectorFactory memory leak with cach…
Browse files Browse the repository at this point in the history
…edUnionStructObjectInspector (Rajkumar Singh)
  • Loading branch information
sershe-apache committed Jul 3, 2018
1 parent 2ca70b9 commit cc8ac97
Showing 1 changed file with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package org.apache.hadoop.hive.serde2.objectinspector;



import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
Expand All @@ -29,11 +31,13 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import com.google.common.cache.CacheBuilder;
import org.apache.hadoop.hive.common.StringInternUtils;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils;
import org.apache.thrift.TUnion;

import java.util.concurrent.TimeUnit;
import com.google.common.cache.Cache;
/**
* ObjectInspectorFactory is the primary way to create new ObjectInspector
* instances.
Expand All @@ -47,7 +51,6 @@
* ObjectInspector.
*/
public final class ObjectInspectorFactory {

/**
* ObjectInspectorOptions describes what ObjectInspector to use. JAVA is to
* use pure JAVA reflection. THRIFT is to use JAVA reflection and filter out
Expand Down Expand Up @@ -342,20 +345,19 @@ public static StandardConstantStructObjectInspector getStandardConstantStructObj
return new StandardConstantStructObjectInspector(structFieldNames, structFieldObjectInspectors, value);
}

static ConcurrentHashMap<List<StructObjectInspector>, UnionStructObjectInspector> cachedUnionStructObjectInspector =
new ConcurrentHashMap<List<StructObjectInspector>, UnionStructObjectInspector>();
static Cache<List<StructObjectInspector>, UnionStructObjectInspector> cachedUnionStructObjectInspector = CacheBuilder.newBuilder()
.initialCapacity(1024)
.concurrencyLevel(Runtime.getRuntime().availableProcessors())
.expireAfterAccess(5,TimeUnit.MINUTES)
.softValues()
.build();

public static UnionStructObjectInspector getUnionStructObjectInspector(
List<StructObjectInspector> structObjectInspectors) {
UnionStructObjectInspector result = cachedUnionStructObjectInspector
.get(structObjectInspectors);
UnionStructObjectInspector result = cachedUnionStructObjectInspector.getIfPresent(structObjectInspectors);
if (result == null) {
result = new UnionStructObjectInspector(structObjectInspectors);
UnionStructObjectInspector prev =
cachedUnionStructObjectInspector.putIfAbsent(structObjectInspectors, result);
if (prev != null) {
result = prev;
}
cachedUnionStructObjectInspector.put(structObjectInspectors, result);
}
return result;
}
Expand Down

0 comments on commit cc8ac97

Please sign in to comment.