diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/wal/BoundedGroupingStrategy.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/wal/BoundedGroupingStrategy.java index 65c774ea4f4..06f879234ca 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/wal/BoundedGroupingStrategy.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/wal/BoundedGroupingStrategy.java @@ -46,7 +46,7 @@ public class BoundedGroupingStrategy implements RegionGroupingStrategy{ String idStr = Bytes.toString(identifier); String groupName = groupNameCache.get(idStr); if (null == groupName) { - groupName = groupNames[counter.getAndIncrement() % groupNames.length]; + groupName = groupNames[getAndIncrAtomicInteger(counter, groupNames.length)]; String extantName = groupNameCache.putIfAbsent(idStr, groupName); if (extantName != null) { return extantName; @@ -55,6 +55,18 @@ public class BoundedGroupingStrategy implements RegionGroupingStrategy{ return groupName; } + // Non-blocking incrementing & resetting of AtomicInteger. + private int getAndIncrAtomicInteger(AtomicInteger atomicInt, int reset) { + for (;;) { + int current = atomicInt.get(); + int next = (current + 1); + if (next == reset) { + next = 0; + } + if (atomicInt.compareAndSet(current, next)) return current; + } + } + @Override public void init(Configuration config, String providerId) { int regionGroupNumber = config.getInt(NUM_REGION_GROUPS, DEFAULT_NUM_REGION_GROUPS);