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 blockUpdates() {
057    region.blockUpdates();
058  }
059
060  public void unblockUpdates() {
061    region.unblockUpdates();
062  }
063
064  public void addMemStoreSize(long dataSizeDelta, long heapSizeDelta, long offHeapSizeDelta,
065      int cellsCountDelta) {
066    region.incMemStoreSize(dataSizeDelta, heapSizeDelta, offHeapSizeDelta, cellsCountDelta);
067  }
068
069  public RegionInfo getRegionInfo() {
070    return region.getRegionInfo();
071  }
072
073  public WAL getWAL() {
074    return region.getWAL();
075  }
076
077  private static ThreadPoolExecutor INMEMORY_COMPACTION_POOL_FOR_TEST;
078
079  private static synchronized ThreadPoolExecutor getInMemoryCompactionPoolForTest() {
080    if (INMEMORY_COMPACTION_POOL_FOR_TEST == null) {
081      INMEMORY_COMPACTION_POOL_FOR_TEST = new ThreadPoolExecutor(10, 10, 60, TimeUnit.SECONDS,
082        new LinkedBlockingQueue<>(), new ThreadFactoryBuilder().setDaemon(true)
083          .setNameFormat("InMemoryCompactionsForTest-%d").build());
084    }
085    return INMEMORY_COMPACTION_POOL_FOR_TEST;
086  }
087
088  ThreadPoolExecutor getInMemoryCompactionPool() {
089    if (rsServices != null) {
090      return rsServices.getExecutorService().getExecutorLazily(ExecutorType.RS_IN_MEMORY_COMPACTION,
091        inMemoryPoolSize);
092    } else {
093      // this could only happen in tests
094      return getInMemoryCompactionPoolForTest();
095    }
096  }
097
098  public long getMemStoreFlushSize() {
099    return region.getMemStoreFlushSize();
100  }
101
102  public int getNumStores() {
103    return region.getTableDescriptor().getColumnFamilyCount();
104  }
105
106  @VisibleForTesting
107  long getMemStoreSize() {
108    return region.getMemStoreDataSize();
109  }
110}