forked from mesosphere/kubernetes-mesos
/
registry.go
266 lines (240 loc) · 7.94 KB
/
registry.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package podtask
import (
"container/ring"
"encoding/json"
"fmt"
"sync"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
log "github.com/golang/glog"
mesos "github.com/mesos/mesos-go/mesosproto"
"github.com/mesosphere/kubernetes-mesos/pkg/scheduler/metrics"
)
/**
HACK(jdef): we're not using etcd but k8s has implemented namespace support and
we're going to try to honor that by namespacing pod keys. Hence, the following
funcs that were stolen from:
https://github.com/GoogleCloudPlatform/kubernetes/blob/release-0.5/pkg/registry/etcd/etcd.go
**/
const (
PodPath = "/pods"
defaultFinishedTasksSize = 1024
)
type Registry interface {
Register(*T, error) (*T, error)
Unregister(*T)
Get(taskId string) (task *T, currentState StateType)
TaskForPod(podID string) (taskID string, ok bool)
UpdateStatus(status *mesos.TaskStatus) (*T, StateType)
// return a list of task ID's that match the given filter, or all task ID's if filter == nil
List(func(*T) bool) []string
}
type inMemoryRegistry struct {
rw sync.RWMutex
taskRegistry map[string]*T
tasksFinished *ring.Ring
podToTask map[string]string
}
func NewInMemoryRegistry() Registry {
return &inMemoryRegistry{
taskRegistry: make(map[string]*T),
tasksFinished: ring.New(defaultFinishedTasksSize),
podToTask: make(map[string]string),
}
}
func (k *inMemoryRegistry) List(accepts func(t *T) bool) (taskids []string) {
k.rw.RLock()
defer k.rw.RUnlock()
for id, task := range k.taskRegistry {
if accepts == nil || accepts(task) {
taskids = append(taskids, id)
}
}
return
}
func (k *inMemoryRegistry) TaskForPod(podID string) (taskID string, ok bool) {
k.rw.RLock()
defer k.rw.RUnlock()
// assume caller is holding scheduler lock
taskID, ok = k.podToTask[podID]
return
}
// registers a pod task unless the spec'd error is not nil
func (k *inMemoryRegistry) Register(task *T, err error) (*T, error) {
if err == nil {
k.rw.Lock()
defer k.rw.Unlock()
k.podToTask[task.podKey] = task.ID
k.taskRegistry[task.ID] = task
}
return task, err
}
func (k *inMemoryRegistry) Unregister(task *T) {
k.rw.Lock()
defer k.rw.Unlock()
delete(k.podToTask, task.podKey)
delete(k.taskRegistry, task.ID)
}
func (k *inMemoryRegistry) Get(taskId string) (*T, StateType) {
k.rw.RLock()
defer k.rw.RUnlock()
return k._get(taskId)
}
// assume that the caller has already locked around access to task state
func (k *inMemoryRegistry) _get(taskId string) (*T, StateType) {
if task, found := k.taskRegistry[taskId]; found {
return task, task.State
}
return nil, StateUnknown
}
func (k *inMemoryRegistry) UpdateStatus(status *mesos.TaskStatus) (*T, StateType) {
taskId := status.GetTaskId().GetValue()
k.rw.Lock()
defer k.rw.Unlock()
task, state := k._get(taskId)
switch status.GetState() {
case mesos.TaskState_TASK_STAGING:
k.handleTaskStaging(task, state, status)
case mesos.TaskState_TASK_STARTING:
k.handleTaskStarting(task, state, status)
case mesos.TaskState_TASK_RUNNING:
k.handleTaskRunning(task, state, status)
case mesos.TaskState_TASK_FINISHED:
k.handleTaskFinished(task, state, status)
case mesos.TaskState_TASK_FAILED:
k.handleTaskFailed(task, state, status)
case mesos.TaskState_TASK_KILLED:
k.handleTaskKilled(task, state, status)
case mesos.TaskState_TASK_LOST:
k.handleTaskLost(task, state, status)
default:
log.Warning("unhandled task status update: %+v", status)
}
return task, state
}
func (k *inMemoryRegistry) handleTaskStaging(task *T, state StateType, status *mesos.TaskStatus) {
if status.GetSource() != mesos.TaskStatus_SOURCE_MASTER {
log.Errorf("received STAGING for task %v with unexpected source: %v",
status.GetTaskId().GetValue(), status.GetSource())
}
}
func (k *inMemoryRegistry) handleTaskStarting(task *T, state StateType, status *mesos.TaskStatus) {
// we expect to receive this when a launched task is finally "bound"
// via the API server. however, there's nothing specific for us to do
// here.
switch state {
case StatePending:
task.UpdatedTime = time.Now()
if !task.Has(Bound) {
task.Set(Bound)
task.bindTime = task.UpdatedTime
timeToBind := task.bindTime.Sub(task.launchTime)
metrics.BindLatency.Observe(metrics.InMicroseconds(timeToBind))
}
default:
log.Warningf("Ignore status TASK_STARTING because the the task is not pending")
}
}
func (k *inMemoryRegistry) handleTaskRunning(task *T, state StateType, status *mesos.TaskStatus) {
switch state {
case StatePending:
task.UpdatedTime = time.Now()
log.Infof("Received running status for pending task: %+v", status)
fillRunningPodInfo(task, status)
task.State = StateRunning
case StateRunning:
task.UpdatedTime = time.Now()
log.V(2).Info("Ignore status TASK_RUNNING because the the task is already running")
case StateFinished:
log.Warningf("Ignore status TASK_RUNNING because the the task is already finished")
default:
log.Warningf("Ignore status TASK_RUNNING (%+v) because the the task is discarded", status.GetTaskId())
}
}
func ParsePodStatusResult(taskStatus *mesos.TaskStatus) (result api.PodStatusResult, err error) {
if taskStatus.Data != nil {
err = json.Unmarshal(taskStatus.Data, &result)
} else {
err = fmt.Errorf("missing TaskStatus.Data")
}
return
}
func fillRunningPodInfo(task *T, taskStatus *mesos.TaskStatus) {
if taskStatus.GetReason() == mesos.TaskStatus_REASON_RECONCILIATION && taskStatus.GetSource() == mesos.TaskStatus_SOURCE_MASTER {
// there is no data..
return
}
//TODO(jdef) determine the usefullness of this information (if any)
if result, err := ParsePodStatusResult(taskStatus); err != nil {
log.Errorf("invalid TaskStatus.Data for task '%v': %v", task.ID, err)
} else {
task.Pod.Status = result.Status
log.Infof("received pod status for task %v: %+v", task.ID, task.Pod.Status)
}
}
func (k *inMemoryRegistry) handleTaskFinished(task *T, state StateType, status *mesos.TaskStatus) {
switch state {
case StatePending:
panic("Pending task finished, this couldn't happen")
case StateRunning:
log.V(2).Infof("received finished status for running task: %+v", status)
delete(k.podToTask, task.podKey)
task.State = StateFinished
task.UpdatedTime = time.Now()
k.tasksFinished = k.recordFinishedTask(task.ID)
case StateFinished:
log.Warningf("Ignore status TASK_FINISHED because the the task is already finished")
default:
log.Warningf("Ignore status TASK_FINISHED because the the task is not running")
}
}
// record that a task has finished.
// older record are expunged one at a time once the historical ring buffer is saturated.
// assumes caller is holding state lock.
func (k *inMemoryRegistry) recordFinishedTask(taskId string) *ring.Ring {
slot := k.tasksFinished.Next()
if slot.Value != nil {
// garbage collect older finished task from the registry
gctaskId := slot.Value.(string)
if gctask, found := k.taskRegistry[gctaskId]; found && gctask.State == StateFinished {
delete(k.taskRegistry, gctaskId)
}
}
slot.Value = taskId
return slot
}
func (k *inMemoryRegistry) handleTaskFailed(task *T, state StateType, status *mesos.TaskStatus) {
log.Errorf("task failed: %+v", status)
switch state {
case StatePending:
delete(k.taskRegistry, task.ID)
delete(k.podToTask, task.podKey)
case StateRunning:
delete(k.taskRegistry, task.ID)
delete(k.podToTask, task.podKey)
}
}
func (k *inMemoryRegistry) handleTaskKilled(task *T, state StateType, status *mesos.TaskStatus) {
defer func() {
msg := fmt.Sprintf("task killed: %+v, task %+v", status, task)
if task != nil && task.Has(Deleted) {
// we were expecting this, nothing out of the ordinary
log.V(2).Infoln(msg)
} else {
log.Errorln(msg)
}
}()
switch state {
case StatePending, StateRunning:
delete(k.taskRegistry, task.ID)
delete(k.podToTask, task.podKey)
}
}
func (k *inMemoryRegistry) handleTaskLost(task *T, state StateType, status *mesos.TaskStatus) {
log.Warningf("task lost: %+v", status)
switch state {
case StateRunning, StatePending:
delete(k.taskRegistry, task.ID)
delete(k.podToTask, task.podKey)
}
}