szetszwo commented on code in PR #7892: URL: https://github.com/apache/hadoop/pull/7892#discussion_r2308244987
########## hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/concurrent/HadoopThread.java: ########## @@ -0,0 +1,105 @@ +/** + * 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.hadoop.util.concurrent; + +import java.security.PrivilegedAction; +import javax.security.auth.Subject; + +import org.apache.hadoop.security.authentication.util.SubjectUtil; + +/** + * Helper class to restore Subject propagation behavior after the JEP411/JEP486 + * changes + * + * Runnables can be specified normally, but the work() method has to be + * overridden instead of run() when subclassing. + */ +public class HadoopThread extends Thread { + + Subject startSubject; + Runnable hadoopTarget; + + public HadoopThread() { + super(); + } + + public HadoopThread(Runnable target) { + super(); + this.hadoopTarget = target; + } + + public HadoopThread(ThreadGroup group, Runnable target) { + // The target passed to Thread has no effect, we only pass it + // because there is no super(group) constructor. + super(group, target); + this.hadoopTarget = target; + } + + public HadoopThread(Runnable target, String name) { + super(name); + this.hadoopTarget = target; + } + + public HadoopThread(String name) { + super(name); + } + + public HadoopThread(ThreadGroup group, String name) { + super(group, name); + } + + public HadoopThread(ThreadGroup group, Runnable target, String name) { + super(group, name); + this.hadoopTarget = target; + } Review Comment: It should add a Builder class instead of having public constructor. Then, we can make it configurable to return `Thread` or `HadoopThread` or something else in the future. ```diff diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/concurrent/HadoopThread.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/concurrent/HadoopThread.java index 5ce2c75f4f4..fedd4e3a107 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/concurrent/HadoopThread.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/concurrent/HadoopThread.java @@ -32,6 +32,22 @@ */ public class HadoopThread extends Thread { + public static Builder newBuilder() { + return new Builder(); + } + + public static class Builder { + private Runnable runnable; + + public Builder setRunnable(Runnable runnable) { + this.runnable = runnable; + return this; + } + public HadoopThread build() { + return new HadoopThread(runnable); + } + } + Subject startSubject; Runnable hadoopTarget; diff --git a/hadoop-hdfs-project/hadoop-hdfs-client/src/test/java/org/apache/hadoop/hdfs/util/TestByteArrayManager.java b/hadoop-hdfs-project/hadoop-hdfs-client/src/test/java/org/apache/hadoop/hdfs/util/TestByteArrayManager.java index 80d2859684f..35e7b71fd2e 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-client/src/test/java/org/apache/hadoop/hdfs/util/TestByteArrayManager.java +++ b/hadoop-hdfs-project/hadoop-hdfs-client/src/test/java/org/apache/hadoop/hdfs/util/TestByteArrayManager.java @@ -178,7 +178,9 @@ public void testAllocateRecycle() throws Exception { waitForAll(allocator.futures); // allocate one more should be blocked - final AllocatorThread t = new AllocatorThread(arrayLength, bam); + final Thread t = HadoopThread.newBuilder() + .setRunnable(() -> new AllocatorMethod().run(arrayLength, bam)) + .build(); t.start(); // check if the thread is waiting, timed wait or runnable. @@ -227,18 +229,10 @@ static <T> void waitForAll(List<Future<T>> furtures) throws Exception { } } - static class AllocatorThread extends HadoopThread { - private final ByteArrayManager bam; - private final int arrayLength; + static class AllocatorMethod { private byte[] array; - AllocatorThread(int arrayLength, ByteArrayManager bam) { - this.bam = bam; - this.arrayLength = arrayLength; - } - - @Override - public void work() { + void run(int arrayLength, ByteArrayManager bam) { try { array = bam.newByteArray(arrayLength); } catch (InterruptedException e) { diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsVolumeList.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsVolumeList.java index a9ecdd46bcb..8b4883beb96 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsVolumeList.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsVolumeList.java @@ -508,8 +508,7 @@ void addBlockPool(final String bpid, final Configuration conf) throws IOExceptio new ConcurrentHashMap<FsVolumeSpi, IOException>(); List<Thread> blockPoolAddingThreads = new ArrayList<Thread>(); for (final FsVolumeImpl v : volumes) { - Thread t = new HadoopThread() { - public void work() { + final Thread t = HadoopThread.newBuilder().setRunnable(() -> { try (FsVolumeReference ref = v.obtainReference()) { FsDatasetImpl.LOG.info("Scanning block pool " + bpid + " on volume " + v + "..."); @@ -523,8 +522,7 @@ public void work() { ". Will throw later.", ioe); unhealthyDataDirs.put(v, ioe); } - } - }; + }).build(); blockPoolAddingThreads.add(t); t.start(); } ``` -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
