HBASE-12839 Remove synchronization in ServerStatisticsTracker

This commit is contained in:
Andrew Purtell 2015-01-12 16:06:51 -08:00
parent 8816fa05c2
commit c32a2c0b16
1 changed files with 11 additions and 10 deletions

View File

@ -34,7 +34,7 @@ import java.util.concurrent.ConcurrentHashMap;
@InterfaceAudience.Private @InterfaceAudience.Private
public class ServerStatisticTracker { public class ServerStatisticTracker {
private final Map<ServerName, ServerStatistics> stats = private final ConcurrentHashMap<ServerName, ServerStatistics> stats =
new ConcurrentHashMap<ServerName, ServerStatistics>(); new ConcurrentHashMap<ServerName, ServerStatistics>();
public void updateRegionStats(ServerName server, byte[] region, ClientProtos.RegionLoadStats public void updateRegionStats(ServerName server, byte[] region, ClientProtos.RegionLoadStats
@ -42,14 +42,15 @@ public class ServerStatisticTracker {
ServerStatistics stat = stats.get(server); ServerStatistics stat = stats.get(server);
if (stat == null) { if (stat == null) {
// create a stats object and update the stats stat = stats.get(server);
synchronized (this) { // We don't have stats for that server yet, so we need to make an entry.
stat = stats.get(server); // If we race with another thread it's a harmless unnecessary allocation.
// we don't have stats for that server yet, so we need to make some if (stat == null) {
if (stat == null) { stat = new ServerStatistics();
stat = new ServerStatistics(); ServerStatistics old = stats.putIfAbsent(server, stat);
stats.put(server, stat); if (old != null) {
} stat = old;
}
} }
} }
stat.update(region, currentStats); stat.update(region, currentStats);
@ -71,4 +72,4 @@ public class ServerStatisticTracker {
ServerStatistics getServerStatsForTesting(ServerName server) { ServerStatistics getServerStatsForTesting(ServerName server) {
return stats.get(server); return stats.get(server);
} }
} }