forked from etcd-io/etcd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
double_barrier.go
126 lines (110 loc) · 3.25 KB
/
double_barrier.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2016 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package recipe
import (
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
"github.com/coreos/etcd/clientv3"
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
"github.com/coreos/etcd/storage/storagepb"
)
// DoubleBarrier blocks processes on Enter until an expected count enters, then
// blocks again on Leave until all processes have left.
type DoubleBarrier struct {
client *clientv3.Client
key string // key for the collective barrier
count int
myKey *EphemeralKV // current key for this process on the barrier
}
func NewDoubleBarrier(client *clientv3.Client, key string, count int) *DoubleBarrier {
return &DoubleBarrier{client, key, count, nil}
}
// Enter waits for "count" processes to enter the barrier then returns
func (b *DoubleBarrier) Enter() error {
ek, err := NewUniqueEphemeralKey(b.client, b.key+"/waiters")
if err != nil {
return err
}
b.myKey = ek
resp, err := NewRange(b.client, b.key+"/waiters").Prefix()
if err != nil {
return err
}
if len(resp.Kvs) > b.count {
return ErrTooManyClients
}
if len(resp.Kvs) == b.count {
// unblock waiters
_, err = putEmptyKey(b.client.KV, b.key+"/ready")
return err
}
_, err = WaitEvents(
b.client,
b.key+"/ready",
ek.Revision(),
[]storagepb.Event_EventType{storagepb.PUT})
return err
}
// Leave waits for "count" processes to leave the barrier then returns
func (b *DoubleBarrier) Leave() error {
resp, err := NewRange(b.client, b.key+"/waiters").Prefix()
if len(resp.Kvs) == 0 {
return nil
}
lowest, highest := resp.Kvs[0], resp.Kvs[0]
for _, k := range resp.Kvs {
if k.ModRevision < lowest.ModRevision {
lowest = k
}
if k.ModRevision > highest.ModRevision {
highest = k
}
}
isLowest := string(lowest.Key) == b.myKey.Key()
if len(resp.Kvs) == 1 {
// this is the only node in the barrier; finish up
req := &pb.DeleteRangeRequest{Key: []byte(b.key + "/ready")}
if _, err = b.client.KV.DeleteRange(context.TODO(), req); err != nil {
return err
}
return b.myKey.Delete()
}
// this ensures that if a process fails, the ephemeral lease will be
// revoked, its barrier key is removed, and the barrier can resume
// lowest process in node => wait on highest process
if isLowest {
_, err = WaitEvents(
b.client,
string(highest.Key),
highest.ModRevision,
[]storagepb.Event_EventType{storagepb.DELETE})
if err != nil {
return err
}
return b.Leave()
}
// delete self and wait on lowest process
if err := b.myKey.Delete(); err != nil {
return err
}
key := string(lowest.Key)
_, err = WaitEvents(
b.client,
key,
lowest.ModRevision,
[]storagepb.Event_EventType{storagepb.DELETE})
if err != nil {
return err
}
return b.Leave()
}