Skip to content

Commit

Permalink
HBASE-14202 Reduce garbage we create.
Browse files Browse the repository at this point in the history
  • Loading branch information
anoopsjohn committed Aug 11, 2015
1 parent 7d4de20 commit d49b1f6
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 55 deletions.
Expand Up @@ -44,7 +44,7 @@
import org.apache.hadoop.hbase.util.ByteBufferUtils;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.ClassSize;
import org.apache.hadoop.hbase.util.Pair;
import org.apache.hadoop.hbase.util.ObjectIntPair;
import org.apache.hadoop.io.WritableUtils;

/**
Expand Down Expand Up @@ -127,10 +127,10 @@ protected static class SeekerState {
protected KeyValue.KeyOnlyKeyValue currentKey = new KeyValue.KeyOnlyKeyValue();
// A temp pair object which will be reused by ByteBuff#asSubByteBuffer calls. This avoids too
// many object creations.
private final Pair<ByteBuffer, Integer> tmpPair;
private final ObjectIntPair<ByteBuffer> tmpPair;
private final boolean includeTags;

public SeekerState(Pair<ByteBuffer, Integer> tmpPair, boolean includeTags) {
public SeekerState(ObjectIntPair<ByteBuffer> tmpPair, boolean includeTags) {
this.tmpPair = tmpPair;
this.includeTags = includeTags;
}
Expand Down Expand Up @@ -696,7 +696,7 @@ public int write(OutputStream out, boolean withTags) throws IOException {
protected KeyValue.KeyOnlyKeyValue keyOnlyKV = new KeyValue.KeyOnlyKeyValue();
// A temp pair object which will be reused by ByteBuff#asSubByteBuffer calls. This avoids too
// many object creations.
protected final Pair<ByteBuffer, Integer> tmpPair = new Pair<ByteBuffer, Integer>();
protected final ObjectIntPair<ByteBuffer> tmpPair = new ObjectIntPair<ByteBuffer>();
protected STATE current, previous;

public BufferedEncodedSeeker(CellComparator comparator,
Expand Down
Expand Up @@ -30,7 +30,7 @@
import org.apache.hadoop.hbase.nio.ByteBuff;
import org.apache.hadoop.hbase.util.ByteBufferUtils;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.Pair;
import org.apache.hadoop.hbase.util.ObjectIntPair;

/**
* Compress using:
Expand Down Expand Up @@ -367,7 +367,7 @@ protected static class DiffSeekerState extends SeekerState {
private int rowLengthWithSize;
private long timestamp;

public DiffSeekerState(Pair<ByteBuffer, Integer> tmpPair, boolean includeTags) {
public DiffSeekerState(ObjectIntPair<ByteBuffer> tmpPair, boolean includeTags) {
super(tmpPair, includeTags);
}

Expand Down
Expand Up @@ -30,7 +30,7 @@
import org.apache.hadoop.hbase.nio.ByteBuff;
import org.apache.hadoop.hbase.util.ByteBufferUtils;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.Pair;
import org.apache.hadoop.hbase.util.ObjectIntPair;

/**
* Encoder similar to {@link DiffKeyDeltaEncoder} but supposedly faster.
Expand Down Expand Up @@ -379,7 +379,7 @@ protected static class FastDiffSeekerState extends SeekerState {
private int rowLengthWithSize;
private int familyLengthWithSize;

public FastDiffSeekerState(Pair<ByteBuffer, Integer> tmpPair, boolean includeTags) {
public FastDiffSeekerState(ObjectIntPair<ByteBuffer> tmpPair, boolean includeTags) {
super(tmpPair, includeTags);
}

Expand Down
Expand Up @@ -22,7 +22,7 @@
import org.apache.hadoop.hbase.classification.InterfaceAudience;
import org.apache.hadoop.hbase.util.ByteBufferUtils;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.Pair;
import org.apache.hadoop.hbase.util.ObjectIntPair;
import org.apache.hadoop.io.WritableUtils;

/**
Expand Down Expand Up @@ -119,7 +119,7 @@ public abstract class ByteBuff {
* the caller. Thus it avoids more object creations that would happen if the pair that is
* returned is created by this method every time.
*/
public abstract void asSubByteBuffer(int offset, int length, Pair<ByteBuffer, Integer> pair);
public abstract void asSubByteBuffer(int offset, int length, ObjectIntPair<ByteBuffer> pair);

