package concurrency

import (
    v3 "github.com/coreos/etcd/clientv3"
    "golang.org/x/net/context"
)

// STM is an interface for software transactional memory.
type STM interface {
    // Get returns the value for a key and inserts the key in the txn's read set.
    // If Get fails, it aborts the transaction with an error, never returning.
    Get(key string) string
    // Put adds a value for a key to the write set.
    Put(key, val string, opts ...v3.OpOption)
    // Rev returns the revision of a key in the read set.
    Rev(key string) int64
    // Del deletes a key.
    Del(key string)

    // commit attempts to apply the txn's changes to the server.
    commit() *v3.TxnResponse
    reset()
}

// stmError safely passes STM errors through panic to the STM error channel.
type stmError struct{ err error }

// NewSTMRepeatable initiates new repeatable read transaction; reads within
// the same transaction attempt always return the same data.
func NewSTMRepeatable(ctx context.Context, c *v3.Client, apply func(STM) error) (*v3.TxnResponse, error) {
    s := &stm{client: c, ctx: ctx, getOpts: []v3.OpOption{v3.WithSerializable()}}
    return runSTM(s, apply)
}

// NewSTMSerializable initiates a new serialized transaction; reads within the
// same transactiona attempt return data from the revision of the first read.
func NewSTMSerializable(ctx context.Context, c *v3.Client, apply func(STM) error) (*v3.TxnResponse, error) {
    s := &stmSerializable{
        stm:      stm{client: c, ctx: ctx},
        prefetch: make(map[string]*v3.GetResponse),
    }
    return runSTM(s, apply)
}

// NewSTMReadCommitted initiates a new read committed transaction.
func NewSTMReadCommitted(ctx context.Context, c *v3.Client, apply func(STM) error) (*v3.TxnResponse, error) {
    s := &stmReadCommitted{stm{client: c, ctx: ctx, getOpts: []v3.OpOption{v3.WithSerializable()}}}
    return runSTM(s, apply)
}

type stmResponse struct {
    resp *v3.TxnResponse
    err  error
}

func runSTM(s STM, apply func(STM) error) (*v3.TxnResponse, error) {
    outc := make(chan stmResponse, 1)
    go func() {
        defer func() {
            if r := recover(); r != nil {
                e, ok := r.(stmError)
                if !ok {
                    // client apply panicked
                    panic(r)
                }
                outc <- stmResponse{nil, e.err}
            }
        }()
        var out stmResponse
        for {
            s.reset()
            if out.err = apply(s); out.err != nil {
                break
            }
            if out.resp = s.commit(); out.resp != nil {
                break
            }
        }
        outc <- out
    }()
    r := <-outc
    return r.resp, r.err
}

// stm implements repeatable-read software transactional memory over etcd
type stm struct {
    client *v3.Client
    ctx    context.Context
    // rset holds read key values and revisions
    rset map[string]*v3.GetResponse
    // wset holds overwritten keys and their values
    wset map[string]stmPut
    // getOpts are the opts used for gets
    getOpts []v3.OpOption
}

type stmPut struct {
    val string
    op  v3.Op
}

func (s *stm) Get(key string) string {
    if wv, ok := s.wset[key]; ok {
        return wv.val
    }
    return respToValue(s.fetch(key))
}

func (s *stm) Put(key, val string, opts ...v3.OpOption) {
    s.wset[key] = stmPut{val, v3.OpPut(key, val, opts...)}
}

func (s *stm) Del(key string) { s.wset[key] = stmPut{"", v3.OpDelete(key)} }

func (s *stm) Rev(key string) int64 {
    if resp := s.fetch(key); resp != nil && len(resp.Kvs) != 0 {
        return resp.Kvs[0].ModRevision
    }
    return 0
}

func (s *stm) commit() *v3.TxnResponse {
    txnresp, err := s.client.Txn(s.ctx).If(s.cmps()...).Then(s.puts()...).Commit()
    if err != nil {
        panic(stmError{err})
    }
    if txnresp.Succeeded {
        return txnresp
    }
    return nil
}

