Skip to content

Commit 8e01971

Browse files
committed
TestZookeeperExternalSessionsRegistryClient
1 parent ef15cf6 commit 8e01971

1 file changed

Lines changed: 140 additions & 0 deletions

File tree

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.hadoop.hive.ql.exec.tez;
20+
21+
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertNotNull;
23+
import static org.junit.Assert.assertTrue;
24+
25+
import org.apache.curator.framework.CuratorFramework;
26+
import org.apache.curator.framework.CuratorFrameworkFactory;
27+
import org.apache.curator.retry.RetryOneTime;
28+
import org.apache.curator.test.TestingServer;
29+
import org.apache.hadoop.hive.conf.HiveConf;
30+
import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
31+
import org.junit.Test;
32+
33+
/**
34+
* Tests for {@link ZookeeperExternalSessionsRegistryClient}.
35+
*/
36+
public class TestZookeeperExternalSessionsRegistryClient {
37+
38+
/**
39+
* Integration-style unit test that ensures {@link ZookeeperExternalSessionsRegistryClient}
40+
* can discover sessions from ZooKeeper and hand them out via {@link ExternalSessionsRegistry#getSession()}.
41+
*/
42+
@Test
43+
public void testGetAndReturnSession() throws Exception {
44+
TestingServer server = null;
45+
CuratorFramework client = null;
46+
ZookeeperExternalSessionsRegistryClient registry = null;
47+
try {
48+
server = new TestingServer();
49+
String connectString = server.getConnectString();
50+
51+
HiveConf conf = new HiveConf();
52+
conf.setVar(ConfVars.HIVE_ZOOKEEPER_QUORUM, connectString);
53+
conf.setVar(ConfVars.HIVE_SERVER2_TEZ_EXTERNAL_SESSIONS_NAMESPACE, "/tez_ns");
54+
conf.setIntVar(ConfVars.HIVE_SERVER2_TEZ_EXTERNAL_SESSIONS_WAIT_MAX_ATTEMPTS, 1);
55+
56+
String namespace = HiveConf.getVar(conf, ConfVars.HIVE_SERVER2_TEZ_EXTERNAL_SESSIONS_NAMESPACE);
57+
String effectivePath = namespace + "/tez_am/server";
58+
59+
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
60+
client = builder.connectString(connectString).retryPolicy(new RetryOneTime(1)).build();
61+
client.start();
62+
63+
client.create().creatingParentsIfNeeded().forPath(effectivePath + "/app_1");
64+
client.create().forPath(effectivePath + "/app_2");
65+
66+
registry = new ZookeeperExternalSessionsRegistryClient(conf);
67+
68+
String first = registry.getSession();
69+
assertTrue(first.equals("app_1") || first.equals("app_2"));
70+
71+
registry.returnSession(first);
72+
String second = registry.getSession();
73+
assertNotNull(second);
74+
assertTrue(second.equals("app_1") || second.equals("app_2"));
75+
} finally {
76+
if (registry != null) {
77+
registry.close();
78+
}
79+
if (client != null) {
80+
client.close();
81+
}
82+
if (server != null) {
83+
server.close();
84+
}
85+
}
86+
}
87+
88+
/**
89+
* Verifies that the same external session ID can be obtained and returned
90+
* multiple times in a row from {@link ZookeeperExternalSessionsRegistryClient}.
91+
*/
92+
@Test
93+
public void testReuseSameSession() throws Exception {
94+
TestingServer server = null;
95+
CuratorFramework client = null;
96+
ZookeeperExternalSessionsRegistryClient registry = null;
97+
try {
98+
server = new TestingServer();
99+
String connectString = server.getConnectString();
100+
101+
HiveConf conf = new HiveConf();
102+
conf.setVar(ConfVars.HIVE_ZOOKEEPER_QUORUM, connectString);
103+
conf.setVar(ConfVars.HIVE_SERVER2_TEZ_EXTERNAL_SESSIONS_NAMESPACE, "/tez_ns_reuse");
104+
conf.setIntVar(ConfVars.HIVE_SERVER2_TEZ_EXTERNAL_SESSIONS_WAIT_MAX_ATTEMPTS, 1);
105+
106+
String namespace = HiveConf.getVar(conf, ConfVars.HIVE_SERVER2_TEZ_EXTERNAL_SESSIONS_NAMESPACE);
107+
String effectivePath = namespace + "/tez_am/server";
108+
109+
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
110+
client = builder.connectString(connectString).retryPolicy(new RetryOneTime(1)).build();
111+
client.start();
112+
113+
client.create().creatingParentsIfNeeded().forPath(effectivePath + "/app_reuse");
114+
115+
registry = new ZookeeperExternalSessionsRegistryClient(conf);
116+
117+
String first = registry.getSession();
118+
assertEquals("app_reuse", first);
119+
120+
registry.returnSession(first);
121+
String second = registry.getSession();
122+
assertEquals("app_reuse", second);
123+
124+
registry.returnSession(second);
125+
String third = registry.getSession();
126+
assertEquals("app_reuse", third);
127+
} finally {
128+
if (registry != null) {
129+
registry.close();
130+
}
131+
if (client != null) {
132+
client.close();
133+
}
134+
if (server != null) {
135+
server.close();
136+
}
137+
}
138+
}
139+
}
140+

0 commit comments

Comments
 (0)