kaxil commented on code in PR #65956: URL: https://github.com/apache/airflow/pull/65956#discussion_r3324719012
########## java-sdk/sdk/src/main/kotlin/org/apache/airflow/sdk/execution/Task.kt: ########## @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.airflow.sdk.execution + +import org.apache.airflow.sdk.Bundle +import org.apache.airflow.sdk.Client +import org.apache.airflow.sdk.Context +import org.apache.airflow.sdk.execution.comm.AssetProfile +import org.apache.airflow.sdk.execution.comm.StartupDetails +import org.apache.airflow.sdk.execution.comm.SucceedTask +import org.apache.airflow.sdk.execution.comm.TaskState +import java.time.OffsetDateTime + +internal object TaskResult { + fun success( + endDate: OffsetDateTime = OffsetDateTime.now(), + taskOutlets: List<AssetProfile> = emptyList(), + outletEvents: List<Map<String, Any?>> = emptyList(), + renderedMapIndex: String? = null, + ) = SucceedTask().also { + it.state = "success" + it.endDate = endDate + it.taskOutlets = taskOutlets + it.outletEvents = outletEvents + it.renderedMapIndex = renderedMapIndex + } + + fun of( + state: TaskState.State, + endDate: OffsetDateTime = OffsetDateTime.now(), + renderedMapIndex: String? = null, + ) = TaskState().also { + it.state = state + it.endDate = endDate + it.renderedMapIndex = renderedMapIndex + } +} + +internal object TaskRunner { + val logger = Logger(TaskRunner::class) + + internal fun runTask( + bundle: Bundle, + request: StartupDetails, + client: Client, + ): Any { + val task = bundle.dags[request.ti.dagId]?.tasks[request.ti.taskId] ?: return TaskResult.of(TaskState.State.REMOVED) + val instance = task.getDeclaredConstructor().newInstance() + return try { + instance.execute(Context.from(request), client) + TaskResult.success() + } catch (e: Exception) { + logger.error("Error executing task", mapOf("ti" to request.ti, "error" to e, "trace" to e.stackTraceToString())) + e.printStackTrace() Review Comment: `e.printStackTrace()` writes to JVM stderr rather than the `--logs` channel forwarded to Airflow, so it won't appear in the task log. The `logger.error` on the line above already captures the trace, so this one looks redundant. ########## java-sdk/sdk/src/main/kotlin/org/apache/airflow/sdk/execution/Task.kt: ########## @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.airflow.sdk.execution + +import org.apache.airflow.sdk.Bundle +import org.apache.airflow.sdk.Client +import org.apache.airflow.sdk.Context +import org.apache.airflow.sdk.execution.comm.AssetProfile +import org.apache.airflow.sdk.execution.comm.StartupDetails +import org.apache.airflow.sdk.execution.comm.SucceedTask +import org.apache.airflow.sdk.execution.comm.TaskState +import java.time.OffsetDateTime + +internal object TaskResult { + fun success( + endDate: OffsetDateTime = OffsetDateTime.now(), + taskOutlets: List<AssetProfile> = emptyList(), + outletEvents: List<Map<String, Any?>> = emptyList(), + renderedMapIndex: String? = null, + ) = SucceedTask().also { + it.state = "success" + it.endDate = endDate + it.taskOutlets = taskOutlets + it.outletEvents = outletEvents + it.renderedMapIndex = renderedMapIndex + } + + fun of( + state: TaskState.State, + endDate: OffsetDateTime = OffsetDateTime.now(), + renderedMapIndex: String? = null, + ) = TaskState().also { + it.state = state + it.endDate = endDate + it.renderedMapIndex = renderedMapIndex + } +} + +internal object TaskRunner { + val logger = Logger(TaskRunner::class) + + internal fun runTask( + bundle: Bundle, + request: StartupDetails, + client: Client, + ): Any { + val task = bundle.dags[request.ti.dagId]?.tasks[request.ti.taskId] ?: return TaskResult.of(TaskState.State.REMOVED) + val instance = task.getDeclaredConstructor().newInstance() Review Comment: `newInstance()` sits outside the `try` block below, so if a task class has no public no-arg constructor or its constructor throws, the exception escapes `runTask` instead of going through the `TaskState.FAILED` path that `execute()` failures get. The supervisor then never receives a terminal state for the task. Could the instantiation move inside the `try` so constructor failures also report FAILED? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
