| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 1 | // 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 | |
| 15 | package concurrency |
| 16 | |
| 17 | import ( |
| 18 | "context" |
| 19 | "errors" |
| 20 | "fmt" |
| 21 | "strings" |
| 22 | "sync" |
| 23 | |
| 24 | pb "go.etcd.io/etcd/api/v3/etcdserverpb" |
| 25 | v3 "go.etcd.io/etcd/client/v3" |
| 26 | ) |
| 27 | |
| 28 | // ErrLocked is returned by TryLock when Mutex is already locked by another session. |
| 29 | var ( |
| 30 | ErrLocked = errors.New("mutex: Locked by another session") |
| 31 | ErrSessionExpired = errors.New("mutex: session is expired") |
| 32 | ErrLockReleased = errors.New("mutex: lock has already been released") |
| 33 | ) |
| 34 | |
| 35 | // Mutex implements the sync Locker interface with etcd |
| 36 | type Mutex struct { |
| 37 | s *Session |
| 38 | |
| 39 | pfx string |
| 40 | myKey string |
| 41 | myRev int64 |
| 42 | hdr *pb.ResponseHeader |
| 43 | } |
| 44 | |
| 45 | func NewMutex(s *Session, pfx string) *Mutex { |
| 46 | return &Mutex{s, pfx + "/", "", -1, nil} |
| 47 | } |
| 48 | |
| 49 | // TryLock locks the mutex if not already locked by another session. |
| 50 | // If lock is held by another session, return immediately after attempting necessary cleanup |
| 51 | // The ctx argument is used for the sending/receiving Txn RPC. |
| 52 | func (m *Mutex) TryLock(ctx context.Context) error { |
| 53 | resp, err := m.tryAcquire(ctx) |
| 54 | if err != nil { |
| 55 | return err |
| 56 | } |
| 57 | // if no key on prefix / the minimum rev is key, already hold the lock |
| 58 | ownerKey := resp.Responses[1].GetResponseRange().Kvs |
| 59 | if len(ownerKey) == 0 || ownerKey[0].CreateRevision == m.myRev { |
| 60 | m.hdr = resp.Header |
| 61 | return nil |
| 62 | } |
| 63 | client := m.s.Client() |
| 64 | // Cannot lock, so delete the key |
| 65 | if _, err := client.Delete(ctx, m.myKey); err != nil { |
| 66 | return err |
| 67 | } |
| 68 | m.myKey = "\x00" |
| 69 | m.myRev = -1 |
| 70 | return ErrLocked |
| 71 | } |
| 72 | |
| 73 | // Lock locks the mutex with a cancelable context. If the context is canceled |
| 74 | // while trying to acquire the lock, the mutex tries to clean its stale lock entry. |
| 75 | func (m *Mutex) Lock(ctx context.Context) error { |
| 76 | resp, err := m.tryAcquire(ctx) |
| 77 | if err != nil { |
| 78 | return err |
| 79 | } |
| 80 | // if no key on prefix / the minimum rev is key, already hold the lock |
| 81 | ownerKey := resp.Responses[1].GetResponseRange().Kvs |
| 82 | if len(ownerKey) == 0 || ownerKey[0].CreateRevision == m.myRev { |
| 83 | m.hdr = resp.Header |
| 84 | return nil |
| 85 | } |
| 86 | client := m.s.Client() |
| 87 | // wait for deletion revisions prior to myKey |
| 88 | // TODO: early termination if the session key is deleted before other session keys with smaller revisions. |
| 89 | werr := waitDeletes(ctx, client, m.pfx, m.myRev-1) |
| 90 | // release lock key if wait failed |
| 91 | if werr != nil { |
| 92 | m.Unlock(client.Ctx()) |
| 93 | return werr |
| 94 | } |
| 95 | |
| 96 | // make sure the session is not expired, and the owner key still exists. |
| 97 | gresp, werr := client.Get(ctx, m.myKey) |
| 98 | if werr != nil { |
| 99 | m.Unlock(client.Ctx()) |
| 100 | return werr |
| 101 | } |
| 102 | |
| 103 | if len(gresp.Kvs) == 0 { // is the session key lost? |
| 104 | return ErrSessionExpired |
| 105 | } |
| 106 | m.hdr = gresp.Header |
| 107 | |
| 108 | return nil |
| 109 | } |
| 110 | |
| 111 | func (m *Mutex) tryAcquire(ctx context.Context) (*v3.TxnResponse, error) { |
| 112 | s := m.s |
| 113 | client := m.s.Client() |
| 114 | |
| 115 | m.myKey = fmt.Sprintf("%s%x", m.pfx, s.Lease()) |
| 116 | cmp := v3.Compare(v3.CreateRevision(m.myKey), "=", 0) |
| 117 | // put self in lock waiters via myKey; oldest waiter holds lock |
| 118 | put := v3.OpPut(m.myKey, "", v3.WithLease(s.Lease())) |
| 119 | // reuse key in case this session already holds the lock |
| 120 | get := v3.OpGet(m.myKey) |
| 121 | // fetch current holder to complete uncontended path with only one RPC |
| 122 | getOwner := v3.OpGet(m.pfx, v3.WithFirstCreate()...) |
| 123 | resp, err := client.Txn(ctx).If(cmp).Then(put, getOwner).Else(get, getOwner).Commit() |
| 124 | if err != nil { |
| 125 | return nil, err |
| 126 | } |
| 127 | m.myRev = resp.Header.Revision |
| 128 | if !resp.Succeeded { |
| 129 | m.myRev = resp.Responses[0].GetResponseRange().Kvs[0].CreateRevision |
| 130 | } |
| 131 | return resp, nil |
| 132 | } |
| 133 | |
| 134 | func (m *Mutex) Unlock(ctx context.Context) error { |
| 135 | if m.myKey == "" || m.myRev <= 0 || m.myKey == "\x00" { |
| 136 | return ErrLockReleased |
| 137 | } |
| 138 | |
| 139 | if !strings.HasPrefix(m.myKey, m.pfx) { |
| 140 | return fmt.Errorf("invalid key %q, it should have prefix %q", m.myKey, m.pfx) |
| 141 | } |
| 142 | |
| 143 | client := m.s.Client() |
| 144 | if _, err := client.Delete(ctx, m.myKey); err != nil { |
| 145 | return err |
| 146 | } |
| 147 | m.myKey = "\x00" |
| 148 | m.myRev = -1 |
| 149 | return nil |
| 150 | } |
| 151 | |
| 152 | func (m *Mutex) IsOwner() v3.Cmp { |
| 153 | return v3.Compare(v3.CreateRevision(m.myKey), "=", m.myRev) |
| 154 | } |
| 155 | |
| 156 | func (m *Mutex) Key() string { return m.myKey } |
| 157 | |
| 158 | // Header is the response header received from etcd on acquiring the lock. |
| 159 | func (m *Mutex) Header() *pb.ResponseHeader { return m.hdr } |
| 160 | |
| 161 | type lockerMutex struct{ *Mutex } |
| 162 | |
| 163 | func (lm *lockerMutex) Lock() { |
| 164 | client := lm.s.Client() |
| 165 | if err := lm.Mutex.Lock(client.Ctx()); err != nil { |
| 166 | panic(err) |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | func (lm *lockerMutex) Unlock() { |
| 171 | client := lm.s.Client() |
| 172 | if err := lm.Mutex.Unlock(client.Ctx()); err != nil { |
| 173 | panic(err) |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // NewLocker creates a sync.Locker backed by an etcd mutex. |
| 178 | func NewLocker(s *Session, pfx string) sync.Locker { |
| 179 | return &lockerMutex{NewMutex(s, pfx)} |
| 180 | } |