HBASE-15563 'counter' may overflow in BoundedGroupingStrategy (Matt Warhaftig)

This commit is contained in:
tedyu 2016-05-05 10:29:10 -07:00
parent 686a31fbde
commit 3a7552a9f3
1 changed files with 13 additions and 1 deletions

View File

@ -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);