2016-05-13 06:51:48 +03:00
|
|
|
// Copyright 2016 The etcd Authors
|
2016-01-20 03:30:31 +03:00
|
|
|
//
|
|
|
|
// 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 (
|
2017-09-07 00:57:25 +03:00
|
|
|
"context"
|
|
|
|
|
2019-05-29 00:59:02 +03:00
|
|
|
"go.etcd.io/etcd/clientv3"
|
|
|
|
"go.etcd.io/etcd/clientv3/concurrency"
|
|
|
|
"go.etcd.io/etcd/mvcc/mvccpb"
|
2016-01-20 03:30:31 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// DoubleBarrier blocks processes on Enter until an expected count enters, then
|
|
|
|
// blocks again on Leave until all processes have left.
|
|
|
|
type DoubleBarrier struct {
|
2016-08-14 19:59:42 +03:00
|
|
|
s *concurrency.Session
|
|
|
|
ctx context.Context
|
2016-02-21 03:07:22 +03:00
|
|
|
|
|
|
|
key string // key for the collective barrier
|
|
|
|
count int
|
|
|
|
myKey *EphemeralKV // current key for this process on the barrier
|
2016-01-20 03:30:31 +03:00
|
|
|
}
|
|
|
|
|
2016-08-14 19:59:42 +03:00
|
|
|
func NewDoubleBarrier(s *concurrency.Session, key string, count int) *DoubleBarrier {
|
2016-02-21 03:07:22 +03:00
|
|
|
return &DoubleBarrier{
|
2016-08-14 19:59:42 +03:00
|
|
|
s: s,
|
|
|
|
ctx: context.TODO(),
|
|
|
|
key: key,
|
|
|
|
count: count,
|
2016-02-21 03:07:22 +03:00
|
|
|
}
|
2016-01-20 03:30:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enter waits for "count" processes to enter the barrier then returns
|
|
|
|
func (b *DoubleBarrier) Enter() error {
|
2016-08-14 19:59:42 +03:00
|
|
|
client := b.s.Client()
|
2016-10-27 22:02:58 +03:00
|
|
|
ek, err := newUniqueEphemeralKey(b.s, b.key+"/waiters")
|
2016-01-20 03:30:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.myKey = ek
|
|
|
|
|
2016-08-14 19:59:42 +03:00
|
|
|
resp, err := client.Get(b.ctx, b.key+"/waiters", clientv3.WithPrefix())
|
2016-01-20 03:30:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Kvs) > b.count {
|
|
|
|
return ErrTooManyClients
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Kvs) == b.count {
|
|
|
|
// unblock waiters
|
2016-08-14 19:59:42 +03:00
|
|
|
_, err = client.Put(b.ctx, b.key+"/ready", "")
|
2016-01-20 03:30:31 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = WaitEvents(
|
2016-08-14 19:59:42 +03:00
|
|
|
client,
|
2016-01-20 03:30:31 +03:00
|
|
|
b.key+"/ready",
|
2016-02-13 03:54:39 +03:00
|
|
|
ek.Revision(),
|
2016-04-25 22:32:58 +03:00
|
|
|
[]mvccpb.Event_EventType{mvccpb.PUT})
|
2016-01-20 03:30:31 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Leave waits for "count" processes to leave the barrier then returns
|
|
|
|
func (b *DoubleBarrier) Leave() error {
|
2016-08-14 19:59:42 +03:00
|
|
|
client := b.s.Client()
|
|
|
|
resp, err := client.Get(b.ctx, b.key+"/waiters", clientv3.WithPrefix())
|
2016-03-04 11:34:36 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-20 03:30:31 +03:00
|
|
|
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
|
2016-08-14 19:59:42 +03:00
|
|
|
if _, err = client.Delete(b.ctx, b.key+"/ready"); err != nil {
|
2016-01-20 03:30:31 +03:00
|
|
|
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(
|
2016-08-14 19:59:42 +03:00
|
|
|
client,
|
2016-01-20 03:30:31 +03:00
|
|
|
string(highest.Key),
|
2016-02-13 03:54:39 +03:00
|
|
|
highest.ModRevision,
|
2016-04-25 22:32:58 +03:00
|
|
|
[]mvccpb.Event_EventType{mvccpb.DELETE})
|
2016-01-20 03:30:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return b.Leave()
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete self and wait on lowest process
|
2016-06-17 21:48:10 +03:00
|
|
|
if err = b.myKey.Delete(); err != nil {
|
2016-01-20 03:30:31 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
key := string(lowest.Key)
|
|
|
|
_, err = WaitEvents(
|
2016-08-14 19:59:42 +03:00
|
|
|
client,
|
2016-01-20 03:30:31 +03:00
|
|
|
key,
|
2016-02-13 03:54:39 +03:00
|
|
|
lowest.ModRevision,
|
2016-04-25 22:32:58 +03:00
|
|
|
[]mvccpb.Event_EventType{mvccpb.DELETE})
|
2016-01-20 03:30:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return b.Leave()
|
|
|
|
}
|