HBASE-17169 Remove Cell variants with ShareableMemory.

This commit is contained in:
anoopsamjohn 2016-11-26 12:20:06 +05:30
parent 219d8a1b71
commit 3f7f1c1353
9 changed files with 60 additions and 82 deletions

View File

@ -426,9 +426,6 @@ public final class CellUtil {
* @return A new cell which is having the extra tags also added to it. * @return A new cell which is having the extra tags also added to it.
*/ */
public static Cell createCell(Cell cell, byte[] tags) { public static Cell createCell(Cell cell, byte[] tags) {
if (cell instanceof ShareableMemory) {
return new ShareableMemoryTagRewriteCell(cell, tags);
}
return new TagRewriteCell(cell, tags); return new TagRewriteCell(cell, tags);
} }
@ -618,22 +615,10 @@ public final class CellUtil {
public long heapOverhead() { public long heapOverhead() {
return ((ExtendedCell) this.cell).heapOverhead() + HEAP_SIZE_OVERHEAD; return ((ExtendedCell) this.cell).heapOverhead() + HEAP_SIZE_OVERHEAD;
} }
}
/**
* Version of TagRewriteCell where the original Cell is ShareableMemory type.
*/
private static class ShareableMemoryTagRewriteCell extends TagRewriteCell implements
ShareableMemory {
public ShareableMemoryTagRewriteCell(Cell cell, byte[] tags) {
super(cell, tags);
assert cell instanceof ShareableMemory;
}
@Override @Override
public Cell cloneToCell() { public Cell deepClone() {
Cell clonedBaseCell = ((ShareableMemory) this.cell).cloneToCell(); Cell clonedBaseCell = ((ExtendedCell) this.cell).deepClone();
return new TagRewriteCell(clonedBaseCell, this.tags); return new TagRewriteCell(clonedBaseCell, this.tags);
} }
} }

View File

@ -72,4 +72,10 @@ public interface ExtendedCell extends Cell, SettableSequenceId, SettableTimestam
* @return The heap size overhead associated with this Cell. * @return The heap size overhead associated with this Cell.
*/ */
long heapOverhead(); long heapOverhead();
/**
* Does a deep copy of the contents to a new memory area and returns it as a new cell.
* @return The deep cloned cell
*/
Cell deepClone();
} }

View File

@ -2815,4 +2815,12 @@ public class KeyValue implements ExtendedCell {
public long heapOverhead() { public long heapOverhead() {
return FIXED_OVERHEAD; return FIXED_OVERHEAD;
} }
@Override
public Cell deepClone() {
byte[] copy = Bytes.copy(this.bytes, this.offset, this.length);
KeyValue kv = new KeyValue(copy, 0, copy.length);
kv.setSequenceId(this.getSequenceId());
return kv;
}
} }

View File

