클라우드 컴퓨팅 & NoSQL/Vert.x & Node.js

Vert.x Note - Verticle & instance and Thread

Terry Cho 2014. 1. 25. 23:37

Vert.x 기본 개념 잡기


1. Verticle

- The package of coe that Vert.x executes

(Java,JavaScript,Python,Groovy 등 여러가지 언어로 작성될 수 있음)

- Verticle은 기본적으로 Non Blocking으로 작동함

- Blocking으로 작동하는 Verticle은 Worker Verticle을 사용함. 


2. Module

- Set of Verticla

- Application 은 1개 이상의 Module로 구성되고, Module은 1개 이상의 Verticle로 구성됨


Vertx의 실행단위는 Verticle 또는 Module



3. Vert.x instance

- Verticle은 instance 내에서 동작하는데, 하나의 instance 내에서는 여러개의 Verticle을 수행할 수 있다.

- instance 간에는 Event Bus를 이용해서 통신 한다.  마치 RPC 와 유사한 개념인데. Tuxedo의 BBL과 컨셉이 비슷하다. 구현체는  HazleCast를 이용한ㄷ.



Golden Rule

- 이벤트 루프 방식을 이용한다.

- 절대 Event Loop를 Blcok 하면 안된다.

그래서 오래 걸리는 작업 등은 Worker Verticle을 사용한다.



전체적인 개념을 보면 Event Loop에서 들어오는 메세지를 빨리빨리 처리하고, DB나 Q작업 등은 뒷단의 Worker Verticle등에서 비동기로 처리 하는 식을 사용한다.

Like standard verticles, worker verticles are never executed concurrently by more than one thread, but unlike standard verticles they can be executed by different threads at different times - whereas a standard verticle is always executed by the exact same thread.




Thread.sleep()

Object.wait()

CountDownLatch.await() or any other blocking operating from java.util.concurrent.

Spinning in a loop

Executing a long-lived computationally intensive operation - number crunching.

Calling a blocking third party library operation that might take some time to complete (e.g. executing a JDBC query)



Vertx내부의 Thread Model

- main thread (1개)

- vert.x-eventloop-thread (N개:Core 수에 따라 자동 생성됨)

- vert.x-worker-thread (Blocking call 수행)


※ 1개의 Verticle 인스턴스가 생성될 때 Event Loops에서 하나의 eventloop-thread 가 할당됨.


예) 

vertx-eventloop-thread-0

 Verticle-0

 Verticle-1

 Verticle-3

vertx-eventloop-thread-1

 Verticle-4

 Verticle-5

 Verticle-9


 Verticle 인스턴스당  쓰레드 한개 사용 (그 순간에만) 나머지는 큐잉됨.

Veticle이 100개라도 하나의 event-loop에서 수행 가능



※ 동기 Blocking call은 안된다. Single Thread이다. 

즉 Single Thread이기 때문에, 하나의 요청이 blocking이면 다른 Request를 처리못한다. 

Read tx나 long running tx가 어떻게 처리되는지 알아볼 필요 있음

--> 다른 Thread (worker)등에 bus로 call해서 넘기고, 끝나면 call-back으로 리턴한다.

예제 코드 " vertx.eventBus().send("mongodb-persistor", json, new ReplyHandler(req, data));"


http://www.smartjava.org/content/create-simpe-restful-service-vertx-20-rxjava-and-mongodb


동기형 read call에는 별로 안맞는듯. Worker verticlle을 이용해서 구현은 가능. 이 경우는 그냥 tomcat이 났겠다.

Q에 쓰는 async write  시나리오는 딱인듯.



그리드형