// cmps guards the txn from updates to read set
func (s *stm) cmps() []v3.Cmp {
    cmps := make([]v3.Cmp, 0, len(s.rset))
    for k, rk := range s.rset {
        cmps = append(cmps, isKeyCurrent(k, rk))
    }
    return cmps
}

func (s *stm) fetch(key string) *v3.GetResponse {
    if resp, ok := s.rset[key]; ok {
        return resp
    }
    resp, err := s.client.Get(s.ctx, key, s.getOpts...)
    if err != nil {
        panic(stmError{err})
    }
    s.rset[key] = resp
    return resp
}

// puts is the list of ops for all pending writes
func (s *stm) puts() []v3.Op {
    puts := make([]v3.Op, 0, len(s.wset))
    for _, v := range s.wset {
        puts = append(puts, v.op)
    }
    return puts
}

func (s *stm) reset() {
    s.rset = make(map[string]*v3.GetResponse)
    s.wset = make(map[string]stmPut)
}

type stmSerializable struct {
    stm
    prefetch map[string]*v3.GetResponse
}

func (s *stmSerializable) Get(key string) string {
    if wv, ok := s.wset[key]; ok {
        return wv.val
    }
    firstRead := len(s.rset) == 0
    if resp, ok := s.prefetch[key]; ok {
        delete(s.prefetch, key)
        s.rset[key] = resp
    }
    resp := s.stm.fetch(key)
    if firstRead {
        // txn's base revision is defined by the first read
        s.getOpts = []v3.OpOption{
            v3.WithRev(resp.Header.Revision),
            v3.WithSerializable(),
        }
    }
    return respToValue(resp)
}

func (s *stmSerializable) Rev(key string) int64 {
    s.Get(key)
    return s.stm.Rev(key)
}

func (s *stmSerializable) gets() ([]string, []v3.Op) {
    keys := make([]string, 0, len(s.rset))
    ops := make([]v3.Op, 0, len(s.rset))
    for k := range s.rset {
        keys = append(keys, k)
        ops = append(ops, v3.OpGet(k))
    }
    return keys, ops
}

func (s *stmSerializable) commit() *v3.TxnResponse {
    keys, getops := s.gets()
    txn := s.client.Txn(s.ctx).If(s.cmps()...).Then(s.puts()...)
    // use Else to prefetch keys in case of conflict to save a round trip
    txnresp, err := txn.Else(getops...).Commit()
    if err != nil {
        panic(stmError{err})
    }
    if txnresp.Succeeded {
        return txnresp
    }
    // load prefetch with Else data
    for i := range keys {
        resp := txnresp.Responses[i].GetResponseRange()
        s.rset[keys[i]] = (*v3.GetResponse)(resp)
    }
    s.prefetch = s.rset
    s.getOpts = nil
    return nil
}

type stmReadCommitted struct{ stm }

// commit always goes through when read committed
func (s *stmReadCommitted) commit() *v3.TxnResponse {
    s.rset = nil
    return s.stm.commit()
}

func isKeyCurrent(k string, r *v3.GetResponse) v3.Cmp {
    rev := r.Header.Revision + 1
    if len(r.Kvs) != 0 {
        rev = r.Kvs[0].ModRevision + 1
    }
    return v3.Compare(v3.ModRevision(k), "<", rev)
}

func respToValue(resp *v3.GetResponse) string {
    if len(resp.Kvs) == 0 {
        return ""
    }
    return string(resp.Kvs[0].Value)
}

