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;
022
023import org.apache.hadoop.hbase.HBaseInterfaceAudience;
024import org.apache.yetus.audience.InterfaceAudience;
025
026/**
027 * A {@link FlushPolicy} that only flushes store larger than a given threshold. If no store is large
028 * enough, then all stores will be flushed.
029 * Gives priority to selecting regular stores first, and only if no other
030 * option, selects sloppy stores which normaly require more memory.
031 */
032@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
033public class FlushNonSloppyStoresFirstPolicy extends FlushLargeStoresPolicy {
034
035  private Collection<HStore> regularStores = new HashSet<>();
036  private Collection<HStore> sloppyStores = new HashSet<>();
037
038  /**
039   * @return the stores need to be flushed.
040   */
041  @Override
042  public Collection<HStore> selectStoresToFlush() {
043    Collection<HStore> specificStoresToFlush = new HashSet<>();
044    for (HStore store : regularStores) {
045      if (shouldFlush(store) || region.shouldFlushStore(store)) {
046        specificStoresToFlush.add(store);
047      }
048    }
049    if (!specificStoresToFlush.isEmpty()) {
050      return specificStoresToFlush;
051    }
052    for (HStore store : sloppyStores) {
053      if (shouldFlush(store)) {
054        specificStoresToFlush.add(store);
055      }
056    }
057    if (!specificStoresToFlush.isEmpty()) {
058      return specificStoresToFlush;
059    }
060    return region.stores.values();
061  }
062
063  @Override
064  protected void configureForRegion(HRegion region) {
065    super.configureForRegion(region);
066    setFlushSizeLowerBounds(region);
067    for (HStore store : region.stores.values()) {
068      if (store.isSloppyMemStore()) {
069        sloppyStores.add(store);
070      } else {
071        regularStores.add(store);
072      }
073    }
074  }
075}