1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.ipc;
19
20 import java.io.DataInput;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.nio.BufferOverflowException;
25 import java.nio.ByteBuffer;
26
27 import org.apache.commons.io.IOUtils;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.hadoop.hbase.classification.InterfaceAudience;
31 import org.apache.hadoop.conf.Configurable;
32 import org.apache.hadoop.conf.Configuration;
33 import org.apache.hadoop.hbase.CellScanner;
34 import org.apache.hadoop.hbase.DoNotRetryIOException;
35 import org.apache.hadoop.hbase.HBaseIOException;
36 import org.apache.hadoop.hbase.codec.Codec;
37 import org.apache.hadoop.hbase.io.BoundedByteBufferPool;
38 import org.apache.hadoop.hbase.io.ByteBufferInputStream;
39 import org.apache.hadoop.hbase.io.ByteBufferOutputStream;
40 import org.apache.hadoop.hbase.io.HeapSize;
41 import org.apache.hadoop.hbase.util.Bytes;
42 import org.apache.hadoop.hbase.util.ClassSize;
43 import org.apache.hadoop.io.compress.CodecPool;
44 import org.apache.hadoop.io.compress.CompressionCodec;
45 import org.apache.hadoop.io.compress.CompressionInputStream;
46 import org.apache.hadoop.io.compress.Compressor;
47 import org.apache.hadoop.io.compress.Decompressor;
48
49 import com.google.common.base.Preconditions;
50 import com.google.protobuf.CodedOutputStream;
51 import com.google.protobuf.Message;
52
53
54
55
56 @InterfaceAudience.Private
57 public class IPCUtil {
58
59 public static final Log LOG = LogFactory.getLog(IPCUtil.class);
60
61
62
63 private final int cellBlockDecompressionMultiplier;
64 private final int cellBlockBuildingInitialBufferSize;
65 private final Configuration conf;
66
67 public IPCUtil(final Configuration conf) {
68 super();
69 this.conf = conf;
70 this.cellBlockDecompressionMultiplier =
71 conf.getInt("hbase.ipc.cellblock.decompression.buffersize.multiplier", 3);
72
73
74
75 this.cellBlockBuildingInitialBufferSize =
76 ClassSize.align(conf.getInt("hbase.ipc.cellblock.building.initial.buffersize", 16 * 1024));
77 }
78
79
80
81
82 public static class CellScannerButNoCodecException extends HBaseIOException {};
83
84
85
86
87
88
89
90
91
92
93
94
95 @SuppressWarnings("resource")
96 public ByteBuffer buildCellBlock(final Codec codec, final CompressionCodec compressor,
97 final CellScanner cellScanner)
98 throws IOException {
99 return buildCellBlock(codec, compressor, cellScanner, null);
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117 @SuppressWarnings("resource")
118 public ByteBuffer buildCellBlock(final Codec codec, final CompressionCodec compressor,
119 final CellScanner cellScanner, final BoundedByteBufferPool pool)
120 throws IOException {
121 if (cellScanner == null) return null;
122 if (codec == null) throw new CellScannerButNoCodecException();
123 int bufferSize = this.cellBlockBuildingInitialBufferSize;
124 ByteBufferOutputStream baos = null;
125 ByteBuffer bb = null;
126 if (pool != null) {
127 bb = pool.getBuffer();
128 bufferSize = bb.capacity();
129 baos = new ByteBufferOutputStream(bb);
130 } else {
131
132 if (cellScanner instanceof HeapSize) {
133 long longSize = ((HeapSize)cellScanner).heapSize();
134
135 if (longSize > Integer.MAX_VALUE) {
136 throw new IOException("Size " + longSize + " > " + Integer.MAX_VALUE);
137 }
138 bufferSize = ClassSize.align((int)longSize);
139 }
140 baos = new ByteBufferOutputStream(bufferSize);
141 }
142 OutputStream os = baos;
143 Compressor poolCompressor = null;
144 try {
145 if (compressor != null) {
146 if (compressor instanceof Configurable) ((Configurable)compressor).setConf(this.conf);
147 poolCompressor = CodecPool.getCompressor(compressor);
148 os = compressor.createOutputStream(os, poolCompressor);
149 }
150 Codec.Encoder encoder = codec.getEncoder(os);
151 int count = 0;
152 while (cellScanner.advance()) {
153 encoder.write(cellScanner.current());
154 count++;
155 }
156 encoder.flush();
157
158
159 if (count == 0) {
160 if (pool != null && bb != null) {
161 pool.putBuffer(bb);
162 }
163 return null;
164 }
165 } catch (BufferOverflowException e) {
166 throw new DoNotRetryIOException(e);
167 } finally {
168 os.close();
169 if (poolCompressor != null) CodecPool.returnCompressor(poolCompressor);
170 }
171 if (LOG.isTraceEnabled()) {
172 if (bufferSize < baos.size()) {
173 LOG.trace("Buffer grew from initial bufferSize=" + bufferSize + " to " + baos.size() +
174 "; up hbase.ipc.cellblock.building.initial.buffersize?");
175 }
176 }
177 return baos.getByteBuffer();
178 }
179
180
181
182
183
184
185
186 public CellScanner createCellScanner(final Codec codec, final CompressionCodec compressor,
187 final byte [] cellBlock)
188 throws IOException {
189 return createCellScanner(codec, compressor, ByteBuffer.wrap(cellBlock));
190 }
191
192
193
194
195
196
197
198
199 public CellScanner createCellScanner(final Codec codec, final CompressionCodec compressor,
200 final ByteBuffer cellBlock)
201 throws IOException {
202
203
204 InputStream is = null;
205 if (compressor != null) {
206
207 if (compressor instanceof Configurable) ((Configurable)compressor).setConf(this.conf);
208 Decompressor poolDecompressor = CodecPool.getDecompressor(compressor);
209 CompressionInputStream cis =
210 compressor.createInputStream(new ByteBufferInputStream(cellBlock), poolDecompressor);
211 ByteBufferOutputStream bbos = null;
212 try {
213
214
215 bbos = new ByteBufferOutputStream(cellBlock.remaining() *
216 this.cellBlockDecompressionMultiplier);
217 IOUtils.copy(cis, bbos);
218 bbos.close();
219 ByteBuffer bb = bbos.getByteBuffer();
220 is = new ByteBufferInputStream(bb);
221 } finally {
222 if (is != null) is.close();
223 if (bbos != null) bbos.close();
224
225 CodecPool.returnDecompressor(poolDecompressor);
226 }
227 } else {
228 is = new ByteBufferInputStream(cellBlock);
229 }
230 return codec.getDecoder(is);
231 }
232
233
234
235
236
237
238
239 public static ByteBuffer getDelimitedMessageAsByteBuffer(final Message m) throws IOException {
240 if (m == null) return null;
241 int serializedSize = m.getSerializedSize();
242 int vintSize = CodedOutputStream.computeRawVarint32Size(serializedSize);
243 byte [] buffer = new byte[serializedSize + vintSize];
244
245 CodedOutputStream cos = CodedOutputStream.newInstance(buffer);
246
247 cos.writeMessageNoTag(m);
248 cos.flush();
249 cos.checkNoSpaceLeft();
250 return ByteBuffer.wrap(buffer);
251 }
252
253
254
255
256
257
258
259
260
261
262 public static int write(final OutputStream dos, final Message header, final Message param,
263 final ByteBuffer cellBlock)
264 throws IOException {
265
266
267
268 int totalSize = IPCUtil.getTotalSizeWhenWrittenDelimited(header, param);
269 if (cellBlock != null) totalSize += cellBlock.remaining();
270 return write(dos, header, param, cellBlock, totalSize);
271 }
272
273 private static int write(final OutputStream dos, final Message header, final Message param,
274 final ByteBuffer cellBlock, final int totalSize)
275 throws IOException {
276
277 dos.write(Bytes.toBytes(totalSize));
278
279 header.writeDelimitedTo(dos);
280 if (param != null) param.writeDelimitedTo(dos);
281 if (cellBlock != null) dos.write(cellBlock.array(), 0, cellBlock.remaining());
282 dos.flush();
283 return totalSize;
284 }
285
286
287
288
289
290
291
292
293
294 public static void readChunked(final DataInput in, byte[] dest, int offset, int len)
295 throws IOException {
296 int maxRead = 8192;
297
298 for (; offset < len; offset += maxRead) {
299 in.readFully(dest, offset, Math.min(len - offset, maxRead));
300 }
301 }
302
303
304
305
306 public static int getTotalSizeWhenWrittenDelimited(Message ... messages) {
307 int totalSize = 0;
308 for (Message m: messages) {
309 if (m == null) continue;
310 totalSize += m.getSerializedSize();
311 totalSize += CodedOutputStream.computeRawVarint32Size(m.getSerializedSize());
312 }
313 Preconditions.checkArgument(totalSize < Integer.MAX_VALUE);
314 return totalSize;
315 }
316
317 static IOException toIOE(Throwable t) {
318 if (t instanceof IOException) {
319 return (IOException) t;
320 } else {
321 return new IOException(t);
322 }
323 }
324 }