miniob源码 架构概览

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

miniob源码 架构概览

整体架构

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

image.png

Reactor模型

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

image.png

  • 在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 
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏 克

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值