View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.ipc;
19  
20  import java.io.ByteArrayInputStream;
21  import java.io.DataInput;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.nio.BufferOverflowException;
26  import java.nio.ByteBuffer;
27  
28  import org.apache.commons.io.IOUtils;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
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.classification.InterfaceAudience;
37  import org.apache.hadoop.hbase.codec.Codec;
38  import org.apache.hadoop.hbase.io.BoundedByteBufferPool;
39  import org.apache.hadoop.hbase.io.ByteBufferInputStream;
40  import org.apache.hadoop.hbase.io.ByteBufferOutputStream;
41  import org.apache.hadoop.hbase.io.HeapSize;
42  import org.apache.hadoop.hbase.util.Bytes;
43  import org.apache.hadoop.hbase.util.ClassSize;
44  import org.apache.hadoop.io.compress.CodecPool;
45  import org.apache.hadoop.io.compress.CompressionCodec;
46  import org.apache.hadoop.io.compress.CompressionInputStream;
47  import org.apache.hadoop.io.compress.Compressor;
48  import org.apache.hadoop.io.compress.Decompressor;
49  
50  import com.google.common.base.Preconditions;
51  import com.google.protobuf.CodedOutputStream;
52  import com.google.protobuf.Message;
53  
54  /**
55   * Utility to help ipc'ing.
56   */
57  @InterfaceAudience.Private
58  class IPCUtil {
59    public static final Log LOG = LogFactory.getLog(IPCUtil.class);
60    /**
61     * How much we think the decompressor will expand the original compressed content.
62     */
63    private final int cellBlockDecompressionMultiplier;
64    private final int cellBlockBuildingInitialBufferSize;
65    private final Configuration conf;
66  
67    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      // Guess that 16k is a good size for rpc buffer.  Could go bigger.  See the TODO below in
74      // #buildCellBlock.
75      this.cellBlockBuildingInitialBufferSize =
76        ClassSize.align(conf.getInt("hbase.ipc.cellblock.building.initial.buffersize", 16 * 1024));
77    }
78  
79    /**
80     * Thrown if a cellscanner but no codec to encode it with.
81     */
82    @InterfaceAudience.Private
83    public static class CellScannerButNoCodecException extends HBaseIOException {};
84  
85    /**
86     * Puts CellScanner Cells into a cell block using passed in <code>codec</code> and/or
87     * <code>compressor</code>.
88     * @param codec
89     * @param compressor
90     * @Param cellScanner
91     * @return Null or byte buffer filled with a cellblock filled with passed-in Cells encoded using
92     * passed in <code>codec</code> and/or <code>compressor</code>; the returned buffer has been
93     * flipped and is ready for reading.  Use limit to find total size.
94     * @throws IOException
95     */
96    @SuppressWarnings("resource")
97    ByteBuffer buildCellBlock(final Codec codec, final CompressionCodec compressor,
98      final CellScanner cellScanner)
99    throws IOException {
100     return buildCellBlock(codec, compressor, cellScanner, null);
101   }
102 
103   /**
104    * Puts CellScanner Cells into a cell block using passed in <code>codec</code> and/or
105    * <code>compressor</code>.
106    * @param codec
107    * @param compressor
108    * @param cellScanner
109    * @param pool Pool of ByteBuffers to make use of. Can be null and then we'll allocate
110    * our own ByteBuffer.
111    * @return Null or byte buffer filled with a cellblock filled with passed-in Cells encoded using
112    * passed in <code>codec</code> and/or <code>compressor</code>; the returned buffer has been
113    * flipped and is ready for reading.  Use limit to find total size. If <code>pool</code> was not
114    * null, then this returned ByteBuffer came from there and should be returned to the pool when
115    * done.
116    * @throws IOException
117    */
118   @SuppressWarnings("resource")
119   public ByteBuffer buildCellBlock(final Codec codec, final CompressionCodec compressor,
120     final CellScanner cellScanner, final BoundedByteBufferPool pool)
121   throws IOException {
122     if (cellScanner == null) return null;
123     if (codec == null) throw new CellScannerButNoCodecException();
124     int bufferSize = this.cellBlockBuildingInitialBufferSize;
125     ByteBufferOutputStream baos = null;
126     if (pool != null) {
127       ByteBuffer bb = pool.getBuffer();
128       bufferSize = bb.capacity();
129       baos = new ByteBufferOutputStream(bb);
130     } else {
131       // Then we need to make our own to return.
132       if (cellScanner instanceof HeapSize) {
133         long longSize = ((HeapSize)cellScanner).heapSize();
134         // Just make sure we don't have a size bigger than an int.
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       // If no cells, don't mess around.  Just return null (could be a bunch of existence checking
158       // gets or something -- stuff that does not return a cell).
159       if (count == 0) return null;
160     } catch (BufferOverflowException e) {
161       throw new DoNotRetryIOException(e);
162     } finally {
163       os.close();
164       if (poolCompressor != null) CodecPool.returnCompressor(poolCompressor);
165     }
166     if (LOG.isTraceEnabled()) {
167       if (bufferSize < baos.size()) {
168         LOG.trace("Buffer grew from initial bufferSize=" + bufferSize + " to " + baos.size() +
169           "; up hbase.ipc.cellblock.building.initial.buffersize?");
170       }
171     }
172     return baos.getByteBuffer();
173   }
174 
175   /**
176    * @param codec
177    * @param cellBlock
178    * @return CellScanner to work against the content of <code>cellBlock</code>
179    * @throws IOException
180    */
181   CellScanner createCellScanner(final Codec codec, final CompressionCodec compressor,
182       final byte [] cellBlock)
183   throws IOException {
184     return createCellScanner(codec, compressor, ByteBuffer.wrap(cellBlock));
185   }
186 
187   /**
188    * @param codec
189    * @param cellBlock ByteBuffer containing the cells written by the Codec. The buffer should be
190    * position()'ed at the start of the cell block and limit()'ed at the end.
191    * @return CellScanner to work against the content of <code>cellBlock</code>
192    * @throws IOException
193    */
194   CellScanner createCellScanner(final Codec codec, final CompressionCodec compressor,
195       final ByteBuffer cellBlock)
196   throws IOException {
197     // If compressed, decompress it first before passing it on else we will leak compression
198     // resources if the stream is not closed properly after we let it out.
199     InputStream is = null;
200     if (compressor != null) {
201       // GZIPCodec fails w/ NPE if no configuration.
202       if (compressor instanceof Configurable) ((Configurable)compressor).setConf(this.conf);
203       Decompressor poolDecompressor = CodecPool.getDecompressor(compressor);
204       CompressionInputStream cis =
205         compressor.createInputStream(new ByteBufferInputStream(cellBlock), poolDecompressor);
206       ByteBufferOutputStream bbos = null;
207       try {
208         // TODO: This is ugly.  The buffer will be resized on us if we guess wrong.
209         // TODO: Reuse buffers.
210         bbos = new ByteBufferOutputStream(cellBlock.remaining() *
211           this.cellBlockDecompressionMultiplier);
212         IOUtils.copy(cis, bbos);
213         bbos.close();
214         ByteBuffer bb = bbos.getByteBuffer();
215         is = new ByteArrayInputStream(bb.array(), 0, bb.limit());
216       } finally {
217         if (is != null) is.close();
218         if (bbos != null) bbos.close();
219 
220         CodecPool.returnDecompressor(poolDecompressor);
221       }
222     } else {
223       is = new ByteBufferInputStream(cellBlock);
224     }
225     return codec.getDecoder(is);
226   }
227 
228   /**
229    * @param m Message to serialize delimited; i.e. w/ a vint of its size preceeding its
230    * serialization.
231    * @return The passed in Message serialized with delimiter.  Return null if <code>m</code> is null
232    * @throws IOException
233    */
234   static ByteBuffer getDelimitedMessageAsByteBuffer(final Message m) throws IOException {
235     if (m == null) return null;
236     int serializedSize = m.getSerializedSize();
237     int vintSize = CodedOutputStream.computeRawVarint32Size(serializedSize);
238     byte [] buffer = new byte[serializedSize + vintSize];
239     // Passing in a byte array saves COS creating a buffer which it does when using streams.
240     CodedOutputStream cos = CodedOutputStream.newInstance(buffer);
241     // This will write out the vint preamble and the message serialized.
242     cos.writeMessageNoTag(m);
243     cos.flush();
244     cos.checkNoSpaceLeft();
245     return ByteBuffer.wrap(buffer);
246   }
247 
248   /**
249    * Write out header, param, and cell block if there is one.
250    * @param dos
251    * @param header
252    * @param param
253    * @param cellBlock
254    * @return Total number of bytes written.
255    * @throws IOException
256    */
257   static int write(final OutputStream dos, final Message header, final Message param,
258       final ByteBuffer cellBlock)
259   throws IOException {
260     // Must calculate total size and write that first so other side can read it all in in one
261     // swoop.  This is dictated by how the server is currently written.  Server needs to change
262     // if we are to be able to write without the length prefixing.
263     int totalSize = IPCUtil.getTotalSizeWhenWrittenDelimited(header, param);
264     if (cellBlock != null) totalSize += cellBlock.remaining();
265     return write(dos, header, param, cellBlock, totalSize);
266   }
267 
268   private static int write(final OutputStream dos, final Message header, final Message param,
269     final ByteBuffer cellBlock, final int totalSize)
270   throws IOException {
271     // I confirmed toBytes does same as DataOutputStream#writeInt.
272     dos.write(Bytes.toBytes(totalSize));
273     // This allocates a buffer that is the size of the message internally.
274     header.writeDelimitedTo(dos);
275     if (param != null) param.writeDelimitedTo(dos);
276     if (cellBlock != null) dos.write(cellBlock.array(), 0, cellBlock.remaining());
277     dos.flush();
278     return totalSize;
279   }
280 
281   /**
282    * Read in chunks of 8K (HBASE-7239)
283    * @param in
284    * @param dest
285    * @param offset
286    * @param len
287    * @throws IOException
288    */
289   static void readChunked(final DataInput in, byte[] dest, int offset, int len)
290       throws IOException {
291     int maxRead = 8192;
292 
293     for (; offset < len; offset += maxRead) {
294       in.readFully(dest, offset, Math.min(len - offset, maxRead));
295     }
296   }
297 
298   /**
299    * @param header
300    * @param body
301    * @return Size on the wire when the two messages are written with writeDelimitedTo
302    */
303   static int getTotalSizeWhenWrittenDelimited(Message ... messages) {
304     int totalSize = 0;
305     for (Message m: messages) {
306       if (m == null) continue;
307       totalSize += m.getSerializedSize();
308       totalSize += CodedOutputStream.computeRawVarint32Size(m.getSerializedSize());
309     }
310     Preconditions.checkArgument(totalSize < Integer.MAX_VALUE);
311     return totalSize;
312   }
313 }