-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathranker.go
209 lines (190 loc) · 6.96 KB
/
ranker.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
// Copyright 2020 Pradyumna Kaushik
//
// 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 taskranker
import (
"fmt"
"github.com/pkg/errors"
df "github.com/pradykaushik/task-ranker/datafetcher"
"github.com/pradykaushik/task-ranker/logger"
"github.com/pradykaushik/task-ranker/logger/topic"
"github.com/pradykaushik/task-ranker/query"
"github.com/pradykaushik/task-ranker/strategies"
"github.com/pradykaushik/task-ranker/strategies/factory"
"github.com/pradykaushik/task-ranker/util"
"github.com/robfig/cron/v3"
"github.com/sirupsen/logrus"
"time"
)
// TaskRanker fetches data pertaining to currently running tasks, deploys a strategy
// to rank them and then feeds the results back to the caller.
// Runs as a cron job on the defined schedule.
type TaskRanker struct {
// DataFetcher used to pull task/container specific data.
DataFetcher df.Interface
// Strategy to use for calibration and ranking of tasks using the data fetched.
Strategy strategies.Interface
// Schedule on which the ranker runs. The schedule should follow the cron schedule format.
// See https://en.wikipedia.org/wiki/Cron.
// Alternatively, Seconds can also be specified as part of the schedule.
// See https://godoc.org/github.com/robfig/cron.
Schedule cron.Schedule
// runner is a cron job runner used to run the task ranker on the specified schedule.
runner *cron.Cron
// termCh is a channel used to signal the task ranker to stop.
termCh *util.SignalChannel
// prometheusScrapeInterval corresponds to the time interval between two successive metrics scrapes.
prometheusScrapeInterval time.Duration
}
func New(options ...Option) (*TaskRanker, error) {
tRanker := new(TaskRanker)
for _, opt := range options {
if err := opt(tRanker); err != nil {
return nil, errors.Wrap(err, "failed to create task ranker")
}
}
// checking if schedule provided.
if tRanker.Schedule == nil {
return nil, errors.New("invalid schedule provided for task ranker")
}
// validate task ranker schedule to be a multiple of prometheus scrape interval.
now := time.Unix(0, 0)
nextTimeTRankerSchedule := tRanker.Schedule.Next(now)
tRankerScheduleIntervalSeconds := int(nextTimeTRankerSchedule.Sub(now).Seconds())
if (tRankerScheduleIntervalSeconds < int(tRanker.prometheusScrapeInterval.Seconds())) ||
((tRankerScheduleIntervalSeconds % int(tRanker.prometheusScrapeInterval.Seconds())) != 0) {
return nil, errors.New(fmt.Sprintf("task ranker schedule (%d seconds) should be a multiple of "+
"prometheus scrape interval (%d seconds)", tRankerScheduleIntervalSeconds, int(tRanker.prometheusScrapeInterval.Seconds())))
}
// Providing the prometheus scrape interval to the strategy.
tRanker.Strategy.SetPrometheusScrapeInterval(tRanker.prometheusScrapeInterval)
tRanker.termCh = util.NewSignalChannel()
// Configuring logger.
err := logger.Configure()
if err != nil {
err = errors.Wrap(err, "failed to configure logger")
if err = logger.Done(); err != nil {
err = errors.Wrap(err, "failed to shutdown logger")
}
}
return tRanker, err
}
type Option func(*TaskRanker) error
func WithDataFetcher(dataFetcher df.Interface) Option {
return func(tRanker *TaskRanker) error {
if dataFetcher == nil {
return errors.New("invalid data fetcher")
}
tRanker.DataFetcher = dataFetcher
return nil
}
}
// WithPrometheusScrapeInterval returns an option that initializes the prometheus scrape interval.
func WithPrometheusScrapeInterval(prometheusScrapeInterval time.Duration) Option {
return func(tRanker *TaskRanker) error {
if prometheusScrapeInterval == 0 {
return errors.New("invalid prometheus scrape interval: should be > 0")
}
tRanker.prometheusScrapeInterval = prometheusScrapeInterval
return nil
}
}
// WithStrategy builds the task ranking strategy associated with the given name using the provided information.
// For backwards compatibility, strategies that use range queries will use the default duration. If the time
// duration for the range query needs to be configured, then use WithStrategyOptions(...) to configure the strategy
// and provide the WithRange(...) option.
func WithStrategy(
strategy string,
labelMatchers []*query.LabelMatcher,
receiver strategies.TaskRanksReceiver) Option {
return func(tRanker *TaskRanker) error {
if strategy == "" {
return errors.New("invalid strategy")
}
if s, err := factory.GetTaskRankStrategy(strategy); err != nil {
return err
} else {
tRanker.Strategy = s
err := strategies.Build(s,
strategies.WithLabelMatchers(labelMatchers),
strategies.WithTaskRanksReceiver(receiver))
if err != nil {
return errors.Wrap(err, "failed to build strategy")
}
tRanker.DataFetcher.SetStrategy(s)
}
return nil
}
}
// WithStrategyOptions builds the strategy associated with the given name using the provided initialization options.
func WithStrategyOptions(strategy string, strategyOptions ...strategies.Option) Option {
return func(tRanker *TaskRanker) error {
if strategy == "" {
return errors.New("invalid strategy")
}
if s, err := factory.GetTaskRankStrategy(strategy); err != nil {
return err
} else {
tRanker.Strategy = s
err := strategies.Build(s, strategyOptions...)
if err != nil {
return errors.Wrap(err, "failed to build strategy")
}
tRanker.DataFetcher.SetStrategy(s)
}
return nil
}
}
func WithSchedule(specString string) Option {
return func(tRanker *TaskRanker) error {
schedule, err := cron.NewParser(
cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow).Parse(specString)
if err != nil {
return errors.Wrap(err, "invalid schedule")
}
tRanker.Schedule = schedule
return nil
}
}
func (tRanker *TaskRanker) Start() {
logger.WithFields(logrus.Fields{
topic.Stage.String(): "task-ranker",
}).Log(logrus.InfoLevel, "starting task ranker cron job")
tRanker.runner = cron.New(cron.WithSeconds())
tRanker.runner.Schedule(tRanker.Schedule, tRanker)
tRanker.runner.Start()
}
func (tRanker *TaskRanker) Run() {
if tRanker.termCh.IsClosed() {
return
}
result, err := tRanker.DataFetcher.Fetch()
if err != nil {
logger.WithFields(logrus.Fields{
topic.Stage.String(): "data-fetcher",
}).Log(logrus.ErrorLevel, err.Error())
} else {
tRanker.Strategy.Execute(result)
}
}
func (tRanker *TaskRanker) Stop() {
logger.WithFields(logrus.Fields{
topic.Stage.String(): "task-ranker",
}).Log(logrus.InfoLevel, "stopping task ranker cron job")
tRanker.termCh.Close()
tRanker.runner.Stop()
err := logger.Done()
if err != nil {
fmt.Printf("failed to shutdown logger: %v", err)
}
}