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.io.ByteBuffAllocator;
027import org.apache.hadoop.hbase.wal.WAL;
028import org.apache.yetus.audience.InterfaceAudience;
029
030import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;
031import org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder;
032
033/**
034 * Services a Store needs from a Region.
035 * RegionServicesForStores class is the interface through which memstore access services at the
036 * region level.
037 * For example, when using alternative memory formats or due to compaction the memstore needs to
038 * take occasional lock and update size counters at the region level.
039 */
040@InterfaceAudience.Private
041public class RegionServicesForStores {
042
043  private final HRegion region;
044  private final RegionServerServices rsServices;
045  private int inMemoryPoolSize;
046
047  public RegionServicesForStores(HRegion region, RegionServerServices rsServices) {
048    this.region = region;
049    this.rsServices = rsServices;
050    if (this.rsServices != null) {
051      this.inMemoryPoolSize = rsServices.getConfiguration().getInt(
052        CompactingMemStore.IN_MEMORY_CONPACTION_POOL_SIZE_KEY,
053        CompactingMemStore.IN_MEMORY_CONPACTION_POOL_SIZE_DEFAULT);
054    }
055  }
056
057  public void addMemStoreSize(long dataSizeDelta, long heapSizeDelta, long offHeapSizeDelta,
058      int cellsCountDelta) {
059    region.incMemStoreSize(dataSizeDelta, heapSizeDelta, offHeapSizeDelta, cellsCountDelta);
060  }
061
062  public RegionInfo getRegionInfo() {
063    return region.getRegionInfo();
064  }
065
066  public WAL getWAL() {
067    return region.getWAL();
068  }
069
070  private static ByteBuffAllocator ALLOCATOR_FOR_TEST;
071
072  private static synchronized ByteBuffAllocator getAllocatorForTest() {
073    if (ALLOCATOR_FOR_TEST == null) {
074      ALLOCATOR_FOR_TEST = ByteBuffAllocator.HEAP;
075    }
076    return ALLOCATOR_FOR_TEST;
077  }
078
079  public ByteBuffAllocator getByteBuffAllocator() {
080    if (rsServices != null && rsServices.getRpcServer() != null) {
081      return rsServices.getRpcServer().getByteBuffAllocator();
082    } else {
083      return getAllocatorForTest();
084    }
085  }
086
087  private static ThreadPoolExecutor INMEMORY_COMPACTION_POOL_FOR_TEST;
088
089  private static synchronized ThreadPoolExecutor getInMemoryCompactionPoolForTest() {
090    if (INMEMORY_COMPACTION_POOL_FOR_TEST == null) {
091      INMEMORY_COMPACTION_POOL_FOR_TEST = new ThreadPoolExecutor(10, 10, 60, TimeUnit.SECONDS,
092        new LinkedBlockingQueue<>(), new ThreadFactoryBuilder().setDaemon(true)
093          .setNameFormat("InMemoryCompactionsForTest-%d").build());
094    }
095    return INMEMORY_COMPACTION_POOL_FOR_TEST;
096  }
097
098  ThreadPoolExecutor getInMemoryCompactionPool() {
099    if (rsServices != null) {
100      return rsServices.getExecutorService().getExecutorLazily(ExecutorType.RS_IN_MEMORY_COMPACTION,
101        inMemoryPoolSize);
102    } else {
103      // this could only happen in tests
104      return getInMemoryCompactionPoolForTest();
105    }
106  }
107
108  public long getMemStoreFlushSize() {
109    return region.getMemStoreFlushSize();
110  }
111
112  public int getNumStores() {
113    return region.getTableDescriptor().getColumnFamilyCount();
114  }
115
116  @VisibleForTesting
117  long getMemStoreSize() {
118    return region.getMemStoreDataSize();
119  }
120}