HBASE-20928 Rewrite calculation of midpoint in binarySearch functions to prevent overflow

HBASE-20928 Rewrite calculation of midpoint - addendum (Xu Cang)

Signed-off-by: tedyu <yuzhihong@gmail.com>

Conflicts:
	hbase-common/src/main/java/org/apache/hadoop/hbase/io/encoding/RowIndexSeekerV1.java
	hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java

Amending-Author: Andrew Purtell <apurtell@apache.org>
This commit is contained in:
Saurabh Singh 2019-01-11 12:14:18 -08:00 committed by Andrew Purtell
parent 2f541f5710
commit 231e6d56b5
No known key found for this signature in database
GPG Key ID: 8597754DD5365CCD
4 changed files with 14 additions and 10 deletions

View File

@ -154,15 +154,13 @@ public class RowIndexSeekerV1 extends AbstractEncodedSeeker {
private int binarySearch(Cell seekCell, boolean seekBefore) { private int binarySearch(Cell seekCell, boolean seekBefore) {
int low = 0; int low = 0;
int high = rowNumber - 1; int high = rowNumber - 1;
int mid = (low + high) >>> 1; int mid = low + ((high - low) >> 1);
int comp = 0; int comp = 0;
SimpleMutableByteRange row = new SimpleMutableByteRange(); SimpleMutableByteRange row = new SimpleMutableByteRange();
while (low <= high) { while (low <= high) {
mid = (low + high) >>> 1; mid = low + ((high - low) >> 1);
getRow(mid, row); getRow(mid, row);
comp = comparator.compareRows(row.getBytes(), row.getOffset(), comp = compareRows(row, seekCell);
row.getLength(), seekCell.getRowArray(), seekCell.getRowOffset(),
seekCell.getRowLength());
if (comp < 0) { if (comp < 0) {
low = mid + 1; low = mid + 1;
} else if (comp > 0) { } else if (comp > 0) {
@ -184,6 +182,12 @@ public class RowIndexSeekerV1 extends AbstractEncodedSeeker {
} }
} }
private int compareRows(SimpleMutableByteRange row, Cell seekCell) {
return comparator.compareRows(row.getBytes(), row.getOffset(),
row.getLength(), seekCell.getRowArray(), seekCell.getRowOffset(),
seekCell.getRowLength());
}
private void getRow(int index, SimpleMutableByteRange row) { private void getRow(int index, SimpleMutableByteRange row) {
int offset = Bytes.toIntUnsafe(rowOffsets.array(), rowOffsets.arrayOffset() int offset = Bytes.toIntUnsafe(rowOffsets.array(), rowOffsets.arrayOffset()
+ (index << 2)); // index * Bytes.SIZEOF_INT + (index << 2)); // index * Bytes.SIZEOF_INT

View File

@ -2145,7 +2145,7 @@ public class Bytes implements Comparable<Bytes> {
int high = arr.length - 1; int high = arr.length - 1;
while (low <= high) { while (low <= high) {
int mid = (low+high) >>> 1; int mid = low + ((high - low) >> 1);
// we have to compare in this order, because the comparator order // we have to compare in this order, because the comparator order
// has special logic when the 'left side' is a special key. // has special logic when the 'left side' is a special key.
int cmp = comparator.compare(key, offset, length, int cmp = comparator.compare(key, offset, length,
@ -2182,7 +2182,7 @@ public class Bytes implements Comparable<Bytes> {
int high = arr.length - 1; int high = arr.length - 1;
KeyValue.KeyOnlyKeyValue r = new KeyValue.KeyOnlyKeyValue(); KeyValue.KeyOnlyKeyValue r = new KeyValue.KeyOnlyKeyValue();
while (low <= high) { while (low <= high) {
int mid = (low+high) >>> 1; int mid = low + ((high - low) >> 1);
// we have to compare in this order, because the comparator order // we have to compare in this order, because the comparator order
// has special logic when the 'left side' is a special key. // has special logic when the 'left side' is a special key.
r.setKey(arr[mid], 0, arr[mid].length); r.setKey(arr[mid], 0, arr[mid].length);
@ -2357,7 +2357,7 @@ public class Bytes implements Comparable<Bytes> {
int high = toIndex - 1; int high = toIndex - 1;
while (low <= high) { while (low <= high) {
int mid = (low + high) >>> 1; int mid = low + ((high - low) >> 1);
int midVal = a[mid] & 0xff; int midVal = a[mid] & 0xff;
if (midVal < unsignedKey) { if (midVal < unsignedKey) {

View File

@ -533,7 +533,7 @@ public class HFileBlockIndex {
// keys[low - 1] < key < keys[high + 1] while narrowing down the range. // keys[low - 1] < key < keys[high + 1] while narrowing down the range.
KeyValue.KeyOnlyKeyValue nonRootIndexKV = new KeyValue.KeyOnlyKeyValue(); KeyValue.KeyOnlyKeyValue nonRootIndexKV = new KeyValue.KeyOnlyKeyValue();
while (low <= high) { while (low <= high) {
mid = (low + high) >>> 1; mid = low + ((high - low) >> 1);
// Midkey's offset relative to the end of secondary index // Midkey's offset relative to the end of secondary index
int midKeyRelOffset = nonRootIndex.getInt( int midKeyRelOffset = nonRootIndex.getInt(

View File

@ -119,7 +119,7 @@ public class BoundedPriorityBlockingQueue<E> extends AbstractQueue<E> implements
private int upperBound(int start, int end, E key) { private int upperBound(int start, int end, E key) {
while (start < end) { while (start < end) {
int mid = (start + end) >>> 1; int mid = start + ((end - start) >> 1);
E mitem = objects[mid]; E mitem = objects[mid];
int cmp = comparator.compare(mitem, key); int cmp = comparator.compare(mitem, key);
if (cmp > 0) { if (cmp > 0) {