|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +// Serializes the shared test Dags with this SDK, for |
| 21 | +// scripts/ci/lang_sdk_serialization/compare.py: |
| 22 | +// |
| 23 | +// java -cp <sdk test runtime classpath> org.apache.airflow.sdk.conformance.SerializeJavaKt \ |
| 24 | +// scripts/ci/lang_sdk_serialization/test_dags.yaml serialized_java.json |
| 25 | +// |
| 26 | +// Each Dag is built with the interface API and written as the runtime answers |
| 27 | +// a parse request, keyed by Dag ID. A task's `upstream` becomes an ordering |
| 28 | +// edge, which a Java task serializes the same way as a data edge. |
| 29 | +package org.apache.airflow.sdk.conformance |
| 30 | + |
| 31 | +import com.fasterxml.jackson.databind.JsonNode |
| 32 | +import com.fasterxml.jackson.databind.ObjectMapper |
| 33 | +import com.fasterxml.jackson.dataformat.yaml.YAMLMapper |
| 34 | +import org.apache.airflow.sdk.Bundle |
| 35 | +import org.apache.airflow.sdk.Client |
| 36 | +import org.apache.airflow.sdk.Context |
| 37 | +import org.apache.airflow.sdk.DagDef |
| 38 | +import org.apache.airflow.sdk.Deps |
| 39 | +import org.apache.airflow.sdk.Task |
| 40 | +import org.apache.airflow.sdk.TaskGroupRef |
| 41 | +import org.apache.airflow.sdk.TaskRef |
| 42 | +import org.apache.airflow.sdk.execution.serializeDag |
| 43 | +import org.apache.airflow.sdk.internal.Field |
| 44 | +import org.apache.airflow.sdk.internal.FieldType |
| 45 | +import org.apache.airflow.sdk.internal.SchemaFields |
| 46 | +import java.io.File |
| 47 | +import java.time.Duration |
| 48 | +import java.time.OffsetDateTime |
| 49 | + |
| 50 | +class ConformanceTask : Task { |
| 51 | + override fun execute( |
| 52 | + context: Context, |
| 53 | + client: Client, |
| 54 | + ) = Unit |
| 55 | +} |
| 56 | + |
| 57 | +fun main(args: Array<String>) { |
| 58 | + require(args.size == 2) { "usage: SerializeJava <test_dags.yaml> <output.json>" } |
| 59 | + val cases = YAMLMapper().readTree(File(args[0])).path("dags") |
| 60 | + val bundle = Bundle() |
| 61 | + cases.forEach { bundle.register(buildDag(it)) } |
| 62 | + val serialized = |
| 63 | + bundle.dags.values.associate { dag -> |
| 64 | + dag.id to mapOf("__version" to 3, "dag" to serializeDag(dag, "", ".")) |
| 65 | + } |
| 66 | + ObjectMapper().writerWithDefaultPrettyPrinter().writeValue(File(args[1]), serialized) |
| 67 | +} |
| 68 | + |
| 69 | +private fun buildDag(case: JsonNode): DagDef { |
| 70 | + val dag = DagDef(case.path("dag_id").asText()) |
| 71 | + case.path("spec").fields().forEach { (key, value) -> dag.config(key, toValue(SchemaFields.DAG, key, value)) } |
| 72 | + |
| 73 | + // A group ID is fully qualified, so its parent is whatever comes before the last dot. |
| 74 | + val groups = linkedMapOf<String, TaskGroupRef>() |
| 75 | + case.path("groups").forEach { node -> |
| 76 | + val groupId = node.asText() |
| 77 | + val parentId = groupId.substringBeforeLast('.', "") |
| 78 | + val localId = groupId.substringAfterLast('.') |
| 79 | + groups[groupId] = if (parentId.isEmpty()) dag.taskGroup(localId) else groups.getValue(parentId).taskGroup(localId) |
| 80 | + } |
| 81 | + |
| 82 | + val tasks = linkedMapOf<String, TaskRef<Unit>>() |
| 83 | + case.path("tasks").forEach { task -> |
| 84 | + val groupId = task.path("group").asText("") |
| 85 | + val localId = task.path("task_id").asText() |
| 86 | + val ref = |
| 87 | + if (groupId.isEmpty()) { |
| 88 | + dag.task<Unit>(localId, ConformanceTask::class.java) |
| 89 | + } else { |
| 90 | + groups.getValue(groupId).task(localId, ConformanceTask::class.java) |
| 91 | + } |
| 92 | + task.path("spec").fields().forEach { (key, value) -> ref.config(key, toValue(SchemaFields.TASK, key, value)) } |
| 93 | + task.path("upstream").forEach { ref.after(tasks.getValue(it.asText())) } |
| 94 | + tasks[ref.def.id] = ref |
| 95 | + } |
| 96 | + |
| 97 | + case.path("order_edges").forEach { edge -> |
| 98 | + val node = { id: String -> groups[id] as Deps.Flow? ?: tasks.getValue(id) } |
| 99 | + node(edge[0].asText()).before(node(edge[1].asText())) |
| 100 | + } |
| 101 | + return dag |
| 102 | +} |
| 103 | + |
| 104 | +/** Reads a YAML value as the Java type the config key takes. */ |
| 105 | +private fun toValue( |
| 106 | + table: Map<String, Field>, |
| 107 | + key: String, |
| 108 | + node: JsonNode, |
| 109 | +): Any = |
| 110 | + when (requireNotNull(table[key]) { "Unknown config key: '$key'" }.type) { |
| 111 | + FieldType.STRING -> node.asText() |
| 112 | + FieldType.BOOLEAN -> node.asBoolean() |
| 113 | + FieldType.INTEGER -> node.asInt() |
| 114 | + FieldType.NUMBER -> node.numberValue() |
| 115 | + // `!datetime` is an ISO 8601 timestamp, and `!timedelta` a number of seconds. |
| 116 | + FieldType.DATETIME -> OffsetDateTime.parse(node.asText()) |
| 117 | + FieldType.TIMEDELTA -> Duration.ofNanos((node.asText().toDouble() * 1e9).toLong()) |
| 118 | + FieldType.STRING_ARRAY -> node.map { it.asText() } |
| 119 | + } |
0 commit comments