Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of etcd Lease Mechanism and automatic expiry

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report