Skip to content

Instantly share code, notes, and snippets.

@hpdvanwyk
Last active January 6, 2020 13:14
Show Gist options
  • Save hpdvanwyk/a94f9cdf2fdad31056cda6ce08402645 to your computer and use it in GitHub Desktop.
Save hpdvanwyk/a94f9cdf2fdad31056cda6ce08402645 to your computer and use it in GitHub Desktop.
Etcd 3.4.3 breaking case.
package main
import (
"context"
"errors"
"flag"
"fmt"
"log"
"time"
"go.etcd.io/etcd/clientv3"
"github.com/google/uuid"
)
const printFreq = 100
func main() {
var endpoint string
flag.StringVar(&endpoint, "endpoint", "http://localhost:2379", "etcd to connect to")
flag.Parse()
endpoints := []string{endpoint}
cfg := clientv3.Config{
Endpoints: endpoints,
DialTimeout: 5 * time.Second,
}
opsChan := make(chan int, 50)
for i := 0; i < 10; i++ {
go func() {
etcdCl, err := clientv3.New(cfg)
if err != nil {
log.Fatalf("%v", err)
}
ops := 0
for {
err = putAndExpire(etcdCl)
if err != nil {
fmt.Printf("err: %v\n", err)
}
ops++
if ops%printFreq == 0 {
opsChan <- printFreq
}
}
}()
}
ticker := time.NewTicker(5 * time.Second)
totalOps := 0
for {
select {
case o := <-opsChan:
totalOps += o
case <-ticker.C:
fmt.Printf("%.00f put+get+expires/s\n", float64(totalOps)/5.0)
totalOps = 0
}
}
}
func putAndExpire(etcdCl *clientv3.Client) error {
ctx := context.Background()
tranBytes, _ := uuid.NewRandom()
key := tranBytes.String()
contentBytes, _ := uuid.NewRandom()
content := contentBytes.String()
lease, err := etcdCl.Grant(ctx, 30)
if err != nil {
return err
}
_, err = etcdCl.Put(ctx, key, content, clientv3.WithLease(lease.ID))
if err != nil {
return err
}
gr, err := etcdCl.Get(ctx, key)
if err != nil {
return err
}
if len(gr.Kvs) == 0 {
return errors.New("no such key")
}
leaseID := gr.Kvs[0].Lease
if leaseID == 0 {
return errors.New("key not leased")
}
_, err = etcdCl.Revoke(ctx, clientv3.LeaseID(leaseID))
if err != nil {
return err
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment