Skip to content

Commit e44ecfb

Browse files
committed
Define a DLocker interface for distributed locker
Also updated the contrib/lock example to demo how to use the DLocker Signed-off-by: Benjamin Wang <[email protected]>
1 parent 2f36df8 commit e44ecfb

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

client/v3/concurrency/mutex.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ func (m *Mutex) IsOwner() v3.Cmp {
152152
}
153153

154154
func (m *Mutex) Key() string { return m.myKey }
155+
func (m *Mutex) Rev() int64 { return m.myRev }
155156

156157
// Header is the response header received from etcd on acquiring the lock.
157158
func (m *Mutex) Header() *pb.ResponseHeader { return m.hdr }
@@ -171,7 +172,17 @@ func (lm *lockerMutex) Unlock() {
171172
}
172173
}
173174

174-
// NewLocker creates a sync.Locker backed by an etcd mutex.
175-
func NewLocker(s *Session, pfx string) sync.Locker {
175+
// NewLocker creates a DLocker backed by an etcd mutex.
176+
func NewLocker(s *Session, pfx string) DLocker {
176177
return &lockerMutex{NewMutex(s, pfx)}
177178
}
179+
180+
// A DLocker represents an object that can be locked and unlocked
181+
// in distributed environment.
182+
type DLocker interface {
183+
sync.Locker
184+
// Rev returns a revision which is monotonically incremental. It can
185+
// be used as a fencing token to prevent expired locker from operating
186+
// the shared resource.
187+
Rev() int64
188+
}

contrib/lock/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ If things go well the second client process invoked as `./client 2` finishes soo
6464
After checking this, please hit any key for `./client 1` and resume the process. It will show an output like below:
6565
```
6666
resuming client 1
67-
expected fail to write to storage with old lease version: error: given version (694d82254d5fa305) is different from the existing version (694d82254e18770a)
67+
expected fail to write to storage with old lease version: error: given version (8) is smaller than the existing version (10)
6868
```
6969

7070
[fencing]: https://martin.kleppmann.com/2016/02/08/how-to-do-distributed-locking.html

contrib/lock/client/client.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -121,26 +121,27 @@ func main() {
121121
locker := concurrency.NewLocker(session, "/lock")
122122
locker.Lock()
123123
defer locker.Unlock()
124-
version := session.Lease()
124+
leaseID := session.Lease()
125+
version := locker.Rev()
125126
log.Printf("acquired lock, version: %x", version)
126127

127128
if mode == 1 {
128-
log.Printf("please manually revoke the lease using 'etcdctl lease revoke %x' or wait for it to expire, then start executing client 2 and hit any key...", version)
129+
log.Printf("please manually revoke the lease using 'etcdctl lease revoke %x' or wait for it to expire, then start executing client 2 and hit any key...", leaseID)
129130
reader := bufio.NewReader(os.Stdin)
130131
_, _ = reader.ReadByte()
131132
log.Print("resuming client 1")
132133
} else {
133134
log.Print("this is client 2, continuing\n")
134135
}
135136

136-
err = write("key0", fmt.Sprintf("value from client %x", mode), int64(version))
137+
err = write("key0", fmt.Sprintf("value from client %x", mode), version)
137138
if err != nil {
138139
if mode == 1 {
139-
log.Printf("expected fail to write to storage with old lease version: %s\n", err) // client 1 should show this message
140+
log.Printf("expected fail to write to storage with old version: %s\n", err) // client 1 should show this message
140141
} else {
141142
log.Fatalf("unexpected fail to write to storage: %s\n", err)
142143
}
143144
} else {
144-
log.Printf("successfully write a key to storage using lease %x\n", int64(version))
145+
log.Printf("successfully write a key to storage with version %x\n", version)
145146
}
146147
}

contrib/lock/storage/storage.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ func handler(w http.ResponseWriter, r *http.Request) {
7878
}
7979
} else if strings.Compare(req.Op, "write") == 0 {
8080
if val, ok := data[req.Key]; ok {
81-
if req.Version != val.version {
82-
writeResponse(response{"", -1, fmt.Sprintf("given version (%x) is different from the existing version (%x)", req.Version, val.version)}, w)
81+
if req.Version < val.version {
82+
writeResponse(response{"", -1, fmt.Sprintf("given version (%d) is smaller than the existing version (%d)", req.Version, val.version)}, w)
8383
} else {
8484
data[req.Key].val = req.Val
8585
data[req.Key].version = req.Version

0 commit comments

Comments
 (0)