package clientv3

import (
    "sync"
    "time"

    "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
    pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
    "golang.org/x/net/context"
    "google.golang.org/grpc"
)

type (
    LeaseRevokeResponse pb.LeaseRevokeResponse
    LeaseID             int64
)

// LeaseGrantResponse is used to convert the protobuf grant response.
type LeaseGrantResponse struct {
    *pb.ResponseHeader
    ID    LeaseID
    TTL   int64
    Error string
}

// LeaseKeepAliveResponse is used to convert the protobuf keepalive response.
type LeaseKeepAliveResponse struct {
    *pb.ResponseHeader
    ID  LeaseID
    TTL int64
}

// LeaseTimeToLiveResponse is used to convert the protobuf lease timetolive response.
type LeaseTimeToLiveResponse struct {
    *pb.ResponseHeader
    ID LeaseID `json:"id"`

    // TTL is the remaining TTL in seconds for the lease; the lease will expire in under TTL+1 seconds.
    TTL int64 `json:"ttl"`

    // GrantedTTL is the initial granted time in seconds upon lease creation/renewal.
    GrantedTTL int64 `json:"granted-ttl"`

    // Keys is the list of keys attached to this lease.
    Keys [][]byte `json:"keys"`
}

const (
    // defaultTTL is the assumed lease TTL used for the first keepalive
    // deadline before the actual TTL is known to the client.
    defaultTTL = 5 * time.Second
    // a small buffer to store unsent lease responses.
    leaseResponseChSize = 16
    // NoLease is a lease ID for the absence of a lease.
    NoLease LeaseID = 0
)

type Lease interface {
    // Grant creates a new lease.
    Grant(ctx context.Context, ttl int64) (*LeaseGrantResponse, error)

    // Revoke revokes the given lease.
    Revoke(ctx context.Context, id LeaseID) (*LeaseRevokeResponse, error)

    // TimeToLive retrieves the lease information of the given lease ID.
    TimeToLive(ctx context.Context, id LeaseID, opts ...LeaseOption) (*LeaseTimeToLiveResponse, error)

    // KeepAlive keeps the given lease alive forever.
    KeepAlive(ctx context.Context, id LeaseID) (<-chan *LeaseKeepAliveResponse, error)

    // KeepAliveOnce renews the lease once. In most of the cases, Keepalive
    // should be used instead of KeepAliveOnce.
    KeepAliveOnce(ctx context.Context, id LeaseID) (*LeaseKeepAliveResponse, error)

    // Close releases all resources Lease keeps for efficient communication
    // with the etcd server.
    Close() error
}

type lessor struct {
    mu sync.Mutex // guards all fields

    // donec is closed when recvKeepAliveLoop stops
    donec chan struct{}

    remote pb.LeaseClient

    stream       pb.Lease_LeaseKeepAliveClient
    streamCancel context.CancelFunc

    stopCtx    context.Context
    stopCancel context.CancelFunc

    keepAlives map[LeaseID]*keepAlive

    // firstKeepAliveTimeout is the timeout for the first keepalive request
    // before the actual TTL is known to the lease client
    firstKeepAliveTimeout time.Duration
}

// keepAlive multiplexes a keepalive for a lease over multiple channels
type keepAlive struct {
    chs  []chan<- *LeaseKeepAliveResponse
    ctxs []context.Context
    // deadline is the time the keep alive channels close if no response
    deadline time.Time
    // nextKeepAlive is when to send the next keep alive message
    nextKeepAlive time.Time
    // donec is closed on lease revoke, expiration, or cancel.
    donec chan struct{}
}

func NewLease(c *Client) Lease {
    l := &lessor{
        donec:                 make(chan struct{}),
        keepAlives:            make(map[LeaseID]*keepAlive),
        remote:                RetryLeaseClient(c),
        firstKeepAliveTimeout: c.cfg.DialTimeout + time.Second,
    }
    if l.firstKeepAliveTimeout == time.Second {
        l.firstKeepAliveTimeout = defaultTTL
    }

    l.stopCtx, l.stopCancel = context.WithCancel(context.Background())
    go l.recvKeepAliveLoop()
    go l.deadlineLoop()
    return l
}

