-
Notifications
You must be signed in to change notification settings - Fork 0
/
recovery.go
157 lines (139 loc) · 4.36 KB
/
recovery.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
// Copyright (c) 2019 Uber Technologies, Inc.
//
// 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 hostmgr
import (
"context"
"github.com/uber/peloton/.gen/peloton/api/v0/job"
"github.com/uber/peloton/.gen/peloton/api/v0/peloton"
"github.com/uber/peloton/.gen/peloton/api/v0/task"
"github.com/uber/peloton/.gen/peloton/private/models"
"github.com/uber/peloton/pkg/common/api"
"github.com/uber/peloton/pkg/common/recovery"
"github.com/uber/peloton/pkg/common/util"
"github.com/uber/peloton/pkg/hostmgr/metrics"
"github.com/uber/peloton/pkg/hostmgr/offer"
"github.com/uber/peloton/pkg/hostmgr/p2k/hostcache"
"github.com/uber/peloton/pkg/storage"
ormobjects "github.com/uber/peloton/pkg/storage/objects"
log "github.com/sirupsen/logrus"
"github.com/uber-go/tally"
"go.uber.org/yarpc/yarpcerrors"
)
// RecoveryHandler defines the interface to
// be called by leader election callbacks.
type RecoveryHandler interface {
Start() error
Stop() error
}
// recoveryHandler restores the contents of MaintenanceQueue
// from Mesos Maintenance Status
type recoveryHandler struct {
metrics *metrics.Metrics
recoveryScope tally.Scope
taskStore storage.TaskStore
hostCache hostcache.HostCache
activeJobsOps ormobjects.ActiveJobsOps
jobConfigOps ormobjects.JobConfigOps
jobRuntimeOps ormobjects.JobRuntimeOps
}
// NewRecoveryHandler creates a recoveryHandler
func NewRecoveryHandler(
parent tally.Scope,
taskStore storage.TaskStore,
ormStore *ormobjects.Store,
hostCache hostcache.HostCache,
) RecoveryHandler {
recovery := &recoveryHandler{
metrics: metrics.NewMetrics(parent),
recoveryScope: parent.SubScope("recovery"),
hostCache: hostCache,
taskStore: taskStore,
activeJobsOps: ormobjects.NewActiveJobsOps(ormStore),
jobConfigOps: ormobjects.NewJobConfigOps(ormStore),
jobRuntimeOps: ormobjects.NewJobRuntimeOps(ormStore),
}
return recovery
}
// Stop is a no-op for recovery handler
func (r *recoveryHandler) Stop() error {
log.Info("Stopping recovery")
return nil
}
// Start requeues all 'DRAINING' hosts into maintenance queue
func (r *recoveryHandler) Start() error {
log.Info("start recovery from DB")
if err := recovery.RecoverActiveJobs(
context.Background(),
r.recoveryScope,
r.activeJobsOps,
r.jobConfigOps,
r.jobRuntimeOps,
r.recoverTasks,
); err != nil {
return err
}
r.metrics.RecoverySuccess.Inc(1)
return nil
}
// recoverTasks recovers tasks from DB on bootstrap after leadership change.
// recovery updates host to task map in offerpool and host summary.
func (r *recoveryHandler) recoverTasks(
ctx context.Context,
id string,
jobConfig *job.JobConfig,
configAddOn *models.ConfigAddOn,
jobRuntime *job.RuntimeInfo,
batch recovery.TasksBatch,
errChan chan<- error,
) {
jobID := &peloton.JobID{Value: id}
offerPool := offer.GetEventHandler().GetOfferPool()
taskInfos, err := r.taskStore.GetTasksForJobByRange(
ctx,
jobID,
&task.InstanceRange{
From: batch.From,
To: batch.To,
})
if err != nil {
log.WithError(err).
WithField("job_id", id).
WithField("from", batch.From).
WithField("to", batch.To).
Error("failed to fetch task infos")
if yarpcerrors.IsNotFound(err) {
// Due to task_config table deprecation, we might see old jobs
// fail to recover due to their task config was created in
// task_config table instead of task_config_v2. Only log the
// error here instead of crashing jobmgr.
return
}
errChan <- err
return
}
for _, taskInfo := range taskInfos {
runtime := taskInfo.GetRuntime()
offerPool.UpdateTasksOnHost(
runtime.GetMesosTaskId().GetValue(),
runtime.GetState(),
taskInfo)
r.hostCache.RecoverPodInfoOnHost(
util.CreatePodIDFromMesosTaskID(runtime.GetMesosTaskId()),
runtime.GetHost(),
api.ConvertTaskStateToPodState(runtime.GetState()),
api.ConvertTaskConfigToPodSpec(taskInfo.Config, id, taskInfo.InstanceId),
)
}
return
}