From 3727ac9bde3454019cd10afbb74d764e97e02e09 Mon Sep 17 00:00:00 2001 From: Tsz-wo Sze Date: Wed, 13 Nov 2013 22:50:58 +0000 Subject: [PATCH] HADOOP-10095. In CodecPool, synchronize pool and codecList separately in order to reduce lock contention. Contributed by Nicolas Liochon git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1541750 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop-common/CHANGES.txt | 3 ++ .../apache/hadoop/io/compress/CodecPool.java | 31 ++++++++++--------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index 0bec5356dc..e7790f70fc 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -378,6 +378,9 @@ Release 2.3.0 - UNRELEASED HADOOP-9594. Update apache commons math dependency (Timothy St. Clair via stevel) + HADOOP-10095. In CodecPool, synchronize pool and codecList separately in + order to reduce lock contention. (Nicolas Liochon via szetszwo) + OPTIMIZATIONS HADOOP-9748. Reduce blocking on UGI.ensureInitialized (daryn) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/compress/CodecPool.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/compress/CodecPool.java index 63e3a09c30..11d88f13cd 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/compress/CodecPool.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/compress/CodecPool.java @@ -85,16 +85,15 @@ private static T borrow(Map, List> pool, T codec = null; // Check if an appropriate codec is available + List codecList; synchronized (pool) { - if (pool.containsKey(codecClass)) { - List codecList = pool.get(codecClass); - - if (codecList != null) { - synchronized (codecList) { - if (!codecList.isEmpty()) { - codec = codecList.remove(codecList.size()-1); - } - } + codecList = pool.get(codecClass); + } + + if (codecList != null) { + synchronized (codecList) { + if (!codecList.isEmpty()) { + codec = codecList.remove(codecList.size() - 1); } } } @@ -105,15 +104,17 @@ private static T borrow(Map, List> pool, private static void payback(Map, List> pool, T codec) { if (codec != null) { Class codecClass = ReflectionUtils.getClass(codec); + List codecList; synchronized (pool) { - if (!pool.containsKey(codecClass)) { - pool.put(codecClass, new ArrayList()); + codecList = pool.get(codecClass); + if (codecList == null) { + codecList = new ArrayList(); + pool.put(codecClass, codecList); } + } - List codecList = pool.get(codecClass); - synchronized (codecList) { - codecList.add(codec); - } + synchronized (codecList) { + codecList.add(codec); } } }