View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Map;
30  import java.util.TreeMap;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.apache.hadoop.hbase.classification.InterfaceAudience;
35  import org.apache.hadoop.conf.Configuration;
36  import org.apache.hadoop.hbase.Cell;
37  import org.apache.hadoop.hbase.HConstants;
38  import org.apache.hadoop.hbase.KeyValue;
39  import org.apache.hadoop.hbase.KeyValue.KVComparator;
40  import org.apache.hadoop.hbase.regionserver.compactions.StripeCompactionPolicy;
41  import org.apache.hadoop.hbase.util.Bytes;
42  import org.apache.hadoop.hbase.util.ConcatenatedLists;
43  import org.apache.hadoop.util.StringUtils;
44  
45  import com.google.common.collect.ImmutableCollection;
46  import com.google.common.collect.ImmutableList;
47  
48  /**
49   * Stripe implementation of StoreFileManager.
50   * Not thread safe - relies on external locking (in HStore). Collections that this class
51   * returns are immutable or unique to the call, so they should be safe.
52   * Stripe store splits the key space of the region into non-overlapping stripes, as well as
53   * some recent files that have all the keys (level 0). Each stripe contains a set of files.
54   * When L0 is compacted, it's split into the files corresponding to existing stripe boundaries,
55   * that can thus be added to stripes.
56   * When scan or get happens, it only has to read the files from the corresponding stripes.
57   * See StripeCompationPolicy on how the stripes are determined; this class doesn't care.
58   *
59   * This class should work together with StripeCompactionPolicy and StripeCompactor.
60   * With regard to how they work, we make at least the following (reasonable) assumptions:
61   *  - Compaction produces one file per new stripe (if any); that is easy to change.
62   *  - Compaction has one contiguous set of stripes both in and out, except if L0 is involved.
63   */
64  @InterfaceAudience.Private
65  public class StripeStoreFileManager
66    implements StoreFileManager, StripeCompactionPolicy.StripeInformationProvider {
67    static final Log LOG = LogFactory.getLog(StripeStoreFileManager.class);
68  
69    /**
70     * The file metadata fields that contain the stripe information.
71     */
72    public static final byte[] STRIPE_START_KEY = Bytes.toBytes("STRIPE_START_KEY");
73    public static final byte[] STRIPE_END_KEY = Bytes.toBytes("STRIPE_END_KEY");
74  
75    private final static Bytes.RowEndKeyComparator MAP_COMPARATOR = new Bytes.RowEndKeyComparator();
76  
77    /**
78     * The key value used for range boundary, indicating that the boundary is open (i.e. +-inf).
79     */
80    public final static byte[] OPEN_KEY = HConstants.EMPTY_BYTE_ARRAY;
81    final static byte[] INVALID_KEY = null;
82  
83    /**
84     * The state class. Used solely to replace results atomically during
85     * compactions and avoid complicated error handling.
86     */
87    private static class State {
88      /**
89       * The end rows of each stripe. The last stripe end is always open-ended, so it's not stored
90       * here. It is invariant that the start row of the stripe is the end row of the previous one
91       * (and is an open boundary for the first one).
92       */
93      public byte[][] stripeEndRows = new byte[0][];
94  
95      /**
96       * Files by stripe. Each element of the list corresponds to stripeEndRow element with the
97       * same index, except the last one. Inside each list, the files are in reverse order by
98       * seqNum. Note that the length of this is one higher than that of stripeEndKeys.
99       */
100     public ArrayList<ImmutableList<StoreFile>> stripeFiles
101       = new ArrayList<ImmutableList<StoreFile>>();
102     /** Level 0. The files are in reverse order by seqNum. */
103     public ImmutableList<StoreFile> level0Files = ImmutableList.<StoreFile>of();
104 
105     /** Cached list of all files in the structure, to return from some calls */
106     public ImmutableList<StoreFile> allFilesCached = ImmutableList.<StoreFile>of();
107   }
108   private State state = null;
109 
110   /** Cached file metadata (or overrides as the case may be) */
111   private HashMap<StoreFile, byte[]> fileStarts = new HashMap<StoreFile, byte[]>();
112   private HashMap<StoreFile, byte[]> fileEnds = new HashMap<StoreFile, byte[]>();
113   /** Normally invalid key is null, but in the map null is the result for "no key"; so use
114    * the following constant value in these maps instead. Note that this is a constant and
115    * we use it to compare by reference when we read from the map. */
116   private static final byte[] INVALID_KEY_IN_MAP = new byte[0];
117 
118   private final KVComparator kvComparator;
119   private StripeStoreConfig config;
120 
121   private final int blockingFileCount;
122 
123   public StripeStoreFileManager(
124       KVComparator kvComparator, Configuration conf, StripeStoreConfig config) {
125     this.kvComparator = kvComparator;
126     this.config = config;
127     this.blockingFileCount = conf.getInt(
128         HStore.BLOCKING_STOREFILES_KEY, HStore.DEFAULT_BLOCKING_STOREFILE_COUNT);
129   }
130 
131   @Override
132   public void loadFiles(List<StoreFile> storeFiles) {
133     loadUnclassifiedStoreFiles(storeFiles);
134   }
135 
136   @Override
137   public Collection<StoreFile> getStorefiles() {
138     return state.allFilesCached;
139   }
140 
141   @Override
142   public void insertNewFiles(Collection<StoreFile> sfs) throws IOException {
143     CompactionOrFlushMergeCopy cmc = new CompactionOrFlushMergeCopy(true);
144     cmc.mergeResults(null, sfs);
145     debugDumpState("Added new files");
146   }
147 
148   @Override
149   public ImmutableCollection<StoreFile> clearFiles() {
150     ImmutableCollection<StoreFile> result = state.allFilesCached;
151     this.state = new State();
152     this.fileStarts.clear();
153     this.fileEnds.clear();
154     return result;
155   }
156 
157   @Override
158   public int getStorefileCount() {
159     return state.allFilesCached.size();
160   }
161 
162   /** See {@link StoreFileManager#getCandidateFilesForRowKeyBefore(KeyValue)}
163    * for details on this methods. */
164   @Override
165   public Iterator<StoreFile> getCandidateFilesForRowKeyBefore(final KeyValue targetKey) {
166     KeyBeforeConcatenatedLists result = new KeyBeforeConcatenatedLists();
167     // Order matters for this call.
168     result.addSublist(state.level0Files);
169     if (!state.stripeFiles.isEmpty()) {
170       int lastStripeIndex = findStripeForRow(targetKey.getRow(), false);
171       for (int stripeIndex = lastStripeIndex; stripeIndex >= 0; --stripeIndex) {
172         result.addSublist(state.stripeFiles.get(stripeIndex));
173       }
174     }
175     return result.iterator();
176   }
177 
178   /** See {@link StoreFileManager#getCandidateFilesForRowKeyBefore(KeyValue)} and
179    * {@link StoreFileManager#updateCandidateFilesForRowKeyBefore(Iterator, KeyValue, Cell)}
180    * for details on this methods. */
181   @Override
182   public Iterator<StoreFile> updateCandidateFilesForRowKeyBefore(
183       Iterator<StoreFile> candidateFiles, final KeyValue targetKey, final Cell candidate) {
184     KeyBeforeConcatenatedLists.Iterator original =
185         (KeyBeforeConcatenatedLists.Iterator)candidateFiles;
186     assert original != null;
187     ArrayList<List<StoreFile>> components = original.getComponents();
188     for (int firstIrrelevant = 0; firstIrrelevant < components.size(); ++firstIrrelevant) {
189       StoreFile sf = components.get(firstIrrelevant).get(0);
190       byte[] endKey = endOf(sf);
191       // Entries are ordered as such: L0, then stripes in reverse order. We never remove
192       // level 0; we remove the stripe, and all subsequent ones, as soon as we find the
193       // first one that cannot possibly have better candidates.
194       if (!isInvalid(endKey) && !isOpen(endKey)
195           && (nonOpenRowCompare(endKey, targetKey.getRow()) <= 0)) {
196         original.removeComponents(firstIrrelevant);
197         break;
198       }
199     }
200     return original;
201   }
202 
203   @Override
204   /**
205    * Override of getSplitPoint that determines the split point as the boundary between two
206    * stripes, unless it causes significant imbalance between split sides' sizes. In that
207    * case, the split boundary will be chosen from the middle of one of the stripes to
208    * minimize imbalance.
209    * @return The split point, or null if no split is possible.
210    */
211   public byte[] getSplitPoint() throws IOException {
212     if (this.getStorefileCount() == 0) return null;
213     if (state.stripeFiles.size() <= 1) {
214       return getSplitPointFromAllFiles();
215     }
216     int leftIndex = -1, rightIndex = state.stripeFiles.size();
217     long leftSize = 0, rightSize = 0;
218     long lastLeftSize = 0, lastRightSize = 0;
219     while (rightIndex - 1 != leftIndex) {
220       if (leftSize >= rightSize) {
221         --rightIndex;
222         lastRightSize = getStripeFilesSize(rightIndex);
223         rightSize += lastRightSize;
224       } else {
225         ++leftIndex;
226         lastLeftSize = getStripeFilesSize(leftIndex);
227         leftSize += lastLeftSize;
228       }
229     }
230     if (leftSize == 0 || rightSize == 0) {
231       String errMsg = String.format("Cannot split on a boundary - left index %d size %d, "
232           + "right index %d size %d", leftIndex, leftSize, rightIndex, rightSize);
233       debugDumpState(errMsg);
234       LOG.warn(errMsg);
235       return getSplitPointFromAllFiles();
236     }
237     double ratio = (double)rightSize / leftSize;
238     if (ratio < 1) {
239       ratio = 1 / ratio;
240     }
241     if (config.getMaxSplitImbalance() > ratio) return state.stripeEndRows[leftIndex];
242 
243     // If the difference between the sides is too large, we could get the proportional key on
244     // the a stripe to equalize the difference, but there's no proportional key method at the
245     // moment, and it's not extremely important.
246     // See if we can achieve better ratio if we split the bigger side in half.
247     boolean isRightLarger = rightSize >= leftSize;
248     double newRatio = isRightLarger
249         ? getMidStripeSplitRatio(leftSize, rightSize, lastRightSize)
250         : getMidStripeSplitRatio(rightSize, leftSize, lastLeftSize);
251     if (newRatio < 1) {
252       newRatio = 1 / newRatio;
253     }
254     if (newRatio >= ratio)  return state.stripeEndRows[leftIndex];
255     LOG.debug("Splitting the stripe - ratio w/o split " + ratio + ", ratio with split "
256         + newRatio + " configured ratio " + config.getMaxSplitImbalance());
257     // Ok, we may get better ratio, get it.
258     return StoreUtils.getLargestFile(state.stripeFiles.get(
259         isRightLarger ? rightIndex : leftIndex)).getFileSplitPoint(this.kvComparator);
260   }
261 
262   private byte[] getSplitPointFromAllFiles() throws IOException {
263     ConcatenatedLists<StoreFile> sfs = new ConcatenatedLists<StoreFile>();
264     sfs.addSublist(state.level0Files);
265     sfs.addAllSublists(state.stripeFiles);
266     if (sfs.isEmpty()) return null;
267     return StoreUtils.getLargestFile(sfs).getFileSplitPoint(this.kvComparator);
268   }
269 
270   private double getMidStripeSplitRatio(long smallerSize, long largerSize, long lastLargerSize) {
271     return (double)(largerSize - lastLargerSize / 2f) / (smallerSize + lastLargerSize / 2f);
272   }
273 
274   @Override
275   public Collection<StoreFile> getFilesForScanOrGet(
276       boolean isGet, byte[] startRow, byte[] stopRow) {
277     if (state.stripeFiles.isEmpty()) {
278       return state.level0Files; // There's just L0.
279     }
280 
281     int firstStripe = findStripeForRow(startRow, true);
282     int lastStripe = findStripeForRow(stopRow, false);
283     assert firstStripe <= lastStripe;
284     if (firstStripe == lastStripe && state.level0Files.isEmpty()) {
285       return state.stripeFiles.get(firstStripe); // There's just one stripe we need.
286     }
287     if (firstStripe == 0 && lastStripe == (state.stripeFiles.size() - 1)) {
288       return state.allFilesCached; // We need to read all files.
289     }
290 
291     ConcatenatedLists<StoreFile> result = new ConcatenatedLists<StoreFile>();
292     result.addAllSublists(state.stripeFiles.subList(firstStripe, lastStripe + 1));
293     result.addSublist(state.level0Files);
294     return result;
295   }
296 
297   @Override
298   public void addCompactionResults(
299     Collection<StoreFile> compactedFiles, Collection<StoreFile> results) throws IOException {
300     // See class comment for the assumptions we make here.
301     LOG.debug("Attempting to merge compaction results: " + compactedFiles.size()
302         + " files replaced by " + results.size());
303     // In order to be able to fail in the middle of the operation, we'll operate on lazy
304     // copies and apply the result at the end.
305     CompactionOrFlushMergeCopy cmc = new CompactionOrFlushMergeCopy(false);
306     cmc.mergeResults(compactedFiles, results);
307     debugDumpState("Merged compaction results");
308   }
309 
310   @Override
311   public int getStoreCompactionPriority() {
312     // If there's only L0, do what the default store does.
313     // If we are in critical priority, do the same - we don't want to trump all stores all
314     // the time due to how many files we have.
315     int fc = getStorefileCount();
316     if (state.stripeFiles.isEmpty() || (this.blockingFileCount <= fc)) {
317       return this.blockingFileCount - fc;
318     }
319     // If we are in good shape, we don't want to be trumped by all other stores due to how
320     // many files we have, so do an approximate mapping to normal priority range; L0 counts
321     // for all stripes.
322     int l0 = state.level0Files.size(), sc = state.stripeFiles.size();
323     int priority = (int)Math.ceil(((double)(this.blockingFileCount - fc + l0) / sc) - l0);
324     return (priority <= HStore.PRIORITY_USER) ? (HStore.PRIORITY_USER + 1) : priority;
325   }
326 
327   /**
328    * Gets the total size of all files in the stripe.
329    * @param stripeIndex Stripe index.
330    * @return Size.
331    */
332   private long getStripeFilesSize(int stripeIndex) {
333     long result = 0;
334     for (StoreFile sf : state.stripeFiles.get(stripeIndex)) {
335       result += sf.getReader().length();
336     }
337     return result;
338   }
339 
340   /**
341    * Loads initial store files that were picked up from some physical location pertaining to
342    * this store (presumably). Unlike adding files after compaction, assumes empty initial
343    * sets, and is forgiving with regard to stripe constraints - at worst, many/all files will
344    * go to level 0.
345    * @param storeFiles Store files to add.
346    */
347   private void loadUnclassifiedStoreFiles(List<StoreFile> storeFiles) {
348     LOG.debug("Attempting to load " + storeFiles.size() + " store files.");
349     TreeMap<byte[], ArrayList<StoreFile>> candidateStripes =
350         new TreeMap<byte[], ArrayList<StoreFile>>(MAP_COMPARATOR);
351     ArrayList<StoreFile> level0Files = new ArrayList<StoreFile>();
352     // Separate the files into tentative stripes; then validate. Currently, we rely on metadata.
353     // If needed, we could dynamically determine the stripes in future.
354     for (StoreFile sf : storeFiles) {
355       byte[] startRow = startOf(sf), endRow = endOf(sf);
356       // Validate the range and put the files into place.
357       if (isInvalid(startRow) || isInvalid(endRow)) {
358         insertFileIntoStripe(level0Files, sf); // No metadata - goes to L0.
359         ensureLevel0Metadata(sf);
360       } else if (!isOpen(startRow) && !isOpen(endRow) &&
361           nonOpenRowCompare(startRow, endRow) >= 0) {
362         LOG.error("Unexpected metadata - start row [" + Bytes.toString(startRow) + "], end row ["
363           + Bytes.toString(endRow) + "] in file [" + sf.getPath() + "], pushing to L0");
364         insertFileIntoStripe(level0Files, sf); // Bad metadata - goes to L0 also.
365         ensureLevel0Metadata(sf);
366       } else {
367         ArrayList<StoreFile> stripe = candidateStripes.get(endRow);
368         if (stripe == null) {
369           stripe = new ArrayList<StoreFile>();
370           candidateStripes.put(endRow, stripe);
371         }
372         insertFileIntoStripe(stripe, sf);
373       }
374     }
375     // Possible improvement - for variable-count stripes, if all the files are in L0, we can
376     // instead create single, open-ended stripe with all files.
377 
378     boolean hasOverlaps = false;
379     byte[] expectedStartRow = null; // first stripe can start wherever
380     Iterator<Map.Entry<byte[], ArrayList<StoreFile>>> entryIter =
381         candidateStripes.entrySet().iterator();
382     while (entryIter.hasNext()) {
383       Map.Entry<byte[], ArrayList<StoreFile>> entry = entryIter.next();
384       ArrayList<StoreFile> files = entry.getValue();
385       // Validate the file start rows, and remove the bad ones to level 0.
386       for (int i = 0; i < files.size(); ++i) {
387         StoreFile sf = files.get(i);
388         byte[] startRow = startOf(sf);
389         if (expectedStartRow == null) {
390           expectedStartRow = startRow; // ensure that first stripe is still consistent
391         } else if (!rowEquals(expectedStartRow, startRow)) {
392           hasOverlaps = true;
393           LOG.warn("Store file doesn't fit into the tentative stripes - expected to start at ["
394               + Bytes.toString(expectedStartRow) + "], but starts at [" + Bytes.toString(startRow)
395               + "], to L0 it goes");
396           StoreFile badSf = files.remove(i);
397           insertFileIntoStripe(level0Files, badSf);
398           ensureLevel0Metadata(badSf);
399           --i;
400         }
401       }
402       // Check if any files from the candidate stripe are valid. If so, add a stripe.
403       byte[] endRow = entry.getKey();
404       if (!files.isEmpty()) {
405         expectedStartRow = endRow; // Next stripe must start exactly at that key.
406       } else {
407         entryIter.remove();
408       }
409     }
410 
411     // In the end, there must be open ends on two sides. If not, and there were no errors i.e.
412     // files are consistent, they might be coming from a split. We will treat the boundaries
413     // as open keys anyway, and log the message.
414     // If there were errors, we'll play it safe and dump everything into L0.
415     if (!candidateStripes.isEmpty()) {
416       StoreFile firstFile = candidateStripes.firstEntry().getValue().get(0);
417       boolean isOpen = isOpen(startOf(firstFile)) && isOpen(candidateStripes.lastKey());
418       if (!isOpen) {
419         LOG.warn("The range of the loaded files does not cover full key space: from ["
420             + Bytes.toString(startOf(firstFile)) + "], to ["
421             + Bytes.toString(candidateStripes.lastKey()) + "]");
422         if (!hasOverlaps) {
423           ensureEdgeStripeMetadata(candidateStripes.firstEntry().getValue(), true);
424           ensureEdgeStripeMetadata(candidateStripes.lastEntry().getValue(), false);
425         } else {
426           LOG.warn("Inconsistent files, everything goes to L0.");
427           for (ArrayList<StoreFile> files : candidateStripes.values()) {
428             for (StoreFile sf : files) {
429               insertFileIntoStripe(level0Files, sf);
430               ensureLevel0Metadata(sf);
431             }
432           }
433           candidateStripes.clear();
434         }
435       }
436     }
437 
438     // Copy the results into the fields.
439     State state = new State();
440     state.level0Files = ImmutableList.copyOf(level0Files);
441     state.stripeFiles = new ArrayList<ImmutableList<StoreFile>>(candidateStripes.size());
442     state.stripeEndRows = new byte[Math.max(0, candidateStripes.size() - 1)][];
443     ArrayList<StoreFile> newAllFiles = new ArrayList<StoreFile>(level0Files);
444     int i = candidateStripes.size() - 1;
445     for (Map.Entry<byte[], ArrayList<StoreFile>> entry : candidateStripes.entrySet()) {
446       state.stripeFiles.add(ImmutableList.copyOf(entry.getValue()));
447       newAllFiles.addAll(entry.getValue());
448       if (i > 0) {
449         state.stripeEndRows[state.stripeFiles.size() - 1] = entry.getKey();
450       }
451       --i;
452     }
453     state.allFilesCached = ImmutableList.copyOf(newAllFiles);
454     this.state = state;
455     debugDumpState("Files loaded");
456   }
457 
458   private void ensureEdgeStripeMetadata(ArrayList<StoreFile> stripe, boolean isFirst) {
459     HashMap<StoreFile, byte[]> targetMap = isFirst ? fileStarts : fileEnds;
460     for (StoreFile sf : stripe) {
461       targetMap.put(sf, OPEN_KEY);
462     }
463   }
464 
465   private void ensureLevel0Metadata(StoreFile sf) {
466     if (!isInvalid(startOf(sf))) this.fileStarts.put(sf, INVALID_KEY_IN_MAP);
467     if (!isInvalid(endOf(sf))) this.fileEnds.put(sf, INVALID_KEY_IN_MAP);
468   }
469 
470   private void debugDumpState(String string) {
471     if (!LOG.isDebugEnabled()) return;
472     StringBuilder sb = new StringBuilder();
473     sb.append("\n" + string + "; current stripe state is as such:");
474     sb.append("\n level 0 with ").append(state.level0Files.size())
475         .append(
476           " files: "
477               + StringUtils.humanReadableInt(StripeCompactionPolicy
478                   .getTotalFileSize(state.level0Files)) + ";");
479     for (int i = 0; i < state.stripeFiles.size(); ++i) {
480       String endRow = (i == state.stripeEndRows.length)
481           ? "(end)" : "[" + Bytes.toString(state.stripeEndRows[i]) + "]";
482       sb.append("\n stripe ending in ").append(endRow).append(" with ")
483           .append(state.stripeFiles.get(i).size())
484           .append(
485             " files: "
486                 + StringUtils.humanReadableInt(StripeCompactionPolicy
487                     .getTotalFileSize(state.stripeFiles.get(i))) + ";");
488     }
489     sb.append("\n").append(state.stripeFiles.size()).append(" stripes total.");
490     sb.append("\n").append(getStorefileCount()).append(" files total.");
491     LOG.debug(sb.toString());
492   }
493 
494   /**
495    * Checks whether the key indicates an open interval boundary (i.e. infinity).
496    */
497   private static final boolean isOpen(byte[] key) {
498     return key != null && key.length == 0;
499   }
500 
501   /**
502    * Checks whether the key is invalid (e.g. from an L0 file, or non-stripe-compacted files).
503    */
504   private static final boolean isInvalid(byte[] key) {
505     return key == INVALID_KEY;
506   }
507 
508   /**
509    * Compare two keys for equality.
510    */
511   private final boolean rowEquals(byte[] k1, byte[] k2) {
512     return kvComparator.matchingRows(k1, 0, k1.length, k2, 0, k2.length);
513   }
514 
515   /**
516    * Compare two keys. Keys must not be open (isOpen(row) == false).
517    */
518   private final int nonOpenRowCompare(byte[] k1, byte[] k2) {
519     assert !isOpen(k1) && !isOpen(k2);
520     return kvComparator.compareRows(k1, 0, k1.length, k2, 0, k2.length);
521   }
522 
523   /**
524    * Finds the stripe index by end row.
525    */
526   private final int findStripeIndexByEndRow(byte[] endRow) {
527     assert !isInvalid(endRow);
528     if (isOpen(endRow)) return state.stripeEndRows.length;
529     return Arrays.binarySearch(state.stripeEndRows, endRow, Bytes.BYTES_COMPARATOR);
530   }
531 
532   /**
533    * Finds the stripe index for the stripe containing a row provided externally for get/scan.
534    */
535   private final int findStripeForRow(byte[] row, boolean isStart) {
536     if (isStart && row == HConstants.EMPTY_START_ROW) return 0;
537     if (!isStart && row == HConstants.EMPTY_END_ROW) return state.stripeFiles.size() - 1;
538     // If there's an exact match below, a stripe ends at "row". Stripe right boundary is
539     // exclusive, so that means the row is in the next stripe; thus, we need to add one to index.
540     // If there's no match, the return value of binarySearch is (-(insertion point) - 1), where
541     // insertion point is the index of the next greater element, or list size if none. The
542     // insertion point happens to be exactly what we need, so we need to add one to the result.
543     return Math.abs(Arrays.binarySearch(state.stripeEndRows, row, Bytes.BYTES_COMPARATOR) + 1);
544   }
545 
546   @Override
547   public final byte[] getStartRow(int stripeIndex) {
548     return (stripeIndex == 0  ? OPEN_KEY : state.stripeEndRows[stripeIndex - 1]);
549   }
550 
551   @Override
552   public final byte[] getEndRow(int stripeIndex) {
553     return (stripeIndex == state.stripeEndRows.length
554         ? OPEN_KEY : state.stripeEndRows[stripeIndex]);
555   }
556 
557 
558   private byte[] startOf(StoreFile sf) {
559     byte[] result = this.fileStarts.get(sf);
560     return result == null ? sf.getMetadataValue(STRIPE_START_KEY)
561         : (result == INVALID_KEY_IN_MAP ? INVALID_KEY : result);
562   }
563 
564   private byte[] endOf(StoreFile sf) {
565     byte[] result = this.fileEnds.get(sf);
566     return result == null ? sf.getMetadataValue(STRIPE_END_KEY)
567         : (result == INVALID_KEY_IN_MAP ? INVALID_KEY : result);
568   }
569 
570   /**
571    * Inserts a file in the correct place (by seqnum) in a stripe copy.
572    * @param stripe Stripe copy to insert into.
573    * @param sf File to insert.
574    */
575   private static void insertFileIntoStripe(ArrayList<StoreFile> stripe, StoreFile sf) {
576     // The only operation for which sorting of the files matters is KeyBefore. Therefore,
577     // we will store the file in reverse order by seqNum from the outset.
578     for (int insertBefore = 0; ; ++insertBefore) {
579       if (insertBefore == stripe.size()
580           || (StoreFile.Comparators.SEQ_ID.compare(sf, stripe.get(insertBefore)) >= 0)) {
581         stripe.add(insertBefore, sf);
582         break;
583       }
584     }
585   }
586 
587   /**
588    * An extension of ConcatenatedLists that has several peculiar properties.
589    * First, one can cut the tail of the logical list by removing last several sub-lists.
590    * Second, items can be removed thru iterator.
591    * Third, if the sub-lists are immutable, they are replaced with mutable copies when needed.
592    * On average KeyBefore operation will contain half the stripes as potential candidates,
593    * but will quickly cut down on them as it finds something in the more likely ones; thus,
594    * the above allow us to avoid unnecessary copying of a bunch of lists.
595    */
596   private static class KeyBeforeConcatenatedLists extends ConcatenatedLists<StoreFile> {
597     @Override
598     public java.util.Iterator<StoreFile> iterator() {
599       return new Iterator();
600     }
601 
602     public class Iterator extends ConcatenatedLists<StoreFile>.Iterator {
603       public ArrayList<List<StoreFile>> getComponents() {
604         return components;
605       }
606 
607       public void removeComponents(int startIndex) {
608         List<List<StoreFile>> subList = components.subList(startIndex, components.size());
609         for (List<StoreFile> entry : subList) {
610           size -= entry.size();
611         }
612         assert size >= 0;
613         subList.clear();
614       }
615 
616       @Override
617       public void remove() {
618         if (!this.nextWasCalled) {
619           throw new IllegalStateException("No element to remove");
620         }
621         this.nextWasCalled = false;
622         List<StoreFile> src = components.get(currentComponent);
623         if (src instanceof ImmutableList<?>) {
624           src = new ArrayList<StoreFile>(src);
625           components.set(currentComponent, src);
626         }
627         src.remove(indexWithinComponent);
628         --size;
629         --indexWithinComponent;
630         if (src.isEmpty()) {
631           components.remove(currentComponent); // indexWithinComponent is already -1 here.
632         }
633       }
634     }
635   }
636 
637   /**
638    * Non-static helper class for merging compaction or flush results.
639    * Since we want to merge them atomically (more or less), it operates on lazy copies,
640    * then creates a new state object and puts it in place.
641    */
642   private class CompactionOrFlushMergeCopy {
643     private ArrayList<List<StoreFile>> stripeFiles = null;
644     private ArrayList<StoreFile> level0Files = null;
645     private ArrayList<byte[]> stripeEndRows = null;
646 
647     private Collection<StoreFile> compactedFiles = null;
648     private Collection<StoreFile> results = null;
649 
650     private List<StoreFile> l0Results = new ArrayList<StoreFile>();
651     private final boolean isFlush;
652 
653     public CompactionOrFlushMergeCopy(boolean isFlush) {
654       // Create a lazy mutable copy (other fields are so lazy they start out as nulls).
655       this.stripeFiles = new ArrayList<List<StoreFile>>(
656           StripeStoreFileManager.this.state.stripeFiles);
657       this.isFlush = isFlush;
658     }
659 
660     public void mergeResults(Collection<StoreFile> compactedFiles, Collection<StoreFile> results)
661         throws IOException {
662       assert this.compactedFiles == null && this.results == null;
663       this.compactedFiles = compactedFiles;
664       this.results = results;
665       // Do logical processing.
666       if (!isFlush) removeCompactedFiles();
667       TreeMap<byte[], StoreFile> newStripes = processResults();
668       if (newStripes != null) {
669         processNewCandidateStripes(newStripes);
670       }
671       // Create new state and update parent.
672       State state = createNewState();
673       StripeStoreFileManager.this.state = state;
674       updateMetadataMaps();
675     }
676 
677     private State createNewState() {
678       State oldState = StripeStoreFileManager.this.state;
679       // Stripe count should be the same unless the end rows changed.
680       assert oldState.stripeFiles.size() == this.stripeFiles.size() || this.stripeEndRows != null;
681       State newState = new State();
682       newState.level0Files = (this.level0Files == null) ? oldState.level0Files
683           : ImmutableList.copyOf(this.level0Files);
684       newState.stripeEndRows = (this.stripeEndRows == null) ? oldState.stripeEndRows
685           : this.stripeEndRows.toArray(new byte[this.stripeEndRows.size()][]);
686       newState.stripeFiles = new ArrayList<ImmutableList<StoreFile>>(this.stripeFiles.size());
687       for (List<StoreFile> newStripe : this.stripeFiles) {
688         newState.stripeFiles.add(newStripe instanceof ImmutableList<?>
689             ? (ImmutableList<StoreFile>)newStripe : ImmutableList.copyOf(newStripe));
690       }
691 
692       List<StoreFile> newAllFiles = new ArrayList<StoreFile>(oldState.allFilesCached);
693       if (!isFlush) newAllFiles.removeAll(compactedFiles);
694       newAllFiles.addAll(results);
695       newState.allFilesCached = ImmutableList.copyOf(newAllFiles);
696       return newState;
697     }
698 
699     private void updateMetadataMaps() {
700       StripeStoreFileManager parent = StripeStoreFileManager.this;
701       if (!isFlush) {
702         for (StoreFile sf : this.compactedFiles) {
703           parent.fileStarts.remove(sf);
704           parent.fileEnds.remove(sf);
705         }
706       }
707       if (this.l0Results != null) {
708         for (StoreFile sf : this.l0Results) {
709           parent.ensureLevel0Metadata(sf);
710         }
711       }
712     }
713 
714     /**
715      * @param index Index of the stripe we need.
716      * @return A lazy stripe copy from current stripes.
717      */
718     private final ArrayList<StoreFile> getStripeCopy(int index) {
719       List<StoreFile> stripeCopy = this.stripeFiles.get(index);
720       ArrayList<StoreFile> result = null;
721       if (stripeCopy instanceof ImmutableList<?>) {
722         result = new ArrayList<StoreFile>(stripeCopy);
723         this.stripeFiles.set(index, result);
724       } else {
725         result = (ArrayList<StoreFile>)stripeCopy;
726       }
727       return result;
728     }
729 
730     /**
731      * @return A lazy L0 copy from current state.
732      */
733     private final ArrayList<StoreFile> getLevel0Copy() {
734       if (this.level0Files == null) {
735         this.level0Files = new ArrayList<StoreFile>(StripeStoreFileManager.this.state.level0Files);
736       }
737       return this.level0Files;
738     }
739 
740     /**
741      * Process new files, and add them either to the structure of existing stripes,
742      * or to the list of new candidate stripes.
743      * @return New candidate stripes.
744      */
745     private TreeMap<byte[], StoreFile> processResults() throws IOException {
746       TreeMap<byte[], StoreFile> newStripes = null;
747       for (StoreFile sf : this.results) {
748         byte[] startRow = startOf(sf), endRow = endOf(sf);
749         if (isInvalid(endRow) || isInvalid(startRow)) {
750           if (!isFlush) {
751             LOG.warn("The newly compacted file doesn't have stripes set: " + sf.getPath());
752           }
753           insertFileIntoStripe(getLevel0Copy(), sf);
754           this.l0Results.add(sf);
755           continue;
756         }
757         if (!this.stripeFiles.isEmpty()) {
758           int stripeIndex = findStripeIndexByEndRow(endRow);
759           if ((stripeIndex >= 0) && rowEquals(getStartRow(stripeIndex), startRow)) {
760             // Simple/common case - add file to an existing stripe.
761             insertFileIntoStripe(getStripeCopy(stripeIndex), sf);
762             continue;
763           }
764         }
765 
766         // Make a new candidate stripe.
767         if (newStripes == null) {
768           newStripes = new TreeMap<byte[], StoreFile>(MAP_COMPARATOR);
769         }
770         StoreFile oldSf = newStripes.put(endRow, sf);
771         if (oldSf != null) {
772           throw new IOException("Compactor has produced multiple files for the stripe ending in ["
773               + Bytes.toString(endRow) + "], found " + sf.getPath() + " and " + oldSf.getPath());
774         }
775       }
776       return newStripes;
777     }
778 
779     /**
780      * Remove compacted files.
781      * @param compactedFiles Compacted files.
782      */
783     private void removeCompactedFiles() throws IOException {
784       for (StoreFile oldFile : this.compactedFiles) {
785         byte[] oldEndRow = endOf(oldFile);
786         List<StoreFile> source = null;
787         if (isInvalid(oldEndRow)) {
788           source = getLevel0Copy();
789         } else {
790           int stripeIndex = findStripeIndexByEndRow(oldEndRow);
791           if (stripeIndex < 0) {
792             throw new IOException("An allegedly compacted file [" + oldFile + "] does not belong"
793                 + " to a known stripe (end row - [" + Bytes.toString(oldEndRow) + "])");
794           }
795           source = getStripeCopy(stripeIndex);
796         }
797         if (!source.remove(oldFile)) {
798           throw new IOException("An allegedly compacted file [" + oldFile + "] was not found");
799         }
800       }
801     }
802 
803     /**
804      * See {@link #addCompactionResults(Collection, Collection)} - updates the stripe list with
805      * new candidate stripes/removes old stripes; produces new set of stripe end rows.
806      * @param newStripes  New stripes - files by end row.
807      */
808     private void processNewCandidateStripes(
809         TreeMap<byte[], StoreFile> newStripes) throws IOException {
810       // Validate that the removed and added aggregate ranges still make for a full key space.
811       boolean hasStripes = !this.stripeFiles.isEmpty();
812       this.stripeEndRows = new ArrayList<byte[]>(
813           Arrays.asList(StripeStoreFileManager.this.state.stripeEndRows));
814       int removeFrom = 0;
815       byte[] firstStartRow = startOf(newStripes.firstEntry().getValue());
816       byte[] lastEndRow = newStripes.lastKey();
817       if (!hasStripes && (!isOpen(firstStartRow) || !isOpen(lastEndRow))) {
818         throw new IOException("Newly created stripes do not cover the entire key space.");
819       }
820 
821       boolean canAddNewStripes = true;
822       Collection<StoreFile> filesForL0 = null;
823       if (hasStripes) {
824         // Determine which stripes will need to be removed because they conflict with new stripes.
825         // The new boundaries should match old stripe boundaries, so we should get exact matches.
826         if (isOpen(firstStartRow)) {
827           removeFrom = 0;
828         } else {
829           removeFrom = findStripeIndexByEndRow(firstStartRow);
830           if (removeFrom < 0) throw new IOException("Compaction is trying to add a bad range.");
831           ++removeFrom;
832         }
833         int removeTo = findStripeIndexByEndRow(lastEndRow);
834         if (removeTo < 0) throw new IOException("Compaction is trying to add a bad range.");
835         // See if there are files in the stripes we are trying to replace.
836         ArrayList<StoreFile> conflictingFiles = new ArrayList<StoreFile>();
837         for (int removeIndex = removeTo; removeIndex >= removeFrom; --removeIndex) {
838           conflictingFiles.addAll(this.stripeFiles.get(removeIndex));
839         }
840         if (!conflictingFiles.isEmpty()) {
841           // This can be caused by two things - concurrent flush into stripes, or a bug.
842           // Unfortunately, we cannot tell them apart without looking at timing or something
843           // like that. We will assume we are dealing with a flush and dump it into L0.
844           if (isFlush) {
845             long newSize = StripeCompactionPolicy.getTotalFileSize(newStripes.values());
846             LOG.warn("Stripes were created by a flush, but results of size " + newSize
847                 + " cannot be added because the stripes have changed");
848             canAddNewStripes = false;
849             filesForL0 = newStripes.values();
850           } else {
851             long oldSize = StripeCompactionPolicy.getTotalFileSize(conflictingFiles);
852             LOG.info(conflictingFiles.size() + " conflicting files (likely created by a flush) "
853                 + " of size " + oldSize + " are moved to L0 due to concurrent stripe change");
854             filesForL0 = conflictingFiles;
855           }
856           if (filesForL0 != null) {
857             for (StoreFile sf : filesForL0) {
858               insertFileIntoStripe(getLevel0Copy(), sf);
859             }
860             l0Results.addAll(filesForL0);
861           }
862         }
863 
864         if (canAddNewStripes) {
865           // Remove old empty stripes.
866           int originalCount = this.stripeFiles.size();
867           for (int removeIndex = removeTo; removeIndex >= removeFrom; --removeIndex) {
868             if (removeIndex != originalCount - 1) {
869               this.stripeEndRows.remove(removeIndex);
870             }
871             this.stripeFiles.remove(removeIndex);
872           }
873         }
874       }
875 
876       if (!canAddNewStripes) return; // Files were already put into L0.
877 
878       // Now, insert new stripes. The total ranges match, so we can insert where we removed.
879       byte[] previousEndRow = null;
880       int insertAt = removeFrom;
881       for (Map.Entry<byte[], StoreFile> newStripe : newStripes.entrySet()) {
882         if (previousEndRow != null) {
883           // Validate that the ranges are contiguous.
884           assert !isOpen(previousEndRow);
885           byte[] startRow = startOf(newStripe.getValue());
886           if (!rowEquals(previousEndRow, startRow)) {
887             throw new IOException("The new stripes produced by "
888                 + (isFlush ? "flush" : "compaction") + " are not contiguous");
889           }
890         }
891         // Add the new stripe.
892         ArrayList<StoreFile> tmp = new ArrayList<StoreFile>();
893         tmp.add(newStripe.getValue());
894         stripeFiles.add(insertAt, tmp);
895         previousEndRow = newStripe.getKey();
896         if (!isOpen(previousEndRow)) {
897           stripeEndRows.add(insertAt, previousEndRow);
898         }
899         ++insertAt;
900       }
901     }
902   }
903 
904   @Override
905   public List<StoreFile> getLevel0Files() {
906     return this.state.level0Files;
907   }
908 
909   @Override
910   public List<byte[]> getStripeBoundaries() {
911     if (this.state.stripeFiles.isEmpty()) return new ArrayList<byte[]>();
912     ArrayList<byte[]> result = new ArrayList<byte[]>(this.state.stripeEndRows.length + 2);
913     result.add(OPEN_KEY);
914     Collections.addAll(result, this.state.stripeEndRows);
915     result.add(OPEN_KEY);
916     return result;
917   }
918 
919   @Override
920   public ArrayList<ImmutableList<StoreFile>> getStripes() {
921     return this.state.stripeFiles;
922   }
923 
924   @Override
925   public int getStripeCount() {
926     return this.state.stripeFiles.size();
927   }
928 
929   @Override
930   public Collection<StoreFile> getUnneededFiles(long maxTs, List<StoreFile> filesCompacting) {
931     // 1) We can never get rid of the last file which has the maximum seqid in a stripe.
932     // 2) Files that are not the latest can't become one due to (1), so the rest are fair game.
933     State state = this.state;
934     Collection<StoreFile> expiredStoreFiles = null;
935     for (ImmutableList<StoreFile> stripe : state.stripeFiles) {
936       expiredStoreFiles = findExpiredFiles(stripe, maxTs, filesCompacting, expiredStoreFiles);
937     }
938     return findExpiredFiles(state.level0Files, maxTs, filesCompacting, expiredStoreFiles);
939   }
940 
941   private Collection<StoreFile> findExpiredFiles(ImmutableList<StoreFile> stripe, long maxTs,
942       List<StoreFile> filesCompacting, Collection<StoreFile> expiredStoreFiles) {
943     // Order by seqnum is reversed.
944     for (int i = 1; i < stripe.size(); ++i) {
945       StoreFile sf = stripe.get(i);
946       long fileTs = sf.getReader().getMaxTimestamp();
947       if (fileTs < maxTs && !filesCompacting.contains(sf)) {
948         LOG.info("Found an expired store file: " + sf.getPath()
949             + " whose maxTimeStamp is " + fileTs + ", which is below " + maxTs);
950         if (expiredStoreFiles == null) {
951           expiredStoreFiles = new ArrayList<StoreFile>();
952         }
953         expiredStoreFiles.add(sf);
954       }
955     }
956     return expiredStoreFiles;
957   }
958 }