伤停等待(wound-wait)在分布式事务中

On the one hand

读写事务中的读操作使用了伤停等待(wound-wait)来避免死锁

伤停等待(wound-wait)是一种用于避免死锁的读写事务管理技术。在多个事务并发访问共享资源时,可能会出现死锁的情况,即多个事务相互等待对方释放资源,而无法继续执行下去。为了避免死锁的发生,系统可以采用不同的调度策略,其中之一就是伤停等待。

在使用伤停等待的调度策略下,当一个事务T_i发现需要等待另一个事务T_j的资源时,系统会进行如下处理:

  1. 如果T_j的时间戳(事务开始时间)比T_i的时间戳早,则T_i会立即放弃等待,进入终止状态,这个过程被称为"伤"。
  2. 如果T_j的时间戳比T_i的时间戳晚,则T_i进入等待状态,等待T_j释放资源。
  3. 一旦T_j释放了所需的资源,T_i将立即重新开始,并且没有其他事务会再对T_i进行伤害。

通过伤停等待机制,能够保证事务会根据时间戳的先后顺序进行调度,较早开始的事务具有较高的优先级。
这样可以确保先来的事务尽可能地完成,防止后来的事务长时间等待,从而提高系统的并发效率。

需要注意的是,伤停等待虽然能够避免死锁,但也可能引入其他问题,比如优先级反转问题。如果系统中长时间存在优先级较低的事务阻塞较高优先级事务的情况,会导致资源浪费和低效的执行顺序。因此,在设计和实施伤停等待策略时,需要综合考虑系统的并发性能和资源利用率。

优先级反转问题

优先级反转问题是指在多任务系统中,当一个低优先级任务占用了一个高优先级任务所需要的共享资源,导致高优先级任务被阻塞的情况。从而低优先级任务造成了高优先级任务的延迟。

具体来说,当一个高优先级任务需要获取一个共享资源时,如果这个资源被一个低优先级任务占用并且该低优先级任务正在执行中,那么高优先级任务将被阻塞,等待低优先级任务完成释放资源。这样就出现了优先级反转问题,因为本应该优先执行的任务被一个较低优先级的任务所阻塞。

优先级反转问题可能会导致以下情况:

  • 高优先级任务无法及时完成,导致系统的响应时间延迟。
  • 高优先级任务被低优先级任务长时间阻塞,可能导致系统的不稳定性和性能下降。

解决优先级反转问题的常用方法包括:

  • 优先级继承(Priority Inheritance):让低优先级任务在占用资源时提升到与高优先级任务相同的优先级,直到释放资源。
  • 优先级屏蔽(Priority Masking):高优先级任务在争用资源时,将自己的优先级设置为低优先级任务的优先级,以避免被阻塞。

优先级反转问题需要在设计和实现多任务系统时充分考虑和解决,以确保系统的稳定性和性能。

On the other hand

Wound-Wait used in read operations in read-write transactions to avoid deadlocks

Wound-wait is a read-write transaction management technique used to avoid deadlocks. In situations where multiple transactions are concurrently accessing shared resources, deadlocks can occur, where transactions are waiting for each other to release resources and can’t proceed. To prevent deadlocks, systems can employ different scheduling strategies, one of which is wound-wait.

In the wound-wait scheduling strategy, when a transaction T_i realizes that it needs to wait for a resource held by another transaction T_j, the system handles it as follows:

  1. If T_j’s timestamp (the time when the transaction started) is earlier than T_i’s timestamp, T_i immediately gives up waiting and enters a terminated state, this process is known as “wounding”.
  2. If T_j’s timestamp is later than T_i’s timestamp, T_i enters a waiting state, waiting for T_j to release the resource.
  3. Once T_j releases the required resource, T_i immediately resumes and no other transaction will wound T_i.

By applying the wound-wait mechanism, transactions are scheduled based on the order of timestamps, where transactions with earlier start times have higher priority. This ensures that earlier transactions can complete as much as possible and prevents later transactions from waiting for long durations, thereby improving system concurrency.

However, it is important to note that while wound-wait can avoid deadlocks, it can introduce other issues such as priority inversion. If lower priority transactions block higher priority transactions for long durations in the system, it can result in resource wastage and inefficient execution order. Therefore, when designing and implementing wound-wait strategies, the balance between system concurrency and resource utilization needs to be considered.

Priority Inversion Problem

The priority inversion problem refers to a scenario in a multitasking system where a low-priority task occupies a shared resource required by a higher-priority task, causing the higher-priority task to be blocked or delayed. In this situation, the low-priority task causes a priority inversion by preventing the higher-priority task from executing.

Specifically, when a high-priority task needs to access a shared resource that is held by a low-priority task currently in execution, the high-priority task is blocked, waiting for the low-priority task to complete and release the resource. This leads to a priority inversion, as the task with a higher priority is delayed or preempted by a task with a lower priority.

The priority inversion problem can result in:

  • Delayed completion of high-priority tasks, causing increased system response time.
  • High-priority tasks being blocked for a prolonged period by low-priority tasks, potentially leading to system instability and performance degradation.

Common approaches to address the priority inversion problem include:

  • Priority inheritance: Low-priority tasks are temporarily elevated to the priority of the highest-priority task waiting for the shared resource, ensuring that the resource is released promptly.
  • Priority masking: High-priority tasks reduce their priority to that of the lowest-priority task contending for the resource, avoiding being blocked altogether.

Addressing the priority inversion problem requires careful consideration and resolution during the design and implementation of multitasking systems to ensure system stability and performance.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

P("Struggler") ?

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

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

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

打赏作者

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

抵扣说明:

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

余额充值