@@ -28,19 +28,8 @@ import (
28
28
29
29
// NewMasterParticipation is part of the topo.Server interface
30
30
func (s * Server ) NewMasterParticipation (name , id string ) (topo.MasterParticipation , error ) {
31
- // Create the lock here.
32
- electionPath := path .Join (s .root , electionsPath , name )
33
- l , err := s .client .LockOpts (& api.LockOptions {
34
- Key : electionPath ,
35
- Value : []byte (id ),
36
- })
37
- if err != nil {
38
- return nil , err
39
- }
40
-
41
31
return & consulMasterParticipation {
42
32
s : s ,
43
- lock : l ,
44
33
name : name ,
45
34
id : id ,
46
35
stop : make (chan struct {}),
@@ -56,9 +45,6 @@ type consulMasterParticipation struct {
56
45
// s is our parent consul topo Server
57
46
s * Server
58
47
59
- // lock is the *api.Lock structure we're going to use.
60
- lock * api.Lock
61
-
62
48
// name is the name of this MasterParticipation
63
49
name string
64
50
@@ -74,6 +60,16 @@ type consulMasterParticipation struct {
74
60
75
61
// WaitForMastership is part of the topo.MasterParticipation interface.
76
62
func (mp * consulMasterParticipation ) WaitForMastership () (context.Context , error ) {
63
+
64
+ electionPath := path .Join (mp .s .root , electionsPath , mp .name )
65
+ l , err := mp .s .client .LockOpts (& api.LockOptions {
66
+ Key : electionPath ,
67
+ Value : []byte (mp .id ),
68
+ })
69
+ if err != nil {
70
+ return nil , err
71
+ }
72
+
77
73
// If Stop was already called, mp.done is closed, so we are interrupted.
78
74
select {
79
75
case <- mp .done :
@@ -82,7 +78,7 @@ func (mp *consulMasterParticipation) WaitForMastership() (context.Context, error
82
78
}
83
79
84
80
// Try to lock until mp.stop is closed.
85
- lost , err := mp . lock .Lock (mp .stop )
81
+ lost , err := l .Lock (mp .stop )
86
82
if err != nil {
87
83
// We can't lock. See if it was because we got canceled.
88
84
select {
@@ -93,19 +89,22 @@ func (mp *consulMasterParticipation) WaitForMastership() (context.Context, error
93
89
return nil , err
94
90
}
95
91
96
- // We have the lock, keep mastership until we loose it.
92
+ // We have the lock, keep mastership until we lose it.
97
93
lockCtx , lockCancel := context .WithCancel (context .Background ())
98
94
go func () {
99
95
select {
100
96
case <- lost :
101
- // We lost the lock, nothing to do but lockCancel().
102
97
lockCancel ()
98
+ // We could have lost the lock. Per consul API, explicitly call Unlock to make sure that session will not be renewed.
99
+ if err := l .Unlock (); err != nil {
100
+ log .Errorf ("master election(%v) Unlock failed: %v" , mp .name , err )
101
+ }
103
102
case <- mp .stop :
104
103
// Stop was called. We stop the context first,
105
104
// so the running process is not thinking it
106
105
// is the master any more, then we unlock.
107
106
lockCancel ()
108
- if err := mp . lock .Unlock (); err != nil {
107
+ if err := l .Unlock (); err != nil {
109
108
log .Errorf ("master election(%v) Unlock failed: %v" , mp .name , err )
110
109
}
111
110
close (mp .done )
0 commit comments