Several high-performance network model

This article first appeared: https://mp.weixin.qq.com/s/b3NSHCTB8Ji7kqipWK6IZA

Micro-channel public number: back-end technology Compass

Welcome attention to get the best reading experience

How to achieve high concurrency is a hot topic, different languages ​​and platforms have different processing means,

This paper describes several high-performance network model in the Linux platform C / C ++ language.
Through this article you can learn about the following:

  1. IO multiplexing
  2. Event handling model
  3. Several concurrency model
  4. Memcache network model


1.IO multiplexing
IO multiplexing is a mechanism that simply is the case of the arrival of IO events, read and write, and others have abnormal help you monitor the kernel, the application only needs to deal with those core inform your IO can handle ,

The premise of this mechanism is to establish even tens of thousands of connections, the number of read-write fd certain time only a small proportion of the total amount of connection,

Therefore, by means of a kernel event-driven mechanism, you can achieve tens of thousands of Socket manage single-threaded.

For chestnut:
a lot of large factory workshop, before each workshop must have a duty, per house per woker model, in most cases there is no abnormality of the workshop,

In order to ensure foolproof, however, still need a staff member. Later, the introduction of a monitoring alarm system, video, audio and other information are transmitted in real time to the control room,

Thus one or several staff members monitor the entire plant can be completed, greatly improving efficiency.
IO multiplexing is not to say, and there is no place too much complication,

Currently popular epoll is one of the Titans, as some detailed mechanisms epoll can not repeat the query itself.


2. Event processing model
network design model, how to deal with various I / O event is a very important part of it,

Reactor and Proactor two event handling model came into being,

Can use synchronous I / O Reactor Model implemented using asynchronous I / O model is achieved Proactor.

  • Reactor event handling model

Reactor model is a common model of synchronous I / O event handling, its core idea: to focus on the I / O events registered on the multiplexer,

Once I / O event is triggered, the event will be distributed to the event handler, the handler is ready to perform I / O events corresponding. Model has three important components:

  1. Multiplexer: an interface provided by the operating system, Linux offers the I / O is multiplexed select, poll, epoll Interface
  2. Event separator: Event Multiplexer will be ready to return to the event handler distribution
  3. Event processor: Processing ready event handler
    typical class diagram Reactor Model Structure:


Reactor class structure contains some major roles:

  1. Handle: mark the file descriptor
  2. Event Demultiplexer: the operating system kernel to achieve I / O multiplexing interface package waiting event occurrence
  3. Event Handler: event processing interface
  4. Event Handler A / B: application to achieve a specific event processing logic provided
  5. Reactor: Reactor defines an interface, and deleting registered event handlers attention, running event processing loop, waiting for a trigger event is ready, distribute the event to the registered callback function.
    Reactor model work to simplify the process:

  • Proactor event handling model

The difference is that with the Reactor, Proactor use asynchronous I / O system interface managed I / O operations to the operating system,

Proactor model distribution process asynchronous I / O completion event, and calls the corresponding event processing interface to handle the business logic.

Proactor class structure as shown:


Proactor class structure comprising the following roles:

  1. Handle: socket handle;
  2. Async Operation Processor: a processor to perform the asynchronous operation asynchronous operation, typically implemented by the kernel
  3. Async Operation: asynchronous operation
  4. Completion Event Queue: the completion of the event queue
  5. Proactor: Active event loop provides for the application process, the result of the asynchronous operation is completed is removed from the event queue, the distributed call processing logic corresponding subsequent
  6. Completion Handler: the completion of the event interface, generally composed by the callback function
  7. Completion Handler A/B:实现接口定义特定的应用处理逻辑。

Proactor模型的简化的工作流程:


Proactor利用异步I/O并行能力带来更高的效率,但同时增加了编程复杂度。windows对异步I/O提供了很好的支持,

而Linux对异步I/O操作支持并不是特别理想,因此Linux平台上还是以Reactor模型为主,Boost asio采用的是Proactor模型。


3.并发模式
在I/O密集型的程序,采用并发方式可以提高CPU的使用率,可采用多进程和多线程两种方式实现并发。

其中包括半同步/半异步模式、半同步/半反应堆模式、半同步/半反应堆模式改进版、Follower/Leader模式。

  • 同步和异步概念

