소스 검색

YARN-11592. Add timeout to GPGUtils#invokeRMWebService. (#6189) Contributed by Shilun Fan.

Reviewed-by: Inigo Goiri <inigoiri@apache.org>
Signed-off-by: Shilun Fan <slfan1989@apache.org>
slfan1989 1 년 전
부모
커밋
40e8300719

+ 5 - 0
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/conf/YarnConfiguration.java

@@ -4565,6 +4565,11 @@ public class YarnConfiguration extends Configuration {
   public static final String DEFAULT_GPG_WEBAPP_HTTPS_ADDRESS =
       "0.0.0.0:" + DEFAULT_GPG_WEBAPP_HTTPS_PORT;
 
+  public static final String GPG_WEBAPP_CONNECT_TIMEOUT = GPG_WEBAPP_PREFIX + "connect-timeout";
+  public static final long DEFAULT_GPG_WEBAPP_CONNECT_TIMEOUT = TimeUnit.SECONDS.toMillis(30);
+  public static final String GPG_WEBAPP_READ_TIMEOUT = GPG_WEBAPP_PREFIX + "read-timeout";
+  public static final long DEFAULT_GPG_WEBAPP_READ_TIMEOUT = TimeUnit.SECONDS.toMillis(30);
+
   /**
    * Connection and Read timeout from the Router to RM.
    */

+ 23 - 0
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/resources/yarn-default.xml

@@ -5723,4 +5723,27 @@
     <value>50000</value>
   </property>
 
+  <property>
+    <description>
+      Set the connect timeout interval, in milliseconds.
+      A value of 0 means no timeout, otherwise values must be between 1 and Integer#MAX_VALUE.
+      This ensures that the connection is established within a specified time,
+      or it triggers a connection timeout exception.
+    </description>
+    <name>yarn.federation.gpg.webapp.connect-timeout</name>
+    <value>30s</value>
+  </property>
+
+  <property>
+    <description>
+      Set the read timeout interval, in milliseconds.
+      A value of 0 means no timeout, otherwise values must be between 1 and Integer#MAX_VALUE.
+      This timeout specifies the maximum time a client should wait for data to be read from a server
+      once the connection has been established.
+      If data is not received within the specified time, a read timeout exception is raised.
+    </description>
+    <name>yarn.federation.gpg.webapp.read-timeout</name>
+    <value>30s</value>
+  </property>
+
 </configuration>

+ 20 - 1
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/main/java/org/apache/hadoop/yarn/server/globalpolicygenerator/GPGUtils.java

@@ -27,6 +27,7 @@ import java.net.InetSocketAddress;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.TimeUnit;
 
 import javax.ws.rs.core.MediaType;
 
@@ -65,7 +66,7 @@ public final class GPGUtils {
    */
   public static <T> T invokeRMWebService(String webAddr, String path, final Class<T> returnType,
       Configuration conf, String selectParam) {
-    Client client = Client.create();
+    Client client = createJerseyClient(conf);
     T obj;
 
     // webAddr stores the form of host:port in subClusterInfo
@@ -128,4 +129,22 @@ public final class GPGUtils {
     }
     return weights;
   }
+
+  /**
+   * Create JerseyClient based on configuration file.
+   * We will set the timeout when creating JerseyClient.
+   *
+   * @param conf Configuration.
+   * @return JerseyClient.
+   */
+  public static Client createJerseyClient(Configuration conf) {
+    Client client = Client.create();
+    int connectTimeOut = (int) conf.getTimeDuration(YarnConfiguration.GPG_WEBAPP_CONNECT_TIMEOUT,
+        YarnConfiguration.DEFAULT_GPG_WEBAPP_CONNECT_TIMEOUT, TimeUnit.MILLISECONDS);
+    client.setConnectTimeout(connectTimeOut);
+    int readTimeout = (int) conf.getTimeDuration(YarnConfiguration.GPG_WEBAPP_READ_TIMEOUT,
+        YarnConfiguration.DEFAULT_GPG_WEBAPP_READ_TIMEOUT, TimeUnit.MILLISECONDS);
+    client.setReadTimeout(readTimeout);
+    return client;
+  }
 }

+ 1 - 2
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-globalpolicygenerator/src/test/java/org/apache/hadoop/yarn/server/globalpolicygenerator/TestGPGPolicyFacade.java

@@ -42,7 +42,6 @@ import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Matchers;
 
-import java.io.IOException;
 import java.util.HashSet;
 import java.util.Set;
 
@@ -73,7 +72,7 @@ public class TestGPGPolicyFacade {
   }
 
   @Before
-  public void setUp() throws IOException, YarnException {
+  public void setUp() throws YarnException {
     stateStore = new MemoryFederationStateStore();
     stateStore.init(conf);
     facade.reinitialize(stateStore, conf);