Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [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 | "fmt" |
| 20 | "sync" |
| 21 | |
| 22 | v3 "go.etcd.io/etcd/clientv3" |
| 23 | pb "go.etcd.io/etcd/etcdserver/etcdserverpb" |
| 24 | ) |
| 25 | |
| 26 | // Mutex implements the sync Locker interface with etcd |
| 27 | type Mutex struct { |
| 28 | s *Session |
| 29 | |
| 30 | pfx string |
| 31 | myKey string |
| 32 | myRev int64 |
| 33 | hdr *pb.ResponseHeader |
| 34 | } |
| 35 | |
| 36 | func NewMutex(s *Session, pfx string) *Mutex { |
| 37 | return &Mutex{s, pfx + "/", "", -1, nil} |
| 38 | } |
| 39 | |
| 40 | // Lock locks the mutex with a cancelable context. If the context is canceled |
| 41 | // while trying to acquire the lock, the mutex tries to clean its stale lock entry. |
| 42 | func (m *Mutex) Lock(ctx context.Context) error { |
| 43 | s := m.s |
| 44 | client := m.s.Client() |
| 45 | |
| 46 | m.myKey = fmt.Sprintf("%s%x", m.pfx, s.Lease()) |
| 47 | cmp := v3.Compare(v3.CreateRevision(m.myKey), "=", 0) |
| 48 | // put self in lock waiters via myKey; oldest waiter holds lock |
| 49 | put := v3.OpPut(m.myKey, "", v3.WithLease(s.Lease())) |
| 50 | // reuse key in case this session already holds the lock |
| 51 | get := v3.OpGet(m.myKey) |
| 52 | // fetch current holder to complete uncontended path with only one RPC |
| 53 | getOwner := v3.OpGet(m.pfx, v3.WithFirstCreate()...) |
| 54 | resp, err := client.Txn(ctx).If(cmp).Then(put, getOwner).Else(get, getOwner).Commit() |
| 55 | if err != nil { |
| 56 | return err |
| 57 | } |
| 58 | m.myRev = resp.Header.Revision |
| 59 | if !resp.Succeeded { |
| 60 | m.myRev = resp.Responses[0].GetResponseRange().Kvs[0].CreateRevision |
| 61 | } |
| 62 | // if no key on prefix / the minimum rev is key, already hold the lock |
| 63 | ownerKey := resp.Responses[1].GetResponseRange().Kvs |
| 64 | if len(ownerKey) == 0 || ownerKey[0].CreateRevision == m.myRev { |
| 65 | m.hdr = resp.Header |
| 66 | return nil |
| 67 | } |
| 68 | |
| 69 | // wait for deletion revisions prior to myKey |
| 70 | hdr, werr := waitDeletes(ctx, client, m.pfx, m.myRev-1) |
| 71 | // release lock key if wait failed |
| 72 | if werr != nil { |
| 73 | m.Unlock(client.Ctx()) |
| 74 | } else { |
| 75 | m.hdr = hdr |
| 76 | } |
| 77 | return werr |
| 78 | } |
| 79 | |
| 80 | func (m *Mutex) Unlock(ctx context.Context) error { |
| 81 | client := m.s.Client() |
| 82 | if _, err := client.Delete(ctx, m.myKey); err != nil { |
| 83 | return err |
| 84 | } |
| 85 | m.myKey = "\x00" |
| 86 | m.myRev = -1 |
| 87 | return nil |
| 88 | } |
| 89 | |
| 90 | func (m *Mutex) IsOwner() v3.Cmp { |
| 91 | return v3.Compare(v3.CreateRevision(m.myKey), "=", m.myRev) |
| 92 | } |
| 93 | |
| 94 | func (m *Mutex) Key() string { return m.myKey } |
| 95 | |
| 96 | // Header is the response header received from etcd on acquiring the lock. |
| 97 | func (m *Mutex) Header() *pb.ResponseHeader { return m.hdr } |
| 98 | |
| 99 | type lockerMutex struct{ *Mutex } |
| 100 | |
| 101 | func (lm *lockerMutex) Lock() { |
| 102 | client := lm.s.Client() |
| 103 | if err := lm.Mutex.Lock(client.Ctx()); err != nil { |
| 104 | panic(err) |
| 105 | } |
| 106 | } |
| 107 | func (lm *lockerMutex) Unlock() { |
| 108 | client := lm.s.Client() |
| 109 | if err := lm.Mutex.Unlock(client.Ctx()); err != nil { |
| 110 | panic(err) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // NewLocker creates a sync.Locker backed by an etcd mutex. |
| 115 | func NewLocker(s *Session, pfx string) sync.Locker { |
| 116 | return &lockerMutex{NewMutex(s, pfx)} |
| 117 | } |