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;
019
020import java.util.Collection;
021import java.util.HashSet;
022import java.util.Set;
023
024import org.apache.hadoop.hbase.HBaseInterfaceAudience;
025import org.apache.yetus.audience.InterfaceAudience;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029/**
030 * A {@link FlushPolicy} that only flushes store larger a given threshold. If no store is large
031 * enough, then all stores will be flushed.
032 */
033@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
034public class FlushAllLargeStoresPolicy extends FlushLargeStoresPolicy {
035
036  private static final Logger LOG = LoggerFactory.getLogger(FlushAllLargeStoresPolicy.class);
037
038  @Override
039  protected void configureForRegion(HRegion region) {
040    super.configureForRegion(region);
041    int familyNumber = region.getTableDescriptor().getColumnFamilyCount();
042    if (familyNumber <= 1) {
043      // No need to parse and set flush size lower bound if only one family
044      // Family number might also be zero in some of our unit test case
045      return;
046    }
047    setFlushSizeLowerBounds(region);
048  }
049
050  @Override
051  public Collection<HStore> selectStoresToFlush() {
052    // no need to select stores if only one family
053    if (region.getTableDescriptor().getColumnFamilyCount() == 1) {
054      return region.stores.values();
055    }
056    // start selection
057    Collection<HStore> stores = region.stores.values();
058    Set<HStore> specificStoresToFlush = new HashSet<>();
059    for (HStore store : stores) {
060      if (shouldFlush(store)) {
061        specificStoresToFlush.add(store);
062      }
063    }
064    if (!specificStoresToFlush.isEmpty()) {
065      return specificStoresToFlush;
066    }
067
068    // Didn't find any CFs which were above the threshold for selection.
069    if (LOG.isDebugEnabled()) {
070      LOG.debug("Since none of the CFs were above the size, flushing all.");
071    }
072    return stores;
073  }
074
075  @Override
076  protected boolean shouldFlush(HStore store) {
077    return super.shouldFlush(store) || region.shouldFlushStore(store);
078  }
079
080}