|
18 | 18 |
|
19 | 19 | package org.apache.hudi.hive.testutils; |
20 | 20 |
|
21 | | -import org.apache.hudi.common.testutils.HoodieTestUtils; |
| 21 | +import org.apache.hudi.common.testutils.NetworkTestUtils; |
22 | 22 | import org.apache.hudi.common.util.FileIOUtils; |
23 | 23 |
|
24 | 24 | import org.apache.hadoop.conf.Configuration; |
|
62 | 62 | public class HiveTestService { |
63 | 63 |
|
64 | 64 | private static final Logger LOG = LogManager.getLogger(HiveTestService.class); |
| 65 | + private static final int CONNECTION_TIMEOUT_MS = 30000; |
| 66 | + private static final String BIND_HOST = "127.0.0.1"; |
65 | 67 |
|
66 | | - private static final int CONNECTION_TIMEOUT = 30000; |
67 | | - |
68 | | - /** |
69 | | - * Configuration settings. |
70 | | - */ |
71 | | - private Configuration hadoopConf; |
72 | | - private String workDir; |
73 | | - private String bindIP = "127.0.0.1"; |
74 | | - private int metastorePort = 9083; |
75 | | - private int serverPort = 9999; |
76 | | - private boolean clean = true; |
77 | | - |
78 | | - private Map<String, String> sysProps = new HashMap<>(); |
| 68 | + private final Configuration hadoopConf; |
| 69 | + private final String workDir; |
| 70 | + private final Map<String, String> sysProps = new HashMap<>(); |
79 | 71 | private ExecutorService executorService; |
80 | 72 | private TServer tServer; |
81 | 73 | private HiveServer2 hiveServer; |
82 | | - private HiveConf serverConf; |
| 74 | + private HiveConf hiveConf; |
83 | 75 |
|
84 | 76 | public HiveTestService(Configuration hadoopConf) throws IOException { |
85 | 77 | this.workDir = Files.createTempDirectory(System.currentTimeMillis() + "-").toFile().getAbsolutePath(); |
86 | 78 | this.hadoopConf = hadoopConf; |
87 | 79 | } |
88 | 80 |
|
89 | | - public Configuration getHadoopConf() { |
90 | | - return hadoopConf; |
91 | | - } |
92 | | - |
93 | | - public TServer getHiveMetaStore() { |
94 | | - return tServer; |
95 | | - } |
96 | | - |
97 | | - public HiveConf getServerConf() { |
98 | | - return serverConf; |
99 | | - } |
100 | | - |
101 | 81 | public HiveServer2 start() throws IOException { |
102 | 82 | Objects.requireNonNull(workDir, "The work dir must be set before starting cluster."); |
103 | 83 |
|
104 | | - if (hadoopConf == null) { |
105 | | - hadoopConf = HoodieTestUtils.getDefaultHadoopConf(); |
106 | | - } |
107 | | - |
108 | 84 | String localHiveLocation = getHiveLocation(workDir); |
109 | | - if (clean) { |
110 | | - LOG.info("Cleaning Hive cluster data at: " + localHiveLocation + " and starting fresh."); |
111 | | - File file = new File(localHiveLocation); |
112 | | - FileIOUtils.deleteDirectory(file); |
113 | | - } |
| 85 | + LOG.info("Cleaning Hive cluster data at: " + localHiveLocation + " and starting fresh."); |
| 86 | + File file = new File(localHiveLocation); |
| 87 | + FileIOUtils.deleteDirectory(file); |
114 | 88 |
|
115 | | - serverConf = configureHive(hadoopConf, localHiveLocation); |
| 89 | + hiveConf = configureHive(hadoopConf, localHiveLocation); |
116 | 90 |
|
117 | 91 | executorService = Executors.newSingleThreadExecutor(); |
118 | | - tServer = startMetaStore(bindIP, serverConf); |
| 92 | + tServer = startMetaStore(hiveConf); |
119 | 93 |
|
120 | | - serverConf.set("hive.in.test", "true"); |
121 | | - hiveServer = startHiveServer(serverConf); |
| 94 | + hiveServer = startHiveServer(hiveConf); |
122 | 95 |
|
123 | | - String serverHostname; |
124 | | - if (bindIP.equals("0.0.0.0")) { |
125 | | - serverHostname = "localhost"; |
126 | | - } else { |
127 | | - serverHostname = bindIP; |
128 | | - } |
129 | | - if (!waitForServerUp(serverConf, serverHostname, CONNECTION_TIMEOUT)) { |
| 96 | + if (!waitForServerUp(hiveConf)) { |
130 | 97 | throw new IOException("Waiting for startup of standalone server"); |
131 | 98 | } |
132 | 99 |
|
@@ -156,76 +123,72 @@ public void stop() { |
156 | 123 | LOG.info("Hive Minicluster service shut down."); |
157 | 124 | tServer = null; |
158 | 125 | hiveServer = null; |
159 | | - hadoopConf = null; |
| 126 | + } |
| 127 | + |
| 128 | + public TServer getHiveMetaStore() { |
| 129 | + return tServer; |
160 | 130 | } |
161 | 131 |
|
162 | 132 | public HiveServer2 getHiveServer() { |
163 | 133 | return hiveServer; |
164 | 134 | } |
165 | 135 |
|
| 136 | + public HiveConf getHiveConf() { |
| 137 | + return hiveConf; |
| 138 | + } |
| 139 | + |
166 | 140 | public int getHiveServerPort() { |
167 | | - return serverPort; |
| 141 | + return hiveConf.getIntVar(ConfVars.HIVE_SERVER2_THRIFT_PORT); |
168 | 142 | } |
169 | 143 |
|
170 | 144 | public String getJdbcHive2Url() { |
171 | | - return String.format("jdbc:hive2://%s:%s/default", bindIP, serverPort); |
| 145 | + return String.format("jdbc:hive2://%s:%s/default", |
| 146 | + hiveConf.getVar(ConfVars.HIVE_SERVER2_THRIFT_BIND_HOST), hiveConf.getIntVar(ConfVars.HIVE_SERVER2_THRIFT_PORT)); |
172 | 147 | } |
173 | 148 |
|
174 | | - public HiveConf configureHive(Configuration conf, String localHiveLocation) throws IOException { |
175 | | - conf.set("hive.metastore.local", "false"); |
176 | | - int port = metastorePort; |
177 | | - if (conf.get(HiveConf.ConfVars.METASTORE_SERVER_PORT.varname, null) == null) { |
178 | | - conf.setInt(ConfVars.METASTORE_SERVER_PORT.varname, metastorePort); |
179 | | - } else { |
180 | | - port = conf.getInt(ConfVars.METASTORE_SERVER_PORT.varname, metastorePort); |
181 | | - } |
182 | | - if (conf.get(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_PORT.varname, null) == null) { |
183 | | - conf.setInt(ConfVars.HIVE_SERVER2_THRIFT_PORT.varname, serverPort); |
184 | | - } |
185 | | - conf.set(HiveConf.ConfVars.METASTOREURIS.varname, "thrift://" + bindIP + ":" + port); |
186 | | - conf.set(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_BIND_HOST.varname, bindIP); |
187 | | - // The following line to turn of SASL has no effect since HiveAuthFactory calls |
188 | | - // 'new HiveConf()'. This is fixed by https://issues.apache.org/jira/browse/HIVE-6657, |
189 | | - // in Hive 0.14. |
190 | | - // As a workaround, the property is set in hive-site.xml in this module. |
191 | | - // conf.set(HiveConf.ConfVars.HIVE_SERVER2_AUTHENTICATION.varname, "NOSASL"); |
| 149 | + public HiveConf configureHive(Configuration hadoopConf, String localHiveLocation) throws IOException { |
| 150 | + hadoopConf.set("hive.metastore.local", "true"); |
| 151 | + hadoopConf.set("datanucleus.schema.autoCreateTables", "true"); |
| 152 | + hadoopConf.set("datanucleus.autoCreateSchema", "true"); |
| 153 | + hadoopConf.set("datanucleus.fixedDatastore", "false"); |
| 154 | + HiveConf conf = new HiveConf(hadoopConf, HiveConf.class); |
| 155 | + conf.setBoolVar(ConfVars.HIVE_IN_TEST, true); |
| 156 | + conf.setBoolVar(ConfVars.METASTORE_SCHEMA_VERIFICATION, false); |
| 157 | + final int metastoreServerPort = NetworkTestUtils.nextFreePort(); |
| 158 | + conf.setIntVar(ConfVars.METASTORE_SERVER_PORT, metastoreServerPort); |
| 159 | + conf.setVar(ConfVars.METASTOREURIS, "thrift://" + BIND_HOST + ":" + metastoreServerPort); |
| 160 | + conf.setVar(ConfVars.HIVE_SERVER2_THRIFT_BIND_HOST, BIND_HOST); |
192 | 161 | File localHiveDir = new File(localHiveLocation); |
193 | 162 | localHiveDir.mkdirs(); |
194 | 163 | File metastoreDbDir = new File(localHiveDir, "metastore_db"); |
195 | | - conf.set(HiveConf.ConfVars.METASTORECONNECTURLKEY.varname, |
196 | | - "jdbc:derby:" + metastoreDbDir.getPath() + ";create=true"); |
| 164 | + conf.setVar(ConfVars.METASTORECONNECTURLKEY, "jdbc:derby:" + metastoreDbDir.getPath() + ";create=true"); |
197 | 165 | File derbyLogFile = new File(localHiveDir, "derby.log"); |
198 | 166 | derbyLogFile.createNewFile(); |
199 | 167 | setSystemProperty("derby.stream.error.file", derbyLogFile.getPath()); |
200 | 168 | setSystemProperty("derby.system.home", localHiveDir.getAbsolutePath()); |
201 | | - conf.set(HiveConf.ConfVars.METASTOREWAREHOUSE.varname, |
202 | | - Files.createTempDirectory(System.currentTimeMillis() + "-").toFile().getAbsolutePath()); |
203 | | - conf.set("datanucleus.schema.autoCreateTables", "true"); |
204 | | - conf.set("hive.metastore.schema.verification", "false"); |
205 | | - conf.set("datanucleus.autoCreateSchema", "true"); |
206 | | - conf.set("datanucleus.fixedDatastore", "false"); |
207 | | - setSystemProperty("derby.stream.error.file", derbyLogFile.getPath()); |
| 169 | + File metastoreWarehouseDir = new File(localHiveDir, "warehouse"); |
| 170 | + metastoreWarehouseDir.mkdir(); |
| 171 | + conf.setVar(ConfVars.METASTOREWAREHOUSE, metastoreWarehouseDir.getAbsolutePath()); |
208 | 172 |
|
209 | | - return new HiveConf(conf, this.getClass()); |
| 173 | + return conf; |
210 | 174 | } |
211 | 175 |
|
212 | | - private boolean waitForServerUp(HiveConf serverConf, String hostname, int timeout) { |
213 | | - long start = System.currentTimeMillis(); |
214 | | - int port = serverConf.getIntVar(HiveConf.ConfVars.METASTORE_SERVER_PORT); |
| 176 | + private boolean waitForServerUp(HiveConf serverConf) { |
| 177 | + LOG.info("waiting for " + serverConf.getVar(ConfVars.METASTOREURIS)); |
| 178 | + final long start = System.currentTimeMillis(); |
215 | 179 | while (true) { |
216 | 180 | try { |
217 | 181 | new HiveMetaStoreClient(serverConf); |
218 | 182 | return true; |
219 | 183 | } catch (MetaException e) { |
220 | 184 | // ignore as this is expected |
221 | | - LOG.info("server " + hostname + ":" + port + " not up " + e); |
222 | 185 | } |
223 | 186 |
|
224 | | - if (System.currentTimeMillis() > start + timeout) { |
| 187 | + if (System.currentTimeMillis() > start + CONNECTION_TIMEOUT_MS) { |
225 | 188 | break; |
226 | 189 | } |
227 | 190 | try { |
228 | | - Thread.sleep(250); |
| 191 | + Thread.sleep(CONNECTION_TIMEOUT_MS / 10); |
229 | 192 | } catch (InterruptedException e) { |
230 | 193 | // ignore |
231 | 194 | } |
@@ -307,36 +270,31 @@ protected TSocket acceptImpl() throws TTransportException { |
307 | 270 | } |
308 | 271 | } |
309 | 272 |
|
310 | | - public TServer startMetaStore(String forceBindIP, HiveConf conf) throws IOException { |
| 273 | + private TServer startMetaStore(HiveConf conf) throws IOException { |
311 | 274 | try { |
312 | 275 | // Server will create new threads up to max as necessary. After an idle |
313 | 276 | // period, it will destory threads to keep the number of threads in the |
314 | 277 | // pool to min. |
315 | | - int port = conf.getIntVar(HiveConf.ConfVars.METASTORE_SERVER_PORT); |
316 | | - int minWorkerThreads = conf.getIntVar(HiveConf.ConfVars.METASTORESERVERMINTHREADS); |
317 | | - int maxWorkerThreads = conf.getIntVar(HiveConf.ConfVars.METASTORESERVERMAXTHREADS); |
318 | | - boolean tcpKeepAlive = conf.getBoolVar(HiveConf.ConfVars.METASTORE_TCP_KEEP_ALIVE); |
319 | | - boolean useFramedTransport = conf.getBoolVar(HiveConf.ConfVars.METASTORE_USE_THRIFT_FRAMED_TRANSPORT); |
| 278 | + String host = conf.getVar(ConfVars.HIVE_SERVER2_THRIFT_BIND_HOST); |
| 279 | + int port = conf.getIntVar(ConfVars.METASTORE_SERVER_PORT); |
| 280 | + int minWorkerThreads = conf.getIntVar(ConfVars.METASTORESERVERMINTHREADS); |
| 281 | + int maxWorkerThreads = conf.getIntVar(ConfVars.METASTORESERVERMAXTHREADS); |
| 282 | + boolean tcpKeepAlive = conf.getBoolVar(ConfVars.METASTORE_TCP_KEEP_ALIVE); |
| 283 | + boolean useFramedTransport = conf.getBoolVar(ConfVars.METASTORE_USE_THRIFT_FRAMED_TRANSPORT); |
320 | 284 |
|
321 | 285 | // don't support SASL yet |
322 | | - // boolean useSasl = conf.getBoolVar(HiveConf.ConfVars.METASTORE_USE_THRIFT_SASL); |
| 286 | + // boolean useSasl = conf.getBoolVar(ConfVars.METASTORE_USE_THRIFT_SASL); |
323 | 287 |
|
324 | | - TServerTransport serverTransport; |
325 | | - if (forceBindIP != null) { |
326 | | - InetSocketAddress address = new InetSocketAddress(forceBindIP, port); |
327 | | - serverTransport = tcpKeepAlive ? new TServerSocketKeepAlive(address) : new TServerSocket(address); |
328 | | - |
329 | | - } else { |
330 | | - serverTransport = tcpKeepAlive ? new TServerSocketKeepAlive(port) : new TServerSocket(port); |
331 | | - } |
| 288 | + InetSocketAddress address = new InetSocketAddress(host, port); |
| 289 | + TServerTransport serverTransport = tcpKeepAlive ? new TServerSocketKeepAlive(address) : new TServerSocket(address); |
332 | 290 |
|
333 | 291 | TProcessor processor; |
334 | 292 | TTransportFactory transFactory; |
335 | 293 |
|
336 | 294 | HiveMetaStore.HMSHandler baseHandler = new HiveMetaStore.HMSHandler("new db based metaserver", conf, false); |
337 | 295 | IHMSHandler handler = RetryingHMSHandler.getProxy(conf, baseHandler, true); |
338 | 296 |
|
339 | | - if (conf.getBoolVar(HiveConf.ConfVars.METASTORE_EXECUTE_SET_UGI)) { |
| 297 | + if (conf.getBoolVar(ConfVars.METASTORE_EXECUTE_SET_UGI)) { |
340 | 298 | transFactory = useFramedTransport |
341 | 299 | ? new ChainedTTransportFactory(new TFramedTransport.Factory(), new TUGIContainingTransport.Factory()) |
342 | 300 | : new TUGIContainingTransport.Factory(); |
|
0 commit comments