엔터프라이즈 솔루션/BEA WebLogic

Non Blocking Sync Call

Terry Cho 2008. 10. 29. 19:48
WebLogic에서 간만에 재미있는 기능을 찾아서 포스팅.
ESB나 EAI 제품과 같은 아키텍쳐에서 Sync Call의 구조를 보면 다음과 같다.
Request가 들어오면 특정 Thread가 그 request를 받아서 Backend의 Component에 보내고, 응답을 기다린다.
응답이 오면 Client에게 그 응답을 전달하는 구조이다.
그런데 이 경우 Client에서 요청을 받은 후 그 요청을 Component에 전달하고 나면 Component로 부터 응답이 오기까지는 Request Thread는 아무것도 하지 않고 단지 기다리기만 한다. 즉 하는 일없이 Thread만 점유하고 있는 것이다.
만약에 Component에서의 처리시간이 오래 걸리게 되면 Request Thread가 held 되는 시간이 늘어나게 되고 이런 요청이 많을 수록, 시스템 내의 Idle Thread는 기하급수적으로 줄어들게 되고, 이는 Thread 부족으로 인한 Slow down이나 hang현상을 유발하게 된다.

이를 해결하기 위한 아키텍쳐가 Non-blocking Sync Mode인데
개념은 다음과 같다. 
Client에서 요청이 들어오면 그 요청을 Request Thread가 처리하여 Backend의 Component로 전달을 하고 Request Thread는 Idle 상태로 전환된다.
그리고 나서 Component로 부터 응답이 들어오면 그 응답을 Idle Thread중에 한 Thread가 응답을 받아서 Client에게 전달을 한다. (Response Thread)
이런 구조에서는 Component로 일단 요청이 전달된후 Component에서 응답이 올때까지는 Thread가 release되어 있기 때문에 Working Thread의 수를 줄일 수 있다.
Server <--> Component 단을 Async/Callback처럼 구현해놓은것인데 EAI나 ESB와 같은 중계 시스템에서는 매우 유용한 아키텍쳐가 될거 같다.

이를 구현한 구현체가 WebLogic에 Future Response model for HTTP 라는 모델이다. (아래 참조)
이 모델은 Backend에서 수행 시간이 오래 걸릴 경우나 비지니스 로직이 JMS등을 이용하는 것과 같은 Async 구조에서는 매우 유용하지만 일반적인 WebApplication에서는 오히려 유용하지 않다. (하나의 Request를 수행하는데 Thread를 두개나 사용하게 되고 어짜피 Response를 만들기 위해서는 비지니스 로직을 처리해야할 쓰레드가 필요하기 때문에..)

이러한 구조는 ALSB 3.0 내부에서 사용하고 있다니 장애 예방이나, 리소스 효율 면에서 상당히 뛰어날듯하다.
간만에 재미있는 아키텍쳐를 봤더니.. 뿌듯하네..

==

A Future Response Model for HTTP Servlets

In general, WebLogic Server processes incoming HTTP requests and the response is returned immediately to the client. Such connections are handled synchronously by the same thread. However, some HTTP requests may require longer processing time. Database connection, for example, may create longer response times. Handling these requests synchronously causes the thread to be held, waiting until the request is processed and the response sent.

To avoid this hung-thread scenario, WebLogic Server provides two classes that handle HTTP requests asynchronously by de-coupling the response from the thread that handles the incoming request. The following sections describe these classes.

Abstract Asynchronous Servlet

The Abstract Asynchronous Servlet enables you to handle incoming requests and servlet responses with different threads. This class explicitly provides a better general framework for handling the response than the Future Response Servlet, including thread handling.

You implement the Abstract Asynchronous Servlet by extending theweblogic.servlet.http.AbstractAsyncServlet.java class. This class provides the following abstract methods that you must override in your extended class.

doRequest

This method processes the servlet request. The following code example demonstrates how to override this method.

Listing 9-3 Overriding doRequest in AbstractAsynchServlet.java
public boolean doRequest(RequestResponseKey rrk) 
      throws ServletException, IOException {
      HttpServletRequest req = rrk.getRequest();
      HttpServletResponse res = rrk.getResponse();

      if (req.getParameter("immediate") != null) {
            res.setContentType("text/html");
            PrintWriter out = res.getWriter();
            out.println("Hello World Immediately!");
            return false ;
      }
      else {
            TimerManagerFactory.getTimerManagerFactory()
            .getDefaultTimerManager().schedule
            (new TimerListener() {
                  public void timerExpired(Timer timer)
                        {try {
                              AbstractAsyncServlet.notify(rrk, null);
                        }
                        catch (Exception e) {
                              e.printStackTrace();
                        }
                  }
            }, 2000);
      return true;
      }
}

doResponse

This method processes the servlet response.

Note: The servlet instance that processed the doRequest() method used to handle the original incoming request method will not necessarily be the one to process the doResponse() method.

If an exception occurs during processing, the container returns an error to the client. The following code example demonstrates how to override this method.

Listing 9-4 Overriding doResponse() in AbstractAsyncServlet.java
public void doResponse (RequestResponseKey rrk, Object context)
   throws ServletException, IOException
      {
      HttpServletRequest req = rrk.getRequest();
      HttpServletResponse res = rrk.getResponse();

      res.setContentType("text/html");
      PrintWriter out = res.getWriter();
      out.println("Hello World!");
}

doTimeOut

This method sends a servlet response error when the notify() method is not called within the timeout period.

Note: The servlet instance that processed the doRequest() method used to handle the original incoming request method will not necessarily be the one to process the doTimeOut() method.
Listing 9-5 Overriding doTimeOut() in AbstractAsyncServlet.java
public void doTimeout (RequestResponseKey rrk)
      throws ServletException, IOException
{
      HttpServletRequest req = rrk.getRequest();
      HttpServletResponse res = rrk.getResponse();

      res.setContentType("text/html");
      PrintWriter out = res.getWriter();
      out.println("Timeout!");
}

Future Response Servlet

Although Oracle recommends using the Abstract Asynchronous Servlet, you can also use the Future Response Servlet to handle servlet responses with a different thread than the one that handles the incoming request. You enable this servlet by extending weblogic.servlet.FutureResponseServlet.java, which gives you full control over how the response is handled and allows more control over thread handling. However, using this class to avoid hung threads requires you to provide most of the code.

The exact implementation depends on your needs, but you must override the service() method of this class at a minimum. The following example shows how you can override the service method.

Listing 9-6 Overriding the service() method of FutureResponseServlet.java
  public void service(HttpServletRequest req, FutureServletResponse rsp)
throws IOException, ServletException {
if(req.getParameter("immediate") != null){
PrintWriter out = rsp.getWriter();
out.println("Immediate response!");
rsp.send();
} else {
Timer myTimer = new Timer();
MyTimerTask mt = new MyTimerTask(rsp, myTimer);
myTimer.schedule(mt, 100);
}
}

private static class MyTimerTask extends TimerTask{
private FutureServletResponse rsp;
Timer timer;
MyTimerTask(FutureServletResponse rsp, Timer timer){
this.rsp = rsp;
this.timer = timer;
}
public void run(){
try{
PrintWriter out = rsp.getWriter();
out.println("Delayed Response");
rsp.send();
timer.cancel();
}
catch(IOException e){
e.printStackTrace();
}
}
}
그리드형