Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
1a679bcf
Commit
1a679bcf
authored
Jun 04, 2015
by
Quinton Hoole
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #9239 from brendandburns/ha
Pro-actively give up the lease if the TTL expires
parents
03ece333
6b837ae0
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
7 deletions
+20
-7
podmaster.go
contrib/pod-master/podmaster.go
+20
-7
No files found.
contrib/pod-master/podmaster.go
View file @
1a679bcf
...
@@ -42,6 +42,7 @@ type Config struct {
...
@@ -42,6 +42,7 @@ type Config struct {
src
string
src
string
dest
string
dest
string
sleep
time
.
Duration
sleep
time
.
Duration
lastLease
time
.
Time
}
}
// runs the election loop. never returns.
// runs the election loop. never returns.
...
@@ -50,7 +51,13 @@ func (c *Config) leaseAndUpdateLoop(etcdClient *etcd.Client) {
...
@@ -50,7 +51,13 @@ func (c *Config) leaseAndUpdateLoop(etcdClient *etcd.Client) {
master
,
err
:=
c
.
acquireOrRenewLease
(
etcdClient
)
master
,
err
:=
c
.
acquireOrRenewLease
(
etcdClient
)
if
err
!=
nil
{
if
err
!=
nil
{
glog
.
Errorf
(
"Error in master election: %v"
,
err
)
glog
.
Errorf
(
"Error in master election: %v"
,
err
)
continue
if
uint64
(
time
.
Now
()
.
Sub
(
c
.
lastLease
)
.
Seconds
())
<
c
.
ttl
{
continue
}
// Our lease has expired due to our own accounting, pro-actively give it
// up, even if we couldn't contact etcd.
glog
.
Infof
(
"Too much time has elapsed, giving up lease."
)
master
=
false
}
}
if
err
:=
c
.
update
(
master
);
err
!=
nil
{
if
err
:=
c
.
update
(
master
);
err
!=
nil
{
glog
.
Errorf
(
"Error updating files: %v"
,
err
)
glog
.
Errorf
(
"Error updating files: %v"
,
err
)
...
@@ -64,13 +71,17 @@ func (c *Config) leaseAndUpdateLoop(etcdClient *etcd.Client) {
...
@@ -64,13 +71,17 @@ func (c *Config) leaseAndUpdateLoop(etcdClient *etcd.Client) {
// TODO: use the master election utility once it is merged in.
// TODO: use the master election utility once it is merged in.
func
(
c
*
Config
)
acquireOrRenewLease
(
etcdClient
*
etcd
.
Client
)
(
bool
,
error
)
{
func
(
c
*
Config
)
acquireOrRenewLease
(
etcdClient
*
etcd
.
Client
)
(
bool
,
error
)
{
result
,
err
:=
etcdClient
.
Get
(
c
.
key
,
false
,
false
)
result
,
err
:=
etcdClient
.
Get
(
c
.
key
,
false
,
false
)
if
tools
.
IsEtcdNotFound
(
err
)
{
if
err
!=
nil
{
// there is no current master, try to become master, create will fail if the key already exists
if
tools
.
IsEtcdNotFound
(
err
)
{
_
,
err
:=
etcdClient
.
Create
(
c
.
key
,
c
.
whoami
,
c
.
ttl
)
// there is no current master, try to become master, create will fail if the key already exists
if
err
!=
nil
{
_
,
err
:=
etcdClient
.
Create
(
c
.
key
,
c
.
whoami
,
c
.
ttl
)
return
false
,
err
if
err
!=
nil
{
return
false
,
err
}
c
.
lastLease
=
time
.
Now
()
return
true
,
nil
}
}
return
true
,
nil
return
false
,
err
}
}
if
result
.
Node
.
Value
==
c
.
whoami
{
if
result
.
Node
.
Value
==
c
.
whoami
{
glog
.
Infof
(
"key already exists, we are the master (%s)"
,
result
.
Node
.
Value
)
glog
.
Infof
(
"key already exists, we are the master (%s)"
,
result
.
Node
.
Value
)
...
@@ -81,6 +92,7 @@ func (c *Config) acquireOrRenewLease(etcdClient *etcd.Client) (bool, error) {
...
@@ -81,6 +92,7 @@ func (c *Config) acquireOrRenewLease(etcdClient *etcd.Client) (bool, error) {
return
false
,
err
return
false
,
err
}
}
}
}
c
.
lastLease
=
time
.
Now
()
return
true
,
nil
return
true
,
nil
}
}
glog
.
Infof
(
"key already exists, the master is %s, sleeping."
,
result
.
Node
.
Value
)
glog
.
Infof
(
"key already exists, the master is %s, sleeping."
,
result
.
Node
.
Value
)
...
@@ -97,6 +109,7 @@ func (c *Config) update(master bool) error {
...
@@ -97,6 +109,7 @@ func (c *Config) update(master bool) error {
switch
{
switch
{
case
master
&&
!
exists
:
case
master
&&
!
exists
:
return
copyFile
(
c
.
src
,
c
.
dest
)
return
copyFile
(
c
.
src
,
c
.
dest
)
// TODO: validate sha hash for the two files and overwrite if dest is different than src.
case
!
master
&&
exists
:
case
!
master
&&
exists
:
return
os
.
Remove
(
c
.
dest
)
return
os
.
Remove
(
c
.
dest
)
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment