forked from raystack/optimus
/
create.go
276 lines (234 loc) · 7.69 KB
/
create.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
267
268
269
270
271
272
273
274
275
276
package replay
import (
"bytes"
"errors"
"fmt"
"time"
"github.com/goto/salt/log"
"github.com/spf13/cobra"
"golang.org/x/net/context"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/goto/optimus/client/cmd/internal"
"github.com/goto/optimus/client/cmd/internal/connection"
"github.com/goto/optimus/client/cmd/internal/logger"
"github.com/goto/optimus/client/cmd/internal/progressbar"
"github.com/goto/optimus/config"
pb "github.com/goto/optimus/protos/gotocompany/optimus/core/v1beta1"
)
const (
replayTimeout = time.Minute * 1
ISOTimeLayout = time.RFC3339
pollIntervalInSecond = 30
)
var (
supportedISOTimeLayouts = [...]string{time.RFC3339, "2006-01-02"}
terminalStatuses = map[string]bool{"success": true, "failed": true, "invalid": true}
)
type createCommand struct {
logger log.Logger
connection connection.Connection
configFilePath string
parallel bool
dryRun bool
description string
jobConfig string
projectName string
namespaceName string
host string
}
// CreateCommand initializes command for creating a replay request
func CreateCommand() *cobra.Command {
refresh := &createCommand{
logger: logger.NewClientLogger(),
}
cmd := &cobra.Command{
Use: "create",
Short: "Run replay operation on a dag based on provided start and end time range",
Long: "This operation takes three arguments, first is DAG name[required]\nused in optimus specification, " +
"second is start time[required] of\nreplay, third is end time[optional] of replay. \nDate ranges are inclusive. " +
"Supported date formats are RFC3339 and \nsimple date YYYY-MM-DD",
Example: "optimus replay create <job_name> <2023-01-01T02:30:00Z00:00> [2023-01-02T02:30:00Z00:00]\noptimus replay create <job_name> <2023-01-01> [2023-01-02]",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("job name is required")
}
if len(args) < 2 { //nolint: gomnd
return errors.New("replay start time is required")
}
return nil
},
RunE: refresh.RunE,
PreRunE: refresh.PreRunE,
}
refresh.injectFlags(cmd)
return cmd
}
func (r *createCommand) injectFlags(cmd *cobra.Command) {
// Config filepath flag
cmd.Flags().StringVarP(&r.configFilePath, "config", "c", config.EmptyPath, "File path for client configuration")
cmd.Flags().StringVarP(&r.namespaceName, "namespace-name", "n", "", "Name of the optimus namespace")
cmd.Flags().BoolVarP(&r.parallel, "parallel", "", false, "Backfill job runs in parallel")
cmd.Flags().StringVarP(&r.description, "description", "d", "", "Description of why backfill is needed")
cmd.Flags().StringVarP(&r.jobConfig, "job-config", "", "", "additional job configurations")
cmd.Flags().BoolVarP(&r.dryRun, "dry-run", "", false, "inspect replayed runs without taking effect on scheduler")
// Mandatory flags if config is not set
cmd.Flags().StringVarP(&r.projectName, "project-name", "p", "", "Name of the optimus project")
cmd.Flags().StringVar(&r.host, "host", "", "Optimus service endpoint url")
}
func (r *createCommand) PreRunE(cmd *cobra.Command, _ []string) error {
conf, err := internal.LoadOptionalConfig(r.configFilePath)
if err != nil {
return err
}
if conf == nil {
internal.MarkFlagsRequired(cmd, []string{"project-name", "host"})
return nil
}
if r.projectName == "" {
r.projectName = conf.Project.Name
}
if r.host == "" {
r.host = conf.Host
}
r.connection = connection.New(r.logger, conf)
return nil
}
func (r *createCommand) RunE(_ *cobra.Command, args []string) error {
jobName := args[0]
startTime := args[1]
endTime := args[1]
if len(args) >= 3 { //nolint: gomnd
endTime = args[2]
}
replayReq, err := r.createReplayRequest(jobName, startTime, endTime, r.jobConfig)
if err != nil {
return err
}
if r.dryRun {
replayDryRunReq := convertReplayToReplayDryRunRequest(replayReq)
err := r.replayDryRun(replayDryRunReq)
if err != nil {
return err
}
return nil
}
return r.replay(replayReq)
}
func convertReplayToReplayDryRunRequest(replayReq *pb.ReplayRequest) *pb.ReplayDryRunRequest {
return &pb.ReplayDryRunRequest{
ProjectName: replayReq.GetProjectName(),
JobName: replayReq.GetJobName(),
NamespaceName: replayReq.GetNamespaceName(),
StartTime: replayReq.GetStartTime(),
EndTime: replayReq.GetEndTime(),
Parallel: replayReq.GetParallel(),
Description: replayReq.GetDescription(),
JobConfig: replayReq.GetJobConfig(),
}
}
func (r *createCommand) replayDryRun(replayDryRunReq *pb.ReplayDryRunRequest) error {
conn, err := r.connection.Create(r.host)
if err != nil {
return err
}
defer conn.Close()
replayService := pb.NewReplayServiceClient(conn)
ctx, cancelFunc := context.WithTimeout(context.Background(), replayTimeout)
defer cancelFunc()
resp, err := replayService.ReplayDryRun(ctx, replayDryRunReq)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
r.logger.Error("Replay dry-run took too long, timing out")
}
return fmt.Errorf("replay dry-run request failed: %w", err)
}
runs := resp.GetReplayRuns()
buff := &bytes.Buffer{}
header := []string{"scheduled at", "status"}
stringifyReplayRuns(buff, header, runs)
r.logger.Info("List of runs to be replayed:")
r.logger.Info(buff.String())
return nil
}
func (r *createCommand) replay(replayReq *pb.ReplayRequest) error {
conn, err := r.connection.Create(r.host)
if err != nil {
return err
}
defer conn.Close()
replayService := pb.NewReplayServiceClient(conn)
ctx, cancelFunc := context.WithTimeout(context.Background(), replayTimeout)
defer cancelFunc()
resp, err := replayService.Replay(ctx, replayReq)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
r.logger.Error("Replay creation took too long, timing out")
}
return fmt.Errorf("replay request failed: %w", err)
}
r.logger.Info("Replay request is accepted and it is in progress")
r.logger.Info("Either you could wait or you could close (ctrl+c) and check the status with `optimus replay status %s` command later", resp.Id)
return r.waitForReplayState(resp.Id)
}
func (r *createCommand) waitForReplayState(replayID string) error {
spinner := progressbar.NewProgressBarWithWriter(r.logger.Writer())
status := "created"
spinner.Start(fmt.Sprintf("%s...", status))
for {
resp, err := r.getReplay(replayID)
if err != nil {
return err
}
if status != resp.Status {
status = resp.Status
spinner.StartNewLine(fmt.Sprintf("%s...", status))
}
if _, ok := terminalStatuses[status]; ok {
spinner.StartNewLine("\n")
spinner.Stop()
r.logger.Info("\n" + stringifyReplayStatus(resp))
break
}
time.Sleep(time.Duration(pollIntervalInSecond) * time.Second)
}
spinner.Stop()
return nil
}
func (r *createCommand) getReplay(replayID string) (*pb.GetReplayResponse, error) {
return getReplay(r.host, replayID, r.connection)
}
func (r *createCommand) createReplayRequest(jobName, startTimeStr, endTimeStr, jobConfig string) (*pb.ReplayRequest, error) {
startTime, err := getTimeProto(startTimeStr)
if err != nil {
return nil, err
}
endTime, err := getTimeProto(endTimeStr)
if err != nil {
return nil, err
}
replayReq := &pb.ReplayRequest{
ProjectName: r.projectName,
JobName: jobName,
NamespaceName: r.namespaceName,
StartTime: startTime,
EndTime: endTime,
Parallel: r.parallel,
Description: r.description,
JobConfig: jobConfig,
}
return replayReq, nil
}
func getTimeProto(timeStr string) (*timestamppb.Timestamp, error) {
var parsedTime time.Time
var err error
for _, ISOTimeLayout := range supportedISOTimeLayouts {
parsedTime, err = time.Parse(ISOTimeLayout, timeStr)
if err == nil {
break
}
}
if err != nil {
return nil, err
}
return timestamppb.New(parsedTime), nil
}