1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.regionserver;
21
22 import java.io.IOException;
23 import java.io.InterruptedIOException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.NavigableSet;
27 import java.util.concurrent.CountDownLatch;
28 import java.util.concurrent.locks.ReentrantLock;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.hadoop.hbase.Cell;
33 import org.apache.hadoop.hbase.CellUtil;
34 import org.apache.hadoop.hbase.DoNotRetryIOException;
35 import org.apache.hadoop.hbase.HConstants;
36 import org.apache.hadoop.hbase.KeyValue;
37 import org.apache.hadoop.hbase.KeyValue.KVComparator;
38 import org.apache.hadoop.hbase.KeyValueUtil;
39 import org.apache.hadoop.hbase.classification.InterfaceAudience;
40 import org.apache.hadoop.hbase.client.IsolationLevel;
41 import org.apache.hadoop.hbase.client.Scan;
42 import org.apache.hadoop.hbase.executor.ExecutorService;
43 import org.apache.hadoop.hbase.filter.Filter;
44 import org.apache.hadoop.hbase.regionserver.ScannerContext.LimitScope;
45 import org.apache.hadoop.hbase.regionserver.ScannerContext.NextState;
46 import org.apache.hadoop.hbase.regionserver.handler.ParallelSeekHandler;
47 import org.apache.hadoop.hbase.util.Bytes;
48 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
49
50 import com.google.common.annotations.VisibleForTesting;
51 import org.apache.hadoop.hbase.util.CollectionUtils;
52
53
54
55
56
57 @InterfaceAudience.Private
58 public class StoreScanner extends NonReversedNonLazyKeyValueScanner
59 implements KeyValueScanner, InternalScanner, ChangedReadersObserver {
60 private static final Log LOG = LogFactory.getLog(StoreScanner.class);
61
62 protected final Store store;
63 protected ScanQueryMatcher matcher;
64 protected KeyValueHeap heap;
65 protected boolean cacheBlocks;
66
67 protected long countPerRow = 0;
68 protected int storeLimit = -1;
69 protected int storeOffset = 0;
70
71
72
73 protected boolean closing = false;
74 protected final boolean get;
75 protected final boolean explicitColumnQuery;
76 protected final boolean useRowColBloom;
77
78
79
80 protected boolean parallelSeekEnabled = false;
81 protected ExecutorService executor;
82 protected final Scan scan;
83 protected final NavigableSet<byte[]> columns;
84 protected final long oldestUnexpiredTS;
85 protected final long now;
86 protected final int minVersions;
87 protected final long maxRowSize;
88 protected final long cellsPerHeartbeatCheck;
89
90
91
92
93
94 private final List<KeyValueScanner> scannersForDelayedClose = new ArrayList<>();
95
96
97
98
99 private long kvsScanned = 0;
100 private Cell prevCell = null;
101
102
103 static final boolean LAZY_SEEK_ENABLED_BY_DEFAULT = true;
104 public static final String STORESCANNER_PARALLEL_SEEK_ENABLE =
105 "hbase.storescanner.parallel.seek.enable";
106
107
108 protected static boolean lazySeekEnabledGlobally =
109 LAZY_SEEK_ENABLED_BY_DEFAULT;
110
111
112
113
114
115
116 public static final String HBASE_CELLS_SCANNED_PER_HEARTBEAT_CHECK =
117 "hbase.cells.scanned.per.heartbeat.check";
118
119
120
121
122 public static final long DEFAULT_HBASE_CELLS_SCANNED_PER_HEARTBEAT_CHECK = 10000;
123
124
125 protected Cell lastTop = null;
126
127
128 private boolean scanUsePread = false;
129
130 private volatile boolean flushed = false;
131
132
133 private final List<KeyValueScanner> flushedstoreFileScanners =
134 new ArrayList<KeyValueScanner>(1);
135
136 private final List<KeyValueScanner> memStoreScannersAfterFlush = new ArrayList<>(1);
137
138 private final List<KeyValueScanner> currentScanners = new ArrayList<KeyValueScanner>();
139
140 private ReentrantLock flushLock = new ReentrantLock();
141
142 private final long readPt;
143 private boolean topChanged = false;
144
145 enum StoreScannerCompactionRace {
146 BEFORE_SEEK,
147 AFTER_SEEK,
148 COMPACT_COMPLETE
149 }
150
151
152 protected StoreScanner(Store store, Scan scan, final ScanInfo scanInfo,
153 final NavigableSet<byte[]> columns, long readPt, boolean cacheBlocks) {
154 this.readPt = readPt;
155 this.store = store;
156 this.cacheBlocks = cacheBlocks;
157 get = scan.isGetScan();
158 int numCol = columns == null ? 0 : columns.size();
159 explicitColumnQuery = numCol > 0;
160 this.scan = scan;
161 this.columns = columns;
162 this.now = EnvironmentEdgeManager.currentTime();
163 this.oldestUnexpiredTS = now - scanInfo.getTtl();
164 this.minVersions = scanInfo.getMinVersions();
165
166
167
168
169
170 this.useRowColBloom = numCol > 1 || (!get && numCol == 1);
171
172 this.maxRowSize = scanInfo.getTableMaxRowSize();
173 this.scanUsePread = scan.isSmall()? true: scanInfo.isUsePread();
174 this.cellsPerHeartbeatCheck = scanInfo.getCellsPerTimeoutCheck();
175
176 if (this.store != null && this.store.getStorefilesCount() > 1) {
177 RegionServerServices rsService = ((HStore)store).getHRegion().getRegionServerServices();
178 if (rsService != null && scanInfo.isParallelSeekEnabled()) {
179 this.parallelSeekEnabled = true;
180 this.executor = rsService.getExecutorService();
181 }
182 }
183 }
184
185 protected void addCurrentScanners(List<? extends KeyValueScanner> scanners) {
186 this.currentScanners.addAll(scanners);
187 }
188
189
190
191
192
193
194
195
196
197 public StoreScanner(Store store, ScanInfo scanInfo, Scan scan, final NavigableSet<byte[]> columns,
198 long readPt)
199 throws IOException {
200 this(store, scan, scanInfo, columns, readPt, scan.getCacheBlocks());
201 if (columns != null && scan.isRaw()) {
202 throw new DoNotRetryIOException("Cannot specify any column for a raw scan");
203 }
204 matcher = new ScanQueryMatcher(scan, scanInfo, columns,
205 ScanType.USER_SCAN, Long.MAX_VALUE, HConstants.LATEST_TIMESTAMP,
206 oldestUnexpiredTS, now, store.getCoprocessorHost());
207
208 this.store.addChangedReaderObserver(this);
209
210 try {
211
212 List<KeyValueScanner> scanners = getScannersNoCompaction();
213
214
215
216
217
218 seekScanners(scanners, matcher.getStartKey(), explicitColumnQuery && lazySeekEnabledGlobally,
219 parallelSeekEnabled);
220
221
222 this.storeLimit = scan.getMaxResultsPerColumnFamily();
223
224
225 this.storeOffset = scan.getRowOffsetPerColumnFamily();
226 addCurrentScanners(scanners);
227
228 resetKVHeap(scanners, store.getComparator());
229 } catch (IOException e) {
230
231
232 this.store.deleteChangedReaderObserver(this);
233 throw e;
234 }
235 }
236
237
238
239
240
241
242
243
244
245
246
247 public StoreScanner(Store store, ScanInfo scanInfo, Scan scan,
248 List<? extends KeyValueScanner> scanners, ScanType scanType,
249 long smallestReadPoint, long earliestPutTs) throws IOException {
250 this(store, scanInfo, scan, scanners, scanType, smallestReadPoint, earliestPutTs, null, null);
251 }
252
253
254
255
256
257
258
259
260
261
262
263
264 public StoreScanner(Store store, ScanInfo scanInfo, Scan scan,
265 List<? extends KeyValueScanner> scanners, long smallestReadPoint, long earliestPutTs,
266 byte[] dropDeletesFromRow, byte[] dropDeletesToRow) throws IOException {
267 this(store, scanInfo, scan, scanners, ScanType.COMPACT_RETAIN_DELETES, smallestReadPoint,
268 earliestPutTs, dropDeletesFromRow, dropDeletesToRow);
269 }
270
271 private StoreScanner(Store store, ScanInfo scanInfo, Scan scan,
272 List<? extends KeyValueScanner> scanners, ScanType scanType, long smallestReadPoint,
273 long earliestPutTs, byte[] dropDeletesFromRow, byte[] dropDeletesToRow) throws IOException {
274 this(store, scan, scanInfo, null,
275 ((HStore)store).getHRegion().getReadpoint(IsolationLevel.READ_COMMITTED), false);
276 if (dropDeletesFromRow == null) {
277 matcher = new ScanQueryMatcher(scan, scanInfo, null, scanType, smallestReadPoint,
278 earliestPutTs, oldestUnexpiredTS, now, store.getCoprocessorHost());
279 } else {
280 matcher = new ScanQueryMatcher(scan, scanInfo, null, smallestReadPoint, earliestPutTs,
281 oldestUnexpiredTS, now, dropDeletesFromRow, dropDeletesToRow, store.getCoprocessorHost());
282 }
283
284
285 scanners = selectScannersFrom(scanners);
286
287
288 seekScanners(scanners, matcher.getStartKey(), false, parallelSeekEnabled);
289 addCurrentScanners(scanners);
290
291 resetKVHeap(scanners, store.getComparator());
292 }
293
294 @VisibleForTesting
295 StoreScanner(final Scan scan, ScanInfo scanInfo,
296 ScanType scanType, final NavigableSet<byte[]> columns,
297 final List<KeyValueScanner> scanners) throws IOException {
298 this(scan, scanInfo, scanType, columns, scanners,
299 HConstants.LATEST_TIMESTAMP,
300
301 0);
302 }
303
304 @VisibleForTesting
305 StoreScanner(final Scan scan, ScanInfo scanInfo,
306 ScanType scanType, final NavigableSet<byte[]> columns,
307 final List<KeyValueScanner> scanners, long earliestPutTs)
308 throws IOException {
309 this(scan, scanInfo, scanType, columns, scanners, earliestPutTs,
310
311 0);
312 }
313
314 private StoreScanner(final Scan scan, ScanInfo scanInfo,
315 ScanType scanType, final NavigableSet<byte[]> columns,
316 final List<KeyValueScanner> scanners, long earliestPutTs, long readPt)
317 throws IOException {
318 this(null, scan, scanInfo, columns, readPt, scan.getCacheBlocks());
319 this.matcher = new ScanQueryMatcher(scan, scanInfo, columns, scanType,
320 Long.MAX_VALUE, earliestPutTs, oldestUnexpiredTS, now, null);
321
322
323 if (this.store != null) {
324 this.store.addChangedReaderObserver(this);
325 }
326
327 seekScanners(scanners, matcher.getStartKey(), false, parallelSeekEnabled);
328 addCurrentScanners(scanners);
329 resetKVHeap(scanners, scanInfo.getComparator());
330 }
331
332
333
334
335
336 protected List<KeyValueScanner> getScannersNoCompaction() throws IOException {
337 final boolean isCompaction = false;
338 boolean usePread = get || scanUsePread;
339 return selectScannersFrom(store.getScanners(cacheBlocks, get, usePread,
340 isCompaction, matcher, scan.getStartRow(), scan.getStopRow(), this.readPt));
341 }
342
343
344
345
346
347
348
349
350
351 protected void seekScanners(List<? extends KeyValueScanner> scanners,
352 Cell seekKey, boolean isLazy, boolean isParallelSeek)
353 throws IOException {
354
355
356
357
358 if (isLazy) {
359 for (KeyValueScanner scanner : scanners) {
360 scanner.requestSeek(seekKey, false, true);
361 }
362 } else {
363 if (!isParallelSeek) {
364 long totalScannersSoughtBytes = 0;
365 for (KeyValueScanner scanner : scanners) {
366 if (totalScannersSoughtBytes >= maxRowSize) {
367 throw new RowTooBigException("Max row size allowed: " + maxRowSize
368 + ", but row is bigger than that");
369 }
370 scanner.seek(seekKey);
371 Cell c = scanner.peek();
372 if (c != null) {
373 totalScannersSoughtBytes += CellUtil.estimatedSerializedSizeOf(c);
374 }
375 }
376 } else {
377 parallelSeek(scanners, seekKey);
378 }
379 }
380 }
381
382 protected void resetKVHeap(List<? extends KeyValueScanner> scanners,
383 KVComparator comparator) throws IOException {
384
385 heap = new KeyValueHeap(scanners, comparator);
386 }
387
388
389
390
391
392 protected List<KeyValueScanner> selectScannersFrom(
393 final List<? extends KeyValueScanner> allScanners) {
394 boolean memOnly;
395 boolean filesOnly;
396 if (scan instanceof InternalScan) {
397 InternalScan iscan = (InternalScan)scan;
398 memOnly = iscan.isCheckOnlyMemStore();
399 filesOnly = iscan.isCheckOnlyStoreFiles();
400 } else {
401 memOnly = false;
402 filesOnly = false;
403 }
404
405 List<KeyValueScanner> scanners =
406 new ArrayList<KeyValueScanner>(allScanners.size());
407
408
409
410 long expiredTimestampCutoff = minVersions == 0 ? oldestUnexpiredTS: Long.MIN_VALUE;
411
412
413 for (KeyValueScanner kvs : allScanners) {
414 boolean isFile = kvs.isFileScanner();
415 if ((!isFile && filesOnly) || (isFile && memOnly)) {
416 continue;
417 }
418
419 if (kvs.shouldUseScanner(scan, store, expiredTimestampCutoff)) {
420 scanners.add(kvs);
421 } else {
422 kvs.close();
423 }
424 }
425 return scanners;
426 }
427
428 @Override
429 public Cell peek() {
430 if (this.heap == null) {
431 return this.lastTop;
432 }
433 return this.heap.peek();
434 }
435
436 @Override
437 public KeyValue next() {
438
439 throw new RuntimeException("Never call StoreScanner.next()");
440 }
441
442 @Override
443 public void close() {
444 if (this.closing) {
445 return;
446 }
447
448
449 if (this.store != null) {
450 this.store.deleteChangedReaderObserver(this);
451 }
452
453
454
455 flushLock.lock();
456 try {
457 this.closing = true;
458 clearAndClose(scannersForDelayedClose);
459 clearAndClose(memStoreScannersAfterFlush);
460
461
462
463 clearAndClose(flushedstoreFileScanners);
464 } finally {
465 flushLock.unlock();
466 }
467 if (this.heap != null)
468 this.heap.close();
469 this.heap = null;
470 this.lastTop = null;
471 }
472
473 @Override
474 public boolean seek(Cell key) throws IOException {
475 boolean flushed = checkFlushed();
476
477 checkReseek(flushed);
478 return this.heap.seek(key);
479 }
480
481 @Override
482 public boolean next(List<Cell> outResult) throws IOException {
483 return next(outResult, NoLimitScannerContext.getInstance());
484 }
485
486
487
488
489
490
491
492 @Override
493 public boolean next(List<Cell> outResult, ScannerContext scannerContext) throws IOException {
494 if (scannerContext == null) {
495 throw new IllegalArgumentException("Scanner context cannot be null");
496 }
497 boolean flushed = checkFlushed();
498 if (checkReseek(flushed)) {
499 return scannerContext.setScannerState(NextState.MORE_VALUES).hasMoreValues();
500 }
501
502
503
504 if (this.heap == null) {
505 close();
506 return scannerContext.setScannerState(NextState.NO_MORE_VALUES).hasMoreValues();
507 }
508
509 Cell cell = this.heap.peek();
510 if (cell == null) {
511 close();
512 return scannerContext.setScannerState(NextState.NO_MORE_VALUES).hasMoreValues();
513 }
514
515
516
517 byte[] row = cell.getRowArray();
518 int offset = cell.getRowOffset();
519 short length = cell.getRowLength();
520
521
522
523
524 if (!scannerContext.hasAnyLimit(LimitScope.BETWEEN_CELLS) || matcher.row == null) {
525 this.countPerRow = 0;
526 matcher.setRow(row, offset, length);
527 }
528
529
530 if (!scannerContext.getKeepProgress()) scannerContext.clearProgress();
531
532
533 KeyValue.KVComparator comparator =
534 store != null ? store.getComparator() : null;
535
536 int count = 0;
537 long totalBytesRead = 0;
538
539 LOOP: do {
540
541 if ((kvsScanned % cellsPerHeartbeatCheck == 0)) {
542 scannerContext.updateTimeProgress();
543 if (scannerContext.checkTimeLimit(LimitScope.BETWEEN_CELLS)) {
544 return scannerContext.setScannerState(NextState.TIME_LIMIT_REACHED).hasMoreValues();
545 }
546 }
547
548 if (prevCell != cell) ++kvsScanned;
549 checkScanOrder(prevCell, cell, comparator);
550 prevCell = cell;
551 topChanged = false;
552 ScanQueryMatcher.MatchCode qcode = matcher.match(cell);
553 switch (qcode) {
554 case INCLUDE:
555 case INCLUDE_AND_SEEK_NEXT_ROW:
556 case INCLUDE_AND_SEEK_NEXT_COL:
557
558 Filter f = matcher.getFilter();
559 if (f != null) {
560
561 cell = f.transformCell(cell);
562 }
563
564 this.countPerRow++;
565 if (storeLimit > -1 &&
566 this.countPerRow > (storeLimit + storeOffset)) {
567
568 if (!matcher.moreRowsMayExistAfter(cell)) {
569 return scannerContext.setScannerState(NextState.NO_MORE_VALUES).hasMoreValues();
570 }
571
572
573
574 matcher.row = null;
575 seekToNextRow(cell);
576 break LOOP;
577 }
578
579
580
581 if (this.countPerRow > storeOffset) {
582 outResult.add(cell);
583
584
585 count++;
586 totalBytesRead += CellUtil.estimatedSerializedSizeOf(cell);
587
588
589 scannerContext.incrementSizeProgress(CellUtil.estimatedHeapSizeOfWithoutTags(cell));
590 scannerContext.incrementBatchProgress(1);
591
592 if (totalBytesRead > maxRowSize) {
593 throw new RowTooBigException("Max row size allowed: " + maxRowSize
594 + ", but the row is bigger than that.");
595 }
596 }
597
598 if (qcode == ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_ROW) {
599 if (!matcher.moreRowsMayExistAfter(cell)) {
600 return scannerContext.setScannerState(NextState.NO_MORE_VALUES).hasMoreValues();
601 }
602
603
604
605 matcher.row = null;
606 seekOrSkipToNextRow(cell);
607 } else if (qcode == ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_COL) {
608 seekOrSkipToNextColumn(cell);
609 } else {
610 this.heap.next();
611 }
612
613 if (scannerContext.checkBatchLimit(LimitScope.BETWEEN_CELLS)) {
614 break LOOP;
615 }
616 if (scannerContext.checkSizeLimit(LimitScope.BETWEEN_CELLS)) {
617 break LOOP;
618 }
619 continue;
620
621 case DONE:
622
623 if (this.scan.isGetScan()) {
624
625 close();
626 return scannerContext.setScannerState(NextState.NO_MORE_VALUES).hasMoreValues();
627 }
628
629
630
631 matcher.row = null;
632 return scannerContext.setScannerState(NextState.MORE_VALUES).hasMoreValues();
633
634 case DONE_SCAN:
635 close();
636 return scannerContext.setScannerState(NextState.NO_MORE_VALUES).hasMoreValues();
637
638 case SEEK_NEXT_ROW:
639
640
641 if (!matcher.moreRowsMayExistAfter(cell)) {
642 return scannerContext.setScannerState(NextState.NO_MORE_VALUES).hasMoreValues();
643 }
644
645
646
647 matcher.row = null;
648 seekOrSkipToNextRow(cell);
649 NextState stateAfterSeekNextRow = needToReturn(outResult);
650 if (stateAfterSeekNextRow != null) {
651 return scannerContext.setScannerState(stateAfterSeekNextRow).hasMoreValues();
652 }
653 break;
654
655 case SEEK_NEXT_COL:
656 seekOrSkipToNextColumn(cell);
657 NextState stateAfterSeekNextColumn = needToReturn(outResult);
658 if (stateAfterSeekNextColumn != null) {
659 return scannerContext.setScannerState(stateAfterSeekNextColumn).hasMoreValues();
660 }
661 break;
662
663 case SKIP:
664 this.heap.next();
665 break;
666
667 case SEEK_NEXT_USING_HINT:
668
669 Cell nextKV = matcher.getNextKeyHint(cell);
670 if (nextKV != null) {
671 seekAsDirection(nextKV);
672 NextState stateAfterSeekByHint = needToReturn(outResult);
673 if (stateAfterSeekByHint != null) {
674 return scannerContext.setScannerState(stateAfterSeekByHint).hasMoreValues();
675 }
676 } else {
677 heap.next();
678 }
679 break;
680
681 default:
682 throw new RuntimeException("UNEXPECTED");
683 }
684 } while((cell = this.heap.peek()) != null);
685
686 if (count > 0) {
687 return scannerContext.setScannerState(NextState.MORE_VALUES).hasMoreValues();
688 }
689
690
691 close();
692 return scannerContext.setScannerState(NextState.NO_MORE_VALUES).hasMoreValues();
693 }
694
695
696
697
698
699
700
701
702
703
704
705
706 private NextState needToReturn(List<Cell> outResult) {
707 if (!outResult.isEmpty() && topChanged) {
708 return heap.peek() == null ? NextState.NO_MORE_VALUES : NextState.MORE_VALUES;
709 }
710 return null;
711 }
712
713 @Override
714 public long getReadPoint() {
715 return readPt;
716 }
717
718 private static void clearAndClose(List<KeyValueScanner> scanners) {
719 for (KeyValueScanner s : scanners) {
720 s.close();
721 }
722 scanners.clear();
723 }
724
725
726 @Override
727 public void updateReaders(List<StoreFile> sfs, List<KeyValueScanner> memStoreScanners) throws IOException {
728 if (CollectionUtils.isEmpty(sfs) && CollectionUtils.isEmpty(memStoreScanners)) {
729 return;
730 }
731 flushLock.lock();
732 try {
733 if (this.closing) {
734
735
736 if (!CollectionUtils.isEmpty(memStoreScanners)) {
737 clearAndClose(new ArrayList<>(memStoreScanners));
738 }
739 return;
740 }
741 flushed = true;
742 final boolean isCompaction = false;
743 boolean usePread = get || scanUsePread;
744
745
746
747
748
749
750 List<KeyValueScanner> scanners = store.getScanners(sfs, cacheBlocks, get, usePread,
751 isCompaction, matcher, scan.getStartRow(), scan.getStopRow(), this.readPt, false);
752 flushedstoreFileScanners.addAll(scanners);
753 if (!CollectionUtils.isEmpty(memStoreScanners)) {
754 clearAndClose(memStoreScannersAfterFlush);
755 memStoreScannersAfterFlush.addAll(memStoreScanners);
756 }
757 } finally {
758 flushLock.unlock();
759 }
760 }
761
762
763 protected void nullifyCurrentHeap() throws IOException {
764 if (this.closing) return;
765
766
767
768
769
770
771 if (this.heap == null) return;
772
773
774 this.lastTop = this.heap.peek();
775
776
777
778
779 this.heap.close();
780 this.heap = null;
781
782
783 }
784
785 private void seekOrSkipToNextRow(Cell cell) throws IOException {
786
787
788 if (!get) {
789 if (trySkipToNextRow(cell)) {
790 return;
791 }
792 }
793 seekToNextRow(cell);
794 }
795
796 private void seekOrSkipToNextColumn(Cell cell) throws IOException {
797 if (!trySkipToNextColumn(cell)) {
798 seekAsDirection(matcher.getKeyForNextColumn(cell));
799 }
800 }
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853 @VisibleForTesting
854 protected boolean trySkipToNextRow(Cell cell) throws IOException {
855 Cell nextCell = null;
856
857
858 Cell previousIndexedKey = null;
859 do {
860 Cell nextIndexedKey = getNextIndexedKey();
861 if (nextIndexedKey != null && nextIndexedKey != KeyValueScanner.NO_NEXT_INDEXED_KEY
862 && (nextIndexedKey == previousIndexedKey
863 || matcher.compareKeyForNextRow(nextIndexedKey, cell) >= 0)) {
864 this.heap.next();
865 ++kvsScanned;
866 previousIndexedKey = nextIndexedKey;
867 } else {
868 return false;
869 }
870 nextCell = this.heap.peek();
871 } while (nextCell != null && CellUtil.matchingRow(cell, nextCell));
872 return true;
873 }
874
875
876
877
878
879
880 @VisibleForTesting
881 protected boolean trySkipToNextColumn(Cell cell) throws IOException {
882 Cell nextCell = null;
883
884
885 Cell previousIndexedKey = null;
886 do {
887 Cell nextIndexedKey = getNextIndexedKey();
888 if (nextIndexedKey != null && nextIndexedKey != KeyValueScanner.NO_NEXT_INDEXED_KEY
889 && (nextIndexedKey == previousIndexedKey
890 || matcher.compareKeyForNextColumn(nextIndexedKey, cell) >= 0)) {
891 this.heap.next();
892 ++kvsScanned;
893 previousIndexedKey = nextIndexedKey;
894 } else {
895 return false;
896 }
897 nextCell = this.heap.peek();
898 } while (nextCell != null && CellUtil.matchingRow(cell, nextCell)
899 && CellUtil.matchingColumn(cell, nextCell));
900
901
902
903 if (nextCell != null && matcher.compareKeyForNextColumn(nextCell, cell) < 0) {
904 return false;
905 }
906 return true;
907 }
908
909
910
911
912
913
914
915 protected boolean checkReseek(boolean flushed) throws IOException {
916 if (flushed && this.lastTop != null) {
917 resetScannerStack(this.lastTop);
918 if (this.heap.peek() == null
919 || store.getComparator().compareRows(this.lastTop, this.heap.peek()) != 0) {
920 LOG.info("Storescanner.peek() is changed where before = "
921 + this.lastTop.toString() + ",and after = " + this.heap.peek());
922 this.lastTop = null;
923 topChanged = true;
924 return true;
925 }
926 this.lastTop = null;
927 }
928
929 topChanged = false;
930 return false;
931 }
932
933 protected void resetScannerStack(Cell lastTopKey) throws IOException {
934
935
936
937
938
939 final boolean isCompaction = false;
940 boolean usePread = get || scanUsePread;
941 List<KeyValueScanner> scanners = null;
942 flushLock.lock();
943 try {
944 List<KeyValueScanner> allScanners =
945 new ArrayList<>(flushedstoreFileScanners.size() + memStoreScannersAfterFlush.size());
946 allScanners.addAll(flushedstoreFileScanners);
947 allScanners.addAll(memStoreScannersAfterFlush);
948 scanners = selectScannersFrom(allScanners);
949
950 flushedstoreFileScanners.clear();
951 memStoreScannersAfterFlush.clear();
952 } finally {
953 flushLock.unlock();
954 }
955
956
957 seekScanners(scanners, lastTopKey, false, parallelSeekEnabled);
958
959 for (int i = 0; i < currentScanners.size(); i++) {
960 if (!currentScanners.get(i).isFileScanner()) {
961 scannersForDelayedClose.add(currentScanners.remove(i));
962 break;
963 }
964 }
965
966 addCurrentScanners(scanners);
967
968 resetKVHeap(this.currentScanners, store.getComparator());
969
970
971
972 Cell kv = heap.peek();
973 if (kv == null) {
974 kv = lastTopKey;
975 }
976 byte[] row = kv.getRowArray();
977 int offset = kv.getRowOffset();
978 short length = kv.getRowLength();
979 if ((matcher.row == null) || !Bytes.equals(row, offset, length, matcher.row,
980 matcher.rowOffset, matcher.rowLength)) {
981 this.countPerRow = 0;
982 matcher.reset();
983 matcher.setRow(row, offset, length);
984 }
985 }
986
987
988
989
990
991
992
993
994 protected void checkScanOrder(Cell prevKV, Cell kv,
995 KeyValue.KVComparator comparator) throws IOException {
996
997 assert prevKV == null || comparator == null
998 || comparator.compare(prevKV, kv) <= 0 : "Key " + prevKV
999 + " followed by a " + "smaller key " + kv + " in cf " + store;
1000 }
1001
1002 protected boolean seekToNextRow(Cell kv) throws IOException {
1003 return reseek(KeyValueUtil.createLastOnRow(kv));
1004 }
1005
1006
1007
1008
1009
1010
1011
1012 protected boolean seekAsDirection(Cell kv)
1013 throws IOException {
1014 return reseek(kv);
1015 }
1016
1017 @Override
1018 public boolean reseek(Cell kv) throws IOException {
1019 boolean flushed = checkFlushed();
1020
1021
1022
1023 checkReseek(flushed);
1024 if (explicitColumnQuery && lazySeekEnabledGlobally) {
1025 return heap.requestSeek(kv, true, useRowColBloom);
1026 }
1027 return heap.reseek(kv);
1028 }
1029
1030 protected boolean checkFlushed() {
1031
1032
1033
1034
1035
1036 if (flushed) {
1037
1038
1039 if(!this.closing) {
1040 this.lastTop = this.peek();
1041 } else {
1042 return false;
1043 }
1044
1045 flushed = false;
1046 return true;
1047 }
1048 return false;
1049 }
1050
1051 @Override
1052 public long getSequenceID() {
1053 return 0;
1054 }
1055
1056
1057
1058
1059
1060
1061
1062 private void parallelSeek(final List<? extends KeyValueScanner>
1063 scanners, final Cell kv) throws IOException {
1064 if (scanners.isEmpty()) return;
1065 int storeFileScannerCount = scanners.size();
1066 CountDownLatch latch = new CountDownLatch(storeFileScannerCount);
1067 List<ParallelSeekHandler> handlers =
1068 new ArrayList<ParallelSeekHandler>(storeFileScannerCount);
1069 for (KeyValueScanner scanner : scanners) {
1070 if (scanner instanceof StoreFileScanner) {
1071 ParallelSeekHandler seekHandler = new ParallelSeekHandler(scanner, kv,
1072 this.readPt, latch);
1073 executor.submit(seekHandler);
1074 handlers.add(seekHandler);
1075 } else {
1076 scanner.seek(kv);
1077 latch.countDown();
1078 }
1079 }
1080
1081 try {
1082 latch.await();
1083 } catch (InterruptedException ie) {
1084 throw (InterruptedIOException)new InterruptedIOException().initCause(ie);
1085 }
1086
1087 for (ParallelSeekHandler handler : handlers) {
1088 if (handler.getErr() != null) {
1089 throw new IOException(handler.getErr());
1090 }
1091 }
1092 }
1093
1094
1095
1096
1097
1098 List<KeyValueScanner> getAllScannersForTesting() {
1099 List<KeyValueScanner> allScanners = new ArrayList<KeyValueScanner>();
1100 KeyValueScanner current = heap.getCurrentForTesting();
1101 if (current != null)
1102 allScanners.add(current);
1103 for (KeyValueScanner scanner : heap.getHeap())
1104 allScanners.add(scanner);
1105 return allScanners;
1106 }
1107
1108 static void enableLazySeekGlobally(boolean enable) {
1109 lazySeekEnabledGlobally = enable;
1110 }
1111
1112
1113
1114
1115 public long getEstimatedNumberOfKvsScanned() {
1116 return this.kvsScanned;
1117 }
1118
1119 @Override
1120 public Cell getNextIndexedKey() {
1121 return this.heap.getNextIndexedKey();
1122 }
1123 }