key.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2016 CoreOS, Inc.
  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 concurrency
  15. import (
  16. "fmt"
  17. "math"
  18. "time"
  19. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  20. v3 "github.com/coreos/etcd/clientv3"
  21. "github.com/coreos/etcd/storage/storagepb"
  22. )
  23. // NewUniqueKey creates a new key from a given prefix.
  24. func NewUniqueKey(ctx context.Context, kv v3.KV, pfx string, opts ...v3.OpOption) (string, int64, error) {
  25. return NewUniqueKV(ctx, kv, pfx, "", opts...)
  26. }
  27. func NewUniqueKV(ctx context.Context, kv v3.KV, pfx, val string, opts ...v3.OpOption) (string, int64, error) {
  28. for {
  29. newKey := fmt.Sprintf("%s/%v", pfx, time.Now().UnixNano())
  30. put := v3.OpPut(newKey, val, opts...)
  31. cmp := v3.Compare(v3.ModifiedRevision(newKey), "=", 0)
  32. resp, err := kv.Txn(ctx).If(cmp).Then(put).Commit()
  33. if err != nil {
  34. return "", 0, err
  35. }
  36. if !resp.Succeeded {
  37. continue
  38. }
  39. return newKey, resp.Header.Revision, nil
  40. }
  41. }
  42. func waitUpdate(ctx context.Context, client *v3.Client, key string, opts ...v3.OpOption) error {
  43. cctx, cancel := context.WithCancel(ctx)
  44. defer cancel()
  45. wresp, ok := <-client.Watch(cctx, key, opts...)
  46. if !ok {
  47. return ctx.Err()
  48. }
  49. return wresp.Err()
  50. }
  51. func waitDelete(ctx context.Context, client *v3.Client, key string, rev int64) error {
  52. cctx, cancel := context.WithCancel(ctx)
  53. defer cancel()
  54. wch := client.Watch(cctx, key, v3.WithRev(rev))
  55. for wr := range wch {
  56. for _, ev := range wr.Events {
  57. if ev.Type == storagepb.DELETE {
  58. return nil
  59. }
  60. }
  61. }
  62. if err := ctx.Err(); err != nil {
  63. return err
  64. }
  65. return fmt.Errorf("lost watcher waiting for delete")
  66. }
  67. // waitDeletes efficiently waits until all keys matched by Get(key, opts...) are deleted
  68. func waitDeletes(ctx context.Context, client *v3.Client, key string, opts ...v3.OpOption) error {
  69. getOpts := []v3.OpOption{v3.WithSort(v3.SortByCreatedRev, v3.SortAscend)}
  70. getOpts = append(getOpts, opts...)
  71. resp, err := client.Get(ctx, key, getOpts...)
  72. maxRev := int64(math.MaxInt64)
  73. getOpts = append(getOpts, v3.WithRev(0))
  74. for err == nil {
  75. for len(resp.Kvs) > 0 {
  76. i := len(resp.Kvs) - 1
  77. if resp.Kvs[i].CreateRevision <= maxRev {
  78. break
  79. }
  80. resp.Kvs = resp.Kvs[:i]
  81. }
  82. if len(resp.Kvs) == 0 {
  83. break
  84. }
  85. lastKV := resp.Kvs[len(resp.Kvs)-1]
  86. maxRev = lastKV.CreateRevision
  87. err = waitDelete(ctx, client, string(lastKV.Key), maxRev)
  88. if err != nil || len(resp.Kvs) == 1 {
  89. break
  90. }
  91. getOpts = append(getOpts, v3.WithLimit(int64(len(resp.Kvs)-1)))
  92. resp, err = client.Get(ctx, key, getOpts...)
  93. }
  94. return err
  95. }