-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathSpecActivityImpl.java
136 lines (121 loc) · 6.16 KB
/
SpecActivityImpl.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
/*
* Copyright (c) 2022 Airbyte, Inc., all rights reserved.
*/
package io.airbyte.workers.temporal.spec;
import static io.airbyte.metrics.lib.ApmTraceConstants.ACTIVITY_TRACE_OPERATION_NAME;
import static io.airbyte.metrics.lib.ApmTraceConstants.Tags.ATTEMPT_NUMBER_KEY;
import static io.airbyte.metrics.lib.ApmTraceConstants.Tags.DOCKER_IMAGE_KEY;
import static io.airbyte.metrics.lib.ApmTraceConstants.Tags.JOB_ID_KEY;
import datadog.trace.api.Trace;
import io.airbyte.api.client.AirbyteApiClient;
import io.airbyte.commons.features.FeatureFlags;
import io.airbyte.commons.functional.CheckedSupplier;
import io.airbyte.commons.protocol.AirbyteMessageSerDeProvider;
import io.airbyte.commons.protocol.AirbyteMessageVersionedMigratorFactory;
import io.airbyte.commons.temporal.CancellationHandler;
import io.airbyte.commons.temporal.config.WorkerMode;
import io.airbyte.commons.version.Version;
import io.airbyte.config.Configs.WorkerEnvironment;
import io.airbyte.config.ConnectorJobOutput;
import io.airbyte.config.JobGetSpecConfig;
import io.airbyte.config.helpers.LogConfigs;
import io.airbyte.metrics.lib.ApmTraceUtils;
import io.airbyte.persistence.job.models.IntegrationLauncherConfig;
import io.airbyte.persistence.job.models.JobRunConfig;
import io.airbyte.workers.Worker;
import io.airbyte.workers.WorkerConfigs;
import io.airbyte.workers.general.DefaultGetSpecWorker;
import io.airbyte.workers.internal.AirbyteStreamFactory;
import io.airbyte.workers.internal.VersionedAirbyteStreamFactory;
import io.airbyte.workers.process.AirbyteIntegrationLauncher;
import io.airbyte.workers.process.IntegrationLauncher;
import io.airbyte.workers.process.ProcessFactory;
import io.airbyte.workers.temporal.TemporalAttemptExecution;
import io.micronaut.context.annotation.Requires;
import io.micronaut.context.annotation.Value;
import io.temporal.activity.Activity;
import io.temporal.activity.ActivityExecutionContext;
import jakarta.inject.Named;
import jakarta.inject.Singleton;
import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
@Singleton
@Requires(env = WorkerMode.CONTROL_PLANE)
public class SpecActivityImpl implements SpecActivity {
private final WorkerConfigs workerConfigs;
private final ProcessFactory processFactory;
private final Path workspaceRoot;
private final WorkerEnvironment workerEnvironment;
private final LogConfigs logConfigs;
private final AirbyteApiClient airbyteApiClient;
private final String airbyteVersion;
private final AirbyteMessageSerDeProvider serDeProvider;
private final AirbyteMessageVersionedMigratorFactory migratorFactory;
private final FeatureFlags featureFlags;
public SpecActivityImpl(@Named("specWorkerConfigs") final WorkerConfigs workerConfigs,
@Named("specProcessFactory") final ProcessFactory processFactory,
@Named("workspaceRoot") final Path workspaceRoot,
final WorkerEnvironment workerEnvironment,
final LogConfigs logConfigs,
final AirbyteApiClient airbyteApiClient,
@Value("${airbyte.version}") final String airbyteVersion,
final AirbyteMessageSerDeProvider serDeProvider,
final AirbyteMessageVersionedMigratorFactory migratorFactory,
final FeatureFlags featureFlags) {
this.workerConfigs = workerConfigs;
this.processFactory = processFactory;
this.workspaceRoot = workspaceRoot;
this.workerEnvironment = workerEnvironment;
this.logConfigs = logConfigs;
this.airbyteApiClient = airbyteApiClient;
this.airbyteVersion = airbyteVersion;
this.serDeProvider = serDeProvider;
this.migratorFactory = migratorFactory;
this.featureFlags = featureFlags;
}
@Trace(operationName = ACTIVITY_TRACE_OPERATION_NAME)
@Override
public ConnectorJobOutput run(final JobRunConfig jobRunConfig, final IntegrationLauncherConfig launcherConfig) {
ApmTraceUtils.addTagsToTrace(Map.of(ATTEMPT_NUMBER_KEY, jobRunConfig.getAttemptId(), DOCKER_IMAGE_KEY, launcherConfig.getDockerImage(),
JOB_ID_KEY, jobRunConfig.getJobId()));
final Supplier<JobGetSpecConfig> inputSupplier =
() -> new JobGetSpecConfig().withDockerImage(launcherConfig.getDockerImage()).withIsCustomConnector(launcherConfig.getIsCustomConnector());
final ActivityExecutionContext context = Activity.getExecutionContext();
final TemporalAttemptExecution<JobGetSpecConfig, ConnectorJobOutput> temporalAttemptExecution = new TemporalAttemptExecution<>(
workspaceRoot,
workerEnvironment,
logConfigs,
jobRunConfig,
getWorkerFactory(launcherConfig),
inputSupplier,
new CancellationHandler.TemporalCancellationHandler(context),
airbyteApiClient,
airbyteVersion,
() -> context);
return temporalAttemptExecution.get();
}
private CheckedSupplier<Worker<JobGetSpecConfig, ConnectorJobOutput>, Exception> getWorkerFactory(
final IntegrationLauncherConfig launcherConfig) {
return () -> {
final AirbyteStreamFactory streamFactory = getStreamFactory(launcherConfig);
final IntegrationLauncher integrationLauncher = new AirbyteIntegrationLauncher(
launcherConfig.getJobId(),
launcherConfig.getAttemptId().intValue(),
launcherConfig.getDockerImage(),
processFactory,
workerConfigs.getResourceRequirements(),
launcherConfig.getAllowedHosts(),
launcherConfig.getIsCustomConnector(),
featureFlags);
return new DefaultGetSpecWorker(integrationLauncher, streamFactory);
};
}
private AirbyteStreamFactory getStreamFactory(final IntegrationLauncherConfig launcherConfig) {
final Version protocolVersion =
launcherConfig.getProtocolVersion() != null ? launcherConfig.getProtocolVersion() : migratorFactory.getMostRecentVersion();
// Try to detect version from the stream
return new VersionedAirbyteStreamFactory<>(serDeProvider, migratorFactory, protocolVersion, Optional.empty()).withDetectVersion(true);
}
}