-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathJob.java
202 lines (171 loc) · 5.75 KB
/
Job.java
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
/*
* Copyright (c) 2022 Airbyte, Inc., all rights reserved.
*/
package io.airbyte.persistence.job.models;
import com.google.common.base.Preconditions;
import io.airbyte.config.JobConfig;
import io.airbyte.config.JobConfig.ConfigType;
import io.airbyte.config.JobOutput;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
public class Job {
public static final Set<ConfigType> REPLICATION_TYPES = EnumSet.of(ConfigType.SYNC, ConfigType.RESET_CONNECTION);
private final long id;
private final ConfigType configType;
private final String scope;
private final JobConfig config;
private final JobStatus status;
private final Long startedAtInSecond;
private final long createdAtInSecond;
private final long updatedAtInSecond;
private final List<Attempt> attempts;
public Job(final long id,
final ConfigType configType,
final String scope,
final JobConfig config,
final List<Attempt> attempts,
final JobStatus status,
final @Nullable Long startedAtInSecond,
final long createdAtInSecond,
final long updatedAtInSecond) {
this.id = id;
this.configType = configType;
this.scope = scope;
this.config = config;
this.attempts = attempts;
this.status = status;
this.startedAtInSecond = startedAtInSecond;
this.createdAtInSecond = createdAtInSecond;
this.updatedAtInSecond = updatedAtInSecond;
}
public long getId() {
return id;
}
public ConfigType getConfigType() {
return configType;
}
public String getScope() {
return scope;
}
public JobConfig getConfig() {
return config;
}
public List<Attempt> getAttempts() {
return attempts;
}
public int getAttemptsCount() {
return attempts.size();
}
public JobStatus getStatus() {
return status;
}
public Optional<Long> getStartedAtInSecond() {
return Optional.ofNullable(startedAtInSecond);
}
public long getCreatedAtInSecond() {
return createdAtInSecond;
}
public long getUpdatedAtInSecond() {
return updatedAtInSecond;
}
@SuppressWarnings("PMD.AvoidLiteralsInIfCondition")
public Optional<Attempt> getSuccessfulAttempt() {
final List<Attempt> successfulAttempts = getAttempts()
.stream()
.filter(a -> a.getStatus() == AttemptStatus.SUCCEEDED)
.collect(Collectors.toList());
Preconditions.checkState(successfulAttempts.size() <= 1, String.format("Job %s has multiple successful attempts.", getId()));
if (successfulAttempts.size() == 1) {
return Optional.of(successfulAttempts.get(0));
} else {
return Optional.empty();
}
}
public Optional<JobOutput> getSuccessOutput() {
return getSuccessfulAttempt().flatMap(Attempt::getOutput);
}
public Optional<Attempt> getLastFailedAttempt() {
return getAttempts()
.stream()
.sorted(Comparator.comparing(Attempt::getCreatedAtInSecond).reversed())
.filter(a -> a.getStatus() == AttemptStatus.FAILED)
.findFirst();
}
public Optional<Attempt> getLastAttemptWithOutput() {
return getAttempts()
.stream()
.sorted(Comparator.comparing(Attempt::getCreatedAtInSecond).reversed())
.filter(a -> a.getOutput().isPresent() && a.getOutput().get().getSync() != null && a.getOutput().get().getSync().getState() != null)
.findFirst();
}
public Optional<Attempt> getLastAttempt() {
return getAttempts()
.stream()
.max(Comparator.comparing(Attempt::getCreatedAtInSecond));
}
public Optional<Attempt> getAttemptByNumber(final int attemptNumber) {
return getAttempts()
.stream()
.filter(a -> a.getAttemptNumber() == attemptNumber)
.findFirst();
}
public boolean hasRunningAttempt() {
return getAttempts().stream().anyMatch(a -> !Attempt.isAttemptInTerminalState(a));
}
public boolean isJobInTerminalState() {
return JobStatus.TERMINAL_STATUSES.contains(getStatus());
}
public void validateStatusTransition(final JobStatus newStatus) throws IllegalStateException {
final Set<JobStatus> validNewStatuses = JobStatus.VALID_STATUS_CHANGES.get(status);
if (!validNewStatuses.contains(newStatus)) {
throw new IllegalStateException(String.format(
"Transitioning Job %d from JobStatus %s to %s is not allowed. The only valid statuses that an be transitioned to from %s are %s",
id,
status,
newStatus,
status,
JobStatus.VALID_STATUS_CHANGES.get(status)));
}
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final Job job = (Job) o;
return id == job.id &&
createdAtInSecond == job.createdAtInSecond &&
updatedAtInSecond == job.updatedAtInSecond &&
Objects.equals(scope, job.scope) &&
Objects.equals(config, job.config) &&
status == job.status &&
Objects.equals(startedAtInSecond, job.startedAtInSecond) &&
Objects.equals(attempts, job.attempts);
}
@Override
public int hashCode() {
return Objects.hash(id, scope, config, status, startedAtInSecond, createdAtInSecond, updatedAtInSecond, attempts);
}
@Override
public String toString() {
return "Job{" +
"id=" + id +
", scope='" + scope + '\'' +
", config=" + config +
", status=" + status +
", startedAtInSecond=" + startedAtInSecond +
", createdAtInSecond=" + createdAtInSecond +
", updatedAtInSecond=" + updatedAtInSecond +
", attempts=" + attempts +
'}';
}
}