/**
* Returns the number of elements between the current position and the
Expand Down
Expand Up @@ -25,7 +25,7 @@
import org.apache.hadoop.hbase.classification.InterfaceAudience;
import org.apache.hadoop.hbase.util.ByteBufferUtils;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.Pair;
import org.apache.hadoop.hbase.util.ObjectIntPair;

/**
* Provides a unified view of all the underlying ByteBuffers and will look as if a bigger
Expand Down Expand Up @@ -982,7 +982,7 @@ public ByteBuffer asSubByteBuffer(int length) {
* set the values on the pair that is passed in by the caller
*/
@Override
public void asSubByteBuffer(int offset, int length, Pair<ByteBuffer, Integer> pair) {
public void asSubByteBuffer(int offset, int length, ObjectIntPair<ByteBuffer> pair) {
if (this.itemBeginPos[this.curItemIndex] <= offset) {
int relOffsetInCurItem = offset - this.itemBeginPos[this.curItemIndex];
if (this.curItem.limit() - relOffsetInCurItem >= length) {
Expand Down
Expand Up @@ -21,7 +21,7 @@

import org.apache.hadoop.hbase.classification.InterfaceAudience;
import org.apache.hadoop.hbase.util.ByteBufferUtils;
import org.apache.hadoop.hbase.util.Pair;
import org.apache.hadoop.hbase.util.ObjectIntPair;

/**
* An implementation of ByteBuff where a single BB backs the BBI. This just acts
Expand Down Expand Up @@ -95,7 +95,7 @@ public ByteBuffer asSubByteBuffer(int length) {
}

@Override
public void asSubByteBuffer(int offset, int length, Pair<ByteBuffer, Integer> pair) {
public void asSubByteBuffer(int offset, int length, ObjectIntPair<ByteBuffer> pair) {
// Just return the single BB that is available
pair.setFirst(this.buf);
pair.setSecond(offset);
Expand Down Expand Up @@ -173,7 +173,7 @@ public SingleByteBuff put(int offset, ByteBuff src, int srcOffset, int length) {
} else {
// TODO we can do some optimization here? Call to asSubByteBuffer might
// create a copy.
Pair<ByteBuffer, Integer> pair = new Pair<ByteBuffer, Integer>();
ObjectIntPair<ByteBuffer> pair = new ObjectIntPair<ByteBuffer>();
src.asSubByteBuffer(srcOffset, length, pair);
ByteBufferUtils.copyFromBufferToBuffer(pair.getFirst(), this.buf, pair.getSecond(), offset,
length);
Expand Down
@@ -0,0 +1,76 @@
/**
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hadoop.hbase.util;

import org.apache.hadoop.hbase.classification.InterfaceAudience;

/**
* A generic class for pair of an Object and and a primitive int value.
*/
@InterfaceAudience.Private
public class ObjectIntPair<T> {

private T first;
private int second;

public ObjectIntPair() {
}

public ObjectIntPair(T first, int second) {
this.setFirst(first);
this.setSecond(second);
}

public T getFirst() {
return first;
}

public void setFirst(T first) {
this.first = first;
}

public int getSecond() {
return second;
}

public void setSecond(int second) {
this.second = second;
}

@Override
public boolean equals(Object other) {
return other instanceof ObjectIntPair && equals(first, ((ObjectIntPair<?>) other).first)
&& (this.second == ((ObjectIntPair<?>) other).second);
}

private static boolean equals(Object x, Object y) {
return (x == null && y == null) || (x != null && x.equals(y));
}

@Override
public int hashCode() {
return first == null ? 0 : (first.hashCode() * 17) + 13 * second;
}

@Override
public String toString() {
return "{" + getFirst() + "," + getSecond() + "}";
}
}
Expand Up @@ -32,7 +32,7 @@
import org.apache.hadoop.hbase.testclassification.SmallTests;
import org.apache.hadoop.hbase.util.ByteBufferUtils;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.Pair;
import org.apache.hadoop.hbase.util.ObjectIntPair;
import org.junit.Test;
import org.junit.experimental.categories.Category;

Expand Down Expand Up @@ -243,11 +243,11 @@ public void testSubBuffer() {
assertFalse(bb2 == sub);
assertEquals(l2, ByteBufferUtils.toLong(sub, sub.position()));
multi.rewind();
Pair<ByteBuffer, Integer> p = new Pair<ByteBuffer, Integer>();
ObjectIntPair<ByteBuffer> p = new ObjectIntPair<ByteBuffer>();
multi.asSubByteBuffer(8, Bytes.SIZEOF_LONG, p);
assertFalse(bb1 == p.getFirst());
assertFalse(bb2 == p.getFirst());
assertEquals(0, p.getSecond().intValue());
assertEquals(0, p.getSecond());
assertEquals(l2, ByteBufferUtils.toLong(sub, p.getSecond()));
}

Expand Down
Expand Up @@ -49,7 +49,7 @@
import org.apache.hadoop.hbase.nio.ByteBuff;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.ClassSize;
import org.apache.hadoop.hbase.util.Pair;
import org.apache.hadoop.hbase.util.ObjectIntPair;
import org.apache.hadoop.io.WritableUtils;
import org.apache.hadoop.util.StringUtils;

Expand Down Expand Up @@ -730,7 +730,7 @@ static int binarySearchNonRootIndex(Cell key, ByteBuff nonRootIndex,
// keys[numEntries] = Infinity, then we are maintaining an invariant that
// keys[low - 1] < key < keys[high + 1] while narrowing down the range.
ByteBufferedKeyOnlyKeyValue nonRootIndexkeyOnlyKV = new ByteBufferedKeyOnlyKeyValue();
Pair<ByteBuffer, Integer> pair = new Pair<ByteBuffer, Integer>();
ObjectIntPair<ByteBuffer> pair = new ObjectIntPair<ByteBuffer>();
while (low <= high) {
mid = (low + high) >>> 1;

Expand Down
Expand Up @@ -57,7 +57,7 @@
import org.apache.hadoop.hbase.util.ByteBufferUtils;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.IdLock;
import org.apache.hadoop.hbase.util.Pair;
import org.apache.hadoop.hbase.util.ObjectIntPair;
import org.apache.hadoop.io.WritableUtils;
import org.apache.htrace.Trace;
import org.apache.htrace.TraceScope;
Expand Down Expand Up @@ -470,7 +470,7 @@ protected static class HFileScannerImpl implements HFileScanner {
// buffer backed keyonlyKV
private ByteBufferedKeyOnlyKeyValue bufBackedKeyOnlyKv = new ByteBufferedKeyOnlyKeyValue();
// A pair for reusing in blockSeek() so that we don't garbage lot of objects
final Pair<ByteBuffer, Integer> pair = new Pair<ByteBuffer, Integer>();
final ObjectIntPair<ByteBuffer> pair = new ObjectIntPair<ByteBuffer>();

/**
* The next indexed key is to keep track of the indexed key of the next data block.
Expand Down Expand Up @@ -945,7 +945,7 @@ public Cell getCell() {
public Cell getKey() {
assertSeeked();
// Create a new object so that this getKey is cached as firstKey, lastKey
Pair<ByteBuffer, Integer> keyPair = new Pair<ByteBuffer, Integer>();
ObjectIntPair<ByteBuffer> keyPair = new ObjectIntPair<ByteBuffer>();
blockBuffer.asSubByteBuffer(blockBuffer.position() + KEY_VALUE_LEN_SIZE, currKeyLen, keyPair);
ByteBuffer keyBuf = keyPair.getFirst();
if (keyBuf.hasArray()) {
Expand Down Expand Up @@ -1001,7 +1001,7 @@ public Cell cloneToCell() {
public ByteBuffer getValue() {
assertSeeked();
// Okie to create new Pair. Not used in hot path
Pair<ByteBuffer, Integer> valuePair = new Pair<ByteBuffer, Integer>();
ObjectIntPair<ByteBuffer> valuePair = new ObjectIntPair<ByteBuffer>();
this.blockBuffer.asSubByteBuffer(blockBuffer.position() + KEY_VALUE_LEN_SIZE + currKeyLen,
currValueLen, valuePair);
ByteBuffer valBuf = valuePair.getFirst().duplicate();
Expand Down
Expand Up @@ -69,7 +69,6 @@
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
import org.apache.hadoop.hbase.util.HasThread;
import org.apache.hadoop.hbase.util.IdLock;
import org.apache.hadoop.hbase.util.Pair;
import org.apache.hadoop.util.StringUtils;

import com.google.common.annotations.VisibleForTesting;
Expand Down Expand Up @@ -423,17 +422,14 @@ public Cacheable getBlock(BlockCacheKey key, boolean caching, boolean repeat,
// TODO : change this area - should be removed after server cells and
// 12295 are available
int len = bucketEntry.getLength();
Pair<ByteBuff, MemoryType> pair = ioEngine.read(bucketEntry.offset(), len);
ByteBuff bb = pair.getFirst();
CacheableDeserializer<Cacheable> deserializer =
bucketEntry.deserializerReference(this.deserialiserMap);
Cacheable cachedBlock = deserializer.deserialize(bb, true, pair.getSecond());
Cacheable cachedBlock = ioEngine.read(bucketEntry.offset(), len,
bucketEntry.deserializerReference(this.deserialiserMap));
long timeTaken = System.nanoTime() - start;
if (updateCacheMetrics) {
cacheStats.hit(caching);
cacheStats.ioHit(timeTaken);
}
if (pair.getSecond() == MemoryType.SHARED) {
if (cachedBlock.getMemoryType() == MemoryType.SHARED) {
bucketEntry.refCount.incrementAndGet();
}
bucketEntry.access(accessCount.incrementAndGet());
Expand Down
Expand Up @@ -22,10 +22,11 @@
import java.nio.ByteBuffer;

import org.apache.hadoop.hbase.classification.InterfaceAudience;
import org.apache.hadoop.hbase.io.hfile.Cacheable;
import org.apache.hadoop.hbase.io.hfile.CacheableDeserializer;
import org.apache.hadoop.hbase.io.hfile.Cacheable.MemoryType;
import org.apache.hadoop.hbase.nio.ByteBuff;
import org.apache.hadoop.hbase.util.ByteBufferArray;
import org.apache.hadoop.hbase.util.Pair;

/**
* IO engine that stores data in memory using an array of ByteBuffers
Expand Down Expand Up @@ -67,15 +68,16 @@ public boolean isPersistent() {
}

@Override
public Pair<ByteBuff, MemoryType> read(long offset, int length) throws IOException {
public Cacheable read(long offset, int length, CacheableDeserializer<Cacheable> deserializer)
throws IOException {
ByteBuff dstBuffer = bufferArray.asSubByteBuff(offset, length);
// Here the buffer that is created directly refers to the buffer in the actual buckets.
// When any cell is referring to the blocks created out of these buckets then it means that
// those cells are referring to a shared memory area which if evicted by the BucketCache would
// lead to corruption of results. Hence we set the type of the buffer as SHARED_MEMORY
// so that the readers using this block are aware of this fact and do the necessary action
// to prevent eviction till the results are either consumed or copied
return new Pair<ByteBuff, MemoryType>(dstBuffer, MemoryType.SHARED);
return deserializer.deserialize(dstBuffer, true, MemoryType.SHARED);
}

/**
Expand Down
Expand Up @@ -26,10 +26,11 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.hbase.classification.InterfaceAudience;
import org.apache.hadoop.hbase.io.hfile.Cacheable;
import org.apache.hadoop.hbase.io.hfile.CacheableDeserializer;
import org.apache.hadoop.hbase.io.hfile.Cacheable.MemoryType;
import org.apache.hadoop.hbase.nio.ByteBuff;
import org.apache.hadoop.hbase.nio.SingleByteBuff;
import org.apache.hadoop.hbase.util.Pair;
import org.apache.hadoop.util.StringUtils;

/**
Expand Down Expand Up @@ -90,7 +91,8 @@ public boolean isPersistent() {
* @throws IOException
*/
@Override
public Pair<ByteBuff, MemoryType> read(long offset, int length) throws IOException {
public Cacheable read(long offset, int length, CacheableDeserializer<Cacheable> deserializer)
throws IOException {
ByteBuffer dstBuffer = ByteBuffer.allocate(length);
fileChannel.read(dstBuffer, offset);
// The buffer created out of the fileChannel is formed by copying the data from the file
Expand All @@ -101,7 +103,7 @@ public Pair<ByteBuff, MemoryType> read(long offset, int length) throws IOExcepti
throw new RuntimeException("Only " + dstBuffer.limit() + " bytes read, " + length
+ " expected");
}
return new Pair<ByteBuff, MemoryType>(new SingleByteBuff(dstBuffer), MemoryType.EXCLUSIVE);
return deserializer.deserialize(new SingleByteBuff(dstBuffer), true, MemoryType.EXCLUSIVE);
}

/**
Expand Down

0 comments on commit d49b1f6

Please sign in to comment.