-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
GenerateInputActivityImpl.java
170 lines (150 loc) · 8.72 KB
/
GenerateInputActivityImpl.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
/*
* Copyright (c) 2022 Airbyte, Inc., all rights reserved.
*/
package io.airbyte.workers.temporal.scheduling.activities;
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.JOB_ID_KEY;
import datadog.trace.api.Trace;
import io.airbyte.commons.json.Jsons;
import io.airbyte.commons.temporal.TemporalWorkflowUtils;
import io.airbyte.commons.temporal.config.WorkerMode;
import io.airbyte.commons.temporal.exception.RetryableException;
import io.airbyte.config.JobConfig.ConfigType;
import io.airbyte.config.JobResetConnectionConfig;
import io.airbyte.config.JobSyncConfig;
import io.airbyte.config.ResetSourceConfiguration;
import io.airbyte.config.StandardDestinationDefinition;
import io.airbyte.config.StandardSourceDefinition;
import io.airbyte.config.StandardSync;
import io.airbyte.config.StandardSyncInput;
import io.airbyte.config.persistence.ConfigRepository;
import io.airbyte.metrics.lib.ApmTraceUtils;
import io.airbyte.persistence.job.JobPersistence;
import io.airbyte.persistence.job.models.IntegrationLauncherConfig;
import io.airbyte.persistence.job.models.Job;
import io.airbyte.persistence.job.models.JobRunConfig;
import io.airbyte.workers.WorkerConstants;
import io.airbyte.workers.utils.ConfigReplacer;
import io.micronaut.context.annotation.Requires;
import jakarta.inject.Singleton;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Singleton
@Requires(env = WorkerMode.CONTROL_PLANE)
public class GenerateInputActivityImpl implements GenerateInputActivity {
private final JobPersistence jobPersistence;
private final ConfigRepository configRepository;
private static final Logger LOGGER = LoggerFactory.getLogger(GenerateInputActivity.class);
public GenerateInputActivityImpl(final JobPersistence jobPersistence,
final ConfigRepository configRepository) {
this.jobPersistence = jobPersistence;
this.configRepository = configRepository;
}
@Trace(operationName = ACTIVITY_TRACE_OPERATION_NAME)
@Override
public GeneratedJobInput getSyncWorkflowInput(final SyncInput input) {
final ConfigReplacer configReplacer = new ConfigReplacer(LOGGER);
try {
ApmTraceUtils.addTagsToTrace(Map.of(ATTEMPT_NUMBER_KEY, input.getAttemptId(), JOB_ID_KEY, input.getJobId()));
final long jobId = input.getJobId();
final int attempt = input.getAttemptId();
final JobSyncConfig config;
final Job job = jobPersistence.getJob(jobId);
final ConfigType jobConfigType = job.getConfig().getConfigType();
if (ConfigType.SYNC.equals(jobConfigType)) {
config = job.getConfig().getSync();
} else if (ConfigType.RESET_CONNECTION.equals(jobConfigType)) {
final JobResetConnectionConfig resetConnection = job.getConfig().getResetConnection();
final ResetSourceConfiguration resetSourceConfiguration = resetConnection.getResetSourceConfiguration();
config = new JobSyncConfig()
.withNamespaceDefinition(resetConnection.getNamespaceDefinition())
.withNamespaceFormat(resetConnection.getNamespaceFormat())
.withPrefix(resetConnection.getPrefix())
.withSourceDockerImage(WorkerConstants.RESET_JOB_SOURCE_DOCKER_IMAGE_STUB)
.withDestinationDockerImage(resetConnection.getDestinationDockerImage())
.withDestinationProtocolVersion(resetConnection.getDestinationProtocolVersion())
// null check for backwards compatibility with reset jobs that did not have a
// resetSourceConfiguration
.withSourceConfiguration(resetSourceConfiguration == null ? Jsons.emptyObject() : Jsons.jsonNode(resetSourceConfiguration))
.withDestinationConfiguration(resetConnection.getDestinationConfiguration())
.withConfiguredAirbyteCatalog(resetConnection.getConfiguredAirbyteCatalog())
.withOperationSequence(resetConnection.getOperationSequence())
.withResourceRequirements(resetConnection.getResourceRequirements())
.withState(resetConnection.getState())
.withIsSourceCustomConnector(resetConnection.getIsSourceCustomConnector())
.withIsDestinationCustomConnector(resetConnection.getIsDestinationCustomConnector())
.withWorkspaceId(resetConnection.getWorkspaceId());
} else {
throw new IllegalStateException(
String.format("Unexpected config type %s for job %d. The only supported config types for this activity are (%s)",
jobConfigType,
jobId,
List.of(ConfigType.SYNC, ConfigType.RESET_CONNECTION)));
}
final JobRunConfig jobRunConfig = TemporalWorkflowUtils.createJobRunConfig(jobId, attempt);
final UUID connectionId = UUID.fromString(job.getScope());
final StandardSync standardSync = configRepository.getStandardSync(connectionId);
final StandardSourceDefinition sourceDefinition =
configRepository.getSourceDefinitionFromSource(standardSync.getSourceId());
final StandardDestinationDefinition destinationDefinition =
configRepository.getDestinationDefinitionFromDestination(standardSync.getDestinationId());
final String destinationNormalizationDockerImage = destinationDefinition.getNormalizationConfig() != null
? destinationDefinition.getNormalizationConfig().getNormalizationRepository() + ":" +
destinationDefinition.getNormalizationConfig().getNormalizationTag()
: null;
final String normalizationIntegrationType =
destinationDefinition.getNormalizationConfig() != null ? destinationDefinition.getNormalizationConfig().getNormalizationIntegrationType()
: null;
final IntegrationLauncherConfig sourceLauncherConfig = new IntegrationLauncherConfig()
.withJobId(String.valueOf(jobId))
.withAttemptId((long) attempt)
.withDockerImage(config.getSourceDockerImage())
.withProtocolVersion(config.getSourceProtocolVersion())
.withIsCustomConnector(config.getIsSourceCustomConnector())
.withAllowedHosts(ConfigType.RESET_CONNECTION.equals(jobConfigType) ? null
: configReplacer.getAllowedHosts(sourceDefinition.getAllowedHosts(), config.getSourceConfiguration()));
final IntegrationLauncherConfig destinationLauncherConfig = new IntegrationLauncherConfig()
.withJobId(String.valueOf(jobId))
.withAttemptId((long) attempt)
.withDockerImage(config.getDestinationDockerImage())
.withProtocolVersion(config.getDestinationProtocolVersion())
.withIsCustomConnector(config.getIsDestinationCustomConnector())
.withNormalizationDockerImage(destinationNormalizationDockerImage)
.withSupportsDbt(destinationDefinition.getSupportsDbt())
.withNormalizationIntegrationType(normalizationIntegrationType)
.withAllowedHosts(configReplacer.getAllowedHosts(destinationDefinition.getAllowedHosts(), config.getDestinationConfiguration()));
final StandardSyncInput syncInput = new StandardSyncInput()
.withNamespaceDefinition(config.getNamespaceDefinition())
.withNamespaceFormat(config.getNamespaceFormat())
.withPrefix(config.getPrefix())
.withSourceId(standardSync.getSourceId())
.withDestinationId(standardSync.getDestinationId())
.withSourceConfiguration(config.getSourceConfiguration())
.withDestinationConfiguration(config.getDestinationConfiguration())
.withOperationSequence(config.getOperationSequence())
.withWebhookOperationConfigs(config.getWebhookOperationConfigs())
.withCatalog(config.getConfiguredAirbyteCatalog())
.withState(config.getState())
.withResourceRequirements(config.getResourceRequirements())
.withSourceResourceRequirements(config.getSourceResourceRequirements())
.withDestinationResourceRequirements(config.getDestinationResourceRequirements())
.withConnectionId(standardSync.getConnectionId())
.withWorkspaceId(config.getWorkspaceId());
return new GeneratedJobInput(jobRunConfig, sourceLauncherConfig, destinationLauncherConfig, syncInput);
} catch (final Exception e) {
throw new RetryableException(e);
}
}
@Trace(operationName = ACTIVITY_TRACE_OPERATION_NAME)
@Override
public GeneratedJobInput getSyncWorkflowInputWithAttemptNumber(final SyncInputWithAttemptNumber input) {
ApmTraceUtils.addTagsToTrace(Map.of(JOB_ID_KEY, input.getJobId()));
return getSyncWorkflowInput(new SyncInput(
input.getAttemptNumber(),
input.getJobId()));
}
}