|
| 1 | +package com.linkedin.hoptimator.operator.flink; |
| 2 | + |
| 3 | +import com.linkedin.hoptimator.catalog.Resource; |
| 4 | +import com.linkedin.hoptimator.catalog.flink.FlinkStreamingSqlJob; |
| 5 | +import com.linkedin.hoptimator.operator.Operator; |
| 6 | +import com.linkedin.hoptimator.models.V1alpha1SqlJob; |
| 7 | +import com.linkedin.hoptimator.models.V1alpha1SqlJobSpec.DialectEnum; |
| 8 | +import com.linkedin.hoptimator.models.V1alpha1SqlJobSpec.ExecutionModeEnum; |
| 9 | +import com.linkedin.hoptimator.models.V1alpha1SqlJobStatus; |
| 10 | + |
| 11 | +import io.kubernetes.client.extended.controller.reconciler.Reconciler; |
| 12 | +import io.kubernetes.client.extended.controller.reconciler.Request; |
| 13 | +import io.kubernetes.client.extended.controller.reconciler.Result; |
| 14 | + |
| 15 | +import org.slf4j.Logger; |
| 16 | +import org.slf4j.LoggerFactory; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | +import java.util.stream.Collectors; |
| 20 | + |
| 21 | +/** |
| 22 | + * Manifests streaming SqlJobs as Flink jobs. |
| 23 | + * |
| 24 | + */ |
| 25 | +public class FlinkStreamingSqlJobReconciler implements Reconciler { |
| 26 | + private final static Logger log = LoggerFactory.getLogger(FlinkStreamingSqlJobReconciler.class); |
| 27 | + private final static String SQLJOB = "hoptimator.linkedin.com/v1alpha1/SqlJob"; |
| 28 | + |
| 29 | + private final Operator operator; |
| 30 | + |
| 31 | + public FlinkStreamingSqlJobReconciler(Operator operator) { |
| 32 | + this.operator = operator; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public Result reconcile(Request request) { |
| 37 | + log.info("Reconciling request {}", request); |
| 38 | + String name = request.getName(); |
| 39 | + String namespace = request.getNamespace(); |
| 40 | + Result result = new Result(true, operator.pendingRetryDuration()); |
| 41 | + |
| 42 | + try { |
| 43 | + V1alpha1SqlJob object = operator.<V1alpha1SqlJob>fetch(SQLJOB, namespace, name); |
| 44 | + |
| 45 | + if (object == null) { |
| 46 | + log.info("Object {}/{} deleted. Skipping."); |
| 47 | + return new Result(false); |
| 48 | + } |
| 49 | + |
| 50 | + V1alpha1SqlJobStatus status = object.getStatus(); |
| 51 | + if (status == null) { |
| 52 | + status = new V1alpha1SqlJobStatus(); |
| 53 | + object.setStatus(status); |
| 54 | + } |
| 55 | + |
| 56 | + List<String> sql = object.getSpec().getSql(); |
| 57 | + String script = sql.stream().collect(Collectors.joining(";\n")); |
| 58 | + |
| 59 | + DialectEnum dialect = object.getSpec().getDialect(); |
| 60 | + if (!DialectEnum.FLINK.equals(dialect)) { |
| 61 | + log.info("Not Flink SQL. Skipping."); |
| 62 | + return new Result(false); |
| 63 | + } |
| 64 | + |
| 65 | + ExecutionModeEnum mode = object.getSpec().getExecutionMode(); |
| 66 | + if (!ExecutionModeEnum.STREAMING.equals(mode)) { |
| 67 | + log.info("Not a streaming job. Skipping."); |
| 68 | + return new Result(false); |
| 69 | + } |
| 70 | + |
| 71 | + Resource.TemplateFactory templateFactory = new Resource.SimpleTemplateFactory(Resource.Environment.EMPTY); |
| 72 | + Resource sqlJob = new FlinkStreamingSqlJob(namespace, name, script); |
| 73 | + boolean allReady = true; |
| 74 | + boolean anyFailed = false; |
| 75 | + for (String yaml : sqlJob.render(templateFactory)) { |
| 76 | + operator.apply(yaml, object); |
| 77 | + if (!operator.isReady(yaml)) { |
| 78 | + allReady = false; |
| 79 | + } |
| 80 | + if (operator.isFailed(yaml)) { |
| 81 | + anyFailed = true; |
| 82 | + allReady = false; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + object.getStatus().setReady(allReady); |
| 87 | + object.getStatus().setFailed(anyFailed); |
| 88 | + |
| 89 | + if (allReady) { |
| 90 | + object.getStatus().setMessage("Ready."); |
| 91 | + result = new Result(false); // done |
| 92 | + } |
| 93 | + if (anyFailed) { |
| 94 | + object.getStatus().setMessage("Failed."); |
| 95 | + result = new Result(false); // done |
| 96 | + } |
| 97 | + |
| 98 | + operator.apiFor(SQLJOB).updateStatus(object, x -> object.getStatus()) |
| 99 | + .onFailure((x, y) -> log.error("Failed to update status of SqlJob {}: {}.", name, y.getMessage())) |
| 100 | + .throwsApiException(); |
| 101 | + |
| 102 | + } catch (Exception e) { |
| 103 | + log.error("Encountered exception while reconciling Flink streaming SqlJob {}/{}", namespace, name, e); |
| 104 | + return new Result(true, operator.failureRetryDuration()); |
| 105 | + } |
| 106 | + return result; |
| 107 | + } |
| 108 | +} |
| 109 | + |
0 commit comments