stm.go的更多相关文章

  1. STM

    STM(System Trace macrocell) STM是coresight system中的一个trace source,可以提供high-bandwidth的trace data. STM优 ...

  2. LDM和STM指令

    LDM批量加载/STM批量存储指令可以实现一组寄存器和一块连续的内存单元之间传输数据. 允许一条指令传送16个寄存器的任意子集和所有寄存器,指令格式如下: LDM{cond}  mode  Rn{!} ...

  3. arm汇编:ldr,str,ldm,stm,伪指令ldr

    ldr,str,ldm,stm的命名规律: 这几个指令命名看起来不易记住,现在找找规律. 指令 样本 效果 归纳名称解释 ldr Rd,addressing ldr r1,[r0] addressin ...

  4. LDM与STM指令详解

    title: LDM与STM指令详解 date: 2019/2/26 17:58:00 toc: true --- LDM与STM指令详解 指令形式如下,这里的存储方向是针对寄存器的 Load Mul ...

  5. STM新建项目

    STM新建项目,为以后开发提供更好的平台,项目代码分级分类管理,便于查看. 1.新建一个文件夹,在里面分别新建固件库.内核.用户文件夹. 在网上下载STM32F10x_StdPeriph_Lib_V3 ...

  6. 汇编指令:ldr和str,ldm和stm的区别

    (1)LDR:L表示LOAD,LOAD的含义应该理解为:Load from memory into register.下面这条语句就说明的很清楚: LDR   R1,     [R2] R1<— ...

  7. STM FLASH在线编程 升级

    注意字节到 stm flash 顺序是反的 例如 12 34 56 78 世纪写入内存 应该是 78 56 34 12

  8. ARM LDR/STR, LDM/STM 指令

    这里比较下容易混淆的四条指令,已经在这4条指令的混淆上花费了很多精力,现在做个小结,LDR,STR,LDM,STM这四条指令, 关于LDM和STM的说明,见另外一个说明文件,说明了这两个文件用于栈操作 ...

  9. STM 软件事务内存——本质是为提高并发,通过事务来管理内存的读写访问以避免锁的使用

    对Java程序员来说,我们对面向对象的编程(OOP)自然都是烂熟于胸的,但语言也极大地影响了我们构建面向对象应用程序的方式.(现在的OOP已经和Alan Kay当初创造这个词时候的初衷大不相同了,他的 ...

随机推荐

  1. linux 常见命令及说明杂记

    systemctl 命令: systemctl 是管制服务的主要工具, 它整合了chkconfig 与 service功能于一体.示例:systemctl is-enabled iptables.se ...

  2. javascript学习(一)构建自己的JS库

    库是一个饱受争议的热门话题.一种观点认为它是一种非常棒的工具,是任何开发者都不可或缺的:另一种观点则认为在不理解库的内部工作原理的情况下对库形成依赖,会助长懒惰的风气从而导致开发者素质下降 库是一个饱 ...

  3. NewLife.Net——开始网络编程

    网络编程的重要性就不说了,先上源码:https://github.com/nnhy/NewLife.Net.Tests 一个服务端,就是监听一些端口,接收客户端连接和数据,进行处理,然后响应. /// ...

  4. Django开发环境静态文件访问配置

    settings主要配置项目: STATIC_ROOT = 'D:\Dev\jpDev\czxg\assets' #这个地方是所在系统标准目录文法配置 STATIC_URL = '/static/' ...

  5. android解析xml文件方法之一-----DOM

    Hello.xml文件 <dict num="219" id="219" name="219"> <key>hell ...

  6. Java虚拟机-类文件

    代码编译的结果从本地机器码转换为字节码,是存储格式发展的一小步,却是编程语言发展的一大步.计算机只认识0和1,所以我们的程序需要经过编译器翻译成由0和1组成的二进制格式才能由计算机执行.经过技术的发展 ...

  7. C++单例

    template<class T> class Singleton { public: using object_type = T; struct object_creator { obj ...

  8. Unity3d Mecanim动画系统Animator学习笔记

    1. unity3d Mecanim学习  Unity3d新版动画系统网上的介绍很多,但多是流水笔记,外人很难看明白,后来我 终于找到介绍的比较明白的两个作者,特别感谢.一个是58开发网的乐天老师,课 ...

  9. QT中正则表达式的简单说明

    使用方法: QRegExp acNumRE("[0-9]{19}"); lineEdit->setValidator(new QRegExpValidator(acNumRE ...

  10. C#System.Text.RegularExpressions.Regex使用(二) .

    (6)特殊字符的匹配 string x = "//"; Regex r1 = new Regex("^////$"); Console.WriteLine(&q ...