You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by "chenBright (via GitHub)" <gi...@apache.org> on 2023/04/16 16:34:29 UTC
[I] 实现一种支持在查询逻辑中挂起bthread的新DBD数据结构 (brpc)
chenBright opened a new issue, #2209:
URL: https://github.com/apache/brpc/issues/2209
**Is your feature request related to a problem? (你需要的功能是否与某个问题有关?)**
> 我们需要写以某种形式和读同步,但读之间相互没竞争。一种解法是,读拿一把thread-local锁,写需要拿到所有的thread-local锁。具体过程如下:
>
> - 数据分前台和后台。
> - 读拿到自己所在线程的thread-local锁,执行查询逻辑后释放锁。
> - 同时只有一个写:修改后台数据,切换前后台,挨个获得所有thread-local锁并立刻释放,结束后再改一遍新后台(老前台)。
以上是brpc[DoublyBufferedData](https://github.com/apache/brpc/blob/master/docs/cn/lalb.md#doublybuffereddata)读写同步的原理。
目前在使用DoublyBufferedData时有个比较容易出问题的地方:因为查询逻辑在thread-local锁的临界区内,如果查询逻辑包含了一些会挂起bthread的操作(如协程锁、bthread_usleep、rpc等),这种在线程锁临界区内加协程锁的逻辑会导致死锁问题。
另一方面,在使用DoublyBufferedData过程中,确实有在查询逻辑中加协程锁的需求。
**Describe the solution you'd like (描述你期望的解决方法)**
减小thread-local锁的临界区,让查询逻辑不在thread-local锁的临界区,实现一种支持在查询逻辑中挂起bthread的新DoublyBufferedData数据结构。
对于目前的DoublyBufferedData,当一个读正在发生时,它会拿着所在线程的thread-local锁,这把锁会挡住同时进行的写,从而保证前台数据不会被修改。为了减小thread-local锁的临界区,在原来DoublyBufferedData的基础上,每把thread-local引入引用计数ref0、ref1和条件变量cond0、cond1,其中引用计数分别表示否有正在读buffer0或者buffer1的读者数量,而条件变量则用来进行读写同步,通知写者对应的buffer已经没有在读的了。具体过程:
- 读拿到自己所在线程的thread-local锁,对当前读的buffer对应的引用计数加一,释放锁。
- 执行查询逻辑。
- 读拿到自己所在线程的thread-local锁,对当前读的buffer对应的引用计数减一,如果引用计数为0,且有写在等,则通过当前读buffer对应的条件变量通知写,最后释放锁。
- 同时只有一个写:修改后台数据,切换前后台,挨个获得所有thread-local锁。每获取一把thread-local锁,判断新后台的引用计数是否为0,如果不为0,则等在新后台对应的条件变量上;如果为0,则释放thread-local锁。所有thread-local锁获取并释放后,再改一遍新后台(老前台)。
**Describe alternatives you've considered (描述你想到的折衷方案)**
**Additional context/screenshots (更多上下文/截图)**
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [I] 实现一种支持在查询逻辑中挂起bthread的新DBD数据结构 (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright closed issue #2209: 实现一种支持在查询逻辑中挂起bthread的新DBD数据结构
URL: https://github.com/apache/brpc/issues/2209
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [I] 实现一种支持在查询逻辑中挂起bthread的新DBD数据结构 (brpc)
Posted by "wwbmmm (via GitHub)" <gi...@apache.org>.
wwbmmm commented on issue #2209:
URL: https://github.com/apache/brpc/issues/2209#issuecomment-1511193631
是否可以把thread local的锁改成thread local的bthread::Mutex
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [I] 实现一种支持在查询逻辑中挂起bthread的新DBD数据结构 (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on issue #2209:
URL: https://github.com/apache/brpc/issues/2209#issuecomment-1511535394
前面的方案对于写线程有一点比较友好:有一些读线程在写线程加锁前就看到了新前台,写线程不需要等这些读线程,只需要等那些还在读老前台的读线程就行了。原来的写线程需要等读线程读完才能获取到锁。
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [I] 实现一种支持在查询逻辑中挂起bthread的新DBD数据结构 (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on issue #2209:
URL: https://github.com/apache/brpc/issues/2209#issuecomment-1511518196
改成协程锁不太合适。thread local的线程锁是为了让不同线程之间的读无竞争的。如果换成了协程锁,某些情况下,对于同一个thread local的协程锁,会一直有竞争。例如,lb中使用了协程锁的实现,在更新实例信息时,读协程会被挂起,读协程被唤醒时可能被调度到其他worker线程。如果这些读协程比较多,则会跟后续的读协程形成竞争,导致后续的读协程被挂起,从而调度到其他worker线程。这个场景下,可能竞争就会一直存在了。所以使用线程锁比较合适吧。
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org