HBASE-13634 Avoid unsafe reference equality checks to EMPTY byte[]

Add comment

Signed-off-by: stack <stack@apache.org>
This commit is contained in:
Lars Francke 2015-05-06 18:13:46 +02:00 committed by stack
parent 671ae8f150
commit fa6dc9c44e
4 changed files with 38 additions and 19 deletions

View File

@ -20,6 +20,7 @@
package org.apache.hadoop.hbase.regionserver; package org.apache.hadoop.hbase.regionserver;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import java.util.NavigableSet; import java.util.NavigableSet;
import org.apache.hadoop.hbase.KeyValue.Type; import org.apache.hadoop.hbase.KeyValue.Type;
@ -474,7 +475,7 @@ public class ScanQueryMatcher {
// dropDeletesFromRow is leq current kv, we start dropping deletes and reset // dropDeletesFromRow is leq current kv, we start dropping deletes and reset
// dropDeletesFromRow; thus the 2nd "if" starts to apply. // dropDeletesFromRow; thus the 2nd "if" starts to apply.
if ((dropDeletesFromRow != null) if ((dropDeletesFromRow != null)
&& ((dropDeletesFromRow == HConstants.EMPTY_START_ROW) && (Arrays.equals(dropDeletesFromRow, HConstants.EMPTY_START_ROW)
|| (Bytes.compareTo(row, offset, length, || (Bytes.compareTo(row, offset, length,
dropDeletesFromRow, 0, dropDeletesFromRow.length) >= 0))) { dropDeletesFromRow, 0, dropDeletesFromRow.length) >= 0))) {
retainDeletesInOutput = false; retainDeletesInOutput = false;
@ -484,7 +485,7 @@ public class ScanQueryMatcher {
// drop-deletes range. When dropDeletesToRow is leq current kv, we stop dropping deletes, // drop-deletes range. When dropDeletesToRow is leq current kv, we stop dropping deletes,
// and reset dropDeletesToRow so that we don't do any more compares. // and reset dropDeletesToRow so that we don't do any more compares.
if ((dropDeletesFromRow == null) if ((dropDeletesFromRow == null)
&& (dropDeletesToRow != null) && (dropDeletesToRow != HConstants.EMPTY_END_ROW) && (dropDeletesToRow != null) && !Arrays.equals(dropDeletesToRow, HConstants.EMPTY_END_ROW)
&& (Bytes.compareTo(row, offset, length, && (Bytes.compareTo(row, offset, length,
dropDeletesToRow, 0, dropDeletesToRow.length) >= 0)) { dropDeletesToRow, 0, dropDeletesToRow.length) >= 0)) {
retainDeletesInOutput = true; retainDeletesInOutput = true;

View File

@ -19,6 +19,7 @@ package org.apache.hadoop.hbase.regionserver;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -116,7 +117,7 @@ public abstract class StripeMultiFileWriter implements Compactor.CellSink {
*/ */
protected void sanityCheckLeft( protected void sanityCheckLeft(
byte[] left, Cell cell) throws IOException { byte[] left, Cell cell) throws IOException {
if (StripeStoreFileManager.OPEN_KEY != left && if (!Arrays.equals(StripeStoreFileManager.OPEN_KEY, left) &&
comparator.compareRows(cell, left, 0, left.length) < 0) { comparator.compareRows(cell, left, 0, left.length) < 0) {
String error = "The first row is lower than the left boundary of [" + Bytes.toString(left) String error = "The first row is lower than the left boundary of [" + Bytes.toString(left)
+ "]: [" + Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()) + "]: [" + Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength())
@ -132,7 +133,7 @@ public abstract class StripeMultiFileWriter implements Compactor.CellSink {
*/ */
protected void sanityCheckRight( protected void sanityCheckRight(
byte[] right, Cell cell) throws IOException { byte[] right, Cell cell) throws IOException {
if (StripeStoreFileManager.OPEN_KEY != right && if (!Arrays.equals(StripeStoreFileManager.OPEN_KEY, right) &&
comparator.compareRows(cell, right, 0, right.length) >= 0) { comparator.compareRows(cell, right, 0, right.length) >= 0) {
String error = "The last row is higher or equal than the right boundary of [" String error = "The last row is higher or equal than the right boundary of ["
+ Bytes.toString(right) + "]: [" + Bytes.toString(right) + "]: ["
@ -178,10 +179,14 @@ public abstract class StripeMultiFileWriter implements Compactor.CellSink {
// must match some target boundaries, let's find them. // must match some target boundaries, let's find them.
assert (majorRangeFrom == null) == (majorRangeTo == null); assert (majorRangeFrom == null) == (majorRangeTo == null);
if (majorRangeFrom != null) { if (majorRangeFrom != null) {
majorRangeFromIndex = (majorRangeFrom == StripeStoreFileManager.OPEN_KEY) ? 0 majorRangeFromIndex = Arrays.equals(majorRangeFrom, StripeStoreFileManager.OPEN_KEY)
: Collections.binarySearch(this.boundaries, majorRangeFrom, Bytes.BYTES_COMPARATOR); ? 0
majorRangeToIndex = (majorRangeTo == StripeStoreFileManager.OPEN_KEY) ? boundaries.size() : Collections.binarySearch(boundaries, majorRangeFrom,
: Collections.binarySearch(this.boundaries, majorRangeTo, Bytes.BYTES_COMPARATOR); Bytes.BYTES_COMPARATOR);
majorRangeToIndex = Arrays.equals(majorRangeTo, StripeStoreFileManager.OPEN_KEY)
? boundaries.size()
: Collections.binarySearch(boundaries, majorRangeTo,
Bytes.BYTES_COMPARATOR);
if (this.majorRangeFromIndex < 0 || this.majorRangeToIndex < 0) { if (this.majorRangeFromIndex < 0 || this.majorRangeToIndex < 0) {
throw new IOException("Major range does not match writer boundaries: [" + throw new IOException("Major range does not match writer boundaries: [" +
Bytes.toString(majorRangeFrom) + "] [" + Bytes.toString(majorRangeTo) + "]; from " Bytes.toString(majorRangeFrom) + "] [" + Bytes.toString(majorRangeTo) + "]; from "
@ -204,9 +209,8 @@ public abstract class StripeMultiFileWriter implements Compactor.CellSink {
} }
private boolean isCellAfterCurrentWriter(Cell cell) { private boolean isCellAfterCurrentWriter(Cell cell) {
return ((currentWriterEndKey != StripeStoreFileManager.OPEN_KEY) && return !Arrays.equals(currentWriterEndKey, StripeStoreFileManager.OPEN_KEY) &&
(comparator.compareRows(cell, (comparator.compareRows(cell, currentWriterEndKey, 0, currentWriterEndKey.length) >= 0);
currentWriterEndKey, 0, currentWriterEndKey.length) >= 0));
} }
@Override @Override

View File

@ -505,6 +505,7 @@ public class StripeStoreFileManager
* Checks whether the key is invalid (e.g. from an L0 file, or non-stripe-compacted files). * Checks whether the key is invalid (e.g. from an L0 file, or non-stripe-compacted files).
*/ */
private static final boolean isInvalid(byte[] key) { private static final boolean isInvalid(byte[] key) {
// No need to use Arrays.equals because INVALID_KEY is null
return key == INVALID_KEY; return key == INVALID_KEY;
} }
@ -536,8 +537,10 @@ public class StripeStoreFileManager
* Finds the stripe index for the stripe containing a row provided externally for get/scan. * Finds the stripe index for the stripe containing a row provided externally for get/scan.
*/ */
private final int findStripeForRow(byte[] row, boolean isStart) { private final int findStripeForRow(byte[] row, boolean isStart) {
if (isStart && row == HConstants.EMPTY_START_ROW) return 0; if (isStart && Arrays.equals(row, HConstants.EMPTY_START_ROW)) return 0;
if (!isStart && row == HConstants.EMPTY_END_ROW) return state.stripeFiles.size() - 1; if (!isStart && Arrays.equals(row, HConstants.EMPTY_END_ROW)) {
return state.stripeFiles.size() - 1;
}
// If there's an exact match below, a stripe ends at "row". Stripe right boundary is // If there's an exact match below, a stripe ends at "row". Stripe right boundary is
// exclusive, so that means the row is in the next stripe; thus, we need to add one to index. // exclusive, so that means the row is in the next stripe; thus, we need to add one to index.
// If there's no match, the return value of binarySearch is (-(insertion point) - 1), where // If there's no match, the return value of binarySearch is (-(insertion point) - 1), where
@ -559,15 +562,25 @@ public class StripeStoreFileManager
private byte[] startOf(StoreFile sf) { private byte[] startOf(StoreFile sf) {
byte[] result = this.fileStarts.get(sf); byte[] result = fileStarts.get(sf);
return result == null ? sf.getMetadataValue(STRIPE_START_KEY)
: (result == INVALID_KEY_IN_MAP ? INVALID_KEY : result); // result and INVALID_KEY_IN_MAP are compared _only_ by reference on purpose here as the latter
// serves only as a marker and is not to be confused with other empty byte arrays.
// See Javadoc of INVALID_KEY_IN_MAP for more information
return (result == null)
? sf.getMetadataValue(STRIPE_START_KEY)
: result == INVALID_KEY_IN_MAP ? INVALID_KEY : result;
} }
private byte[] endOf(StoreFile sf) { private byte[] endOf(StoreFile sf) {
byte[] result = this.fileEnds.get(sf); byte[] result = fileEnds.get(sf);
return result == null ? sf.getMetadataValue(STRIPE_END_KEY)
: (result == INVALID_KEY_IN_MAP ? INVALID_KEY : result); // result and INVALID_KEY_IN_MAP are compared _only_ by reference on purpose here as the latter
// serves only as a marker and is not to be confused with other empty byte arrays.
// See Javadoc of INVALID_KEY_IN_MAP for more information
return (result == null)
? sf.getMetadataValue(STRIPE_END_KEY)
: result == INVALID_KEY_IN_MAP ? INVALID_KEY : result;
} }
/** /**

View File

@ -115,6 +115,7 @@ public class RegionSplitCalculator<R extends KeyRange> {
byte[] start = range.getStartKey(); byte[] start = range.getStartKey();
byte[] end = specialEndKey(range); byte[] end = specialEndKey(range);
// No need to use Arrays.equals because ENDKEY is null
if (end != ENDKEY && Bytes.compareTo(start, end) > 0) { if (end != ENDKEY && Bytes.compareTo(start, end) > 0) {
// don't allow backwards edges // don't allow backwards edges
LOG.debug("attempted to add backwards edge: " + Bytes.toString(start) LOG.debug("attempted to add backwards edge: " + Bytes.toString(start)