szetszwo commented on PR #7892:
URL: https://github.com/apache/hadoop/pull/7892#issuecomment-3271843449
@stoty , the code will look like below:
```diff
diff --git
a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/ha/EditLogTailer.java
b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/ha/EditLogTailer.java
index cf416307f47..3260b770ac0 100644
---
a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/ha/EditLogTailer.java
+++
b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/ha/EditLogTailer.java
@@ -85,8 +85,8 @@ public class EditLogTailer {
public static final long DFS_HA_TAILEDITS_MAX_TXNS_PER_LOCK_DEFAULT =
Long.MAX_VALUE;
- private final EditLogTailerThread tailerThread;
-
+ private final SubjectDoAsThread<EditLogTailerThread> tailerThread;
+
private final Configuration conf;
private final FSNamesystem namesystem;
private final Iterator<RemoteNameNodeInfo> nnLookup;
@@ -180,7 +180,11 @@ public class EditLogTailer {
private Timer timer;
public EditLogTailer(FSNamesystem namesystem, Configuration conf) {
- this.tailerThread = new EditLogTailerThread();
+ final EditLogTailerThread t = new EditLogTailerThread();
+ this.tailerThread = SubjectDoAsThread.<EditLogTailerThread>newBuilder()
+ .setThread(new EditLogTailerThread())
+ .setOriginal(t)
+ .build();
this.conf = conf;
this.namesystem = namesystem;
this.timer = new Timer();
@@ -271,7 +275,7 @@ public void start() {
}
public void stop() throws IOException {
- tailerThread.setShouldRun(false);
+ tailerThread.getOriginal().setShouldRun(false);
tailerThread.interrupt();
try {
tailerThread.join();
@@ -664,6 +668,6 @@ public RemoteNameNodeInfo getCurrentNN() {
@VisibleForTesting
public void setShouldRunForTest(boolean shouldRun) {
- this.tailerThread.setShouldRun(shouldRun);
+ this.tailerThread.getOriginal().setShouldRun(shouldRun);
}
}
diff --git
a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/ha/SubjectDoAsThread.java
b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/ha/SubjectDoAsThread.java
new file mode 100644
index 00000000000..edac81048a6
--- /dev/null
+++
b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/ha/SubjectDoAsThread.java
@@ -0,0 +1,64 @@
+package org.apache.hadoop.hdfs.server.namenode.ha;
+
+import org.apache.hadoop.security.authentication.util.SubjectUtil;
+
+import javax.security.auth.Subject;
+import java.security.PrivilegedAction;
+import java.util.Objects;
+
+public class SubjectDoAsThread<T> extends Thread {
+ private final T original;
+ private final Runnable runnable;
+ private Subject startSubject;
+
+ public static class Builder<T> {
+ private ThreadGroup group;
+ private String name;
+ private Runnable runnable;
+ private T original;
+
+ public Builder<T> setThread(Thread thread) {
+ this.group = thread.getThreadGroup();
+ this.name = thread.getName();
+ this.runnable = thread;
+ return this;
+ }
+
+ public Builder<T> setOriginal(T original) {
+ this.original = original;
+ return this;
+ }
+
+ public SubjectDoAsThread<T> build() {
+ return new SubjectDoAsThread<>(group, name, original, runnable);
+ }
+ }
+
+ private SubjectDoAsThread(ThreadGroup group, String name, T original,
Runnable runnable) {
+ super(group, name);
+ this.original = original;
+ this.runnable = Objects.requireNonNull(runnable, "runnable == null");
+ }
+
+ static <T> Builder<T> newBuilder() {
+ return new Builder<>();
+ }
+
+ @Override
+ public final void start() {
+ startSubject = SubjectUtil.current();
+ super.start();
+ }
+
+ @Override
+ public final void run() {
+ SubjectUtil.doAs(startSubject, (PrivilegedAction<Void>) () -> {
+ runnable.run();
+ return null;
+ });
+ }
+
+ public T getOriginal() {
+ return original;
+ }
+}
```
--
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]