func (l *lessor) Grant(ctx context.Context, ttl int64) (*LeaseGrantResponse, error) {
    cctx, cancel := context.WithCancel(ctx)
    done := cancelWhenStop(cancel, l.stopCtx.Done())
    defer close(done)

    for {
        r := &pb.LeaseGrantRequest{TTL: ttl}
        resp, err := l.remote.LeaseGrant(cctx, r)
        if err == nil {
            gresp := &LeaseGrantResponse{
                ResponseHeader: resp.GetHeader(),
                ID:             LeaseID(resp.ID),
                TTL:            resp.TTL,
                Error:          resp.Error,
            }
            return gresp, nil
        }
        if isHaltErr(cctx, err) {
            return nil, toErr(cctx, err)
        }
        if nerr := l.newStream(); nerr != nil {
            return nil, nerr
        }
    }
}

func (l *lessor) Revoke(ctx context.Context, id LeaseID) (*LeaseRevokeResponse, error) {
    cctx, cancel := context.WithCancel(ctx)
    done := cancelWhenStop(cancel, l.stopCtx.Done())
    defer close(done)

    for {
        r := &pb.LeaseRevokeRequest{ID: int64(id)}
        resp, err := l.remote.LeaseRevoke(cctx, r)

        if err == nil {
            return (*LeaseRevokeResponse)(resp), nil
        }
        if isHaltErr(ctx, err) {
            return nil, toErr(ctx, err)
        }
        if nerr := l.newStream(); nerr != nil {
            return nil, nerr
        }
    }
}

func (l *lessor) TimeToLive(ctx context.Context, id LeaseID, opts ...LeaseOption) (*LeaseTimeToLiveResponse, error) {
    cctx, cancel := context.WithCancel(ctx)
    done := cancelWhenStop(cancel, l.stopCtx.Done())
    defer close(done)

    for {
        r := toLeaseTimeToLiveRequest(id, opts...)
        resp, err := l.remote.LeaseTimeToLive(cctx, r, grpc.FailFast(false))
        if err == nil {
            gresp := &LeaseTimeToLiveResponse{
                ResponseHeader: resp.GetHeader(),
                ID:             LeaseID(resp.ID),
                TTL:            resp.TTL,
                GrantedTTL:     resp.GrantedTTL,
                Keys:           resp.Keys,
            }
            return gresp, nil
        }
        if isHaltErr(cctx, err) {
            return nil, toErr(cctx, err)
        }
    }
}

func (l *lessor) KeepAlive(ctx context.Context, id LeaseID) (<-chan *LeaseKeepAliveResponse, error) {
    ch := make(chan *LeaseKeepAliveResponse, leaseResponseChSize)

    l.mu.Lock()
    ka, ok := l.keepAlives[id]
    if !ok {
        // create fresh keep alive
        ka = &keepAlive{
            chs:           []chan<- *LeaseKeepAliveResponse{ch},
            ctxs:          []context.Context{ctx},
            deadline:      time.Now().Add(l.firstKeepAliveTimeout),
            nextKeepAlive: time.Now(),
            donec:         make(chan struct{}),
        }
        l.keepAlives[id] = ka
    } else {
        // add channel and context to existing keep alive
        ka.ctxs = append(ka.ctxs, ctx)
        ka.chs = append(ka.chs, ch)
    }
    l.mu.Unlock()

    go l.keepAliveCtxCloser(id, ctx, ka.donec)

    return ch, nil
}

func (l *lessor) KeepAliveOnce(ctx context.Context, id LeaseID) (*LeaseKeepAliveResponse, error) {
    cctx, cancel := context.WithCancel(ctx)
    done := cancelWhenStop(cancel, l.stopCtx.Done())
    defer close(done)

    for {
        resp, err := l.keepAliveOnce(cctx, id)
        if err == nil {
            if resp.TTL == 0 {
                err = rpctypes.ErrLeaseNotFound
            }
            return resp, err
        }
        if isHaltErr(ctx, err) {
            return nil, toErr(ctx, err)
        }

        if nerr := l.newStream(); nerr != nil {
            return nil, nerr
        }
    }
}

