2121import com .facebook .drift .codec .ThriftCodecManager ;
2222import com .facebook .drift .transport .netty .codec .Protocol ;
2323import com .facebook .presto .server .testing .TestingPrestoServer ;
24+ import com .facebook .presto .spi .NodeLoadMetrics ;
2425import com .facebook .presto .spi .NodeState ;
26+ import com .facebook .presto .spi .NodeStats ;
2527import com .facebook .presto .tests .DistributedQueryRunner ;
2628import com .google .common .collect .ImmutableMap ;
2729import org .testng .annotations .AfterClass ;
28- import org .testng .annotations .AfterGroups ;
2930import org .testng .annotations .BeforeClass ;
3031import org .testng .annotations .BeforeGroups ;
3132import org .testng .annotations .DataProvider ;
4445import static com .facebook .presto .tests .tpch .TpchQueryRunner .createQueryRunnerWithNoClusterReadyCheck ;
4546import static org .testng .Assert .assertEquals ;
4647
47- @ Test (singleThreaded = true )
48+ @ Test (singleThreaded = true , groups = "TestServerInfoResource" )
4849public class TestServerInfoResource
4950{
5051 private HttpClient client ;
51- private DistributedQueryRunner queryRunner ;
52- private DistributedQueryRunner queryRunnerWithNoClusterReadyCheck ;
52+ private DistributedQueryRunner queryRunnerActive ;
53+ private DistributedQueryRunner queryRunnerInactiveResourceManagers ;
54+ private DistributedQueryRunner queryRunnerInactiveCoordinators ;
55+ private DistributedQueryRunner queryRunnerInactiveResourceGroup ;
5356 private ThriftCodecManager thriftCodeManager ;
5457
5558 @ BeforeClass
@@ -73,19 +76,36 @@ public void teardown()
7376 this .client = null ;
7477 }
7578
76- @ AfterGroups ( groups = { "createQueryRunner" , "getServerStateWithoutRequiredResourceManagers" , "getServerStateWithoutRequiredCoordinators" , "getServerStateWithoutRequiredCoordinators" , "createQueryRunnerWithNoClusterReadyCheckSkipLoadingResourceGroupConfigurationManager" } )
79+ @ AfterClass ( alwaysRun = true )
7780 public void serverTearDown ()
7881 {
79- for (TestingPrestoServer server : queryRunner .getServers ()) {
80- closeQuietly (server );
82+ if (queryRunnerActive != null ) {
83+ for (TestingPrestoServer server : queryRunnerActive .getServers ()) {
84+ closeQuietly (server );
85+ }
86+ }
87+ if (queryRunnerInactiveResourceManagers != null ) {
88+ for (TestingPrestoServer server : queryRunnerInactiveResourceManagers .getServers ()) {
89+ closeQuietly (server );
90+ }
91+ }
92+ if (queryRunnerInactiveCoordinators != null ) {
93+ for (TestingPrestoServer server : queryRunnerInactiveCoordinators .getServers ()) {
94+ closeQuietly (server );
95+ }
96+ }
97+ if (queryRunnerInactiveResourceGroup != null ) {
98+ for (TestingPrestoServer server : queryRunnerInactiveResourceGroup .getServers ()) {
99+ closeQuietly (server );
100+ }
81101 }
82102 }
83103
84104 @ BeforeGroups ("createQueryRunner" )
85105 public void createQueryRunnerSetup ()
86106 throws Exception
87107 {
88- queryRunner = createQueryRunner (
108+ queryRunnerActive = createQueryRunner (
89109 ImmutableMap .of (),
90110 ImmutableMap .of (),
91111 ImmutableMap .of (),
@@ -96,17 +116,27 @@ public void createQueryRunnerSetup()
96116 @ Test (timeOut = 30_000 , groups = {"createQueryRunner" }, dataProvider = "thriftEncodingToggle" )
97117 public void testGetServerStateWithRequiredResourceManagerCoordinators (boolean useThriftEncoding , Protocol thriftProtocol )
98118 {
99- TestingPrestoServer server = queryRunner .getCoordinator (0 );
119+ TestingPrestoServer server = queryRunnerActive .getCoordinator (0 );
100120 URI uri = uriBuilderFrom (server .getBaseUrl ().resolve ("/v1/info/state" )).build ();
101121 NodeState state = getNodeState (uri , useThriftEncoding , thriftProtocol );
102122 assertEquals (state , NodeState .ACTIVE );
103123 }
104124
125+ @ Test (timeOut = 30_000 , groups = {"createQueryRunner" }, dataProvider = "thriftEncodingToggle" , dependsOnMethods = "testGetServerStateWithRequiredResourceManagerCoordinators" )
126+ public void testGetServerStatsWithRequiredResourceManagerCoordinators (boolean useThriftEncoding , Protocol thriftProtocol )
127+ {
128+ TestingPrestoServer server = queryRunnerActive .getCoordinator (0 );
129+ URI uri = uriBuilderFrom (server .getBaseUrl ().resolve ("/v1/info/stats" )).build ();
130+ NodeStats stats = getNodeStats (uri , useThriftEncoding , thriftProtocol );
131+ assertEquals (stats .getNodeState (), NodeState .ACTIVE );
132+ assertEquals (stats .getLoadMetrics (), NodeLoadMetrics .NO_LOAD );
133+ }
134+
105135 @ BeforeGroups ("getServerStateWithoutRequiredResourceManagers" )
106136 public void createQueryRunnerWithNoClusterReadyCheckSetup ()
107137 throws Exception
108138 {
109- queryRunner = createQueryRunnerWithNoClusterReadyCheck (
139+ queryRunnerInactiveResourceManagers = createQueryRunnerWithNoClusterReadyCheck (
110140 ImmutableMap .of (),
111141 ImmutableMap .of (),
112142 ImmutableMap .of (),
@@ -117,17 +147,27 @@ public void createQueryRunnerWithNoClusterReadyCheckSetup()
117147 @ Test (timeOut = 30_000 , groups = {"getServerStateWithoutRequiredResourceManagers" }, dataProvider = "thriftEncodingToggle" )
118148 public void testGetServerStateWithoutRequiredResourceManagers (boolean useThriftEncoding , Protocol thriftProtocol )
119149 {
120- TestingPrestoServer server = queryRunner .getCoordinator (0 );
150+ TestingPrestoServer server = queryRunnerInactiveResourceManagers .getCoordinator (0 );
121151 URI uri = uriBuilderFrom (server .getBaseUrl ().resolve ("/v1/info/state" )).build ();
122152 NodeState state = getNodeState (uri , useThriftEncoding , thriftProtocol );
123153 assertEquals (state , NodeState .INACTIVE );
124154 }
125155
156+ @ Test (timeOut = 30_000 , groups = {"getServerStateWithoutRequiredResourceManagers" }, dataProvider = "thriftEncodingToggle" , dependsOnMethods = "testGetServerStateWithoutRequiredResourceManagers" )
157+ public void testGetServerStatsWithoutRequiredResourceManagers (boolean useThriftEncoding , Protocol thriftProtocol )
158+ {
159+ TestingPrestoServer server = queryRunnerInactiveResourceManagers .getCoordinator (0 );
160+ URI uri = uriBuilderFrom (server .getBaseUrl ().resolve ("/v1/info/stats" )).build ();
161+ NodeStats stats = getNodeStats (uri , useThriftEncoding , thriftProtocol );
162+ assertEquals (stats .getNodeState (), NodeState .INACTIVE );
163+ assertEquals (stats .getLoadMetrics (), NodeLoadMetrics .NO_LOAD );
164+ }
165+
126166 @ BeforeGroups ("getServerStateWithoutRequiredCoordinators" )
127167 public void getServerStateWithoutRequiredCoordinatorsSetup ()
128168 throws Exception
129169 {
130- queryRunner = createQueryRunnerWithNoClusterReadyCheck (
170+ queryRunnerInactiveCoordinators = createQueryRunnerWithNoClusterReadyCheck (
131171 ImmutableMap .of (),
132172 ImmutableMap .of (),
133173 ImmutableMap .of (),
@@ -138,18 +178,28 @@ public void getServerStateWithoutRequiredCoordinatorsSetup()
138178 @ Test (timeOut = 30_000 , groups = {"getServerStateWithoutRequiredCoordinators" }, dataProvider = "thriftEncodingToggle" )
139179 public void testGetServerStateWithoutRequiredCoordinators (boolean useThriftEncoding , Protocol thriftProtocol )
140180 {
141- TestingPrestoServer server = queryRunner .getCoordinator (0 );
181+ TestingPrestoServer server = queryRunnerInactiveCoordinators .getCoordinator (0 );
142182 URI uri = uriBuilderFrom (server .getBaseUrl ().resolve ("/v1/info/state" )).build ();
143183 NodeState state = getNodeState (uri , useThriftEncoding , thriftProtocol );
144184
145185 assertEquals (state , NodeState .INACTIVE );
146186 }
147187
188+ @ Test (timeOut = 30_000 , groups = {"getServerStateWithoutRequiredCoordinators" }, dataProvider = "thriftEncodingToggle" , dependsOnMethods = "testGetServerStateWithoutRequiredCoordinators" )
189+ public void testGetServerStatsWithoutRequiredCoordinators (boolean useThriftEncoding , Protocol thriftProtocol )
190+ {
191+ TestingPrestoServer server = queryRunnerInactiveCoordinators .getCoordinator (0 );
192+ URI uri = uriBuilderFrom (server .getBaseUrl ().resolve ("/v1/info/stats" )).build ();
193+ NodeStats stats = getNodeStats (uri , useThriftEncoding , thriftProtocol );
194+ assertEquals (stats .getNodeState (), NodeState .INACTIVE );
195+ assertEquals (stats .getLoadMetrics (), NodeLoadMetrics .NO_LOAD );
196+ }
197+
148198 @ BeforeGroups ("createQueryRunnerWithNoClusterReadyCheckSkipLoadingResourceGroupConfigurationManager" )
149199 public void createQueryRunnerWithNoClusterReadyCheckSkipLoadingResourceGroupConfigurationManager ()
150200 throws Exception
151201 {
152- queryRunner = createQueryRunnerWithNoClusterReadyCheck (
202+ queryRunnerInactiveResourceGroup = createQueryRunnerWithNoClusterReadyCheck (
153203 ImmutableMap .of (),
154204 ImmutableMap .of (),
155205 ImmutableMap .of (),
@@ -161,12 +211,22 @@ public void createQueryRunnerWithNoClusterReadyCheckSkipLoadingResourceGroupConf
161211 public void testGetServerStateWhenResourceGroupConfigurationManagerNotLoaded ()
162212 throws Exception
163213 {
164- TestingPrestoServer server = queryRunner .getCoordinator (0 );
214+ TestingPrestoServer server = queryRunnerInactiveResourceGroup .getCoordinator (0 );
165215 URI uri = uriBuilderFrom (server .getBaseUrl ().resolve ("/v1/info/state" )).build ();
166216 NodeState state = getNodeState (uri , false , null );
167217 assertEquals (state , NodeState .INACTIVE );
168218 }
169219
220+ @ Test (groups = {"createQueryRunnerWithNoClusterReadyCheckSkipLoadingResourceGroupConfigurationManager" }, dependsOnMethods = "testGetServerStateWhenResourceGroupConfigurationManagerNotLoaded" )
221+ public void testGetServerStatsWhenResourceGroupConfigurationManagerNotLoaded ()
222+ {
223+ TestingPrestoServer server = queryRunnerInactiveResourceGroup .getCoordinator (0 );
224+ URI uri = uriBuilderFrom (server .getBaseUrl ().resolve ("/v1/info/stats" )).build ();
225+ NodeStats stats = getNodeStats (uri , false , null );
226+ assertEquals (stats .getNodeState (), NodeState .INACTIVE );
227+ assertEquals (stats .getLoadMetrics (), NodeLoadMetrics .NO_LOAD );
228+ }
229+
170230 private NodeState getNodeState (URI uri , boolean useThriftEncoding , Protocol thriftProtocol )
171231 {
172232 Request .Builder requestBuilder = useThriftEncoding ? ThriftRequestUtils .prepareThriftGet (thriftProtocol ) : getJsonTransportBuilder (prepareGet ());
@@ -181,4 +241,19 @@ private NodeState getNodeState(URI uri, boolean useThriftEncoding, Protocol thri
181241 return client .execute (request , createJsonResponseHandler (jsonCodec (NodeState .class )));
182242 }
183243 }
244+
245+ private NodeStats getNodeStats (URI uri , boolean useThriftEncoding , Protocol thriftProtocol )
246+ {
247+ Request .Builder requestBuilder = useThriftEncoding ? ThriftRequestUtils .prepareThriftGet (thriftProtocol ) : getJsonTransportBuilder (prepareGet ());
248+ Request request = requestBuilder
249+ .setHeader (PRESTO_USER , "user" )
250+ .setUri (uri )
251+ .build ();
252+ if (useThriftEncoding ) {
253+ return client .execute (request , new ThriftResponseHandler <>(thriftCodeManager .getCodec (NodeStats .class ))).getValue ();
254+ }
255+ else {
256+ return client .execute (request , createJsonResponseHandler (jsonCodec (NodeStats .class )));
257+ }
258+ }
184259}
0 commit comments