1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.junit.AfterClass;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.junit.experimental.categories.Category;
40
41 import com.google.common.base.Joiner;
42
43
44
45
46
47 @Category(LargeTests.class)
48
49 public class TestThriftHttpServer {
50
51 public static final Log LOG =
52 LogFactory.getLog(TestThriftHttpServer.class);
53
54 private static final HBaseTestingUtility TEST_UTIL =
55 new HBaseTestingUtility();
56
57 private Thread httpServerThread;
58 private volatile Exception httpServerException;
59
60 private Exception clientSideException;
61
62 private ThriftServer thriftServer;
63 private int port;
64
65 @BeforeClass
66 public static void setUpBeforeClass() throws Exception {
67 TEST_UTIL.getConfiguration().setBoolean("hbase.regionserver.thrift.http", true);
68 TEST_UTIL.getConfiguration().setBoolean("hbase.table.sanity.checks", false);
69 TEST_UTIL.startMiniCluster();
70
71
72 EnvironmentEdgeManagerTestHelper.injectEdge(new IncrementingEnvironmentEdge());
73 }
74
75 @AfterClass
76 public static void tearDownAfterClass() throws Exception {
77 TEST_UTIL.shutdownMiniCluster();
78 EnvironmentEdgeManager.reset();
79 }
80
81 private void startHttpServerThread(final String[] args) {
82 LOG.info("Starting HBase Thrift server with HTTP server: " + Joiner.on(" ").join(args));
83
84 httpServerException = null;
85 httpServerThread = new Thread(new Runnable() {
86 @Override
87 public void run() {
88 try {
89 thriftServer.doMain(args);
90 } catch (Exception e) {
91 httpServerException = e;
92 }
93 }
94 });
95 httpServerThread.setName(ThriftServer.class.getSimpleName() +
96 "-httpServer");
97 httpServerThread.start();
98 }
99
100 @Test(timeout=600000)
101 public void testRunThriftServer() throws Exception {
102 List<String> args = new ArrayList<String>();
103 port = HBaseTestingUtility.randomFreePort();
104 args.add("-" + ThriftServer.PORT_OPTION);
105 args.add(String.valueOf(port));
106 args.add("start");
107
108 thriftServer = new ThriftServer(TEST_UTIL.getConfiguration());
109 startHttpServerThread(args.toArray(new String[args.size()]));
110
111
112 for (int i = 0; i < 100
113 && ( thriftServer.serverRunner == null || thriftServer.serverRunner.httpServer ==
114 null); i++) {
115 Thread.sleep(100);
116 }
117
118 try {
119 talkToThriftServer();
120 } catch (Exception ex) {
121 clientSideException = ex;
122 } finally {
123 stopHttpServerThread();
124 }
125
126 if (clientSideException != null) {
127 LOG.error("Thrift client threw an exception " + clientSideException);
128 throw new Exception(clientSideException);
129 }
130 }
131
132 private static volatile boolean tableCreated = false;
133
134 private void talkToThriftServer() throws Exception {
135 THttpClient httpClient = new THttpClient(
136 "http://"+ HConstants.LOCALHOST + ":" + port);
137 httpClient.open();
138 try {
139 TProtocol prot;
140 prot = new TBinaryProtocol(httpClient);
141 Hbase.Client client = new Hbase.Client(prot);
142 if (!tableCreated){
143 TestThriftServer.createTestTables(client);
144 tableCreated = true;
145 }
146 TestThriftServer.checkTableList(client);
147 } finally {
148 httpClient.close();
149 }
150 }
151
152 private void stopHttpServerThread() throws Exception {
153 LOG.debug("Stopping " + " Thrift HTTP server");
154 thriftServer.stop();
155 httpServerThread.join();
156 if (httpServerException != null) {
157 LOG.error("Command-line invocation of HBase Thrift server threw an " +
158 "exception", httpServerException);
159 throw new Exception(httpServerException);
160 }
161 }
162 }