key.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. v3 "github.com/coreos/etcd/clientv3"
  20. "github.com/coreos/etcd/storage/storagepb"
  21. "golang.org/x/net/context"
  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.ModRevision(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 waitDelete(ctx context.Context, client *v3.Client, key string, rev int64) error {
  43. cctx, cancel := context.WithCancel(ctx)
  44. defer cancel()
  45. wch := client.Watch(cctx, key, v3.WithRev(rev))
  46. for wr := range wch {
  47. for _, ev := range wr.Events {
  48. if ev.Type == storagepb.DELETE {
  49. return nil
  50. }
  51. }
  52. }
  53. if err := ctx.Err(); err != nil {
  54. return err
  55. }
  56. return fmt.Errorf("lost watcher waiting for delete")
  57. }
  58. // waitDeletes efficiently waits until all keys matched by Get(key, opts...) are deleted
  59. func waitDeletes(ctx context.Context, client *v3.Client, key string, opts ...v3.OpOption) error {
  60. getOpts := []v3.OpOption{v3.WithSort(v3.SortByCreateRevision, v3.SortAscend)}
  61. getOpts = append(getOpts, opts...)
  62. resp, err := client.Get(ctx, key, getOpts...)
  63. maxRev := int64(math.MaxInt64)
  64. getOpts = append(getOpts, v3.WithRev(0))
  65. for err == nil {
  66. for len(resp.Kvs) > 0 {
  67. i := len(resp.Kvs) - 1
  68. if resp.Kvs[i].CreateRevision <= maxRev {
  69. break
  70. }
  71. resp.Kvs = resp.Kvs[:i]
  72. }
  73. if len(resp.Kvs) == 0 {
  74. break
  75. }
  76. lastKV := resp.Kvs[len(resp.Kvs)-1]
  77. maxRev = lastKV.CreateRevision
  78. err = waitDelete(ctx, client, string(lastKV.Key), maxRev)
  79. if err != nil || len(resp.Kvs) == 1 {
  80. break
  81. }
  82. getOpts = append(getOpts, v3.WithLimit(int64(len(resp.Kvs)-1)))
  83. resp, err = client.Get(ctx, key, getOpts...)
  84. }
  85. return err
  86. }