-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into goroutine-pool
- Loading branch information
Showing
454 changed files
with
62,341 additions
and
24,639 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
_vendor/src/github.com/coreos/etcd/contrib/recipes/barrier.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2016 The etcd Authors | ||
// | ||
// 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 ( | ||
v3 "github.com/coreos/etcd/clientv3" | ||
"github.com/coreos/etcd/mvcc/mvccpb" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
// Barrier creates a key in etcd to block processes, then deletes the key to | ||
// release all blocked processes. | ||
type Barrier struct { | ||
client *v3.Client | ||
ctx context.Context | ||
|
||
key string | ||
} | ||
|
||
func NewBarrier(client *v3.Client, key string) *Barrier { | ||
return &Barrier{client, context.TODO(), key} | ||
} | ||
|
||
// Hold creates the barrier key causing processes to block on Wait. | ||
func (b *Barrier) Hold() error { | ||
_, err := newKey(b.client, b.key, 0) | ||
return err | ||
} | ||
|
||
// Release deletes the barrier key to unblock all waiting processes. | ||
func (b *Barrier) Release() error { | ||
_, err := b.client.Delete(b.ctx, b.key) | ||
return err | ||
} | ||
|
||
// Wait blocks on the barrier key until it is deleted. If there is no key, Wait | ||
// assumes Release has already been called and returns immediately. | ||
func (b *Barrier) Wait() error { | ||
resp, err := b.client.Get(b.ctx, b.key, v3.WithFirstKey()...) | ||
if err != nil { | ||
return err | ||
} | ||
if len(resp.Kvs) == 0 { | ||
// key already removed | ||
return nil | ||
} | ||
_, err = WaitEvents( | ||
b.client, | ||
b.key, | ||
resp.Header.Revision, | ||
[]mvccpb.Event_EventType{mvccpb.PUT, mvccpb.DELETE}) | ||
return err | ||
} |
55 changes: 55 additions & 0 deletions
55
_vendor/src/github.com/coreos/etcd/contrib/recipes/client.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2016 The etcd Authors | ||
// | ||
// 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 ( | ||
"errors" | ||
|
||
v3 "github.com/coreos/etcd/clientv3" | ||
spb "github.com/coreos/etcd/mvcc/mvccpb" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
var ( | ||
ErrKeyExists = errors.New("key already exists") | ||
ErrWaitMismatch = errors.New("unexpected wait result") | ||
ErrTooManyClients = errors.New("too many clients") | ||
ErrNoWatcher = errors.New("no watcher channel") | ||
) | ||
|
||
// deleteRevKey deletes a key by revision, returning false if key is missing | ||
func deleteRevKey(kv v3.KV, key string, rev int64) (bool, error) { | ||
cmp := v3.Compare(v3.ModRevision(key), "=", rev) | ||
req := v3.OpDelete(key) | ||
txnresp, err := kv.Txn(context.TODO()).If(cmp).Then(req).Commit() | ||
if err != nil { | ||
return false, err | ||
} else if !txnresp.Succeeded { | ||
return false, nil | ||
} | ||
return true, nil | ||
} | ||
|
||
func claimFirstKey(kv v3.KV, kvs []*spb.KeyValue) (*spb.KeyValue, error) { | ||
for _, k := range kvs { | ||
ok, err := deleteRevKey(kv, string(k.Key), k.ModRevision) | ||
if err != nil { | ||
return nil, err | ||
} else if ok { | ||
return k, nil | ||
} | ||
} | ||
return nil, nil | ||
} |
137 changes: 137 additions & 0 deletions
137
_vendor/src/github.com/coreos/etcd/contrib/recipes/double_barrier.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
// Copyright 2016 The etcd Authors | ||
// | ||
// 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/clientv3" | ||
"github.com/coreos/etcd/clientv3/concurrency" | ||
"github.com/coreos/etcd/mvcc/mvccpb" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
// DoubleBarrier blocks processes on Enter until an expected count enters, then | ||
// blocks again on Leave until all processes have left. | ||
type DoubleBarrier struct { | ||
s *concurrency.Session | ||
ctx context.Context | ||
|
||
key string // key for the collective barrier | ||
count int | ||
myKey *EphemeralKV // current key for this process on the barrier | ||
} | ||
|
||
func NewDoubleBarrier(s *concurrency.Session, key string, count int) *DoubleBarrier { | ||
return &DoubleBarrier{ | ||
s: s, | ||
ctx: context.TODO(), | ||
key: key, | ||
count: count, | ||
} | ||
} | ||
|
||
// Enter waits for "count" processes to enter the barrier then returns | ||
func (b *DoubleBarrier) Enter() error { | ||
client := b.s.Client() | ||
ek, err := newUniqueEphemeralKey(b.s, b.key+"/waiters") | ||
if err != nil { | ||
return err | ||
} | ||
b.myKey = ek | ||
|
||
resp, err := client.Get(b.ctx, b.key+"/waiters", clientv3.WithPrefix()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(resp.Kvs) > b.count { | ||
return ErrTooManyClients | ||
} | ||
|
||
if len(resp.Kvs) == b.count { | ||
// unblock waiters | ||
_, err = client.Put(b.ctx, b.key+"/ready", "") | ||
return err | ||
} | ||
|
||
_, err = WaitEvents( | ||
client, | ||
b.key+"/ready", | ||
ek.Revision(), | ||
[]mvccpb.Event_EventType{mvccpb.PUT}) | ||
return err | ||
} | ||
|
||
// Leave waits for "count" processes to leave the barrier then returns | ||
func (b *DoubleBarrier) Leave() error { | ||
client := b.s.Client() | ||
resp, err := client.Get(b.ctx, b.key+"/waiters", clientv3.WithPrefix()) | ||
if err != nil { | ||
return err | ||
} | ||
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 | ||
if _, err = client.Delete(b.ctx, b.key+"/ready"); 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( | ||
client, | ||
string(highest.Key), | ||
highest.ModRevision, | ||
[]mvccpb.Event_EventType{mvccpb.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( | ||
client, | ||
key, | ||
lowest.ModRevision, | ||
[]mvccpb.Event_EventType{mvccpb.DELETE}) | ||
if err != nil { | ||
return err | ||
} | ||
return b.Leave() | ||
} |
Oops, something went wrong.