HBASE-21843 RegionGroupingProvider breaks the meta wal file name pattern which may...
[hbase.git] / hbase-server / src / main / java / org / apache / hadoop / hbase / wal / BoundedGroupingStrategy.java
blobbafcee339e7d7bc418ff16f3430ab46a710147cc
1 /**
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 package org.apache.hadoop.hbase.wal;
21 import static org.apache.hadoop.hbase.util.ConcurrentMapUtils.computeIfAbsent;
23 import java.util.concurrent.ConcurrentHashMap;
24 import java.util.concurrent.atomic.AtomicInteger;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.yetus.audience.InterfaceAudience;
28 import org.apache.hadoop.hbase.util.Bytes;
29 import org.apache.hadoop.hbase.wal.RegionGroupingProvider.RegionGroupingStrategy;
31 /**
32 * A WAL grouping strategy that limits the number of wal groups to
33 * "hbase.wal.regiongrouping.numgroups".
35 @InterfaceAudience.Private
36 public class BoundedGroupingStrategy implements RegionGroupingStrategy{
38 static final String NUM_REGION_GROUPS = "hbase.wal.regiongrouping.numgroups";
39 static final int DEFAULT_NUM_REGION_GROUPS = 2;
41 private ConcurrentHashMap<String, String> groupNameCache = new ConcurrentHashMap<>();
42 private AtomicInteger counter = new AtomicInteger(0);
43 private String[] groupNames;
45 @Override
46 public String group(byte[] identifier, byte[] namespace) {
47 String idStr = Bytes.toString(identifier);
48 return computeIfAbsent(groupNameCache, idStr,
49 () -> groupNames[getAndIncrAtomicInteger(counter, groupNames.length)]);
52 // Non-blocking incrementing & resetting of AtomicInteger.
53 private int getAndIncrAtomicInteger(AtomicInteger atomicInt, int reset) {
54 for (;;) {
55 int current = atomicInt.get();
56 int next = (current + 1);
57 if (next == reset) {
58 next = 0;
60 if (atomicInt.compareAndSet(current, next)) return current;
64 @Override
65 public void init(Configuration config, String providerId) {
66 int regionGroupNumber = config.getInt(NUM_REGION_GROUPS, DEFAULT_NUM_REGION_GROUPS);
67 groupNames = new String[regionGroupNumber];
68 for (int i = 0; i < regionGroupNumber; i++) {
69 groupNames[i] = providerId + GROUP_NAME_DELIMITER + "regiongroup-" + i;