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 com.google.errorprone.annotations.RestrictedApi;
021import java.io.IOException;
022import java.util.Collection;
023import java.util.Comparator;
024import java.util.Iterator;
025import java.util.List;
026import java.util.Optional;
027import org.apache.hadoop.hbase.Cell;
028import org.apache.hadoop.hbase.KeyValue;
029import org.apache.yetus.audience.InterfaceAudience;
030
031import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableCollection;
032
033/**
034 * Manages the store files and basic metadata about that that determines the logical structure (e.g.
035 * what files to return for scan, how to determine split point, and such). Does NOT affect the
036 * physical structure of files in HDFS. Example alternative structures - the default list of files
037 * by seqNum; levelDB one sorted by level and seqNum.
038 * <p/>
039 * Notice that, all the states are only in memory, we do not persist anything here. The only place
040 * where we throw an {@link IOException} is the {@link #getSplitPoint()} method, where we need to
041 * read startKey, endKey etc, which may lead to an {@link IOException}.
042 * <p/>
043 * Implementations are assumed to be not thread safe.
044 */
045@InterfaceAudience.Private
046public interface StoreFileManager {
047  /**
048   * Loads the initial store files into empty StoreFileManager.
049   * @param storeFiles The files to load.
050   */
051  @RestrictedApi(explanation = "Should only be called in StoreEngine", link = "",
052      allowedOnPath = ".*(/org/apache/hadoop/hbase/regionserver/StoreEngine.java|/src/test/.*)")
053  void loadFiles(List<HStoreFile> storeFiles) throws IOException;
054
055  /**
056   * Adds new files, either for from MemStore flush or bulk insert, into the structure.
057   * @param sfs New store files.
058   */
059  @RestrictedApi(explanation = "Should only be called in StoreEngine", link = "",
060      allowedOnPath = ".*(/org/apache/hadoop/hbase/regionserver/StoreEngine.java|/src/test/.*)")
061  void insertNewFiles(Collection<HStoreFile> sfs) throws IOException;
062
063  /**
064   * Adds only the new compaction results into the structure.
065   * @param compactedFiles The input files for the compaction.
066   * @param results        The resulting files for the compaction.
067   */
068  @RestrictedApi(explanation = "Should only be called in StoreEngine", link = "",
069      allowedOnPath = ".*(/org/apache/hadoop/hbase/regionserver/StoreEngine.java|/src/test/.*)")
070  void addCompactionResults(Collection<HStoreFile> compactedFiles, Collection<HStoreFile> results)
071    throws IOException;
072
073  /**
074   * Remove the compacted files
075   * @param compactedFiles the list of compacted files
076   */
077  @RestrictedApi(explanation = "Should only be called in StoreEngine", link = "",
078      allowedOnPath = ".*(/org/apache/hadoop/hbase/regionserver/StoreEngine.java|/src/test/.*)")
079  void removeCompactedFiles(Collection<HStoreFile> compactedFiles);
080
081  /**
082   * Clears all the files currently in use and returns them.
083   * @return The files previously in use.
084   */
085  ImmutableCollection<HStoreFile> clearFiles();
086
087  /**
088   * Clears all the compacted files and returns them. This method is expected to be accessed single
089   * threaded.
090   * @return The files compacted previously.
091   */
092  Collection<HStoreFile> clearCompactedFiles();
093
094  /**
095   * Gets the snapshot of the store files currently in use. Can be used for things like metrics and
096   * checks; should not assume anything about relations between store files in the list.
097   * @return The list of StoreFiles.
098   */
099  Collection<HStoreFile> getStoreFiles();
100
101  /**
102   * List of compacted files inside this store that needs to be excluded in reads because further
103   * new reads will be using only the newly created files out of compaction. These compacted files
104   * will be deleted/cleared once all the existing readers on these compacted files are done.
105   * @return the list of compacted files
106   */
107  Collection<HStoreFile> getCompactedfiles();
108
109  /**
110   * Returns the number of files currently in use.
111   * @return The number of files.
112   */
113  int getStorefileCount();
114
115  /**
116   * Returns the number of compacted files.
117   * @return The number of files.
118   */
119  int getCompactedFilesCount();
120
121  /**
122   * Gets the store files to scan for a Scan or Get request.
123   * @param startRow          Start row of the request.
124   * @param stopRow           Stop row of the request.
125   * @param onlyLatestVersion Scan only latest live version cells.
126   * @return The list of files that are to be read for this request.
127   */
128  Collection<HStoreFile> getFilesForScan(byte[] startRow, boolean includeStartRow, byte[] stopRow,
129    boolean includeStopRow, boolean onlyLatestVersion);
130
131  /**
132   * Gets initial, full list of candidate store files to check for row-key-before.
133   * @param targetKey The key that is the basis of the search.
134   * @return The files that may have the key less than or equal to targetKey, in reverse order of
135   *         new-ness, and preference for target key.
136   */
137  Iterator<HStoreFile> getCandidateFilesForRowKeyBefore(KeyValue targetKey);
138
139  /**
140   * Updates the candidate list for finding row key before. Based on the list of candidates
141   * remaining to check from getCandidateFilesForRowKeyBefore, targetKey and current candidate, may
142   * trim and reorder the list to remove the files where a better candidate cannot be found.
143   * @param candidateFiles The candidate files not yet checked for better candidates - return value
144   *                       from {@link #getCandidateFilesForRowKeyBefore(KeyValue)}, with some files
145   *                       already removed.
146   * @param targetKey      The key to search for.
147   * @param candidate      The current best candidate found.
148   * @return The list to replace candidateFiles.
149   */
150  Iterator<HStoreFile> updateCandidateFilesForRowKeyBefore(Iterator<HStoreFile> candidateFiles,
151    KeyValue targetKey, Cell candidate);
152
153  /**
154   * Gets the split point for the split of this set of store files (approx. middle).
155   * @return The mid-point if possible.
156   */
157  Optional<byte[]> getSplitPoint() throws IOException;
158
159  /** Returns The store compaction priority. */
160  int getStoreCompactionPriority();
161
162  /**
163   * @param maxTs           Maximum expired timestamp.
164   * @param filesCompacting Files that are currently compacting.
165   * @return The files which don't have any necessary data according to TTL and other criteria.
166   */
167  Collection<HStoreFile> getUnneededFiles(long maxTs, List<HStoreFile> filesCompacting);
168
169  /**
170   * @return the compaction pressure used for compaction throughput tuning.
171   * @see HStore#getCompactionPressure()
172   */
173  double getCompactionPressure();
174
175  /**
176   * @return the comparator used to sort storefiles. Usually, the
177   *         {@link HStoreFile#getMaxSequenceId()} is the first priority.
178   */
179  Comparator<HStoreFile> getStoreFileComparator();
180}