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 static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23
24 import java.net.InetAddress;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.List;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.hadoop.hbase.HBaseTestingUtility;
32 import org.apache.hadoop.hbase.LargeTests;
33 import org.apache.hadoop.hbase.thrift.ThriftServerRunner.ImplType;
34 import org.apache.hadoop.hbase.thrift.generated.Hbase;
35 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
36 import org.apache.hadoop.hbase.util.EnvironmentEdgeManagerTestHelper;
37 import org.apache.hadoop.hbase.util.IncrementingEnvironmentEdge;
38 import org.apache.thrift.protocol.TBinaryProtocol;
39 import org.apache.thrift.protocol.TCompactProtocol;
40 import org.apache.thrift.protocol.TProtocol;
41 import org.apache.thrift.server.TServer;
42 import org.apache.thrift.transport.TFramedTransport;
43 import org.apache.thrift.transport.TSocket;
44 import org.apache.thrift.transport.TTransport;
45 import org.junit.AfterClass;
46 import org.junit.BeforeClass;
47 import org.junit.Test;
48 import org.junit.experimental.categories.Category;
49 import org.junit.runner.RunWith;
50 import org.junit.runners.Parameterized;
51 import org.junit.runners.Parameterized.Parameters;
52
53 import com.google.common.base.Joiner;
54
55
56
57
58
59 @Category(LargeTests.class)
60 @RunWith(Parameterized.class)
61 public class TestThriftServerCmdLine {
62
63 public static final Log LOG =
64 LogFactory.getLog(TestThriftServerCmdLine.class);
65
66 private final ImplType implType;
67 private boolean specifyFramed;
68 private boolean specifyBindIP;
69 private boolean specifyCompact;
70
71 private static final HBaseTestingUtility TEST_UTIL =
72 new HBaseTestingUtility();
73
74 private Thread cmdLineThread;
75 private volatile Exception cmdLineException;
76
77 private Exception clientSideException;
78
79 private ThriftServer thriftServer;
80 private int port;
81
82 @Parameters
83 public static Collection<Object[]> getParameters() {
84 Collection<Object[]> parameters = new ArrayList<Object[]>();
85 for (ImplType implType : ImplType.values()) {
86 for (boolean specifyFramed : new boolean[] {false, true}) {
87 for (boolean specifyBindIP : new boolean[] {false, true}) {
88 if (specifyBindIP && !implType.canSpecifyBindIP) {
89 continue;
90 }
91 for (boolean specifyCompact : new boolean[] {false, true}) {
92 parameters.add(new Object[]{implType, specifyFramed,
93 specifyBindIP, specifyCompact});
94 }
95 }
96 }
97 }
98 return parameters;
99 }
100
101 public TestThriftServerCmdLine(ImplType implType, boolean specifyFramed,
102 boolean specifyBindIP, boolean specifyCompact) {
103 this.implType = implType;
104 this.specifyFramed = specifyFramed;
105 this.specifyBindIP = specifyBindIP;
106 this.specifyCompact = specifyCompact;
107 LOG.debug(getParametersString());
108 }
109
110 private String getParametersString() {
111 return "implType=" + implType + ", " +
112 "specifyFramed=" + specifyFramed + ", " +
113 "specifyBindIP=" + specifyBindIP + ", " +
114 "specifyCompact=" + specifyCompact;
115 }
116
117 @BeforeClass
118 public static void setUpBeforeClass() throws Exception {
119 TEST_UTIL.startMiniCluster();
120
121
122 EnvironmentEdgeManagerTestHelper.injectEdge(new IncrementingEnvironmentEdge());
123 }
124
125 @AfterClass
126 public static void tearDownAfterClass() throws Exception {
127 TEST_UTIL.shutdownMiniCluster();
128 EnvironmentEdgeManager.reset();
129 }
130
131 private void startCmdLineThread(final String[] args) {
132 LOG.info("Starting HBase Thrift server with command line: " + Joiner.on(" ").join(args));
133
134 cmdLineException = null;
135 cmdLineThread = new Thread(new Runnable() {
136 @Override
137 public void run() {
138 try {
139 thriftServer.doMain(args);
140 } catch (Exception e) {
141 cmdLineException = e;
142 }
143 }
144 });
145 cmdLineThread.setName(ThriftServer.class.getSimpleName() +
146 "-cmdline");
147 cmdLineThread.start();
148 }
149
150 @Test(timeout=600000)
151 public void testRunThriftServer() throws Exception {
152 List<String> args = new ArrayList<String>();
153 if (implType != null) {
154 String serverTypeOption = implType.toString();
155 assertTrue(serverTypeOption.startsWith("-"));
156 args.add(serverTypeOption);
157 }
158 port = HBaseTestingUtility.randomFreePort();
159 args.add("-" + ThriftServer.PORT_OPTION);
160 args.add(String.valueOf(port));
161 if (specifyFramed) {
162 args.add("-" + ThriftServer.FRAMED_OPTION);
163 }
164 if (specifyBindIP) {
165 args.add("-" + ThriftServer.BIND_OPTION);
166 args.add(InetAddress.getLocalHost().getHostName());
167 }
168 if (specifyCompact) {
169 args.add("-" + ThriftServer.COMPACT_OPTION);
170 }
171 args.add("start");
172
173 thriftServer = new ThriftServer(TEST_UTIL.getConfiguration());
174 startCmdLineThread(args.toArray(new String[0]));
175
176 while ( thriftServer.serverRunner == null || thriftServer.serverRunner.tserver == null ){
177 Thread.sleep(1);
178 }
179
180 Class<? extends TServer> expectedClass = implType != null ?
181 implType.serverClass : TBoundedThreadPoolServer.class;
182 assertEquals(expectedClass,
183 thriftServer.serverRunner.tserver.getClass());
184
185 try {
186 talkToThriftServer();
187 } catch (Exception ex) {
188 clientSideException = ex;
189 } finally {
190 stopCmdLineThread();
191 }
192
193 if (clientSideException != null) {
194 LOG.error("Thrift client threw an exception. Parameters:" +
195 getParametersString(), clientSideException);
196 throw new Exception(clientSideException);
197 }
198 }
199
200 private static volatile boolean tableCreated = false;
201
202 private void talkToThriftServer() throws Exception {
203 TSocket sock = new TSocket(InetAddress.getLocalHost().getHostName(),
204 port);
205 TTransport transport = sock;
206 if (specifyFramed || implType.isAlwaysFramed) {
207 transport = new TFramedTransport(transport);
208 }
209
210 sock.open();
211 try {
212 TProtocol prot;
213 if (specifyCompact) {
214 prot = new TCompactProtocol(transport);
215 } else {
216 prot = new TBinaryProtocol(transport);
217 }
218 Hbase.Client client = new Hbase.Client(prot);
219 if (!tableCreated){
220 TestThriftServer.createTestTables(client);
221 tableCreated = true;
222 }
223 TestThriftServer.checkTableList(client);
224
225 } finally {
226 sock.close();
227 }
228 }
229
230 private void stopCmdLineThread() throws Exception {
231 LOG.debug("Stopping " + implType.simpleClassName() + " Thrift server");
232 thriftServer.stop();
233 cmdLineThread.join();
234 if (cmdLineException != null) {
235 LOG.error("Command-line invocation of HBase Thrift server threw an " +
236 "exception", cmdLineException);
237 throw new Exception(cmdLineException);
238 }
239 }
240
241
242 }
243