From 1db25b59aa19dae53414fe442877cd4f3f725104 Mon Sep 17 00:00:00 2001 From: Ashu Pachauri Date: Thu, 6 Oct 2016 17:09:19 -0700 Subject: [PATCH] HBASE-16752: Return error back to rpc client on exceeding rpc request size limit. --- .../hbase/exceptions/ClientExceptionsUtil.java | 2 +- .../hbase/exceptions/RequestTooBigException.java | 38 +++++++++++++++++ .../org/apache/hadoop/hbase/ipc/RpcServer.java | 48 ++++++++++++++++++++-- .../apache/hadoop/hbase/ipc/AbstractTestIPC.java | 10 ++--- 4 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 hbase-client/src/main/java/org/apache/hadoop/hbase/exceptions/RequestTooBigException.java diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/exceptions/ClientExceptionsUtil.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/exceptions/ClientExceptionsUtil.java index f367ed9..8acfe49 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/exceptions/ClientExceptionsUtil.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/exceptions/ClientExceptionsUtil.java @@ -63,7 +63,7 @@ public final class ClientExceptionsUtil { || cur instanceof RegionTooBusyException || cur instanceof ThrottlingException || cur instanceof MultiActionResultTooLarge || cur instanceof RetryImmediatelyException || cur instanceof CallQueueTooBigException || cur instanceof CallDroppedException - || cur instanceof NotServingRegionException); + || cur instanceof NotServingRegionException || cur instanceof RequestTooBigException); } diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/exceptions/RequestTooBigException.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/exceptions/RequestTooBigException.java new file mode 100644 index 0000000..f574dd8 --- /dev/null +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/exceptions/RequestTooBigException.java @@ -0,0 +1,38 @@ +/** + * 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.exceptions; + +import org.apache.hadoop.hbase.DoNotRetryIOException; +import org.apache.hadoop.hbase.classification.InterfaceAudience; +import org.apache.hadoop.hbase.classification.InterfaceStability; + +@InterfaceAudience.Public +@InterfaceStability.Evolving +public class RequestTooBigException extends DoNotRetryIOException { + + private static final long serialVersionUID = -1593339239809586516L; + + public RequestTooBigException() { + super(); + } + + public RequestTooBigException(String message) { + super(message); + } +} diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/ipc/RpcServer.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/ipc/RpcServer.java index dd9bb01..c386617 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/ipc/RpcServer.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/ipc/RpcServer.java @@ -26,6 +26,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; +import java.io.InputStream; import java.net.BindException; import java.net.InetAddress; import java.net.InetSocketAddress; @@ -85,6 +86,7 @@ import org.apache.hadoop.hbase.client.VersionInfoUtil; import org.apache.hadoop.hbase.codec.Codec; import org.apache.hadoop.hbase.conf.ConfigurationObserver; import org.apache.hadoop.hbase.exceptions.RegionMovedException; +import org.apache.hadoop.hbase.exceptions.RequestTooBigException; import org.apache.hadoop.hbase.io.ByteBufferInputStream; import org.apache.hadoop.hbase.io.ByteBufferListOutputStream; import org.apache.hadoop.hbase.io.ByteBufferOutputStream; @@ -257,6 +259,9 @@ public class RpcServer implements RpcServerInterface, ConfigurationObserver { protected HBaseRPCErrorHandler errorHandler = null; static final String MAX_REQUEST_SIZE = "hbase.ipc.max.request.size"; + private static final RequestTooBigException REQUEST_TOO_BIG_EXCEPTION = + new RequestTooBigException(); + private static final String WARN_RESPONSE_TIME = "hbase.ipc.warn.response.time"; private static final String WARN_RESPONSE_SIZE = "hbase.ipc.warn.response.size"; @@ -1614,10 +1619,46 @@ public class RpcServer implements RpcServerInterface, ConfigurationObserver { + dataLength + "!! from " + getHostAddress()); } + if (dataLength > maxRequestSize) { - throw new DoNotRetryIOException("RPC data length of " + dataLength + " received from " - + getHostAddress() + " is greater than max allowed " + maxRequestSize + ". Set \"" - + MAX_REQUEST_SIZE + "\" on server to override this limit (not recommended)"); + String msg = "RPC data length of " + dataLength + " received from " + + getHostAddress() + " is greater than max allowed " + + maxRequestSize + ". Set \"" + MAX_REQUEST_SIZE + + "\" on server to override this limit (not recommended)"; + LOG.warn(msg); + + if (connectionHeaderRead && connectionPreambleRead) { + incRpcCount(); + // Construct InputStream for the non-blocking SocketChannel + // We need the InputStream because we want to read only the request header + // instead of the whole rpc. + ByteBuffer buf = ByteBuffer.allocate(1); + InputStream is = new InputStream() { + @Override + public int read() throws IOException { + channelRead(channel, buf); + buf.flip(); + int x = buf.get(); + buf.flip(); + return x; + } + }; + CodedInputStream cis = CodedInputStream.newInstance(is); + int headerSize = cis.readRawVarint32(); + Message.Builder builder = RequestHeader.newBuilder(); + ProtobufUtil.mergeFrom(builder, cis, headerSize); + RequestHeader header = (RequestHeader) builder.build(); + + Call reqTooBig = new Call(header.getCallId(), this.service, null, null, null, + null, this, responder, 0, null, this.addr,0); + metrics.exception(REQUEST_TOO_BIG_EXCEPTION); + setupResponse(null, reqTooBig, REQUEST_TOO_BIG_EXCEPTION, msg); + responder.doRespond(reqTooBig); + return 0; + } + // This means the connection setup rpc was too large. This should not + // happen, we already logged the warning, close the connection. + return -1; } data = ByteBuffer.allocate(dataLength); @@ -1637,6 +1678,7 @@ public class RpcServer implements RpcServerInterface, ConfigurationObserver { return count; } + /** * Process the data buffer and clean the connection state for the next call. */ diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/ipc/AbstractTestIPC.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/ipc/AbstractTestIPC.java index 652ba3f..8f508af 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/ipc/AbstractTestIPC.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/ipc/AbstractTestIPC.java @@ -204,13 +204,13 @@ public abstract class AbstractTestIPC { @Test public void testRpcMaxRequestSize() throws IOException, ServiceException { Configuration conf = new Configuration(CONF); - conf.setInt(RpcServer.MAX_REQUEST_SIZE, 100); + conf.setInt(RpcServer.MAX_REQUEST_SIZE, 1000); RpcServer rpcServer = new TestRpcServer(conf); try (AbstractRpcClient client = createRpcClient(conf)) { rpcServer.start(); BlockingInterface stub = newBlockingStub(client, rpcServer.getListenerAddress()); - StringBuilder message = new StringBuilder(120); - for (int i = 0; i < 20; i++) { + StringBuilder message = new StringBuilder(1200); + for (int i = 0; i < 200; i++) { message.append("hello."); } // set total RPC size bigger than 100 bytes @@ -220,8 +220,8 @@ public abstract class AbstractTestIPC { param); fail("RPC should have failed because it exceeds max request size"); } catch (ServiceException e) { - LOG.info("Caught expected exception: " + e.toString()); - // the rpc server just close the connection so we can not get the detail message. + assertTrue(e.toString(), + StringUtils.stringifyException(e).contains("RequestTooBigException")); } finally { rpcServer.stop(); } -- 2.8.0-rc2