Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit 90d10c4

Browse files
author
Dongsu Park
committed
*: introduce an option UseLeaseTTL for lease manager's TTL
* Introduce an option UseLeaseTTL. Default is false. * If UseLeaseTTL turned on, create lease manager with the TTL value. * When gRPC turned on, remove previous workaround of setting leaseTTL to a huge value. * Increase engine reconcile interval from 2 to 5 sec. Originally written by Hector Fernandez <[email protected]> Taken from https://github.com/giantswarm/fleet/tree/patch_lease_ttl
1 parent 686bddf commit 90d10c4

File tree

5 files changed

+30
-16
lines changed

5 files changed

+30
-16
lines changed

config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type Config struct {
4242
UnitsDirectory string
4343
SystemdUser bool
4444
AuthorizedKeysFile string
45+
UseLeaseTTL bool
4546
}
4647

4748
func (c *Config) Capabilities() machine.Capabilities {

engine/engine.go

-7
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,6 @@ func New(reg CompleteRegistry, lManager lease.Manager, rStream pkg.EventStream,
6767

6868
func (e *Engine) Run(ival time.Duration, stop <-chan struct{}) {
6969
leaseTTL := ival * 5
70-
if e.machine.State().Capabilities.Has(machine.CapGRPC) {
71-
// With grpc it doesn't make sense to set to 5secs the TTL of the etcd key.
72-
// This has a special impact whenever we have high worload in the cluster, cause
73-
// it'd provoke constant leader re-elections.
74-
// TODO: IMHO, this should be configurable via a flag to disable the TTL.
75-
leaseTTL = ival * 500000
76-
}
7770
machID := e.machine.State().ID
7871

7972
reconcile := func() {

fleetd/fleetd.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func Main() {
8484
cfgset.String("etcd_cafile", "", "SSL Certificate Authority file used to secure etcd communication")
8585
cfgset.String("etcd_key_prefix", registry.DefaultKeyPrefix, "Keyspace for fleet data in etcd")
8686
cfgset.Float64("etcd_request_timeout", 1.0, "Amount of time in seconds to allow a single etcd request before considering it failed.")
87-
cfgset.Float64("engine_reconcile_interval", 2.0, "Interval at which the engine should reconcile the cluster schedule in etcd.")
87+
cfgset.Float64("engine_reconcile_interval", 5.0, "Interval at which the engine should reconcile the cluster schedule in etcd.")
8888
cfgset.String("public_ip", "", "IP address that fleet machine should publish")
8989
cfgset.String("metadata", "", "List of key-value metadata to assign to the fleet machine")
9090
cfgset.String("agent_ttl", agent.DefaultTTL, "TTL in seconds of fleet machine state in etcd")
@@ -96,6 +96,7 @@ func Main() {
9696
cfgset.Bool("disable_watches", false, "Disable the use of etcd watches. Increases scheduling latency")
9797
cfgset.Bool("verify_units", false, "DEPRECATED - This option is ignored")
9898
cfgset.String("authorized_keys_file", "", "DEPRECATED - This option is ignored")
99+
cfgset.Bool("use_lease_ttl", false, "Enable the usage of TTL option when creating a lease key in etcd")
99100

100101
globalconf.Register("", cfgset)
101102
cfg, err := getConfig(cfgset, *cfgPath)
@@ -237,6 +238,7 @@ func getConfig(flagset *flag.FlagSet, userCfgFile string) (*config.Config, error
237238
SystemdUser: (*flagset.Lookup("systemd_user")).Value.(flag.Getter).Get().(bool),
238239
TokenLimit: (*flagset.Lookup("token_limit")).Value.(flag.Getter).Get().(int),
239240
AuthorizedKeysFile: (*flagset.Lookup("authorized_keys_file")).Value.(flag.Getter).Get().(string),
241+
UseLeaseTTL: (*flagset.Lookup("use_lease_ttl")).Value.(flag.Getter).Get().(bool),
240242
}
241243

242244
if cfg.VerifyUnits {

pkg/lease/etcd.go

+25-7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"time"
2121

2222
etcd "github.com/coreos/etcd/client"
23+
"github.com/coreos/fleet/log"
24+
2325
"golang.org/x/net/context"
2426
)
2527

@@ -53,8 +55,14 @@ func (l *etcdLease) Renew(period time.Duration) error {
5355
val, err := serializeLeaseMetadata(l.meta.MachineID, l.meta.Version)
5456
opts := &etcd.SetOptions{
5557
PrevIndex: l.idx,
56-
TTL: period,
5758
}
59+
60+
log.Infof("Renew %v", l.mgr.UseLeaseTTL)
61+
62+
if l.mgr.UseLeaseTTL {
63+
opts.TTL = period
64+
}
65+
5866
resp, err := l.mgr.kAPI.Set(l.mgr.ctx(), l.key, val, opts)
5967
if err != nil {
6068
return err
@@ -97,12 +105,13 @@ func serializeLeaseMetadata(machID string, ver int) (string, error) {
97105
}
98106

99107
type etcdLeaseManager struct {
100-
kAPI etcd.KeysAPI
101-
keyPrefix string
108+
kAPI etcd.KeysAPI
109+
keyPrefix string
110+
UseLeaseTTL bool
102111
}
103112

104-
func NewEtcdLeaseManager(kAPI etcd.KeysAPI, keyPrefix string) *etcdLeaseManager {
105-
return &etcdLeaseManager{kAPI: kAPI, keyPrefix: keyPrefix}
113+
func NewEtcdLeaseManager(kAPI etcd.KeysAPI, keyPrefix string, useLeaseTTL bool) *etcdLeaseManager {
114+
return &etcdLeaseManager{kAPI: kAPI, keyPrefix: keyPrefix, UseLeaseTTL: useLeaseTTL}
106115
}
107116

108117
func (r *etcdLeaseManager) ctx() context.Context {
@@ -136,7 +145,12 @@ func (r *etcdLeaseManager) StealLease(name, machID string, ver int, period time.
136145
key := r.leasePath(name)
137146
opts := &etcd.SetOptions{
138147
PrevIndex: idx,
139-
TTL: period,
148+
}
149+
150+
log.Infof("StealLease %v", r.UseLeaseTTL)
151+
152+
if r.UseLeaseTTL {
153+
opts.TTL = period
140154
}
141155
resp, err := r.kAPI.Set(r.ctx(), key, val, opts)
142156
if err != nil {
@@ -158,9 +172,13 @@ func (r *etcdLeaseManager) AcquireLease(name string, machID string, ver int, per
158172

159173
key := r.leasePath(name)
160174
opts := &etcd.SetOptions{
161-
TTL: period,
162175
PrevExist: etcd.PrevNoExist,
163176
}
177+
log.Infof("StealLease %v", r.UseLeaseTTL)
178+
179+
if r.UseLeaseTTL {
180+
opts.TTL = period
181+
}
164182

165183
resp, err := r.kAPI.Set(r.ctx(), key, val, opts)
166184
if err != nil {

server/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func New(cfg config.Config, listeners []net.Listener) (*Server, error) {
110110
reg engine.CompleteRegistry
111111
genericReg interface{}
112112
)
113-
lManager := lease.NewEtcdLeaseManager(kAPI, cfg.EtcdKeyPrefix)
113+
lManager := lease.NewEtcdLeaseManager(kAPI, cfg.EtcdKeyPrefix, cfg.UseLeaseTTL)
114114

115115
if !cfg.EnableGRPC {
116116
genericReg = registry.NewEtcdRegistry(kAPI, cfg.EtcdKeyPrefix)

0 commit comments

Comments
 (0)