Cockroach Design 翻译 ( 五) 无锁分布式事务

6  Lock-Free Distributed Transactions无锁分布式事务

Cockroachprovides distributed transactions without locks. Cockroach transactions supporttwo isolation levels:

l  snapshot isolation (SI) and

serializable snapshot isolation(SSI).

Cockroach提供无锁分布式事务,Cockroach事务支持两种隔离级别:

快照隔离级别(SI);

序列化快照隔离级别(SSI)。

SI is simple to implement, highly performant, and correct for allbut a handful of anomalous conditions(e.g. write skew). SSIrequires just a touch more complexity, isstill highly performant (less so with contention), and has no anomalousconditions. Cockroach’s SSI implementation is based on ideas from theliterature and some possibly novel insights.

SI 实现简单、性能高,在排除少数特殊情况(如:写偏序)下也具有正确性。SSI则更为复杂,但也具有高性能(随竞争增加而降低),同时也保障了正确性(没有特殊情况引致错误)。Cockroach的SSI实现基于来自文献的想法和一些创新性见解。

SSI is thedefault level, with SI provided for application developers who are certainenough of their need for performance and the absence of write skew conditionsto consciously elect to use it. In a lightly contended system, ourimplementation of SSI is just as performant as SI, requiring no locking oradditional writes. With contention, our implementation of SSI still requires nolocking, but will end up aborting more transactions. Cockroach’s SI and SSIimplementations prevent starvation scenarios even for arbitrarily longtransactions.

SSI是默认的隔离级别。但CockroachDB也为应用开发者提供了SI隔离级别供选择,当确认需要高性能并没有写偏序的情况下使用该隔离级别。在轻度竞争系统中,SSI的实现和SI一样性能相当。在竞争系统中,SSI的实现仍然无锁定,但最终结果会中止更多的事务。CockroachDBSISSI的实现即使在任意长事务情况下也均能防止饥饿情况的发生。

See the Cahill paper for onepossible implementation of SSI.This is another great paper.For a discussion of SSI implemented by preventing read-write conflicts (incontrast to detecting them, called write-snapshot isolation), see the Yabandehpaper, which is the source of much inspiration for Cockroach’s SSI.

SSI的一种可供参考实现参见 MICHAEL JAMES CAHILL的论文(译注:指《SerializableIsolation for Snapshot Databases》)。还有另一个很棒的论文,是关于通过防止读写冲突来实现SSI的讨论,参见Yabandeh 的论文(译注:指《Predicting and Preventing Inconsistencies in DeployedDistributed Systems》),该论文是CockroachDBSSI实现中许多灵感的源泉。

Both SI andSSI require that the outcome of reads must be preserved, i.e. a write of a keyat a lower timestamp than a previous read must not succeed. To this end, eachrange maintains a bounded in-memory cache from key range to thelatest timestamp at which it was read.

SISSI都需要对读过的结果进行保存,例如,在比读操作更早时间戳版本Key上的写操作必须不能成功。为达到这一效果,每个range维护了一个有界内存缓存,从keyrange到其被读过的最新时间戳。

Most updatesto this timestamp cache correspond to keys being read, thoughthe timestamp cache also protects the outcome of some writes(notably range deletions) which consequently must also populate the cache. Thecache’s entries are evicted oldest timestamp first,updating the low water mark of the cache appropriately.

大多时候,时间戳缓存的更新相当于Key正在被读,尽管时间戳缓存也保护一些写操作(这些写操作的后果一定位于cache内)的结果(尤其是range删除操作)。该缓存首先淘汰掉最老时间戳条目,并适时地更新低水位线。

EachCockroach transaction is assigned a random priority and a "candidatetimestamp" at start. The candidate timestamp is the provisional timestampat which the transaction will commit, and is chosen as the current clock timeof the node coordinating the transaction. This means that a transaction withoutconflicts will usually commit with a timestamp that, in absolute time, precedesthe actual work done by that transaction.

每一个Cockroach事务在开始时都会分配一个随机优先级和“候选时间戳”。在事务将要提交时,候选时间戳是一个暂时未决定的时间戳,选自处理该事务的节点的当前时间。这意味着,一个没有冲突的事务将使用该时间戳提交,从绝对时间来看,此时间戳先于事务结束时间。

In the courseof coordinating a transaction between one or moredistributed nodes, the candidate timestamp may be increased, but will never bedecreased. The core difference between the two isolation levels SI and SSI isthat the former allows the transaction's candidate timestamp to increase andthe latter does not.

在跨一个或者多个分布式节点的事务的协同过程中,候选时间戳可能会增长,但不会降低。SISSI两种隔离级别的核心不同点是前者允许事务的候选时间戳增长,而后者不允许。

你可能感兴趣的:(分布式数据库)