Skip to content

Commit 94513f0

Browse files
authored
remove config repository from config fetch activity impl scheduling (#20908)
* Remove config repository from config fetch activity impl for scheduling data
1 parent 16a591e commit 94513f0

File tree

2 files changed

+153
-106
lines changed

2 files changed

+153
-106
lines changed

airbyte-workers/src/main/java/io/airbyte/workers/temporal/scheduling/activities/ConfigFetchActivityImpl.java

Lines changed: 71 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@
1111
import datadog.trace.api.Trace;
1212
import io.airbyte.api.client.generated.ConnectionApi;
1313
import io.airbyte.api.client.invoker.generated.ApiException;
14+
import io.airbyte.api.client.model.generated.ConnectionIdRequestBody;
1415
import io.airbyte.api.client.model.generated.ConnectionRead;
16+
import io.airbyte.api.client.model.generated.ConnectionSchedule;
17+
import io.airbyte.api.client.model.generated.ConnectionScheduleDataBasicSchedule;
18+
import io.airbyte.api.client.model.generated.ConnectionScheduleDataBasicSchedule.TimeUnitEnum;
19+
import io.airbyte.api.client.model.generated.ConnectionScheduleDataCron;
20+
import io.airbyte.api.client.model.generated.ConnectionScheduleType;
1521
import io.airbyte.api.client.model.generated.ConnectionStatus;
1622
import io.airbyte.commons.temporal.config.WorkerMode;
1723
import io.airbyte.commons.temporal.exception.RetryableException;
18-
import io.airbyte.config.Cron;
1924
import io.airbyte.config.StandardSync;
20-
import io.airbyte.config.StandardSync.ScheduleType;
21-
import io.airbyte.config.StandardSync.Status;
22-
import io.airbyte.config.helpers.ScheduleHelpers;
2325
import io.airbyte.config.persistence.ConfigNotFoundException;
2426
import io.airbyte.config.persistence.ConfigRepository;
2527
import io.airbyte.metrics.lib.ApmTraceUtils;
@@ -41,6 +43,7 @@
4143
import java.util.Set;
4244
import java.util.TimeZone;
4345
import java.util.UUID;
46+
import java.util.concurrent.TimeUnit;
4447
import java.util.function.Supplier;
4548
import lombok.extern.slf4j.Slf4j;
4649
import org.joda.time.DateTimeZone;
@@ -106,48 +109,48 @@ public StandardSync getStandardSync(final UUID connectionId) throws JsonValidati
106109
public ScheduleRetrieverOutput getTimeToWait(final ScheduleRetrieverInput input) {
107110
try {
108111
ApmTraceUtils.addTagsToTrace(Map.of(CONNECTION_ID_KEY, input.getConnectionId()));
109-
final StandardSync standardSync = configRepository.getStandardSync(input.getConnectionId());
110-
111-
if (standardSync.getScheduleType() != null) {
112-
return this.getTimeToWaitFromScheduleType(standardSync, input.getConnectionId());
112+
final ConnectionIdRequestBody connectionIdRequestBody = new ConnectionIdRequestBody().connectionId(input.getConnectionId());
113+
final ConnectionRead connectionRead = connectionApi.getConnection(connectionIdRequestBody);
114+
if (connectionRead.getScheduleType() != null) {
115+
return this.getTimeToWaitFromScheduleType(connectionRead, input.getConnectionId());
113116
}
114-
return this.getTimeToWaitFromLegacy(standardSync, input.getConnectionId());
115-
} catch (final IOException | JsonValidationException | ConfigNotFoundException e) {
117+
return this.getTimeToWaitFromLegacy(connectionRead, input.getConnectionId());
118+
} catch (final IOException | ApiException e) {
116119
throw new RetryableException(e);
117120
}
118121
}
119122

120123
/**
121-
* @param standardSync
124+
* @param connectionRead
122125
* @param connectionId
123126
* @return
124127
* @throws IOException
125128
*
126129
* This method consumes the `scheduleType` and `scheduleData` fields.
127130
*/
128-
private ScheduleRetrieverOutput getTimeToWaitFromScheduleType(final StandardSync standardSync, final UUID connectionId) throws IOException {
129-
if (standardSync.getScheduleType() == ScheduleType.MANUAL || standardSync.getStatus() != Status.ACTIVE) {
131+
private ScheduleRetrieverOutput getTimeToWaitFromScheduleType(final ConnectionRead connectionRead, final UUID connectionId) throws IOException {
132+
if (connectionRead.getScheduleType() == ConnectionScheduleType.MANUAL || connectionRead.getStatus() != ConnectionStatus.ACTIVE) {
130133
// Manual syncs wait for their first run
131134
return new ScheduleRetrieverOutput(Duration.ofDays(100 * 365));
132135
}
133136

134137
final Optional<Job> previousJobOptional = jobPersistence.getLastReplicationJob(connectionId);
135138

136-
if (standardSync.getScheduleType() == ScheduleType.BASIC_SCHEDULE) {
139+
if (connectionRead.getScheduleType() == ConnectionScheduleType.BASIC) {
137140
if (previousJobOptional.isEmpty()) {
138141
// Basic schedules don't wait for their first run.
139142
return new ScheduleRetrieverOutput(Duration.ZERO);
140143
}
141144
final Job previousJob = previousJobOptional.get();
142145
final long prevRunStart = previousJob.getStartedAtInSecond().orElse(previousJob.getCreatedAtInSecond());
143-
final long nextRunStart = prevRunStart + ScheduleHelpers.getIntervalInSecond(standardSync.getScheduleData().getBasicSchedule());
146+
final long nextRunStart = prevRunStart + getIntervalInSecond(connectionRead.getScheduleData().getBasicSchedule());
144147
final Duration timeToWait = Duration.ofSeconds(
145148
Math.max(0, nextRunStart - currentSecondsSupplier.get()));
146149
return new ScheduleRetrieverOutput(timeToWait);
147150
}
148151

149-
else { // standardSync.getScheduleType() == ScheduleType.CRON
150-
final Cron scheduleCron = standardSync.getScheduleData().getCron();
152+
else { // connectionRead.getScheduleType() == ConnectionScheduleType.CRON
153+
final ConnectionScheduleDataCron scheduleCron = connectionRead.getScheduleData().getCron();
151154
final TimeZone timeZone = DateTimeZone.forID(scheduleCron.getCronTimeZone()).toTimeZone();
152155
try {
153156
final CronExpression cronExpression = new CronExpression(scheduleCron.getCronExpression());
@@ -164,18 +167,18 @@ private ScheduleRetrieverOutput getTimeToWaitFromScheduleType(final StandardSync
164167
Duration timeToWait = Duration.ofSeconds(
165168
Math.max(0, nextRunStart.getTime() / MS_PER_SECOND - currentSecondsSupplier.get()));
166169

167-
timeToWait = addSchedulingNoiseForAllowListedWorkspace(timeToWait, standardSync);
170+
timeToWait = addSchedulingNoiseForAllowListedWorkspace(timeToWait, connectionRead);
168171
return new ScheduleRetrieverOutput(timeToWait);
169172
} catch (final ParseException e) {
170173
throw (DateTimeException) new DateTimeException(e.getMessage()).initCause(e);
171174
}
172175
}
173176
}
174177

175-
private Duration addSchedulingNoiseForAllowListedWorkspace(Duration timeToWait, StandardSync standardSync) {
178+
private Duration addSchedulingNoiseForAllowListedWorkspace(Duration timeToWait, ConnectionRead connectionRead) {
176179
final UUID workspaceId;
177180
try {
178-
workspaceId = workspaceHelper.getWorkspaceForConnectionId(standardSync.getConnectionId());
181+
workspaceId = workspaceHelper.getWorkspaceForConnectionId(connectionRead.getConnectionId());
179182
} catch (JsonValidationException | ConfigNotFoundException e) {
180183
// We tolerate exceptions and fail open by doing nothing.
181184
return timeToWait;
@@ -184,7 +187,7 @@ private Duration addSchedulingNoiseForAllowListedWorkspace(Duration timeToWait,
184187
// Only apply to a specific set of workspaces.
185188
return timeToWait;
186189
}
187-
if (!standardSync.getScheduleType().equals(ScheduleType.CRON)) {
190+
if (!connectionRead.getScheduleType().equals(ConnectionScheduleType.CRON)) {
188191
// Only apply noise to cron connections.
189192
return timeToWait;
190193
}
@@ -197,30 +200,30 @@ private Duration addSchedulingNoiseForAllowListedWorkspace(Duration timeToWait,
197200
}
198201

199202
/**
200-
* @param standardSync
203+
* @param connectionRead
201204
* @param connectionId
202205
* @return
203206
* @throws IOException
204207
*
205208
* This method consumes the `schedule` field.
206209
*/
207-
private ScheduleRetrieverOutput getTimeToWaitFromLegacy(final StandardSync standardSync, final UUID connectionId) throws IOException {
208-
if (standardSync.getSchedule() == null || standardSync.getStatus() != Status.ACTIVE) {
210+
private ScheduleRetrieverOutput getTimeToWaitFromLegacy(final ConnectionRead connectionRead, final UUID connectionId) throws IOException {
211+
if (connectionRead.getSchedule() == null || connectionRead.getStatus() != ConnectionStatus.ACTIVE) {
209212
// Manual syncs wait for their first run
210213
return new ScheduleRetrieverOutput(Duration.ofDays(100 * 365));
211214
}
212215

213216
final Optional<Job> previousJobOptional = jobPersistence.getLastReplicationJob(connectionId);
214217

215-
if (previousJobOptional.isEmpty() && standardSync.getSchedule() != null) {
218+
if (previousJobOptional.isEmpty() && connectionRead.getSchedule() != null) {
216219
// Non-manual syncs don't wait for their first run
217220
return new ScheduleRetrieverOutput(Duration.ZERO);
218221
}
219222

220223
final Job previousJob = previousJobOptional.get();
221224
final long prevRunStart = previousJob.getStartedAtInSecond().orElse(previousJob.getCreatedAtInSecond());
222225

223-
final long nextRunStart = prevRunStart + ScheduleHelpers.getIntervalInSecond(standardSync.getSchedule());
226+
final long nextRunStart = prevRunStart + getIntervalInSecond(connectionRead.getSchedule());
224227

225228
final Duration timeToWait = Duration.ofSeconds(
226229
Math.max(0, nextRunStart - currentSecondsSupplier.get()));
@@ -261,4 +264,46 @@ public Optional<ConnectionStatus> getStatus(final UUID connectionId) {
261264
}
262265
}
263266

267+
private Long getIntervalInSecond(final ConnectionScheduleDataBasicSchedule schedule) {
268+
return getSecondsInUnit(schedule.getTimeUnit()) * schedule.getUnits();
269+
}
270+
271+
private Long getIntervalInSecond(final ConnectionSchedule schedule) {
272+
return getSecondsInUnit(schedule.getTimeUnit()) * schedule.getUnits();
273+
}
274+
275+
private Long getSecondsInUnit(final TimeUnitEnum timeUnitEnum) {
276+
switch (timeUnitEnum) {
277+
case MINUTES:
278+
return TimeUnit.MINUTES.toSeconds(1);
279+
case HOURS:
280+
return TimeUnit.HOURS.toSeconds(1);
281+
case DAYS:
282+
return TimeUnit.DAYS.toSeconds(1);
283+
case WEEKS:
284+
return TimeUnit.DAYS.toSeconds(1) * 7;
285+
case MONTHS:
286+
return TimeUnit.DAYS.toSeconds(1) * 30;
287+
default:
288+
throw new RuntimeException("Unhandled TimeUnitEnum: " + timeUnitEnum);
289+
}
290+
}
291+
292+
private Long getSecondsInUnit(final ConnectionSchedule.TimeUnitEnum timeUnitEnum) {
293+
switch (timeUnitEnum) {
294+
case MINUTES:
295+
return TimeUnit.MINUTES.toSeconds(1);
296+
case HOURS:
297+
return TimeUnit.HOURS.toSeconds(1);
298+
case DAYS:
299+
return TimeUnit.DAYS.toSeconds(1);
300+
case WEEKS:
301+
return TimeUnit.DAYS.toSeconds(1) * 7;
302+
case MONTHS:
303+
return TimeUnit.DAYS.toSeconds(1) * 30;
304+
default:
305+
throw new RuntimeException("Unhandled TimeUnitEnum: " + timeUnitEnum);
306+
}
307+
}
308+
264309
}

0 commit comments

Comments
 (0)