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.lang.management.ManagementFactory;
23 import java.lang.management.RuntimeMXBean;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.NavigableSet;
29 import java.util.SortedSet;
30 import java.util.concurrent.atomic.AtomicInteger;
31 import java.util.concurrent.atomic.AtomicLong;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.hadoop.hbase.classification.InterfaceAudience;
36 import org.apache.hadoop.conf.Configuration;
37 import org.apache.hadoop.hbase.Cell;
38 import org.apache.hadoop.hbase.CellUtil;
39 import org.apache.hadoop.hbase.HBaseConfiguration;
40 import org.apache.hadoop.hbase.HConstants;
41 import org.apache.hadoop.hbase.KeyValue;
42 import org.apache.hadoop.hbase.KeyValueUtil;
43 import org.apache.hadoop.hbase.client.Scan;
44 import org.apache.hadoop.hbase.exceptions.UnexpectedStateException;
45 import org.apache.hadoop.hbase.io.TimeRange;
46 import org.apache.hadoop.hbase.util.ByteRange;
47 import org.apache.hadoop.hbase.util.Bytes;
48 import org.apache.hadoop.hbase.util.ClassSize;
49 import org.apache.hadoop.hbase.util.CollectionBackedScanner;
50 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
51 import org.apache.hadoop.hbase.util.ReflectionUtils;
52 import org.apache.htrace.Trace;
53
54 import com.google.common.annotations.VisibleForTesting;
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 @InterfaceAudience.Private
73 public class DefaultMemStore implements MemStore {
74 private static final Log LOG = LogFactory.getLog(DefaultMemStore.class);
75 @VisibleForTesting
76 static final String USEMSLAB_KEY = "hbase.hregion.memstore.mslab.enabled";
77 private static final boolean USEMSLAB_DEFAULT = true;
78 private static final String MSLAB_CLASS_NAME = "hbase.regionserver.mslab.class";
79
80 private Configuration conf;
81
82 @VisibleForTesting
83 final KeyValue.KVComparator comparator;
84
85
86 private volatile long timeOfOldestEdit = Long.MAX_VALUE;
87
88 private volatile long snapshotId;
89 private volatile boolean tagsPresent;
90
91 @VisibleForTesting
92 volatile Section activeSection;
93 @VisibleForTesting
94 volatile Section snapshotSection;
95
96
97
98
99 public DefaultMemStore() {
100 this(HBaseConfiguration.create(), KeyValue.COMPARATOR);
101 }
102
103
104
105
106
107 public DefaultMemStore(final Configuration conf,
108 final KeyValue.KVComparator c) {
109 this.conf = conf;
110 this.comparator = c;
111 this.activeSection = Section.newActiveSection(comparator, conf);
112 this.snapshotSection = Section.newSnapshotSection(comparator);
113 }
114
115
116
117
118
119 @Override
120 public MemStoreSnapshot snapshot() {
121
122
123 if (!snapshotSection.getCellSkipListSet().isEmpty()) {
124 LOG.warn("Snapshot called again without clearing previous. " +
125 "Doing nothing. Another ongoing flush or did we fail last attempt?");
126 } else {
127 this.snapshotId = EnvironmentEdgeManager.currentTime();
128 if (!activeSection.getCellSkipListSet().isEmpty()) {
129 snapshotSection = activeSection;
130 activeSection = Section.newActiveSection(comparator, conf);
131 snapshotSection.getHeapSize().addAndGet(-DEEP_OVERHEAD);
132 timeOfOldestEdit = Long.MAX_VALUE;
133 }
134 }
135 MemStoreSnapshot memStoreSnapshot = new MemStoreSnapshot(this.snapshotId,
136 snapshotSection.getCellsCount().get(), snapshotSection.getHeapSize().get(),
137 snapshotSection.getTimeRangeTracker(),
138 new CollectionBackedScanner(snapshotSection.getCellSkipListSet(), this.comparator),
139 this.tagsPresent);
140 this.tagsPresent = false;
141 return memStoreSnapshot;
142 }
143
144
145
146
147
148
149
150 @Override
151 public void clearSnapshot(long id) throws UnexpectedStateException {
152 if (this.snapshotId == -1) return;
153 if (this.snapshotId != id) {
154 throw new UnexpectedStateException("Current snapshot id is " + this.snapshotId + ",passed "
155 + id);
156 }
157
158 MemStoreLAB tmpAllocator = snapshotSection.getMemStoreLAB();
159 snapshotSection = Section.newSnapshotSection(comparator);
160 if (tmpAllocator != null) {
161 tmpAllocator.close();
162 }
163 this.snapshotId = -1;
164 }
165
166 @Override
167 public long getFlushableSize() {
168 long snapshotSize = snapshotSection.getHeapSize().get();
169 return snapshotSize > 0 ? snapshotSize : keySize();
170 }
171
172 @Override
173 public long getSnapshotSize() {
174 return snapshotSection.getHeapSize().get();
175 }
176
177
178
179
180
181
182 @Override
183 public long add(Cell cell) {
184 Cell toAdd = maybeCloneWithAllocator(cell);
185 boolean mslabUsed = (toAdd != cell);
186 return internalAdd(toAdd, mslabUsed);
187 }
188
189 @Override
190 public long add(Iterable<Cell> cells) {
191 long size = 0;
192 for (Cell cell : cells) {
193 size += add(cell);
194 }
195 return size;
196 }
197
198 @Override
199 public long timeOfOldestEdit() {
200 return timeOfOldestEdit;
201 }
202
203 private boolean addToCellSet(Cell e) {
204 boolean b = this.activeSection.getCellSkipListSet().add(e);
205
206
207
208
209 if(e.getTagsLength() > 0) {
210 tagsPresent = true;
211 }
212 setOldestEditTimeToNow();
213 return b;
214 }
215
216 private boolean removeFromCellSet(Cell e) {
217 boolean b = this.activeSection.getCellSkipListSet().remove(e);
218 setOldestEditTimeToNow();
219 return b;
220 }
221
222 void setOldestEditTimeToNow() {
223 if (timeOfOldestEdit == Long.MAX_VALUE) {
224 timeOfOldestEdit = EnvironmentEdgeManager.currentTime();
225 }
226 }
227
228
229
230
231
232
233
234
235
236
237 private long internalAdd(final Cell toAdd, boolean mslabUsed) {
238 boolean notPresent = addToCellSet(toAdd);
239 long s = heapSizeChange(toAdd, notPresent);
240 if (notPresent) {
241 activeSection.getCellsCount().incrementAndGet();
242 }
243
244
245
246 if (!notPresent && mslabUsed) {
247 s += getCellLength(toAdd);
248 }
249 activeSection.getTimeRangeTracker().includeTimestamp(toAdd);
250 activeSection.getHeapSize().addAndGet(s);
251 return s;
252 }
253
254
255
256
257 @VisibleForTesting
258 int getCellLength(Cell cell) {
259 return KeyValueUtil.length(cell);
260 }
261
262 private Cell maybeCloneWithAllocator(Cell cell) {
263 if (activeSection.getMemStoreLAB() == null) {
264 return cell;
265 }
266
267 int len = getCellLength(cell);
268 ByteRange alloc = activeSection.getMemStoreLAB().allocateBytes(len);
269 if (alloc == null) {
270
271
272 return cell;
273 }
274 assert alloc.getBytes() != null;
275 KeyValueUtil.appendToByteArray(cell, alloc.getBytes(), alloc.getOffset());
276 KeyValue newKv = new KeyValue(alloc.getBytes(), alloc.getOffset(), len);
277 newKv.setSequenceId(cell.getSequenceId());
278 return newKv;
279 }
280
281
282
283
284
285
286
287
288
289 @Override
290 public void rollback(Cell cell) {
291
292
293
294
295
296 Cell found = snapshotSection.getCellSkipListSet().get(cell);
297 if (found != null && found.getSequenceId() == cell.getSequenceId()) {
298 snapshotSection.getCellSkipListSet().remove(cell);
299 long sz = heapSizeChange(cell, true);
300 snapshotSection.getHeapSize().addAndGet(-sz);
301 snapshotSection.getCellsCount().decrementAndGet();
302 }
303
304
305 found = activeSection.getCellSkipListSet().get(cell);
306 if (found != null && found.getSequenceId() == cell.getSequenceId()) {
307 removeFromCellSet(found);
308 long sz = heapSizeChange(found, true);
309 activeSection.getHeapSize().addAndGet(-sz);
310 activeSection.getCellsCount().decrementAndGet();
311 }
312 }
313
314
315
316
317
318
319 @Override
320 public long delete(Cell deleteCell) {
321 Cell toAdd = maybeCloneWithAllocator(deleteCell);
322 boolean mslabUsed = (toAdd != deleteCell);
323 return internalAdd(toAdd, mslabUsed);
324 }
325
326
327
328
329
330
331 Cell getNextRow(final Cell cell) {
332 return getLowest(getNextRow(cell, activeSection.getCellSkipListSet()),
333 getNextRow(cell, snapshotSection.getCellSkipListSet()));
334 }
335
336
337
338
339
340
341 private Cell getLowest(final Cell a, final Cell b) {
342 if (a == null) {
343 return b;
344 }
345 if (b == null) {
346 return a;
347 }
348 return comparator.compareRows(a, b) <= 0? a: b;
349 }
350
351
352
353
354
355
356
357 private Cell getNextRow(final Cell key,
358 final NavigableSet<Cell> set) {
359 Cell result = null;
360 SortedSet<Cell> tail = key == null? set: set.tailSet(key);
361
362 for (Cell cell: tail) {
363 if (comparator.compareRows(cell, key) <= 0)
364 continue;
365
366
367 result = cell;
368 break;
369 }
370 return result;
371 }
372
373
374
375
376 @Override
377 public void getRowKeyAtOrBefore(final GetClosestRowBeforeTracker state) {
378 getRowKeyAtOrBefore(activeSection.getCellSkipListSet(), state);
379 getRowKeyAtOrBefore(snapshotSection.getCellSkipListSet(), state);
380 }
381
382
383
384
385
386 private void getRowKeyAtOrBefore(final NavigableSet<Cell> set,
387 final GetClosestRowBeforeTracker state) {
388 if (set.isEmpty()) {
389 return;
390 }
391 if (!walkForwardInSingleRow(set, state.getTargetKey(), state)) {
392
393 getRowKeyBefore(set, state);
394 }
395 }
396
397
398
399
400
401
402
403
404
405
406
407 private boolean walkForwardInSingleRow(final SortedSet<Cell> set,
408 final Cell firstOnRow, final GetClosestRowBeforeTracker state) {
409 boolean foundCandidate = false;
410 SortedSet<Cell> tail = set.tailSet(firstOnRow);
411 if (tail.isEmpty()) return foundCandidate;
412 for (Iterator<Cell> i = tail.iterator(); i.hasNext();) {
413 Cell kv = i.next();
414
415 if (state.isTooFar(kv, firstOnRow)) break;
416 if (state.isExpired(kv)) {
417 i.remove();
418 continue;
419 }
420
421 if (state.handle(kv)) {
422 foundCandidate = true;
423 break;
424 }
425 }
426 return foundCandidate;
427 }
428
429
430
431
432
433
434
435 private void getRowKeyBefore(NavigableSet<Cell> set,
436 final GetClosestRowBeforeTracker state) {
437 Cell firstOnRow = state.getTargetKey();
438 for (Member p = memberOfPreviousRow(set, state, firstOnRow);
439 p != null; p = memberOfPreviousRow(p.set, state, firstOnRow)) {
440
441 if (!state.isTargetTable(p.cell)) break;
442
443 if (!state.isBetterCandidate(p.cell)) break;
444
445 firstOnRow = new KeyValue(p.cell.getRowArray(), p.cell.getRowOffset(), p.cell.getRowLength(),
446 HConstants.LATEST_TIMESTAMP);
447
448 if (walkForwardInSingleRow(p.set, firstOnRow, state)) break;
449 }
450 }
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469 @Override
470 public long updateColumnValue(byte[] row,
471 byte[] family,
472 byte[] qualifier,
473 long newValue,
474 long now) {
475 Cell firstCell = KeyValueUtil.createFirstOnRow(row, family, qualifier);
476
477 SortedSet<Cell> snSs = snapshotSection.getCellSkipListSet().tailSet(firstCell);
478 if (!snSs.isEmpty()) {
479 Cell snc = snSs.first();
480
481 if (CellUtil.matchingRow(snc, firstCell) && CellUtil.matchingQualifier(snc, firstCell)) {
482 if (snc.getTimestamp() == now) {
483
484 now += 1;
485 }
486 }
487 }
488
489
490
491
492
493
494
495 SortedSet<Cell> ss = activeSection.getCellSkipListSet().tailSet(firstCell);
496 for (Cell cell : ss) {
497
498 if (!CellUtil.matchingColumn(cell, family, qualifier)
499 || !CellUtil.matchingRow(cell, firstCell)) {
500 break;
501 }
502
503
504 if (cell.getTypeByte() == KeyValue.Type.Put.getCode() &&
505 cell.getTimestamp() > now && CellUtil.matchingQualifier(firstCell, cell)) {
506 now = cell.getTimestamp();
507 }
508 }
509
510
511
512 List<Cell> cells = new ArrayList<Cell>(1);
513 cells.add(new KeyValue(row, family, qualifier, now, Bytes.toBytes(newValue)));
514 return upsert(cells, 1L, null);
515 }
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536 @Override
537 public long upsert(Iterable<Cell> cells, long readpoint, List<Cell> removedCells) {
538 long size = 0;
539 for (Cell cell : cells) {
540 size += upsert(cell, readpoint, removedCells);
541 }
542 return size;
543 }
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559 private long upsert(Cell cell, long readpoint, List<Cell> removedCells) {
560
561
562
563
564
565
566 long addedSize = internalAdd(cell, false);
567
568
569
570 Cell firstCell = KeyValueUtil.createFirstOnRow(
571 cell.getRowArray(), cell.getRowOffset(), cell.getRowLength(),
572 cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength(),
573 cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
574 SortedSet<Cell> ss = activeSection.getCellSkipListSet().tailSet(firstCell);
575 Iterator<Cell> it = ss.iterator();
576
577 int versionsVisible = 0;
578 while ( it.hasNext() ) {
579 Cell cur = it.next();
580
581 if (cell == cur) {
582
583 continue;
584 }
585
586 if (CellUtil.matchingRow(cell, cur) && CellUtil.matchingQualifier(cell, cur)) {
587
588 if (cur.getTypeByte() == KeyValue.Type.Put.getCode() &&
589 cur.getSequenceId() <= readpoint) {
590 if (versionsVisible >= 1) {
591
592
593
594
595 long delta = heapSizeChange(cur, true);
596 addedSize -= delta;
597 activeSection.getHeapSize().addAndGet(-delta);
598 activeSection.getCellsCount().decrementAndGet();
599 if (removedCells != null) {
600 removedCells.add(cur);
601 }
602 it.remove();
603 setOldestEditTimeToNow();
604 } else {
605 versionsVisible++;
606 }
607 }
608 } else {
609
610 break;
611 }
612 }
613 return addedSize;
614 }
615
616
617
618
619
620 private static class Member {
621 final Cell cell;
622 final NavigableSet<Cell> set;
623 Member(final NavigableSet<Cell> s, final Cell kv) {
624 this.cell = kv;
625 this.set = s;
626 }
627 }
628
629
630
631
632
633
634
635
636
637 private Member memberOfPreviousRow(NavigableSet<Cell> set,
638 final GetClosestRowBeforeTracker state, final Cell firstOnRow) {
639 NavigableSet<Cell> head = set.headSet(firstOnRow, false);
640 if (head.isEmpty()) return null;
641 for (Iterator<Cell> i = head.descendingIterator(); i.hasNext();) {
642 Cell found = i.next();
643 if (state.isExpired(found)) {
644 i.remove();
645 continue;
646 }
647 return new Member(head, found);
648 }
649 return null;
650 }
651
652
653
654
655 @Override
656 public List<KeyValueScanner> getScanners(long readPt) {
657 MemStoreScanner scanner =
658 new MemStoreScanner(activeSection, snapshotSection, readPt, comparator);
659 scanner.seek(CellUtil.createCell(HConstants.EMPTY_START_ROW));
660 if (scanner.peek() == null) {
661 scanner.close();
662 return null;
663 }
664 return Collections.<KeyValueScanner> singletonList(scanner);
665 }
666
667
668
669
670
671
672
673
674 public boolean shouldSeek(Scan scan, Store store, long oldestUnexpiredTS) {
675 return shouldSeek(activeSection.getTimeRangeTracker(),
676 snapshotSection.getTimeRangeTracker(), scan, store, oldestUnexpiredTS);
677 }
678
679
680
681
682
683
684
685
686
687
688 private static boolean shouldSeek(TimeRangeTracker activeTimeRangeTracker,
689 TimeRangeTracker snapshotTimeRangeTracker, Scan scan, Store store, long oldestUnexpiredTS) {
690 byte[] cf = store.getFamily().getName();
691 TimeRange timeRange = scan.getColumnFamilyTimeRange().get(cf);
692 if (timeRange == null) {
693 timeRange = scan.getTimeRange();
694 }
695 return (activeTimeRangeTracker.includesTimeRange(timeRange) ||
696 snapshotTimeRangeTracker.includesTimeRange(timeRange)) &&
697 (Math.max(activeTimeRangeTracker.getMax(), snapshotTimeRangeTracker.getMax()) >= oldestUnexpiredTS);
698 }
699
700
701
702
703
704
705
706 protected static class MemStoreScanner extends NonLazyKeyValueScanner {
707
708 private Cell cellSetNextRow = null;
709 private Cell snapshotNextRow = null;
710
711
712 private Cell cellSetItRow = null;
713 private Cell snapshotItRow = null;
714
715
716 private Iterator<Cell> cellSetIt;
717 private Iterator<Cell> snapshotIt;
718
719
720 private final Section activeAtCreation;
721 private final Section snapshotAtCreation;
722
723
724 private Cell theNext;
725
726
727
728 private boolean stopSkippingCellsIfNextRow = false;
729
730 private Cell stopSkippingKVsRow;
731
732 private final long readPoint;
733 private final KeyValue.KVComparator comparator;
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755 MemStoreScanner(Section activeSection, Section snapshotSection, long readPoint, final KeyValue.KVComparator c) {
756 this.readPoint = readPoint;
757 this.comparator = c;
758 activeAtCreation = activeSection;
759 snapshotAtCreation = snapshotSection;
760 if (activeAtCreation.getMemStoreLAB() != null) {
761 activeAtCreation.getMemStoreLAB().incScannerCount();
762 }
763 if (snapshotAtCreation.getMemStoreLAB() != null) {
764 snapshotAtCreation.getMemStoreLAB().incScannerCount();
765 }
766 if (Trace.isTracing() && Trace.currentSpan() != null) {
767 Trace.currentSpan().addTimelineAnnotation("Creating MemStoreScanner");
768 }
769 }
770
771
772
773
774
775
776 private Cell getNext(Iterator<Cell> it) {
777 Cell v = null;
778 try {
779 while (it.hasNext()) {
780 v = it.next();
781 if (v.getSequenceId() <= this.readPoint) {
782 return v;
783 }
784 if (stopSkippingCellsIfNextRow && stopSkippingKVsRow != null
785 && comparator.compareRows(v, stopSkippingKVsRow) > 0) {
786 return null;
787 }
788 }
789
790 return null;
791 } finally {
792 if (v != null) {
793
794 if (it == snapshotIt) {
795 snapshotItRow = v;
796 } else {
797 cellSetItRow = v;
798 }
799 }
800 }
801 }
802
803
804
805
806
807
808
809
810 @Override
811 public synchronized boolean seek(Cell key) {
812 if (key == null) {
813 close();
814 return false;
815 }
816
817
818 cellSetIt = activeAtCreation.getCellSkipListSet().tailSet(key).iterator();
819 snapshotIt = snapshotAtCreation.getCellSkipListSet().tailSet(key).iterator();
820 cellSetItRow = null;
821 snapshotItRow = null;
822
823 return seekInSubLists(key);
824 }
825
826
827
828
829
830 private synchronized boolean seekInSubLists(Cell key){
831 cellSetNextRow = getNext(cellSetIt);
832 snapshotNextRow = getNext(snapshotIt);
833
834
835 theNext = getLowest(cellSetNextRow, snapshotNextRow);
836
837
838 return (theNext != null);
839 }
840
841
842
843
844
845
846
847 @Override
848 public synchronized boolean reseek(Cell key) {
849
850
851
852
853
854
855
856
857
858
859
860
861 cellSetIt = activeAtCreation.getCellSkipListSet().tailSet(getHighest(key, cellSetItRow)).iterator();
862 snapshotIt = snapshotAtCreation.getCellSkipListSet().tailSet(getHighest(key, snapshotItRow)).iterator();
863
864 return seekInSubLists(key);
865 }
866
867
868 @Override
869 public synchronized Cell peek() {
870
871 return theNext;
872 }
873
874 @Override
875 public synchronized Cell next() {
876 if (theNext == null) {
877 return null;
878 }
879
880 final Cell ret = theNext;
881
882
883 if (theNext == cellSetNextRow) {
884 cellSetNextRow = getNext(cellSetIt);
885 } else {
886 snapshotNextRow = getNext(snapshotIt);
887 }
888
889
890 theNext = getLowest(cellSetNextRow, snapshotNextRow);
891
892
893
894
895 return ret;
896 }
897
898
899
900
901
902
903 private Cell getLowest(Cell first, Cell second) {
904 if (first == null && second == null) {
905 return null;
906 }
907 if (first != null && second != null) {
908 int compare = comparator.compare(first, second);
909 return (compare <= 0 ? first : second);
910 }
911 return (first != null ? first : second);
912 }
913
914
915
916
917
918
919 private Cell getHighest(Cell first, Cell second) {
920 if (first == null && second == null) {
921 return null;
922 }
923 if (first != null && second != null) {
924 int compare = comparator.compare(first, second);
925 return (compare > 0 ? first : second);
926 }
927 return (first != null ? first : second);
928 }
929
930 @Override
931 public synchronized void close() {
932 this.cellSetNextRow = null;
933 this.snapshotNextRow = null;
934
935 this.cellSetIt = null;
936 this.snapshotIt = null;
937
938 if (activeAtCreation != null && activeAtCreation.getMemStoreLAB() != null) {
939 activeAtCreation.getMemStoreLAB().decScannerCount();
940 }
941 if (snapshotAtCreation != null && snapshotAtCreation.getMemStoreLAB() != null) {
942 snapshotAtCreation.getMemStoreLAB().decScannerCount();
943 }
944
945 this.cellSetItRow = null;
946 this.snapshotItRow = null;
947 }
948
949
950
951
952
953
954 @Override
955 public long getScannerOrder() {
956 return Long.MAX_VALUE;
957 }
958
959 @Override
960 public boolean shouldUseScanner(Scan scan, Store store, long oldestUnexpiredTS) {
961 return shouldSeek(activeAtCreation.getTimeRangeTracker(),
962 snapshotAtCreation.getTimeRangeTracker(), scan, store, oldestUnexpiredTS);
963 }
964
965
966
967
968
969
970 @Override
971 public synchronized boolean backwardSeek(Cell key) {
972 seek(key);
973 if (peek() == null || comparator.compareRows(peek(), key) > 0) {
974 return seekToPreviousRow(key);
975 }
976 return true;
977 }
978
979
980
981
982
983
984 @Override
985 public synchronized boolean seekToPreviousRow(Cell originalKey) {
986 boolean keepSeeking = false;
987 Cell key = originalKey;
988 do {
989 Cell firstKeyOnRow = KeyValueUtil.createFirstOnRow(key.getRowArray(), key.getRowOffset(),
990 key.getRowLength());
991 SortedSet<Cell> cellHead = activeAtCreation.getCellSkipListSet().headSet(firstKeyOnRow);
992 Cell cellSetBeforeRow = cellHead.isEmpty() ? null : cellHead.last();
993 SortedSet<Cell> snapshotHead = snapshotAtCreation.getCellSkipListSet()
994 .headSet(firstKeyOnRow);
995 Cell snapshotBeforeRow = snapshotHead.isEmpty() ? null : snapshotHead
996 .last();
997 Cell lastCellBeforeRow = getHighest(cellSetBeforeRow, snapshotBeforeRow);
998 if (lastCellBeforeRow == null) {
999 theNext = null;
1000 return false;
1001 }
1002 Cell firstKeyOnPreviousRow = KeyValueUtil.createFirstOnRow(lastCellBeforeRow.getRowArray(),
1003 lastCellBeforeRow.getRowOffset(), lastCellBeforeRow.getRowLength());
1004 this.stopSkippingCellsIfNextRow = true;
1005 this.stopSkippingKVsRow = firstKeyOnPreviousRow;
1006 seek(firstKeyOnPreviousRow);
1007 this.stopSkippingCellsIfNextRow = false;
1008 if (peek() == null
1009 || comparator.compareRows(peek(), firstKeyOnPreviousRow) > 0) {
1010 keepSeeking = true;
1011 key = firstKeyOnPreviousRow;
1012 continue;
1013 } else {
1014 keepSeeking = false;
1015 }
1016 } while (keepSeeking);
1017 return true;
1018 }
1019
1020 @Override
1021 public synchronized boolean seekToLastRow() {
1022 Cell first = activeAtCreation.getCellSkipListSet().isEmpty() ? null
1023 : activeAtCreation.getCellSkipListSet().last();
1024 Cell second = snapshotAtCreation.getCellSkipListSet().isEmpty() ? null
1025 : snapshotAtCreation.getCellSkipListSet().last();
1026 Cell higherCell = getHighest(first, second);
1027 if (higherCell == null) {
1028 return false;
1029 }
1030 Cell firstCellOnLastRow = KeyValueUtil.createFirstOnRow(higherCell.getRowArray(),
1031 higherCell.getRowOffset(), higherCell.getRowLength());
1032 if (seek(firstCellOnLastRow)) {
1033 return true;
1034 } else {
1035 return seekToPreviousRow(higherCell);
1036 }
1037
1038 }
1039 }
1040
1041 public final static long FIXED_OVERHEAD = ClassSize.align(ClassSize.OBJECT
1042 + (4 * ClassSize.REFERENCE) + (2 * Bytes.SIZEOF_LONG) + Bytes.SIZEOF_BOOLEAN);
1043
1044 public final static long DEEP_OVERHEAD = ClassSize.align(FIXED_OVERHEAD +
1045 (2 * ClassSize.ATOMIC_LONG) + (2 * ClassSize.TIMERANGE_TRACKER) +
1046 (2 * ClassSize.CELL_SKIPLIST_SET) + (2 * ClassSize.CONCURRENT_SKIPLISTMAP) +
1047 ClassSize.ATOMIC_INTEGER);
1048
1049
1050
1051
1052
1053
1054
1055
1056 static long heapSizeChange(final Cell cell, final boolean notpresent) {
1057 return notpresent ? ClassSize.align(ClassSize.CONCURRENT_SKIPLISTMAP_ENTRY
1058 + CellUtil.estimatedHeapSizeOf(cell)) : 0;
1059 }
1060
1061 private long keySize() {
1062 return heapSize() - DEEP_OVERHEAD;
1063 }
1064
1065
1066
1067
1068
1069 @Override
1070 public long heapSize() {
1071 return activeSection.getHeapSize().get();
1072 }
1073
1074 @Override
1075 public long size() {
1076 return heapSize();
1077 }
1078
1079
1080
1081
1082
1083
1084
1085
1086 public static void main(String [] args) {
1087 RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
1088 LOG.info("vmName=" + runtime.getVmName() + ", vmVendor=" +
1089 runtime.getVmVendor() + ", vmVersion=" + runtime.getVmVersion());
1090 LOG.info("vmInputArguments=" + runtime.getInputArguments());
1091 DefaultMemStore memstore1 = new DefaultMemStore();
1092
1093 long size = 0;
1094 final int count = 10000;
1095 byte [] fam = Bytes.toBytes("col");
1096 byte [] qf = Bytes.toBytes("umn");
1097 byte [] empty = new byte[0];
1098 for (int i = 0; i < count; i++) {
1099
1100 size += memstore1.add(new KeyValue(Bytes.toBytes(i), fam, qf, i, empty));
1101 }
1102 LOG.info("memstore1 estimated size=" + size);
1103 for (int i = 0; i < count; i++) {
1104 size += memstore1.add(new KeyValue(Bytes.toBytes(i), fam, qf, i, empty));
1105 }
1106 LOG.info("memstore1 estimated size (2nd loading of same data)=" + size);
1107
1108 DefaultMemStore memstore2 = new DefaultMemStore();
1109 for (int i = 0; i < count; i++) {
1110 size += memstore2.add(new KeyValue(Bytes.toBytes(i), fam, qf, i, new byte[i]));
1111 }
1112 LOG.info("memstore2 estimated size=" + size);
1113 final int seconds = 30;
1114 LOG.info("Waiting " + seconds + " seconds while heap dump is taken");
1115 for (int i = 0; i < seconds; i++) {
1116
1117 }
1118 LOG.info("Exiting.");
1119 }
1120
1121
1122
1123
1124 @InterfaceAudience.Private
1125 @VisibleForTesting
1126 static class Section {
1127
1128
1129
1130
1131
1132
1133 private final CellSkipListSet cellSet;
1134 private final TimeRangeTracker tracker = new TimeRangeTracker();
1135
1136
1137
1138 private final AtomicLong heapSize;
1139 private final AtomicInteger cellCount;
1140 private final MemStoreLAB allocator;
1141
1142 static Section newSnapshotSection(final KeyValue.KVComparator c) {
1143 return new Section(c, null, 0);
1144 }
1145
1146 static Section newActiveSection(final KeyValue.KVComparator c,
1147 final Configuration conf) {
1148 return new Section(c, conf, DEEP_OVERHEAD);
1149 }
1150
1151 private Section(final KeyValue.KVComparator c,
1152 final Configuration conf, long initHeapSize) {
1153 this.cellSet = new CellSkipListSet(c);
1154 this.heapSize = new AtomicLong(initHeapSize);
1155 this.cellCount = new AtomicInteger(0);
1156 if (conf != null && conf.getBoolean(USEMSLAB_KEY, USEMSLAB_DEFAULT)) {
1157 String className = conf.get(MSLAB_CLASS_NAME, HeapMemStoreLAB.class.getName());
1158 this.allocator = ReflectionUtils.instantiateWithCustomCtor(className,
1159 new Class[]{Configuration.class}, new Object[]{conf});
1160 } else {
1161 this.allocator = null;
1162 }
1163 }
1164
1165 CellSkipListSet getCellSkipListSet() {
1166 return cellSet;
1167 }
1168
1169 TimeRangeTracker getTimeRangeTracker() {
1170 return tracker;
1171 }
1172
1173 AtomicLong getHeapSize() {
1174 return heapSize;
1175 }
1176
1177 AtomicInteger getCellsCount() {
1178 return cellCount;
1179 }
1180
1181 MemStoreLAB getMemStoreLAB() {
1182 return allocator;
1183 }
1184 }
1185 }