func (l *lessor) Close() error {
    l.stopCancel()
    <-l.donec
    return nil
}

func (l *lessor) keepAliveCtxCloser(id LeaseID, ctx context.Context, donec <-chan struct{}) {
    select {
    case <-donec:
        return
    case <-l.donec:
        return
    case <-ctx.Done():
    }

    l.mu.Lock()
    defer l.mu.Unlock()

    ka, ok := l.keepAlives[id]
    if !ok {
        return
    }

    // close channel and remove context if still associated with keep alive
    for i, c := range ka.ctxs {
        if c == ctx {
            close(ka.chs[i])
            ka.ctxs = append(ka.ctxs[:i], ka.ctxs[i+1:]...)
            ka.chs = append(ka.chs[:i], ka.chs[i+1:]...)
            break
        }
    }
    // remove if no one more listeners
    if len(ka.chs) == 0 {
        delete(l.keepAlives, id)
    }
}

func (l *lessor) keepAliveOnce(ctx context.Context, id LeaseID) (*LeaseKeepAliveResponse, error) {
    cctx, cancel := context.WithCancel(ctx)
    defer cancel()

    stream, err := l.remote.LeaseKeepAlive(cctx, grpc.FailFast(false))
    if err != nil {
        return nil, toErr(ctx, err)
    }

    err = stream.Send(&pb.LeaseKeepAliveRequest{ID: int64(id)})
    if err != nil {
        return nil, toErr(ctx, err)
    }

    resp, rerr := stream.Recv()
    if rerr != nil {
        return nil, toErr(ctx, rerr)
    }

    karesp := &LeaseKeepAliveResponse{
        ResponseHeader: resp.GetHeader(),
        ID:             LeaseID(resp.ID),
        TTL:            resp.TTL,
    }
    return karesp, nil
}

func (l *lessor) recvKeepAliveLoop() {
    defer func() {
        l.mu.Lock()
        close(l.donec)
        for _, ka := range l.keepAlives {
            ka.Close()
        }
        l.keepAlives = make(map[LeaseID]*keepAlive)
        l.mu.Unlock()
    }()

    stream, serr := l.resetRecv()
    for serr == nil {
        resp, err := stream.Recv()
        if err != nil {
            if isHaltErr(l.stopCtx, err) {
                return
            }
            stream, serr = l.resetRecv()
            continue
        }
        l.recvKeepAlive(resp)
    }
}

// resetRecv opens a new lease stream and starts sending LeaseKeepAliveRequests
func (l *lessor) resetRecv() (pb.Lease_LeaseKeepAliveClient, error) {
    if err := l.newStream(); err != nil {
        return nil, err
    }
    stream := l.getKeepAliveStream()
    go l.sendKeepAliveLoop(stream)
    return stream, nil
}

// recvKeepAlive updates a lease based on its LeaseKeepAliveResponse
func (l *lessor) recvKeepAlive(resp *pb.LeaseKeepAliveResponse) {
    karesp := &LeaseKeepAliveResponse{
        ResponseHeader: resp.GetHeader(),
        ID:             LeaseID(resp.ID),
        TTL:            resp.TTL,
    }

    l.mu.Lock()
    defer l.mu.Unlock()

    ka, ok := l.keepAlives[karesp.ID]
    if !ok {
        return
    }

    if karesp.TTL <= 0 {
        // lease expired; close all keep alive channels
        delete(l.keepAlives, karesp.ID)
        ka.Close()
        return
    }

    // send update to all channels
    nextKeepAlive := time.Now().Add(1 + time.Duration(karesp.TTL/3)*time.Second)
    ka.deadline = time.Now().Add(time.Duration(karesp.TTL) * time.Second)
    for _, ch := range ka.chs {
        select {
        case ch <- karesp:
            ka.nextKeepAlive = nextKeepAlive
        default:
        }
    }
}

