diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/SerialNumberMap.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/SerialNumberMap.java index ff116b5118..453844ec95 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/SerialNumberMap.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/SerialNumberMap.java @@ -61,17 +61,22 @@ public int get(T t) { } Integer sn = t2i.get(t); if (sn == null) { - sn = current.getAndIncrement(); - if (sn > max) { - current.getAndDecrement(); - throw new IllegalStateException(name + ": serial number map is full"); + synchronized (this) { + sn = t2i.get(t); + if (sn == null) { + sn = current.getAndIncrement(); + if (sn > max) { + current.getAndDecrement(); + throw new IllegalStateException(name + ": serial number map is full"); + } + Integer old = t2i.putIfAbsent(t, sn); + if (old != null) { + current.getAndDecrement(); + return old; + } + i2t.put(sn, t); + } } - Integer old = t2i.putIfAbsent(t, sn); - if (old != null) { - current.getAndDecrement(); - return old; - } - i2t.put(sn, t); } return sn; }