double_barrier.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 recipe
  15. import (
  16. "context"
  17. "github.com/coreos/etcd/clientv3"
  18. "github.com/coreos/etcd/clientv3/concurrency"
  19. "github.com/coreos/etcd/mvcc/mvccpb"
  20. )
  21. // DoubleBarrier blocks processes on Enter until an expected count enters, then
  22. // blocks again on Leave until all processes have left.
  23. type DoubleBarrier struct {
  24. s *concurrency.Session
  25. ctx context.Context
  26. key string // key for the collective barrier
  27. count int
  28. myKey *EphemeralKV // current key for this process on the barrier
  29. }
  30. func NewDoubleBarrier(s *concurrency.Session, key string, count int) *DoubleBarrier {
  31. return &DoubleBarrier{
  32. s: s,
  33. ctx: context.TODO(),
  34. key: key,
  35. count: count,
  36. }
  37. }
  38. // Enter waits for "count" processes to enter the barrier then returns
  39. func (b *DoubleBarrier) Enter() error {
  40. client := b.s.Client()
  41. ek, err := newUniqueEphemeralKey(b.s, b.key+"/waiters")
  42. if err != nil {
  43. return err
  44. }
  45. b.myKey = ek
  46. resp, err := client.Get(b.ctx, b.key+"/waiters", clientv3.WithPrefix())
  47. if err != nil {
  48. return err
  49. }
  50. if len(resp.Kvs) > b.count {
  51. return ErrTooManyClients
  52. }
  53. if len(resp.Kvs) == b.count {
  54. // unblock waiters
  55. _, err = client.Put(b.ctx, b.key+"/ready", "")
  56. return err
  57. }
  58. _, err = WaitEvents(
  59. client,
  60. b.key+"/ready",
  61. ek.Revision(),
  62. []mvccpb.Event_EventType{mvccpb.PUT})
  63. return err
  64. }
  65. // Leave waits for "count" processes to leave the barrier then returns
  66. func (b *DoubleBarrier) Leave() error {
  67. client := b.s.Client()
  68. resp, err := client.Get(b.ctx, b.key+"/waiters", clientv3.WithPrefix())
  69. if err != nil {
  70. return err
  71. }
  72. if len(resp.Kvs) == 0 {
  73. return nil
  74. }
  75. lowest, highest := resp.Kvs[0], resp.Kvs[0]
  76. for _, k := range resp.Kvs {
  77. if k.ModRevision < lowest.ModRevision {
  78. lowest = k
  79. }
  80. if k.ModRevision > highest.ModRevision {
  81. highest = k
  82. }
  83. }
  84. isLowest := string(lowest.Key) == b.myKey.Key()
  85. if len(resp.Kvs) == 1 {
  86. // this is the only node in the barrier; finish up
  87. if _, err = client.Delete(b.ctx, b.key+"/ready"); err != nil {
  88. return err
  89. }
  90. return b.myKey.Delete()
  91. }
  92. // this ensures that if a process fails, the ephemeral lease will be
  93. // revoked, its barrier key is removed, and the barrier can resume
  94. // lowest process in node => wait on highest process
  95. if isLowest {
  96. _, err = WaitEvents(
  97. client,
  98. string(highest.Key),
  99. highest.ModRevision,
  100. []mvccpb.Event_EventType{mvccpb.DELETE})
  101. if err != nil {
  102. return err
  103. }
  104. return b.Leave()
  105. }
  106. // delete self and wait on lowest process
  107. if err = b.myKey.Delete(); err != nil {
  108. return err
  109. }
  110. key := string(lowest.Key)
  111. _, err = WaitEvents(
  112. client,
  113. key,
  114. lowest.ModRevision,
  115. []mvccpb.Event_EventType{mvccpb.DELETE})
  116. if err != nil {
  117. return err
  118. }
  119. return b.Leave()
  120. }