// deadlineLoop reaps any keep alive channels that have not received a response
// within the lease TTL
func (l *lessor) deadlineLoop() {
    for {
        select {
        case <-time.After(time.Second):
        case <-l.donec:
            return
        }
        now := time.Now()
        l.mu.Lock()
        for id, ka := range l.keepAlives {
            if ka.deadline.Before(now) {
                // waited too long for response; lease may be expired
                ka.Close()
                delete(l.keepAlives, id)
            }
        }
        l.mu.Unlock()
    }
}

// sendKeepAliveLoop sends LeaseKeepAliveRequests for the lifetime of a lease stream
func (l *lessor) sendKeepAliveLoop(stream pb.Lease_LeaseKeepAliveClient) {
    for {
        select {
        case <-time.After(500 * time.Millisecond):
        case <-stream.Context().Done():
            return
        case <-l.donec:
            return
        case <-l.stopCtx.Done():
            return
        }

        var tosend []LeaseID

        now := time.Now()
        l.mu.Lock()
        for id, ka := range l.keepAlives {
            if ka.nextKeepAlive.Before(now) {
                tosend = append(tosend, id)
            }
        }
        l.mu.Unlock()

        for _, id := range tosend {
            r := &pb.LeaseKeepAliveRequest{ID: int64(id)}
            if err := stream.Send(r); err != nil {
                // TODO do something with this error?
                return
            }
        }
    }
}

func (l *lessor) getKeepAliveStream() pb.Lease_LeaseKeepAliveClient {
    l.mu.Lock()
    defer l.mu.Unlock()
    return l.stream
}

func (l *lessor) newStream() error {
    sctx, cancel := context.WithCancel(l.stopCtx)
    stream, err := l.remote.LeaseKeepAlive(sctx, grpc.FailFast(false))
    if err != nil {
        cancel()
        return toErr(sctx, err)
    }

    l.mu.Lock()
    defer l.mu.Unlock()
    if l.stream != nil && l.streamCancel != nil {
        l.stream.CloseSend()
        l.streamCancel()
    }

    l.streamCancel = cancel
    l.stream = stream
    return nil
}

func (ka *keepAlive) Close() {
    close(ka.donec)
    for _, ch := range ka.chs {
        close(ch)
    }
}

// cancelWhenStop calls cancel when the given stopc fires. It returns a done chan. done
// should be closed when the work is finished. When done fires, cancelWhenStop will release
// its internal resource.
func cancelWhenStop(cancel context.CancelFunc, stopc <-chan struct{}) chan<- struct{} {
    done := make(chan struct{}, 1)

    go func() {
        select {
        case <-stopc:
        case <-done:
        }
        cancel()
    }()

    return done
}

