traffic server设计了一个基于事件驱动的多线程模型,通过EventSystem+Continuation完成了所有函数的异步调度。下图摘自apache提供的traffic server开发文档,给出了事件模型的结构图。
EventSystem=EventProcessor+Event+Ethread。EventProcessor负责创建一个(组)线程Ethread,并分配给该(组)线程一个事件类型EventType。
Continuation是一个被动的由事件驱动的状态机。一个Continuation执行的是一个逻辑上相对完整的功能。
EventProcessor通过指定具体的EventType调度某个Continuation,根据EventType与Continuation构造一个Event,通过Event触发一个正确的Ethread进行异步调度执行该Continuation。
TS启动时候已经创建了一些处理多种事件类型的Ethread,如专用于网络处理的线程,对应事件类型为ET_NET。默认事件类型为ET_CALL。目前,最新的ts2.1.5版本在启动时新增了一组事件类型为ET_TASK的线程,这组线程专门用于插件开发以及后台任务执行等作用。
有三种方式调度一个Continuation:在将来的某个时间调度,在距当前某个时间以后调度,以及每隔一个时间段就执行一次Continuation,在EventProcessor中对应的三个函数为schedule_at,schedule_in, schedule_every。上图中将Continuation以sleeping作为修饰,意指Continuation需要Event事件唤醒执行。
最后给出trafficserver的sdk开发文档中对异步事件模型的描述:
- Traffic Server is a multi-threaded process. There are two main reasons why a server might use multiple threads:
- To take advantage of the concurrency available with multiple CPUs and multiple I/O devices.
- To manage concurrency from having many simultaneous client connections. For example, a server could create one thread for each connection, allowing the operating system (OS) to control switching between threads.
- Traffic Server uses multiple threads for the first reason. However, Traffic Server does not use a separate OS thread per transaction because it would not be efficient when handling thousands of simultaneous connections.
- Instead, Traffic Server provides special event-driven mechanisms for efficiently scheduling work: the event system and continuations. The event system is used to schedule work to be done on threads. A continuation is a passive, event-driven state machine that can do some work until it reaches a waiting point; it then sleeps until it receives notification that conditions are right for doing more work. For example, HTTP state machines (which handle HTTP transactions) are implemented as continuations.
- Continuation objects are used throughout Traffic Server. Some might live for the duration of the Traffic Server process, while others are created (perhaps by other continuations) for specific needs and then destroyed. Traffic Server Internals (below) shows how the major components of Traffic Server interact. Traffic Server has several processors, such as cache processor and net processor, that consolidate cache or network I/O tasks. Processors talk to the event system and schedule work on threads. An executing thread calls back a continuation by sending it an event. When a continuation receives an event, it wakes up, does some work, and either destroys itself or goes back to sleep & waits for the next event.
阅读(5869) | 评论(0) | 转发(2) |