From c32a2c0b16b1d7e41fd0ad4a2737b7f0f2806c82 Mon Sep 17 00:00:00 2001 From: Andrew Purtell Date: Mon, 12 Jan 2015 16:06:51 -0800 Subject: [PATCH] HBASE-12839 Remove synchronization in ServerStatisticsTracker --- .../hbase/client/ServerStatisticTracker.java | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ServerStatisticTracker.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ServerStatisticTracker.java index 0c7b683d79a..42da0b3dd7d 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ServerStatisticTracker.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ServerStatisticTracker.java @@ -34,7 +34,7 @@ import java.util.concurrent.ConcurrentHashMap; @InterfaceAudience.Private public class ServerStatisticTracker { - private final Map stats = + private final ConcurrentHashMap stats = new ConcurrentHashMap(); public void updateRegionStats(ServerName server, byte[] region, ClientProtos.RegionLoadStats @@ -42,14 +42,15 @@ public class ServerStatisticTracker { ServerStatistics stat = stats.get(server); if (stat == null) { - // create a stats object and update the stats - synchronized (this) { - stat = stats.get(server); - // we don't have stats for that server yet, so we need to make some - if (stat == null) { - stat = new ServerStatistics(); - stats.put(server, stat); - } + stat = stats.get(server); + // We don't have stats for that server yet, so we need to make an entry. + // If we race with another thread it's a harmless unnecessary allocation. + if (stat == null) { + stat = new ServerStatistics(); + ServerStatistics old = stats.putIfAbsent(server, stat); + if (old != null) { + stat = old; + } } } stat.update(region, currentStats); @@ -71,4 +72,4 @@ public class ServerStatisticTracker { ServerStatistics getServerStatsForTesting(ServerName server) { return stats.get(server); } -} \ No newline at end of file +}