lease.go的更多相关文章

  1. 基于Lease分布式系统重试服务选举

    /** * Copyright (c) 2015, www.cubbery.com. All rights reserved. */ package com.cubbery.event.retry; ...

  2. 分布式入门之1:Lease机制

      引子: 分布式系统中,如何确认一个节点是否工作正常?   如果有3副本A.B.C,并通过中心结点M来管理.其中A为主副本. 未接触过分布式的直观的处理方法是在每个副本与中心节点M中维护一个心跳,期 ...

  3. Azure 删除VHD时报错:There is currently a lease on the blob and no lease ID was specified in the request

    可下载:http://clumsyleaf.com/products/cloudxplorer 然后在Accounts中新建一个Account,账号与Key,可在相应的storage Manage A ...

  4. sudo -u hdfs hdfs balancer出现异常 No lease on /system/balancer.id

    16/06/02 20:34:05 INFO balancer.Balancer: namenodes = [hdfs://dlhtHadoop101:8022, hdfs://dlhtHadoop1 ...

  5. Hey,man,are you ok? -- 关于心跳、故障监测、lease机制

    电话之于短信.微信的一个很大的不同点在于,前者更加及时,有更快速直接的反馈:而后面两个虽然称之为instant message,但经常时发出去了就得等对方回复,等多久是不确定的.打电话能明确知道对方在 ...

  6. Lease问题

    经过查明原来是lease引发的问题.不过查问题的过程让我们耽误了很多修复故障的时间,很是不爽. 起因:datanode和regionserver以及master同时挂掉 现象:datanode重启后, ...

  7. 分析dhcp.lease文件,统计DHCP服务器IP自动分配

    #!/usr/bin/env python # coding=utf-8 import string import time,datetime class TIMEFORMAT: def __init ...

  8. 深入NAS协议系列: 召唤SMB2 OpLock/Lease

    这是从事存储行业十年以来我写的第一篇博客,希望借此开始把自己这些年所积累的一些干货借这个平台做分享. 虽然NAS协议众多,但核心的就那个几个:NFS,SMB/CIFS, FTP/SFTP, 其中SMB ...

  9. HDFS Lease Recovey 和 Block Recovery

    这篇分析一下Lease Recovery 和 Block Recovery hdfs支持hflush后,需要保证hflush的数据被读到,datanode重启不能简单的丢弃文件的最后一个block,而 ...

随机推荐

  1. MySQL 表名区分大小写设置

    1.关闭MySQL服务:         控制面板主页-管理工具-服务-MySQL服务 2.在服务器运行目录找到my.ini 或者my.cnf文件: 在[mysqld]下面增加一行添加 :lower_ ...

  2. html5中新增的元素和废除的元素

    一.新增的结构元素 1.section元素表示页面中的一个内容区块,比如章节.页眉.页脚或页面中的其他部分.它可以与h1.h2.h3.h4.h5.h6等元素结合起来使用,标示文档结构. h5中的代码事 ...

  3. 《转》xcode创建一个工程的多个taget,便于测试和发布多个版本

    背景:很多时候,我们需要在一个工程中创立多个target,也就是说我们希望同一份代码可以创建两个应用,放到模拟器或者真机上,或者是,我们平时有N多人合作开发,当测试的时候,在A这里装了一遍测A写的那块 ...

  4. 修改flume源码,使其HTTPSource具备访问路径功能

    目前有一个需求,就是Flume可以作为一个类似于tomcat的服务器,可以通过post请求进行访问,并且路径需要:ip:port/contextPath格式. 经过一些资料获悉,httpSource只 ...

  5. WEB 集群与负载均衡(一)基本概念-上

    Web集群是由多个同时运行同一个web应用的服务器组成,在外界看来就像一个服务器一样,这多台服务器共同来为客户提供更高性能的服务.集群更标准的定义是:一组相互独立的服务器在网络中表现为单一的系统,并以 ...

  6. 自动生成MyEclipse 安装破解码

    新建一个class 文件,Debug 模式运行一个,输入任意值 ,回车得到破解安装码 代码文件如下: import java.io.*; public class MyEclipseGen { pri ...

  7. 原生Eclipse下Java服务器调试的一个问题

    当你对Server的配置修改以后,最好到 workspacedir\.metadata\.plugins\org.eclipse.wst.server.core\tmp0目录下把缓存文件给删除了,否则 ...

  8. Eclipse+Resin开发环境迁移中发生的一些问题

    换新机器了,系统也从XP升级到64位WIn7.某些旧工具直接无法用了.下面简单谈一下标题的内容 1.非泛型的容器类引入在JDK1.7以下编译好像已经不行了.比如Java.util.ArrayList这 ...

  9. Android设计开发笔记

    1.因为Android的开发是基于框架的开发:往对方指定的位置加代码:其运行的Message\Handler机制也决定了其单步跟踪也不方便,所以建立新代码时要多Log,这样不但便于调试,而且帮助你加深 ...

  10. 关于Python的super用法研究

    一.问题的发现与提出 在Python类的方法(method)中,要调用父类的某个方法,在python 2.2以前,通常的写法如代码段1: 代码段1: class A:  def __init__(se ...