View Javadoc

1   /*
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements. See the NOTICE file distributed with this
6    * work for additional information regarding copyright ownership. The ASF
7    * licenses this file to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16   * License for the specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.hadoop.hbase.thrift;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.hbase.HBaseTestingUtility;
27  import org.apache.hadoop.hbase.HConstants;
28  import org.apache.hadoop.hbase.testclassification.LargeTests;
29  import org.apache.hadoop.hbase.thrift.generated.Hbase;
30  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
31  import org.apache.hadoop.hbase.util.EnvironmentEdgeManagerTestHelper;
32  import org.apache.hadoop.hbase.util.IncrementingEnvironmentEdge;
33  import org.apache.thrift.protocol.TBinaryProtocol;
34  import org.apache.thrift.protocol.TProtocol;
35  import org.apache.thrift.transport.THttpClient;
36  import org.apache.thrift.transport.TTransportException;
37  import org.junit.AfterClass;
38  import org.junit.BeforeClass;
39  import org.junit.Test;
40  import org.junit.experimental.categories.Category;
41  import static org.junit.Assert.assertFalse;
42  import static org.junit.Assert.assertTrue;
43  import org.junit.Rule;
44  import org.junit.rules.ExpectedException;
45  
46  import com.google.common.base.Joiner;
47  
48  /**
49   * Start the HBase Thrift HTTP server on a random port through the command-line
50   * interface and talk to it from client side.
51   */
52  @Category(LargeTests.class)
53  
54  public class TestThriftHttpServer {
55  
56    public static final Log LOG =
57        LogFactory.getLog(TestThriftHttpServer.class);
58  
59    private static final HBaseTestingUtility TEST_UTIL =
60        new HBaseTestingUtility();
61  
62    private Thread httpServerThread;
63    private volatile Exception httpServerException;
64  
65    private Exception clientSideException;
66  
67    private ThriftServer thriftServer;
68    private int port;
69  
70    @BeforeClass
71    public static void setUpBeforeClass() throws Exception {
72      TEST_UTIL.getConfiguration().setBoolean("hbase.regionserver.thrift.http", true);
73      TEST_UTIL.getConfiguration().setBoolean("hbase.table.sanity.checks", false);
74      TEST_UTIL.startMiniCluster();
75      //ensure that server time increments every time we do an operation, otherwise
76      //successive puts having the same timestamp will override each other
77      EnvironmentEdgeManagerTestHelper.injectEdge(new IncrementingEnvironmentEdge());
78    }
79  
80    @AfterClass
81    public static void tearDownAfterClass() throws Exception {
82      TEST_UTIL.shutdownMiniCluster();
83      EnvironmentEdgeManager.reset();
84    }
85  
86    private void startHttpServerThread(final String[] args) {
87      LOG.info("Starting HBase Thrift server with HTTP server: " + Joiner.on(" ").join(args));
88  
89      httpServerException = null;
90      httpServerThread = new Thread(new Runnable() {
91        @Override
92        public void run() {
93          try {
94            thriftServer.doMain(args);
95          } catch (Exception e) {
96            httpServerException = e;
97          }
98        }
99      });
100     httpServerThread.setName(ThriftServer.class.getSimpleName() +
101         "-httpServer");
102     httpServerThread.start();
103   }
104 
105   @Rule
106   public ExpectedException exception = ExpectedException.none();
107 
108   @Test(timeout=600000)
109   public void testRunThriftServerWithHeaderBufferLength() throws Exception {
110 
111     // Test thrift server with HTTP header length less than 64k
112     try {
113       runThriftServer(1024 * 63);
114     } catch (TTransportException tex) {
115       assertFalse(tex.getMessage().equals("HTTP Response code: 413"));
116     }
117 
118     // Test thrift server with HTTP header length more than 64k, expect an exception
119     exception.expect(TTransportException.class);
120     exception.expectMessage("HTTP Response code: 413");
121     runThriftServer(1024 * 64);
122   }
123 
124   @Test(timeout=600000)
125   public void testRunThriftServer() throws Exception {
126     runThriftServer(0);
127   }
128 
129   private void runThriftServer(int customHeaderSize) throws Exception {
130     List<String> args = new ArrayList<String>();
131     port = HBaseTestingUtility.randomFreePort();
132     args.add("-" + ThriftServer.PORT_OPTION);
133     args.add(String.valueOf(port));
134     args.add("start");
135 
136     thriftServer = new ThriftServer(TEST_UTIL.getConfiguration());
137     startHttpServerThread(args.toArray(new String[args.size()]));
138 
139     // wait up to 10s for the server to start
140     for (int i = 0; i < 100
141         && ( thriftServer.serverRunner == null ||  thriftServer.serverRunner.httpServer ==
142         null); i++) {
143       Thread.sleep(100);
144     }
145 
146     try {
147       talkToThriftServer(customHeaderSize);
148     } catch (Exception ex) {
149       clientSideException = ex;
150     } finally {
151       stopHttpServerThread();
152     }
153 
154     if (clientSideException != null) {
155       LOG.error("Thrift client threw an exception " + clientSideException);
156       if (clientSideException instanceof  TTransportException) {
157         throw clientSideException;
158       } else {
159         throw new Exception(clientSideException);
160       }
161     }
162   }
163 
164   private static volatile boolean tableCreated = false;
165 
166   private void talkToThriftServer(int customHeaderSize) throws Exception {
167     THttpClient httpClient = new THttpClient(
168         "http://"+ HConstants.LOCALHOST + ":" + port);
169     httpClient.open();
170 
171     if (customHeaderSize > 0) {
172       StringBuilder sb = new StringBuilder();
173       for (int i = 0; i < customHeaderSize; i++) {
174         sb.append("a");
175       }
176       httpClient.setCustomHeader("User-Agent", sb.toString());
177     }
178 
179     try {
180       TProtocol prot;
181       prot = new TBinaryProtocol(httpClient);
182       Hbase.Client client = new Hbase.Client(prot);
183       if (!tableCreated){
184         TestThriftServer.createTestTables(client);
185         tableCreated = true;
186       }
187       TestThriftServer.checkTableList(client);
188     } finally {
189       httpClient.close();
190     }
191   }
192 
193   private void stopHttpServerThread() throws Exception {
194     LOG.debug("Stopping " + " Thrift HTTP server");
195     thriftServer.stop();
196     httpServerThread.join();
197     if (httpServerException != null) {
198       LOG.error("Command-line invocation of HBase Thrift server threw an " +
199           "exception", httpServerException);
200       throw new Exception(httpServerException);
201     }
202   }
203 }