main.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "flag"
  17. "fmt"
  18. "log"
  19. "math/rand"
  20. "os"
  21. "strings"
  22. "sync"
  23. "time"
  24. "golang.org/x/net/context"
  25. "google.golang.org/grpc"
  26. "google.golang.org/grpc/codes"
  27. "github.com/coreos/etcd/clientv3"
  28. "github.com/coreos/etcd/clientv3/concurrency"
  29. )
  30. func main() {
  31. log.SetFlags(log.Lmicroseconds)
  32. endpointStr := flag.String("endpoints", "localhost:2379", "endpoints of etcd cluster")
  33. mode := flag.String("mode", "lock-racer", "test mode (lock-racer)")
  34. round := flag.Int("rounds", 100, "number of rounds to run")
  35. flag.Parse()
  36. eps := strings.Split(*endpointStr, ",")
  37. switch *mode {
  38. case "lock-racer":
  39. runRacer(eps, *round)
  40. case "lease-renewer":
  41. runLeaseRenewer(eps)
  42. default:
  43. fmt.Fprintf(os.Stderr, "unsupported mode %v\n", *mode)
  44. }
  45. }
  46. func runLeaseRenewer(eps []string) {
  47. c := randClient(eps)
  48. ctx := context.Background()
  49. for {
  50. var (
  51. l *clientv3.LeaseGrantResponse
  52. lk *clientv3.LeaseKeepAliveResponse
  53. err error
  54. )
  55. for {
  56. l, err = c.Lease.Grant(ctx, 5)
  57. if err == nil {
  58. break
  59. }
  60. }
  61. expire := time.Now().Add(time.Duration(l.TTL-1) * time.Second)
  62. for {
  63. lk, err = c.Lease.KeepAliveOnce(ctx, l.ID)
  64. if grpc.Code(err) == codes.NotFound {
  65. if time.Since(expire) < 0 {
  66. log.Printf("bad renew! exceeded: %v", time.Since(expire))
  67. for {
  68. lk, err = c.Lease.KeepAliveOnce(ctx, l.ID)
  69. fmt.Println(lk, err)
  70. time.Sleep(time.Second)
  71. }
  72. }
  73. log.Printf("lost lease %d, expire: %v\n", l.ID, expire)
  74. break
  75. }
  76. if err != nil {
  77. continue
  78. }
  79. expire = time.Now().Add(time.Duration(lk.TTL-1) * time.Second)
  80. log.Printf("renewed lease %d, expire: %v\n", lk.ID, expire)
  81. time.Sleep(time.Duration(lk.TTL-2) * time.Second)
  82. }
  83. }
  84. }
  85. func runRacer(eps []string, round int) {
  86. nrace := 15
  87. prefix := "racers"
  88. racers := make([]*concurrency.Mutex, nrace)
  89. clis := make([]*clientv3.Client, nrace)
  90. progress := make([]int, nrace)
  91. finished := make(chan struct{}, 0)
  92. var (
  93. mu sync.Mutex
  94. cnt int
  95. )
  96. ctx := context.Background()
  97. var wg sync.WaitGroup
  98. for i := range racers {
  99. clis[i] = randClient(eps)
  100. racers[i] = concurrency.NewMutex(clis[i], prefix)
  101. wg.Add(1)
  102. go func(i int) {
  103. defer wg.Done()
  104. for {
  105. if progress[i] >= round {
  106. return
  107. }
  108. for {
  109. err := racers[i].Lock(ctx)
  110. if err == nil {
  111. break
  112. }
  113. }
  114. mu.Lock()
  115. if cnt > 0 {
  116. log.Fatalf("bad lock")
  117. }
  118. cnt = 1
  119. mu.Unlock()
  120. time.Sleep(10 * time.Millisecond)
  121. progress[i]++
  122. finished <- struct{}{}
  123. mu.Lock()
  124. for {
  125. err := racers[i].Unlock()
  126. if err == nil {
  127. break
  128. }
  129. }
  130. cnt = 0
  131. mu.Unlock()
  132. }
  133. }(i)
  134. }
  135. start := time.Now()
  136. for i := 1; i < nrace*round+1; i++ {
  137. select {
  138. case <-finished:
  139. if i%100 == 0 {
  140. fmt.Printf("finished %d, took %v\n", i, time.Since(start))
  141. start = time.Now()
  142. }
  143. case <-time.After(time.Minute):
  144. log.Panic("no progress after 1 minute!")
  145. }
  146. }
  147. wg.Wait()
  148. for _, cli := range clis {
  149. cli.Close()
  150. }
  151. }
  152. func randClient(eps []string) *clientv3.Client {
  153. neps := make([]string, len(eps))
  154. copy(neps, eps)
  155. for i := range neps {
  156. j := rand.Intn(i + 1)
  157. neps[i], neps[j] = neps[j], neps[i]
  158. }
  159. c, err := clientv3.New(clientv3.Config{
  160. Endpoints: eps,
  161. DialTimeout: 5 * time.Second,
  162. })
  163. if err != nil {
  164. log.Fatal(err)
  165. }
  166. return c
  167. }