blob: 92e365c47151a65ed0eb7b9c41a85507756483ae [file] [log] [blame]
Abhay Kumar40252eb2025-10-13 13:25:53 +00001// Copyright 2016 The etcd Authors
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
15package concurrency
16
17import (
18 "context"
19 "errors"
20
21 "go.etcd.io/etcd/api/v3/mvccpb"
22 v3 "go.etcd.io/etcd/client/v3"
23)
24
25func waitDelete(ctx context.Context, client *v3.Client, key string, rev int64) error {
26 cctx, cancel := context.WithCancel(ctx)
27 defer cancel()
28
29 var wr v3.WatchResponse
30 wch := client.Watch(cctx, key, v3.WithRev(rev))
31 for wr = range wch {
32 for _, ev := range wr.Events {
33 if ev.Type == mvccpb.DELETE {
34 return nil
35 }
36 }
37 }
38 if err := wr.Err(); err != nil {
39 return err
40 }
41 if err := ctx.Err(); err != nil {
42 return err
43 }
44 return errors.New("lost watcher waiting for delete")
45}
46
47// waitDeletes efficiently waits until all keys matching the prefix and no greater
48// than the create revision are deleted.
49func waitDeletes(ctx context.Context, client *v3.Client, pfx string, maxCreateRev int64) error {
50 getOpts := append(v3.WithLastCreate(), v3.WithMaxCreateRev(maxCreateRev))
51 for {
52 resp, err := client.Get(ctx, pfx, getOpts...)
53 if err != nil {
54 return err
55 }
56 if len(resp.Kvs) == 0 {
57 return nil
58 }
59 lastKey := string(resp.Kvs[0].Key)
60 if err = waitDelete(ctx, client, lastKey, resp.Header.Revision); err != nil {
61 return err
62 }
63 }
64}