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