001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.regionserver.throttle;
019
020import java.util.concurrent.atomic.AtomicInteger;
021
022import org.apache.hadoop.hbase.regionserver.HStore;
023import org.apache.yetus.audience.InterfaceAudience;
024
025/**
026 * Helper methods for throttling
027 */
028@InterfaceAudience.Private
029public final class ThroughputControlUtil {
030  private ThroughputControlUtil() {
031  }
032
033  private static final AtomicInteger NAME_COUNTER = new AtomicInteger(0);
034  private static final String NAME_DELIMITER = "#";
035
036  /**
037   * Generate a name for throttling, to prevent name conflict when multiple IO operation running
038   * parallel on the same store.
039   * @param store the Store instance on which IO operation is happening
040   * @param opName Name of the IO operation, e.g. "flush", "compaction", etc.
041   * @return The name for throttling
042   */
043  public static String getNameForThrottling(HStore store, String opName) {
044    int counter;
045    for (;;) {
046      counter = NAME_COUNTER.get();
047      int next = counter == Integer.MAX_VALUE ? 0 : counter + 1;
048      if (NAME_COUNTER.compareAndSet(counter, next)) {
049        break;
050      }
051    }
052    return store.getRegionInfo().getEncodedName() + NAME_DELIMITER +
053        store.getColumnFamilyDescriptor().getNameAsString() + NAME_DELIMITER + opName +
054        NAME_DELIMITER + counter;
055  }
056}