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