|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.gluten.task; |
| 18 | + |
| 19 | +import org.apache.spark.TaskContext; |
| 20 | + |
| 21 | +/** |
| 22 | + * Callback surface that native code uses to obtain the current thread's Spark task attempt id via |
| 23 | + * JNI, without having to plumb it explicitly through every JNI entry point. |
| 24 | + * |
| 25 | + * <p>Native side reads the value from {@link TaskContext#get()} at the moment it is needed. Since |
| 26 | + * {@link TaskContext} is a per-thread ThreadLocal, this only returns a meaningful value when the |
| 27 | + * native call is running on the executor task thread (or on a JVM thread inheriting the same |
| 28 | + * ThreadLocal). It returns {@code -1L} on the driver or on a native worker thread that has no |
| 29 | + * associated Spark task. |
| 30 | + */ |
| 31 | +public final class TaskContextJniWrapper { |
| 32 | + private TaskContextJniWrapper() {} |
| 33 | + |
| 34 | + /** |
| 35 | + * Returns the current thread's Spark task attempt id, or {@code -1L} when there is no task |
| 36 | + * context on the calling thread. |
| 37 | + */ |
| 38 | + public static long currentTaskAttemptId() { |
| 39 | + TaskContext tc = TaskContext.get(); |
| 40 | + return tc == null ? -1L : tc.taskAttemptId(); |
| 41 | + } |
| 42 | +} |
0 commit comments