001/*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.regionserver;
020
021import java.util.concurrent.LinkedBlockingQueue;
022import java.util.concurrent.ThreadPoolExecutor;
023import java.util.concurrent.TimeUnit;
024import org.apache.hadoop.hbase.client.RegionInfo;
025import org.apache.hadoop.hbase.executor.ExecutorType;
026import org.apache.hadoop.hbase.wal.WAL;
027import org.apache.yetus.audience.InterfaceAudience;
028
029import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;
030import org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder;
031
032/**
033 * Services a Store needs from a Region.
034 * RegionServicesForStores class is the interface through which memstore access services at the
035 * region level.
036 * For example, when using alternative memory formats or due to compaction the memstore needs to
037 * take occasional lock and update size counters at the region level.
038 */
039@InterfaceAudience.Private
040public class RegionServicesForStores {
041
042  private final HRegion region;
043  private final RegionServerServices rsServices;
044  private int inMemoryPoolSize;
045
046  public RegionServicesForStores(HRegion region, RegionServerServices rsServices) {
047    this.region = region;
048    this.rsServices = rsServices;
049    if (this.rsServices != null) {
050      this.inMemoryPoolSize = rsServices.getConfiguration().getInt(
051        CompactingMemStore.IN_MEMORY_CONPACTION_POOL_SIZE_KEY,
052        CompactingMemStore.IN_MEMORY_CONPACTION_POOL_SIZE_DEFAULT);
053    }
054  }
055
056  public void addMemStoreSize(long dataSizeDelta, long heapSizeDelta, long offHeapSizeDelta,
057      int cellsCountDelta) {
058    region.incMemStoreSize(dataSizeDelta, heapSizeDelta, offHeapSizeDelta, cellsCountDelta);
059  }
060
061  public RegionInfo getRegionInfo() {
062    return region.getRegionInfo();
063  }
064
065  public WAL getWAL() {
066    return region.getWAL();
067  }
068
069  private static ThreadPoolExecutor INMEMORY_COMPACTION_POOL_FOR_TEST;
070
071  private static synchronized ThreadPoolExecutor getInMemoryCompactionPoolForTest() {
072    if (INMEMORY_COMPACTION_POOL_FOR_TEST == null) {
073      INMEMORY_COMPACTION_POOL_FOR_TEST = new ThreadPoolExecutor(10, 10, 60, TimeUnit.SECONDS,
074        new LinkedBlockingQueue<>(), new ThreadFactoryBuilder().setDaemon(true)
075          .setNameFormat("InMemoryCompactionsForTest-%d").build());
076    }
077    return INMEMORY_COMPACTION_POOL_FOR_TEST;
078  }
079
080  ThreadPoolExecutor getInMemoryCompactionPool() {
081    if (rsServices != null) {
082      return rsServices.getExecutorService().getExecutorLazily(ExecutorType.RS_IN_MEMORY_COMPACTION,
083        inMemoryPoolSize);
084    } else {
085      // this could only happen in tests
086      return getInMemoryCompactionPoolForTest();
087    }
088  }
089
090  public long getMemStoreFlushSize() {
091    return region.getMemStoreFlushSize();
092  }
093
094  public int getNumStores() {
095    return region.getTableDescriptor().getColumnFamilyCount();
096  }
097
098  @VisibleForTesting
099  long getMemStoreSize() {
100    return region.getMemStoreDataSize();
101  }
102}