@ -23,6 +23,7 @@ import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import org.apache.hadoop.hbase.classification.InterfaceAudience; import org.apache.hadoop.hbase.classification.InterfaceAudience;
import org.apache.hadoop.hbase.util.Bytes;
/** /**
* An extension of the KeyValue where the tags length is always 0 * An extension of the KeyValue where the tags length is always 0
@ -48,4 +49,12 @@ public class NoTagsKeyValue extends KeyValue {
public int getSerializedSize(boolean withTags) { public int getSerializedSize(boolean withTags) {
return this.length; return this.length;
} }
@Override
public Cell deepClone() {
byte[] copy = Bytes.copy(this.bytes, this.offset, this.length);
KeyValue kv = new NoTagsKeyValue(copy, 0, copy.length);
kv.setSequenceId(this.getSequenceId());
return kv;
}
} }

View File

@ -296,4 +296,18 @@ public class OffheapKeyValue extends ByteBufferCell implements ExtendedCell {
public long heapOverhead() { public long heapOverhead() {
return FIXED_OVERHEAD; return FIXED_OVERHEAD;
} }
@Override
public Cell deepClone() {
byte[] copy = new byte[this.length];
ByteBufferUtils.copyFromBufferToArray(copy, this.buf, this.offset, 0, this.length);
KeyValue kv;
if (this.hasTags) {
kv = new KeyValue(copy, 0, copy.length);
} else {
kv = new NoTagsKeyValue(copy, 0, copy.length);
}
kv.setSequenceId(this.getSequenceId());
return kv;
}
} }

View File

@ -24,15 +24,12 @@ import java.nio.ByteBuffer;
import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseInterfaceAudience; import org.apache.hadoop.hbase.HBaseInterfaceAudience;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.KeyValueUtil; import org.apache.hadoop.hbase.KeyValueUtil;
import org.apache.hadoop.hbase.NoTagsKeyValue; import org.apache.hadoop.hbase.NoTagsKeyValue;
import org.apache.hadoop.hbase.OffheapKeyValue; import org.apache.hadoop.hbase.OffheapKeyValue;
import org.apache.hadoop.hbase.ShareableMemory;
import org.apache.hadoop.hbase.classification.InterfaceAudience; import org.apache.hadoop.hbase.classification.InterfaceAudience;
import org.apache.hadoop.hbase.nio.ByteBuff; import org.apache.hadoop.hbase.nio.ByteBuff;
import org.apache.hadoop.hbase.util.ByteBufferUtils; import org.apache.hadoop.hbase.util.ByteBufferUtils;
import org.apache.hadoop.hbase.util.Bytes;
/** /**
* Codec that does KeyValue version 1 serialization. * Codec that does KeyValue version 1 serialization.
@ -109,66 +106,14 @@ public class KeyValueCodec implements Codec {
} }
protected Cell createCell(byte[] buf, int offset, int len) { protected Cell createCell(byte[] buf, int offset, int len) {
return new ShareableMemoryNoTagsKeyValue(buf, offset, len); return new NoTagsKeyValue(buf, offset, len);
} }
protected Cell createCell(ByteBuffer bb, int pos, int len) { protected Cell createCell(ByteBuffer bb, int pos, int len) {
// We know there is not going to be any tags. // We know there is not going to be any tags.
return new ShareableMemoryOffheapKeyValue(bb, pos, len, false, 0); return new OffheapKeyValue(bb, pos, len, false, 0);
} }
static class ShareableMemoryKeyValue extends KeyValue implements ShareableMemory {
public ShareableMemoryKeyValue(byte[] bytes, int offset, int length) {
super(bytes, offset, length);
}
@Override
public Cell cloneToCell() {
byte[] copy = Bytes.copy(this.bytes, this.offset, this.length);
KeyValue kv = new KeyValue(copy, 0, copy.length);
kv.setSequenceId(this.getSequenceId());
return kv;
}
}
static class ShareableMemoryNoTagsKeyValue extends NoTagsKeyValue implements ShareableMemory {
public ShareableMemoryNoTagsKeyValue(byte[] bytes, int offset, int length) {
super(bytes, offset, length);
}
@Override
public Cell cloneToCell() {
byte[] copy = Bytes.copy(this.bytes, this.offset, this.length);
KeyValue kv = new NoTagsKeyValue(copy, 0, copy.length);
kv.setSequenceId(this.getSequenceId());
return kv;
}
}
static class ShareableMemoryOffheapKeyValue extends OffheapKeyValue implements ShareableMemory {
public ShareableMemoryOffheapKeyValue(ByteBuffer buf, int offset, int length) {
super(buf, offset, length);
}
public ShareableMemoryOffheapKeyValue(ByteBuffer buf, int offset, int length, boolean hasTags,
long seqId) {
super(buf, offset, length, hasTags, seqId);
}
@Override
public Cell cloneToCell() {
byte[] copy = new byte[this.length];
ByteBufferUtils.copyFromBufferToArray(copy, this.buf, this.offset, 0, this.length);
KeyValue kv;
if (this.hasTags) {
kv = new KeyValue(copy, 0, copy.length);
} else {
kv = new NoTagsKeyValue(copy, 0, copy.length);
}
kv.setSequenceId(this.getSequenceId());
return kv;
}
}
} }
/** /**

View File

@ -24,7 +24,9 @@ import java.nio.ByteBuffer;
import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseInterfaceAudience; import org.apache.hadoop.hbase.HBaseInterfaceAudience;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.KeyValueUtil; import org.apache.hadoop.hbase.KeyValueUtil;
import org.apache.hadoop.hbase.OffheapKeyValue;
import org.apache.hadoop.hbase.classification.InterfaceAudience; import org.apache.hadoop.hbase.classification.InterfaceAudience;
import org.apache.hadoop.hbase.nio.ByteBuff; import org.apache.hadoop.hbase.nio.ByteBuff;
import org.apache.hadoop.hbase.util.ByteBufferUtils; import org.apache.hadoop.hbase.util.ByteBufferUtils;
@ -87,12 +89,12 @@ public class KeyValueCodecWithTags implements Codec {
@Override @Override
protected Cell createCell(byte[] buf, int offset, int len) { protected Cell createCell(byte[] buf, int offset, int len) {
return new ShareableMemoryKeyValue(buf, offset, len); return new KeyValue(buf, offset, len);
} }
@Override @Override
protected Cell createCell(ByteBuffer bb, int pos, int len) { protected Cell createCell(ByteBuffer bb, int pos, int len) {
return new ShareableMemoryOffheapKeyValue(bb, pos, len); return new OffheapKeyValue(bb, pos, len);
} }
} }

View File

@ -473,6 +473,12 @@ abstract class BufferedDataBlockEncoder extends AbstractDataBlockEncoder {
public long heapOverhead() { public long heapOverhead() {
return FIXED_OVERHEAD; return FIXED_OVERHEAD;
} }
@Override
public Cell deepClone() {
// This is not used in actual flow. Throwing UnsupportedOperationException
throw new UnsupportedOperationException();
}
} }
protected static class OffheapDecodedCell extends ByteBufferCell implements ExtendedCell { protected static class OffheapDecodedCell extends ByteBufferCell implements ExtendedCell {
@ -717,6 +723,12 @@ abstract class BufferedDataBlockEncoder extends AbstractDataBlockEncoder {
public long heapOverhead() { public long heapOverhead() {
return FIXED_OVERHEAD; return FIXED_OVERHEAD;
} }
@Override
public Cell deepClone() {
// This is not used in actual flow. Throwing UnsupportedOperationException
throw new UnsupportedOperationException();
}
} }
protected abstract static class BufferedEncodedSeeker<STATE extends SeekerState> protected abstract static class BufferedEncodedSeeker<STATE extends SeekerState>

View File

@ -28,7 +28,7 @@ import org.apache.commons.logging.Log;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellComparator; import org.apache.hadoop.hbase.CellComparator;
import org.apache.hadoop.hbase.ShareableMemory; import org.apache.hadoop.hbase.ExtendedCell;
import org.apache.hadoop.hbase.classification.InterfaceAudience; import org.apache.hadoop.hbase.classification.InterfaceAudience;
import org.apache.hadoop.hbase.exceptions.UnexpectedStateException; import org.apache.hadoop.hbase.exceptions.UnexpectedStateException;
import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Bytes;
@ -107,11 +107,8 @@ public abstract class AbstractMemStore implements MemStore {
} }
private static Cell deepCopyIfNeeded(Cell cell) { private static Cell deepCopyIfNeeded(Cell cell) {
// When Cell is backed by a shared memory chunk (this can be a chunk of memory where we read the if (cell instanceof ExtendedCell) {
// req into) the Cell instance will be of type ShareableMemory. Later we will add feature to return ((ExtendedCell) cell).deepClone();
// read the RPC request into pooled direct ByteBuffers.
if (cell instanceof ShareableMemory) {
return ((ShareableMemory) cell).cloneToCell();
} }
return cell; return cell;
} }