并发模式中的同步异步和 I/O模型中的同步异步并不一样:

  1. 并发模式中同步指程序按照代码顺序执行,异步指程序依赖事件驱动
  2. I/O模型中同步异步用来区分是主动读取数据结构,还是内核帮忙完成数据的读取再返回给线程
  • 半同步/半异步HAHS模式

半异步/半同步模式Half-Sync/Half-Async简称HSHA,是说这个网络模型中异步和同步都存在,IO层是异步处理,业务处理是同步,因此需要依赖于内核的异步IO机制。
HSHA模式工作流程如图:

 

  1. 异步线程监听到事件后,由内核完成读取数据,异步将其封装为请求对象插入到请求队列中
  2. 请求队列有新的请求对象,通知同步线程获取请求对象,这个对象是包含了数据的结构,并不是fd
  3. 同步线程处理请求对象,实现业务逻辑

综上可知当客户端发送请求时,服务端接收数据是异步的,工作线程从队列收到数据之后是同步的,所以称为半同步/半异步。

  • 半同步/半反应堆HSHR模式

由于HAHS模式依赖于内核的异步IO支持,Linux本身AIO并不理想,因此可以借助于epoll等IO多路复用来模拟这个过程,

但是本质上epoll仍然是同步的,因为连接的数据仍然需要自己来读取,因此epoll返回的只是活跃的fd。
半同步/半反应堆模式Half-Sync/Half-Reactor简称HSHR,和HAHS模式很类似,

区别在于IO层使用epoll多路复用并非纯异步IO实现,数据处理线程和IO线程之间仍然通过队列来实现数据传输,

但是相比HSHA来说,HSHR模式传输的是活跃fd而不是读取的数据。


虽然HSHR模式克服了对纯异步IO的依赖,但是基础版本的HSHR模式,IO线程完成连接的到达处理和响应处理,

也就是同时监听新连接和已建立的连接,并且基于队列与后面的工作线程传输数据,在非常高的并发场景可能存在瓶颈,因此后续有对此的改进版本。

  • 半同步/半反应堆模式的改进版

HSHR模式中单一IO线程既要处理新连接又要监控已连接fd的读写事件,因此改进版本增加从线程进行已连接fd的读写事件监听,

原来的IO主线程只负责处理新连接,然后将建立的fd扔给从线程进行监听读写事件。有的地方也称之为主从Reactor模式,如图:


其工作流程为:

  1. 主线程实现连接监听,只处理网络I/O连接事件
  2. 新的连接fd分发至工作线程中,这些fd上的I/O事件都由该工作线程处理,工作线程都可以处理多个fd的读写事件
  3. 工作线程独立维护自己的事件循环,监听不同连接fd的I/O事件
  4. 处理任务可以使用线程池或者从线程自己处理
  • 领导者/追随者LF模式

Follower/Leader模式是多个工作线程轮流进行事件监听、事件分发、处理事件的模式。

任何一个时间点,只有一个工作线程处理成为Leader负责I/O事件监听,而其他线程都是Follower,并等待成为Leader。Follower/Leader模式的工作流概述如下:

  • 当前Leader Thread监听到就绪事件后,从Follower 线程集中推选出 空闲Thread成为新的Leader
  • 新的Leader Thread继续I/O监听
  • 之后继续处理I/O就绪事件,执行完后加入到Follower 线程集中,等待成为Leader

从上描述,Leader/Follower模式的工作线程存在三种状态,工作线程同一时间只能处于一种状态,这三种状态为:

  • Leader:线程处于领导者状态,负责监听I/O事件;
  • Processing:线程处理就绪I/O事件;
  • Follower:等待成为新的领导者或者可能被当前Leader指定处理就绪事件。


4.Memcache网络模型分析
Memcached采用了很典型的Master-Worker模型,采用的是多线程而不是多进程. 主线程Master接收连接, 然后把连接分派给工作线程Worker,

工作线程处理业务逻辑。核心的共享数据是消息队列,主线程会把收到的事件请求放入队列,

随后调度程序会选择一个空闲的Worker线程来从队列中取出事件请求进行处理Memcached使用libevent实现事件循环,

如图:



5.参考资料

  1. https://tech.youzan.com/yi-bu-wang-luo-mo-xing/
  2. https://zhuanlan.zhihu.com/p/58860015
  3. http://xiaobaoqiu.github.io/blog/2014/11/03/memcachedwang-luo-mo-xing/
  4. 《Linux高性能服务器编程》游双

 

Guess you like

Origin www.cnblogs.com/backnullptr/p/11907506.html