You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
shutDownGracefully 는 사용하는 내장 웹 서버 구현체에 따라 동작 방식이 다르고, 아래는 Tomcat 의 코드이다.
스레드를 하나 생성하고, CountDownLatch와 함께 doShutdown() 를 실행한다.
publicvoidshutDownGracefully(GracefulShutdownCallbackcallback) {
logger.info("Commencing graceful shutdown. Waiting for active requests to complete");
CountDownLatchshutdownUnderway = newCountDownLatch(1);
newThread(() -> doShutdown(callback, shutdownUnderway), "tomcat-shutdown").start();
try {
shutdownUnderway.await();
}
catch (InterruptedExceptionex) {
Thread.currentThread().interrupt();
}
}
doShutdown() 안에서는 Connectors 들을 모두 Close() 하고 넘겨 받은 CountDownLatch를 1 낮추는데, 초기화된 Count 값이 1이었기에 이로써 doShutdown()를 비동기 호출한 shutDownGracefully()는 종료될 수 있다.
이로써 Tomcat 의 shutDownGracefully 는 Connectors 의 종료까지는 대기하되, 그 이후 작업들은 비동기로 남겨둘 수 있게 된다.
privatevoiddoShutdown(GracefulShutdownCallbackcallback, CountDownLatchshutdownUnderway) {
try {
List<Connector> connectors = getConnectors();
connectors.forEach(this::close);
shutdownUnderway.countDown();
awaitInactiveOrAborted();
if (this.aborted) {
logger.info("Graceful shutdown aborted with one or more requests still active");
callback.shutdownComplete(GracefulShutdownResult.REQUESTS_ACTIVE);
}
else {
logger.info("Graceful shutdown complete");
callback.shutdownComplete(GracefulShutdownResult.IDLE);
}
}
finally {
shutdownUnderway.countDown();
}
}
Connector 들을 종료 후 처리 중인 요청이 더 없는지를 50ms 마다 확인하며 요청 처리가 마무리 되길 대기한다.
그리고 모두 정상 처리 또는 Abort 되었음을 확인하면 맨 처음 전달받은 callback 을 통해 Shutdown 결과를 알린다.
privatevoidawaitInactiveOrAborted() {
try {
for (Containerhost : this.tomcat.getEngine().findChildren()) {
for (Containercontext : host.findChildren()) {
while (!this.aborted && isActive(context)) {
Thread.sleep(50);
}
}
}
}
catch (InterruptedExceptionex) {
Thread.currentThread().interrupt();
}
}
커넥션들 종료는 블록킹 (CountDownLatch - await) 로 처리 후 반환하고,
남은 요청 처리 대기는 비동기로 작업 후에 callback 으로 결과를 알리는 패턴이 재밌어서 기록해본다.
The text was updated successfully, but these errors were encountered:
k8s Rolling update 무중단 배포
배포 Down time
문제 여지 1 : Container 의 삭제보다 IpTable update가 더 느린 경우
문제 여지 2 : 요청이 처리되는 중에 Container가 종료되는 경우
문제 여지 3 : 컨테이너에서 요청 처리가 준비되지 않았는데 Routing rule 에 추가되는 경우
테스트
내장 톰캣 Graceful shutdown 동작 원리
The text was updated successfully, but these errors were encountered: