This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 302
/
agent.go
193 lines (161 loc) · 4.66 KB
/
agent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2014 The fleet Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package agent
import (
"encoding/json"
"fmt"
"time"
"github.com/coreos/fleet/job"
"github.com/coreos/fleet/log"
"github.com/coreos/fleet/machine"
"github.com/coreos/fleet/pkg"
"github.com/coreos/fleet/registry"
"github.com/coreos/fleet/unit"
)
const (
// TTL to use with all state pushed to Registry
DefaultTTL = "30s"
)
type Agent struct {
registry registry.Registry
um unit.UnitManager
uGen *unit.UnitStateGenerator
Machine machine.Machine
ttl time.Duration
cache *agentCache
}
func New(mgr unit.UnitManager, uGen *unit.UnitStateGenerator, reg registry.Registry, mach machine.Machine, ttl time.Duration) *Agent {
return &Agent{reg, mgr, uGen, mach, ttl, &agentCache{}}
}
func (a *Agent) MarshalJSON() ([]byte, error) {
data := struct {
Cache *agentCache
}{
Cache: a.cache,
}
return json.Marshal(data)
}
// Heartbeat updates the Registry periodically with an acknowledgement of the
// Jobs this Agent is expected to be running.
func (a *Agent) Heartbeat(stop <-chan struct{}) {
a.heartbeatJobs(a.ttl, stop)
}
func (a *Agent) heartbeatJobs(ttl time.Duration, stop <-chan struct{}) {
heartbeat := func() {
machID := a.Machine.State().ID
launched := a.cache.launchedJobs()
for _, j := range launched {
go a.registry.UnitHeartbeat(j, machID, ttl)
}
}
var interval time.Duration
if ttl > 10*time.Second {
interval = ttl * 4 / 5
} else {
interval = ttl / 2
}
ticker := time.Tick(interval)
for {
select {
case <-stop:
log.Debug("HeartbeatJobs exiting due to stop signal")
return
case <-ticker:
log.Debug("HeartbeatJobs tick")
heartbeat()
}
}
}
func (a *Agent) reloadUnitFiles() error {
return a.um.ReloadUnitFiles()
}
func (a *Agent) loadUnit(u *job.Unit) error {
a.cache.setTargetState(u.Name, job.JobStateLoaded)
a.uGen.Subscribe(u.Name)
return a.um.Load(u.Name, u.Unit)
}
func (a *Agent) unloadUnit(unitName string) error {
a.registry.ClearUnitHeartbeat(unitName)
a.cache.dropTargetState(unitName)
errStop := a.um.TriggerStop(unitName)
if errStop != nil {
log.Warningf("TriggerStop on systemd unit %s returned: %v", unitName, errStop)
} else {
log.Infof("Stopped unit(%s)", unitName)
}
a.uGen.Unsubscribe(unitName)
// unit should be unloaded and unit file should be removed, only if the unit
// could be successfully stopped. Otherwise the unit could get into a state
// where the unit cannot be stopped via fleet, because the unit file was
// already removed. See also https://github.com/coreos/fleet/issues/1216.
var errUnload error
if errStop == nil {
errUnload = a.um.Unload(unitName)
}
return errUnload
}
func (a *Agent) startUnit(unitName string) error {
a.cache.setTargetState(unitName, job.JobStateLaunched)
machID := a.Machine.State().ID
a.registry.UnitHeartbeat(unitName, machID, a.ttl)
return a.um.TriggerStart(unitName)
}
func (a *Agent) stopUnit(unitName string) error {
a.cache.setTargetState(unitName, job.JobStateLoaded)
a.registry.ClearUnitHeartbeat(unitName)
return a.um.TriggerStop(unitName)
}
type unitState struct {
state job.JobState
hash string
}
type unitStates map[string]unitState
// units returns a map representing the current state of units known by the agent.
func (a *Agent) units() (unitStates, error) {
launched := pkg.NewUnsafeSet()
for _, jName := range a.cache.launchedJobs() {
launched.Add(jName)
}
loaded := pkg.NewUnsafeSet()
for _, jName := range a.cache.loadedJobs() {
loaded.Add(jName)
}
units, err := a.um.Units()
if err != nil {
return nil, fmt.Errorf("failed fetching loaded units from UnitManager: %v", err)
}
filter := pkg.NewUnsafeSet()
for _, u := range units {
filter.Add(u)
}
uStates, err := a.um.GetUnitStates(filter)
if err != nil {
return nil, fmt.Errorf("failed fetching unit states from UnitManager: %v", err)
}
states := make(unitStates)
for uName, uState := range uStates {
js := job.JobStateInactive
if loaded.Contains(uName) {
js = job.JobStateLoaded
} else if launched.Contains(uName) {
js = job.JobStateLaunched
}
us := unitState{
state: js,
hash: uState.UnitHash,
}
states[uName] = us
}
return states, nil
}