Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [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 | "sync" |
| 22 | |
| 23 | v3 "go.etcd.io/etcd/clientv3" |
| 24 | pb "go.etcd.io/etcd/etcdserver/etcdserverpb" |
| 25 | ) |
| 26 | |
| 27 | // ErrLocked is returned by TryLock when Mutex is already locked by another session. |
| 28 | var ErrLocked = errors.New("mutex: Locked by another session") |
| 29 | |
| 30 | // Mutex implements the sync Locker interface with etcd |
| 31 | type Mutex struct { |
| 32 | s *Session |
| 33 | |
| 34 | pfx string |
| 35 | myKey string |
| 36 | myRev int64 |
| 37 | hdr *pb.ResponseHeader |
| 38 | } |
| 39 | |
| 40 | func NewMutex(s *Session, pfx string) *Mutex { |
| 41 | return &Mutex{s, pfx + "/", "", -1, nil} |
| 42 | } |
| 43 | |
| 44 | // TryLock locks the mutex if not already locked by another session. |
| 45 | // If lock is held by another session, return immediately after attempting necessary cleanup |
| 46 | // The ctx argument is used for the sending/receiving Txn RPC. |
| 47 | func (m *Mutex) TryLock(ctx context.Context) error { |
| 48 | resp, err := m.tryAcquire(ctx) |
| 49 | if err != nil { |
| 50 | return err |
| 51 | } |
| 52 | // if no key on prefix / the minimum rev is key, already hold the lock |
| 53 | ownerKey := resp.Responses[1].GetResponseRange().Kvs |
| 54 | if len(ownerKey) == 0 || ownerKey[0].CreateRevision == m.myRev { |
| 55 | m.hdr = resp.Header |
| 56 | return nil |
| 57 | } |
| 58 | client := m.s.Client() |
| 59 | // Cannot lock, so delete the key |
| 60 | if _, err := client.Delete(ctx, m.myKey); err != nil { |
| 61 | return err |
| 62 | } |
| 63 | m.myKey = "\x00" |
| 64 | m.myRev = -1 |
| 65 | return ErrLocked |
| 66 | } |
| 67 | |
| 68 | // Lock locks the mutex with a cancelable context. If the context is canceled |
| 69 | // while trying to acquire the lock, the mutex tries to clean its stale lock entry. |
| 70 | func (m *Mutex) Lock(ctx context.Context) error { |
| 71 | resp, err := m.tryAcquire(ctx) |
| 72 | if err != nil { |
| 73 | return err |
| 74 | } |
| 75 | // if no key on prefix / the minimum rev is key, already hold the lock |
| 76 | ownerKey := resp.Responses[1].GetResponseRange().Kvs |
| 77 | if len(ownerKey) == 0 || ownerKey[0].CreateRevision == m.myRev { |
| 78 | m.hdr = resp.Header |
| 79 | return nil |
| 80 | } |
| 81 | client := m.s.Client() |
| 82 | // wait for deletion revisions prior to myKey |
| 83 | hdr, werr := waitDeletes(ctx, client, m.pfx, m.myRev-1) |
| 84 | // release lock key if wait failed |
| 85 | if werr != nil { |
| 86 | m.Unlock(client.Ctx()) |
| 87 | } else { |
| 88 | m.hdr = hdr |
| 89 | } |
| 90 | return werr |
| 91 | } |
| 92 | |
| 93 | func (m *Mutex) tryAcquire(ctx context.Context) (*v3.TxnResponse, error) { |
| 94 | s := m.s |
| 95 | client := m.s.Client() |
| 96 | |
| 97 | m.myKey = fmt.Sprintf("%s%x", m.pfx, s.Lease()) |
| 98 | cmp := v3.Compare(v3.CreateRevision(m.myKey), "=", 0) |
| 99 | // put self in lock waiters via myKey; oldest waiter holds lock |
| 100 | put := v3.OpPut(m.myKey, "", v3.WithLease(s.Lease())) |
| 101 | // reuse key in case this session already holds the lock |
| 102 | get := v3.OpGet(m.myKey) |
| 103 | // fetch current holder to complete uncontended path with only one RPC |
| 104 | getOwner := v3.OpGet(m.pfx, v3.WithFirstCreate()...) |
| 105 | resp, err := client.Txn(ctx).If(cmp).Then(put, getOwner).Else(get, getOwner).Commit() |
| 106 | if err != nil { |
| 107 | return nil, err |
| 108 | } |
| 109 | m.myRev = resp.Header.Revision |
| 110 | if !resp.Succeeded { |
| 111 | m.myRev = resp.Responses[0].GetResponseRange().Kvs[0].CreateRevision |
| 112 | } |
| 113 | return resp, nil |
| 114 | } |
| 115 | |
| 116 | func (m *Mutex) Unlock(ctx context.Context) error { |
| 117 | client := m.s.Client() |
| 118 | if _, err := client.Delete(ctx, m.myKey); err != nil { |
| 119 | return err |
| 120 | } |
| 121 | m.myKey = "\x00" |
| 122 | m.myRev = -1 |
| 123 | return nil |
| 124 | } |
| 125 | |
| 126 | func (m *Mutex) IsOwner() v3.Cmp { |
| 127 | return v3.Compare(v3.CreateRevision(m.myKey), "=", m.myRev) |
| 128 | } |
| 129 | |
| 130 | func (m *Mutex) Key() string { return m.myKey } |
| 131 | |
| 132 | // Header is the response header received from etcd on acquiring the lock. |
| 133 | func (m *Mutex) Header() *pb.ResponseHeader { return m.hdr } |
| 134 | |
| 135 | type lockerMutex struct{ *Mutex } |
| 136 | |
| 137 | func (lm *lockerMutex) Lock() { |
| 138 | client := lm.s.Client() |
| 139 | if err := lm.Mutex.Lock(client.Ctx()); err != nil { |
| 140 | panic(err) |
| 141 | } |
| 142 | } |
| 143 | func (lm *lockerMutex) Unlock() { |
| 144 | client := lm.s.Client() |
| 145 | if err := lm.Mutex.Unlock(client.Ctx()); err != nil { |
| 146 | panic(err) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // NewLocker creates a sync.Locker backed by an etcd mutex. |
| 151 | func NewLocker(s *Session, pfx string) sync.Locker { |
| 152 | return &lockerMutex{NewMutex(s, pfx)} |
| 153 | } |