• Tech News/Blog Notebook [22.2.6]


    深度解析:分布式存储系统实现快照隔离的常见时钟方案

    数据库ACID

    • atomic 原子性
    • consistency 一致性
    • isolation 隔离性
    • durability 持久性

    Isolation 持久性:数据库在并发事务下的表现。

    参考:《A Critique of ANSI SQL Isolation Levels》,简要版本

    事务并发时,会出现的8类问题:

    P0:Dirty Write

    事务T1改写了事务T2写入但还未提交的值。

    Dirty Write occurs when one transaction overwrites a value that has previously been written by another still in-flight transaction.

    会造成的两个影响:

    1. violates the integrity constraint
    2. the system can’t automatically rollback to a before image on transaction abort.

    解法:long-duration write lock

    P1:Dirty Read

    事务T1读取到了事务T2已写入但还未commit的值。

    Dirty Read occurs when one transaction reads a value that has been written by another still in-flight transaction.

    解法:short-duration read locks + long-duration write locks

    P2:Non-Repeatable Read(Fussy Read)

    在一个事务中已经被读取的值,被另一个事务给改写了。

    Fuzzy or Non-Repeatable Read occurs when a value that has been read by a still in-flight transaction is overwritten by another transaction. 

    解法:long-duration read and write locks,or 基于谓词的short-duration lock读取(Select... where P)

    P3:Phantom(幻读)

    当事务T1使用Select... Where P读取数据时,事务T2同时在执行且刚好将值更新为P的状态,导致T1以为自己读到的是正确条件下的值,但其实不是。

    Phantom occurs when a transaction does a predicate-based read (e.g. SELECT… WHERE P) and another transaction writes a data item matched by that predicate while the first transaction is still in flight. 

    解法:long-duration read and write locks

    P4:Lost Update

    一图胜千言,T2事务的更新丢失了(因为被T1事务给覆盖了)

    P4C:Cursor Lost Update

    Cursor Lost Update是Lost Update的一个变体,效果是一样的。

    解法:holding a lock on the current item of the cursor until the cursor moves or is closed.

    A5A:Read Skew(读偏序)

    同一事务的两个数据读取之间,被其他事务更改了部分数据的值。

    A5B:Write Skew(写偏序)

    事务中更新某个值的谓语,在用于校验后被其他事务修改了。

    六种隔离级别(Isolation Level):

    • Read Uncommitted(Degree 1)
    • Read Committed(Degree 2)
    • Cursor Stability
    • Repeatable Read
    • Snapshot
    • ANSI SQL(Serializble 可串行化,Degree 3)

      Snapshot Isolation的核心思想是事务的读操作从已提交的数据中读取,此时数据的版本时间戳为StartTimestamp,然后进行一些列事务内部的操作,提交时生成一个CommitTimestamp。当StartTimestamp和CommitTimestamp之间没有任何WriteSets Rows被其他事务写入时,本次提交成功,从而可以避免Dirty Write、DIrty Read、Fuzzy Read、Lost Update、Cursor Lost Update以及Read Skew,但无法避免Phantom和Write Skew。

      因为Snapshot Isolation只关注WriteSet Rows有没有被其他事务更新,所以在依赖谓语的更新语句中,若谓语的数据不在WriteSet Rows中而被其他并行地事务更新时,Snapshot Isolation是感知不到的,因此SI无法避免Write Skew。

    待扩展阅读:https://zhuanlan.zhihu.com/p/54979396

  • 相关阅读:
    QT全局热键(用nativeKeycode封装API,不跨平台)
    Android 短信模块分析(二) MMS中四大组件核心功能详解
    一个高效过滤非UTF8字符的C函数(也可用来判断是否utf8)
    Windows-1252对Latin1编码有改变(并不完全兼容),而且Latin1缺失了好多西欧字符(法语,德语,西班牙语都有)
    C++静态库与动态库
    CFBundleName系列参数的含义
    QT完美转换特殊字符的大小写
    Java-继承的应用
    RTTI、虚函数和虚基类的实现方式、开销分析及使用指导(虚函数的开销很小,就2次操作而已)
    delphi如何获得当前操作系统语言环境
  • 原文地址:https://www.cnblogs.com/elaron/p/15866766.html
Copyright © 2020-2023  润新知