miniob源码 架构概览
整体架构
如下图,简单描述了,observer启动后,建立监听、注册libevent事件,recv后触发各stags的handle_event、处理结果回调、threadpool运行机制等等几个方面对整体线程模型、reactor模型和各组件工作流进行分析。

Reactor模型
- miniob运行框架是通过libevent实现了对网络事件的监听,当链接建立后,读缓冲区事件触发回调recv函数的处理流程。
event_set(&client_context->read_event, client_context->fd, EV_READ | EV_PERSIST, recv, client_context);

- 在recv函数的最后将SessionEvent添加到SessionStage的事件队列中,同时将SessionStage添加到threapool的Stage队列中。
SessionEvent *sev = new SessionEvent(client);
session_stage_->add_event(sev);
- thread_pool的stage队列(thread_pool.h)
std::deque<Stage *> run_queue_; //< list of stages with work to do
- stage_event队列(stage.h)
std::deque<StageEvent *> event_list_; // event queue
线程模型
miniob采用多线程的架构,通过Threadpool类创建线程池,根据etc/observer.ini中的配置创建线程。
# threadpools' name, it will contain the threadpool's section
ThreadPools=SQLThreads,IOThreads,DefaultThreads
[SQLThreads]
# the thread number of this threadpool, 0 means cpu's cores.
# if miss the setting of count, it will use cpu's core number;
count=3
#count=0
[IOThreads]
# the thread number of this threadpool, 0 means cpu's cores.
# if miss the setting of count, it will use cpu's core number;
count=3
#count=0
[DefaultThreads]
# If Stage haven't set threadpool, it will use this threadpool
# This threadpool is used for backend operation, such as timer, sedastats and so on.
# the thread number of this threadpool, 0 means cpu's cores.
# if miss the setting of count, it will use cpu's core number;
count=3
ThreadPools 配置了3个线程池:SQLThreads,IOThreads,DefaultThreads,每个线程池可以配置count,即线程的个数。
- 创建线程池和线程,初始化时通过new ThreadPools,在构造函数中调用add_threads,根据线程个数的配置创建线程。
// attempt to start the requested number of threads
for (i = 0; i < threads; i++) {
int stat = pthread_create(&pthread, &pthread_attrs, Threadpool::run_thread, (void
MiniOB架构详解:Reactor模型、线程池与Stage链路

本文概述了MiniOB框架的架构,涉及libevent事件驱动、多线程线程池、Stage处理流水线和配置文件驱动的Stage链。核心内容包括Reactor模型、线程模型如何操作SessionEvent和StageEvent,以及Stage之间的协作与配置管理。
最低0.47元/天 解锁文章
2475





