-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added PUT method in /v1/info/state for transitioning the server to th…
…e SHUTING_DOWN state for graceful shutdown
- Loading branch information
1 parent
b036356
commit 32f1458
Showing
4 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...tion/src/test/java/com/facebook/presto/nativeworker/TestPrestoNativeGracefulShutdown.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.facebook.presto.nativeworker; | ||
|
||
import com.facebook.presto.testing.QueryRunner; | ||
import com.facebook.presto.tests.DistributedQueryRunner; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.OutputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URI; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
|
||
public class TestPrestoNativeGracefulShutdown | ||
{ | ||
@Test | ||
public void testGracefulShutdown() throws Exception | ||
{ | ||
QueryRunner queryRunner = createQueryRunner(); | ||
String baseUri = getServerUri(queryRunner); | ||
int responseCode = sendShutdownRequest(baseUri, "\"SHUTTING_DOWN\""); | ||
assertEquals(responseCode, 200, "Expected a 200 OK response for valid shutdown request"); | ||
queryRunner.close(); | ||
} | ||
|
||
@Test | ||
public void testInvalidShutdownRequest() throws Exception | ||
{ | ||
QueryRunner queryRunner = createQueryRunner(); | ||
String baseUri = getServerUri(queryRunner); | ||
int responseCode = sendShutdownRequest(baseUri, "\"INVALID_SHUTDOWN\""); | ||
assertEquals(responseCode, 400, "Expected a 400 Bad Request response for invalid body"); | ||
queryRunner.close(); | ||
} | ||
|
||
@Test | ||
public void testEmptyBodyShutdownRequest() throws Exception | ||
{ | ||
QueryRunner queryRunner = createQueryRunner(); | ||
String baseUri = getServerUri(queryRunner); | ||
int responseCode = sendShutdownRequest(baseUri, ""); | ||
assertEquals(responseCode, 400, "Expected a 400 Bad Request response for empty body"); | ||
queryRunner.close(); | ||
} | ||
|
||
public static int sendShutdownRequest(String shutdownUrl, String body) | ||
{ | ||
try { | ||
URL url = new URL(shutdownUrl); | ||
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | ||
connection.setRequestMethod("PUT"); | ||
connection.setDoOutput(true); | ||
connection.setRequestProperty("Content-Type", "application/json"); | ||
|
||
try (OutputStream os = connection.getOutputStream()) { | ||
byte[] input = body.getBytes(StandardCharsets.UTF_8); | ||
os.write(input, 0, input.length); | ||
} | ||
return connection.getResponseCode(); | ||
} | ||
catch (Exception e) { | ||
e.printStackTrace(); | ||
return 500; | ||
} | ||
} | ||
|
||
private QueryRunner createQueryRunner() throws Exception | ||
{ | ||
return PrestoNativeQueryRunnerUtils.createNativeQueryRunner(true); | ||
} | ||
|
||
private String getServerUri(QueryRunner queryRunner) | ||
{ | ||
DistributedQueryRunner distributedQueryRunner = (DistributedQueryRunner) queryRunner; | ||
URI coordinatorUri = distributedQueryRunner.getCoordinator().getBaseUrl(); | ||
return coordinatorUri.toString() + "/v1/info/state"; | ||
} | ||
} |