|
| 1 | +package com.linkedin.openhouse.jobs.scheduler.tasks; |
| 2 | + |
| 3 | +import com.linkedin.openhouse.common.JobState; |
| 4 | +import com.linkedin.openhouse.jobs.client.JobsClient; |
| 5 | +import com.linkedin.openhouse.jobs.client.TablesClient; |
| 6 | +import com.linkedin.openhouse.jobs.client.model.JobConf; |
| 7 | +import com.linkedin.openhouse.jobs.util.AppConstants; |
| 8 | +import com.linkedin.openhouse.jobs.util.OtelConfig; |
| 9 | +import com.linkedin.openhouse.jobs.util.ReplicationConfig; |
| 10 | +import com.linkedin.openhouse.jobs.util.TableMetadata; |
| 11 | +import io.opentelemetry.api.common.AttributeKey; |
| 12 | +import io.opentelemetry.api.common.Attributes; |
| 13 | +import io.opentelemetry.api.metrics.Meter; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Optional; |
| 16 | +import lombok.extern.slf4j.Slf4j; |
| 17 | + |
| 18 | +/** A task to apply replication to a table. */ |
| 19 | +@Slf4j |
| 20 | +public class TableReplicationTask extends TableOperationTask<TableMetadata> { |
| 21 | + public static final JobConf.JobTypeEnum OPERATION_TYPE = JobConf.JobTypeEnum.REPLICATION; |
| 22 | + private static final Meter METER = OtelConfig.getMeter(OperationTask.class.getName()); |
| 23 | + |
| 24 | + protected TableReplicationTask( |
| 25 | + JobsClient jobsClient, TablesClient tablesClient, TableMetadata tableMetadata) { |
| 26 | + super(jobsClient, tablesClient, tableMetadata); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public JobConf.JobTypeEnum getType() { |
| 31 | + return OPERATION_TYPE; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + protected List<String> getArgs() { |
| 36 | + return null; |
| 37 | + } |
| 38 | + |
| 39 | + /* Returns empty value iff the callable was interrupted by future cancel. */ |
| 40 | + @Override |
| 41 | + public Optional<JobState> call() { |
| 42 | + if (!shouldRun()) { |
| 43 | + log.info("Skipping job for {}, since the operation doesn't need to be run", metadata); |
| 44 | + return Optional.empty(); |
| 45 | + } |
| 46 | + List<ReplicationConfig> replicationConfigs = metadata.getReplicationConfig(); |
| 47 | + for (ReplicationConfig config : replicationConfigs) { |
| 48 | + log.info("Launching job for {}", metadata); |
| 49 | + Attributes typeAttributes = |
| 50 | + Attributes.of( |
| 51 | + AttributeKey.stringKey(AppConstants.TYPE), |
| 52 | + getType().getValue(), |
| 53 | + (metadata.getClass().equals(TableMetadata.class) |
| 54 | + ? AttributeKey.stringKey(AppConstants.TABLE_NAME) |
| 55 | + : AttributeKey.stringKey(AppConstants.DATABASE_NAME)), |
| 56 | + metadata.getEntityName()); |
| 57 | + try { |
| 58 | + OtelConfig.executeWithStats( |
| 59 | + () -> { |
| 60 | + // this is a wrapper to convert boolean false to an exception |
| 61 | + if (!launchJob(config)) { |
| 62 | + throw new Exception(); |
| 63 | + } |
| 64 | + return null; |
| 65 | + }, |
| 66 | + METER, |
| 67 | + "submit", |
| 68 | + typeAttributes); |
| 69 | + } catch (Exception e) { |
| 70 | + log.error( |
| 71 | + "Could not launch job {} for {}. Exception {}", getType(), metadata, e.getMessage()); |
| 72 | + return Optional.empty(); |
| 73 | + } |
| 74 | + log.info("Launched a job for {}", metadata); |
| 75 | + // TODO: implement wait loop for job to finish and update metrics and job state |
| 76 | + // TODO: update the jobState with returned value from Airflow client |
| 77 | + } |
| 78 | + return Optional.of(Enum.valueOf(JobState.class, JobState.FAILED.name())); |
| 79 | + } |
| 80 | + |
| 81 | + protected boolean launchJob(ReplicationConfig config) { |
| 82 | + String jobName = |
| 83 | + String.format( |
| 84 | + "%s_%s_%s_%s", |
| 85 | + getType(), config.getCluster(), metadata.getDbName(), metadata.getTableName()); |
| 86 | + // TODO: Trigger Airflow job using airflow job client. Config can be used to create airflow |
| 87 | + // client params |
| 88 | + // TODO: Poll for job ID |
| 89 | + log.info("Triggering Replication job: {} via airflow client", jobName); |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + protected boolean shouldRun() { |
| 95 | + return metadata.isPrimary() && metadata.getReplicationConfig() != null; |
| 96 | + } |
| 97 | +} |
0 commit comments