当事务在时间戳排序协议中回滚时,为什么要给它一个新的时间戳?为什么我们不保留旧的时间戳?
3 回答
如果你说的是一个调度器,它的操作是基于时间戳的,并且一个回滚的事务被允许使用它的“旧”时间戳“重新进入调度队列”,那么最终的效果可能是调度器立即给出来自该事务的任何请求的最高优先级,其净效果可能是导致该事务回滚的任何问题几乎立即重新出现,可能导致新的回滚,从而导致新的“重新进入计划队列”等。
或者“立即重新进入队列”的净效果可能是所有其他事务都停止了。
想一想邮局里有一排人,有人的请求无法得到服务,而那个人被允许立即重新进入前面的队列(而不是后面的队列)。然后需要多长时间才能轮到你?
因为可能有其他事务已使用新时间戳提交
- 初始时间戳在 X
- 事务 T1 开始
- T1 将时间戳增量分配给 X+1
- 事务 T2 开始
- T2 将时间戳增量分配给 X+2
- T2 提交
- T1 回滚
如果 T1 将时间戳回滚到 X,那么第三个事务将与 T2 的分配值产生冲突。增量和序列也是如此。如果您需要单片序列值(无间隙),则事务必须序列化,而这会以糟糕的性能为代价。
In a timestamp ordering protocol, the timestamp assigned to the transaction when starting is used to identify potential conflicts with other transactions. These could be transactions that updated an object this transaction is trying to read or transactions that read the value this transaction is trying to overwrite. As a result, when a transaction is aborted and restarted (i.e. to maintain serializability), then all the operations of the transaction will be executed anew and this is the reason a new timestamp needs to be assigned.
From a theoretical perspective, rerunning the operations again while the transaction is still using the old timestamp would be incorrect & unsafe, since it would be reading/overwriting new values while thinking it's situated in an older moment in time. From a practical perspective, if the transaction keeps using the old timestamp, most likely it will keep aborting & restarting continuously, since it will keep conflicting with the same transactions again and again.