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. C# 视频多人脸识别

    上一篇内容的调整,并按 @轮回 的说法,提交到git了,https://github.com/catzhou2002/ArcFaceDemo 基本思路如下: 一.识别线程 1.获取当前图片 2.识别当 ...

  2. jquery 滚动事件

    $(window).scroll(function () { if ($(window).scrollTop() >50) {  alert('show!!'); }});

  3. Activiti初学问题,求解

    <userTask id="writeReportTask" name="Write monthly financial report" > < ...

  4. 移动App开发基本技术面

    1.UI布局 1.1.熟悉系统布局基本机制和使用方法 2.界面效果 2.1.熟悉系统提供的所有界面组件 2.2.熟悉各种功能界面效果的实现途径 2,3.动画等特殊UI效果的实现机制 3.网络请求 3. ...

  5. sqlite db数据的导出

    sqlite的db数据一般是filename.db的格式,用普通文本编辑器打开是乱码,用sqlite名令操作比较麻烦,有时版本格式问题还会起阻扰,有一个GUI工具可以对sqlite db格式数据进行管 ...

  6. Android layout_margin 无效的解决办法

    http://www.aichengxu.com/view/31025 1.如果LinearLayout中使用Android:layout_marginRight不起作用,通过测试原来在android ...

  7. 在WinForm应用程序中快速实现多语言的处理

    在国际化环境下,越来越多的程序需要做多语言版本,以适应各种业务需求的变化.在Winform应用程序中实现多语言也有常规的处理方式处理,不过需要针对每个语言版本,重新修改Winform界面的显示,对一些 ...

  8. 搭建centos7的开发环境2-单机版Hadoop2.7.3配置

    最近公司准备升级spark环境,主要原因是生产环境的spark和hadoop版本都比较低,但是具体升级到何种版本还不确定,需要做进一步的测试分析.这个任务对于大数据开发环境配置有要求,这里记录一下配置 ...

  9. Hadoop 实现 TF-IDF 计算

    学习Hadoop 实现TF-IDF 算法,使用的是CDH5.13.1 VM版本,Hadoop用的是2.6.0的jar包,Maven中增加如下即可 <dependency> <grou ...

  10. 遍历php数组的几种方法

    第一.foreach() foreach()是一个用来遍历数组中数据的最简单有效的方法. <?php $urls= array('aaa','bbb','ccc','ddd'); foreach ...