-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: reject requests to new API server if server is not ready (#5048)
- Loading branch information
Showing
12 changed files
with
191 additions
and
10 deletions.
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
48 changes: 48 additions & 0 deletions
48
ksqldb-rest-app/src/main/java/io/confluent/ksql/api/server/ServerStateHandler.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,48 @@ | ||
/* | ||
* Copyright 2020 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.api.server; | ||
|
||
import io.confluent.ksql.rest.entity.KsqlErrorMessage; | ||
import io.confluent.ksql.rest.server.state.ServerState; | ||
import io.vertx.core.Handler; | ||
import io.vertx.ext.web.RoutingContext; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import javax.ws.rs.core.Response; | ||
import org.eclipse.jetty.http.HttpStatus; | ||
|
||
public class ServerStateHandler implements Handler<RoutingContext> { | ||
|
||
private final ServerState serverState; | ||
|
||
ServerStateHandler(final ServerState serverState) { | ||
this.serverState = Objects.requireNonNull(serverState, "serverState"); | ||
} | ||
|
||
@Override | ||
public void handle(final RoutingContext routingContext) { | ||
final Optional<Response> response = serverState.checkReady(); | ||
if (response.isPresent()) { | ||
final KsqlErrorMessage errorMsg = (KsqlErrorMessage) response.get().getEntity(); | ||
routingContext.fail( | ||
HttpStatus.SERVICE_UNAVAILABLE_503, | ||
new KsqlApiException(errorMsg.getMessage(), errorMsg.getErrorCode()) | ||
); | ||
} else { | ||
routingContext.next(); | ||
} | ||
} | ||
} |
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
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
68 changes: 68 additions & 0 deletions
68
ksqldb-rest-app/src/test/java/io/confluent/ksql/api/ServerStateTest.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,68 @@ | ||
/* | ||
* Copyright 2020 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.api; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import io.confluent.ksql.api.utils.QueryResponse; | ||
import io.confluent.ksql.rest.Errors; | ||
import io.confluent.ksql.rest.entity.KsqlErrorMessage; | ||
import io.vertx.core.buffer.Buffer; | ||
import io.vertx.ext.web.client.HttpResponse; | ||
import org.junit.Test; | ||
|
||
public class ServerStateTest extends BaseApiTest { | ||
|
||
private static final int INITIALIZING_ERROR_CODE = 50333; | ||
private static final String INITIALIZING_MESSAGE = "waiting for preconditions"; | ||
|
||
@Test | ||
public void shouldReturn503IfInitializing() throws Exception { | ||
// Given: | ||
this.serverState.setInitializingReason(new KsqlErrorMessage(INITIALIZING_ERROR_CODE, INITIALIZING_MESSAGE)); | ||
|
||
// When/Then: | ||
validateResponse(503, INITIALIZING_ERROR_CODE, INITIALIZING_MESSAGE); | ||
} | ||
|
||
@Test | ||
public void shouldReturn503IfTerminating() throws Exception { | ||
// Given: | ||
this.serverState.setTerminating(); | ||
|
||
// When/Then: | ||
validateResponse(503, Errors.ERROR_CODE_SERVER_SHUTTING_DOWN, "The server is shutting down"); | ||
} | ||
|
||
private void validateResponse( | ||
final int expectedStatus, | ||
final int expectedErrorCode, | ||
final String expectedMessage | ||
) throws Exception { | ||
// When: | ||
HttpResponse<Buffer> response = sendRequest( | ||
"/query-stream", | ||
DEFAULT_PUSH_QUERY_REQUEST_BODY.toBuffer() | ||
); | ||
|
||
// Then | ||
assertThat(response.statusCode(), is(expectedStatus)); | ||
|
||
QueryResponse queryResponse = new QueryResponse(response.bodyAsString()); | ||
validateError(expectedErrorCode, expectedMessage, queryResponse.responseObject); | ||
} | ||
} |
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