Get the App
SLTechnology News&Howtos  ›  Development  › 

Case Analysis of etcd Lease Mechanism and automatic expiry

Shulou Source: shulou.com Published: 2022-06-02 03:45:46 09月30日 Update

This article introduces the etcd lease mechanism and automatic expiration of the case analysis, the content is very detailed, interested friends can refer to, hope to be helpful to you.

It is very important to implement distributed optimistic locks. If the lock is locked and suddenly goes down, the lock needs to be released automatically. So the lock in etcd requires a lifetime.

Expired demo:

Package mainimport ("context"fmt"go.etcd.io/etcd/clientv3"time") func main () {var (config clientv3.Config client * clientv3.Client err error lease clientv3.Lease leaseGrantResp * clientv3.LeaseGrantResponse leaseId clientv3.LeaseID putResp * clientv3.PutResponse kv clientv3.KV getResp * clientv3.GetResponse) / / customer Side configuration config = clientv3.Config {Endpoints: [] string {"0.0.0.0pur2379"} / / Cluster list DialTimeout: 5 * time.Second,} / / create client if client, err = clientv3.New (config) Err! = nil {fmt.Println (err) return} / / apply for a lease (lease) lease = clientv3.NewLease (client) / / apply for a 5-second lease if leaseGrantResp, err = lease.Grant (context.TODO (), 5) Err! = nil {fmt.Println (err) return} / / get id leaseId = leaseGrantResp.ID / / get a subset of kv api kv = clientv3.NewKV (client) / / put a kv, associate it with the lease, and automatically expire if putResp after 10 seconds, err = kv.Put (context.TODO (), "/ cron/lock/job1", ", clientv3.WithLease (leaseId)) Err! = nil {fmt.Println (err) return} fmt.Println ("write successful:", putResp.Header.Revision) / / check whether key expires for {if getResp, err = kv.Get (context.TODO (), "/ cron/lock/job1") Err! = nil {fmt.Println (err) return} if getResp.Count = 0 {fmt.Println ("kv expired") break} fmt.Println ("not expired:", getResp.Kvs) time.Sleep (time.Second)}}

[root@bogon etcd] # go run demo6.go

Write successful: 27

Not out of date: [key: "/ cron/lock/job1" create_revision:27 mod_revision:27 version:1 lease:7587837741646622005]

Not out of date: [key: "/ cron/lock/job1" create_revision:27 mod_revision:27 version:1 lease:7587837741646622005]

Not out of date: [key: "/ cron/lock/job1" create_revision:27 mod_revision:27 version:1 lease:7587837741646622005]

Not out of date: [key: "/ cron/lock/job1" create_revision:27 mod_revision:27 version:1 lease:7587837741646622005]

Not out of date: [key: "/ cron/lock/job1" create_revision:27 mod_revision:27 version:1 lease:7587837741646622005]

Not out of date: [key: "/ cron/lock/job1" create_revision:27 mod_revision:27 version:1 lease:7587837741646622005]

Kv expired.

[root@bogon etcd] #

When applying for a distributed lock, whoever grabs the key is the lock. If the lock is not released actively, the lease should not expire. The reason for the expiration of the lease is that after the program is down, the lock is automatically released to prevent the program from exiting abnormally. If the program grabs the lock, we want the lock to remain intact until we release it voluntarily:

Package mainimport ("context"fmt"go.etcd.io/etcd/clientv3"time") func main () {var (config clientv3.Config client * clientv3.Client err error lease clientv3.Lease leaseGrantResp * clientv3.LeaseGrantResponse leaseId clientv3.LeaseID putResp * clientv3.PutResponse kv clientv3.KV getResp * clientv3.GetResponse keepResp * clientv3.LeaseKeepAliveResponse keepRespChan)

Tags: Lease success client client program instance instance analysis mechanism analysis initiative content distributed subset more cluster association help configuration good important Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology vpn Huawei MariaDB Docker