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. 使用Interlocked在多线程下进行原子操作,无锁无阻塞的实现线程运行状态判断

    巧妙地使用Interlocked的各个方法,再无锁无阻塞的情况下判断出所有线程的运行完成状态. 昨晚耐着性子看完了clr via c#的第29章<<基元线程同步构造>>,尽管这 ...

  2. Memcache架构新思考

    2011年初Marc Kwiatkowski通过Memecache@Facebook介绍了Facebook的Memcache架构,现在重新审视这个架构,仍有很多方面在业界保持先进性.作为weibo内部 ...

  3. JAVA基础经典面试

    [前言] 整理好久,这篇总结写的超级好,很全面.要全部背下来,至于框架应用那块,一定要有针对的,多写项目,照着慕课的对应项目. 链接:http://www.importnew.com/22083.ht ...

  4. python---haproxy---文件操作

    haproxy 文件操作,操作属于简单操作,不复杂 # -*- coding:utf-8 -*- # LC def search(*args): #查找Haproxy文件中的服务器 list1 = [ ...

  5. codeforces——961A Tetris

    本文是博主原创文章,未经允许不得转载. 我在csdn上也同步发布了此文,链接 https://blog.csdn.net/umbrellalalalala/article/details/798915 ...

  6. Spring JTA multiple resource transactions in Tomcat with Atomikos example

    http://www.byteslounge.com/tutorials/spring-jta-multiple-resource-transactions-in-tomcat-with-atomik ...

  7. css多类选择器

    <!DOCTYPE html> <html lang="zh-CN"> <head> <style type="text/css ...

  8. CSS 外边距合并。

    <div id = "parent"> <div id = "child"> demo </div> </div> ...

  9. 洛谷 P1613 解题报告

    P1613 跑路 题目描述 小\(A\)的工作不仅繁琐,更有苛刻的规定,要求小\(A\)每天早上在\(6:00\)之前到达公司,否则这个月工资清零.可是小\(A\)偏偏又有赖床的坏毛病.于是为了保住自 ...

  10. Mego(05) - Mego Tools使用教程

    前言 使用过EntityFramework6的朋友应该都知道EF中的PowerTools这个工具可以帮助初学者或者开发人员快速构建一个EF的数据上下文,并且可以很直观的看到实体之间的关系.不过目前升级 ...