Browse Source

YARN-9782. Avoid DNS resolution while running SLS. Contributed by Abhishek Modi.

Abhishek Modi 5 years ago
parent
commit
2478cbafe6

+ 25 - 0
hadoop-tools/hadoop-sls/src/main/java/org/apache/hadoop/yarn/sls/SLSRunner.java

@@ -22,6 +22,7 @@ import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.Reader;
+import java.security.Security;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -150,6 +151,10 @@ public class SLSRunner extends Configured implements Tool {
     SLS, RUMEN, SYNTH
   }
 
+  public static final String NETWORK_CACHE_TTL = "networkaddress.cache.ttl";
+  public static final String NETWORK_NEGATIVE_CACHE_TTL =
+      "networkaddress.cache.negative.ttl";
+
   private TraceType inputType;
   private SynthTraceJobProducer stjp;
 
@@ -241,6 +246,9 @@ public class SLSRunner extends Configured implements Tool {
 
   public void start() throws IOException, ClassNotFoundException, YarnException,
       InterruptedException {
+
+    enableDNSCaching(getConf());
+
     // start resource manager
     startRM();
     // start node managers
@@ -260,6 +268,23 @@ public class SLSRunner extends Configured implements Tool {
     runner.start();
   }
 
+  /**
+   * Enables DNS Caching based on config. If DNS caching is enabled, then set
+   * the DNS cache to infinite time. Since in SLS random nodes are added, DNS
+   * resolution can take significant time which can cause erroneous results.
+   * For more details, check <a href=
+   * "https://docs.oracle.com/javase/8/docs/technotes/guides/net/properties.html">
+   * Java Networking Properties</a>
+   * @param conf Configuration object.
+   */
+  static void enableDNSCaching(Configuration conf) {
+    if (conf.getBoolean(SLSConfiguration.DNS_CACHING_ENABLED,
+        SLSConfiguration.DNS_CACHING_ENABLED_DEFAULT)) {
+      Security.setProperty(NETWORK_CACHE_TTL, "-1");
+      Security.setProperty(NETWORK_NEGATIVE_CACHE_TTL, "-1");
+    }
+  }
+
   private void startRM() throws ClassNotFoundException, YarnException {
     Configuration rmConf = new YarnConfiguration(getConf());
     String schedulerClass = rmConf.get(YarnConfiguration.RM_SCHEDULER);

+ 3 - 0
hadoop-tools/hadoop-sls/src/main/java/org/apache/hadoop/yarn/sls/conf/SLSConfiguration.java

@@ -28,6 +28,9 @@ import org.apache.hadoop.yarn.api.records.Resource;
 public class SLSConfiguration {
   // sls
   public static final String PREFIX = "yarn.sls.";
+  public static final String DNS_CACHING_ENABLED = PREFIX
+      + "dns.caching.enabled";
+  public static final boolean DNS_CACHING_ENABLED_DEFAULT = false;
   // runner
   public static final String RUNNER_PREFIX = PREFIX + "runner.";
   public static final String RUNNER_POOL_SIZE = RUNNER_PREFIX + "pool.size";

+ 3 - 1
hadoop-tools/hadoop-sls/src/test/java/org/apache/hadoop/yarn/sls/BaseSLSRunnerTest.java

@@ -64,7 +64,9 @@ public abstract class BaseSLSRunnerTest {
 
   @After
   public void tearDown() throws InterruptedException {
-    sls.stop();
+    if (sls != null) {
+      sls.stop();
+    }
   }
 
   public void runSLS(Configuration conf, long timeout) throws Exception {

+ 39 - 0
hadoop-tools/hadoop-sls/src/test/java/org/apache/hadoop/yarn/sls/TestSLSRunner.java

@@ -22,14 +22,18 @@ import net.jcip.annotations.NotThreadSafe;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacityScheduler;
 import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.FairScheduler;
+import org.apache.hadoop.yarn.sls.conf.SLSConfiguration;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
 import org.junit.runners.Parameterized.*;
 
+import java.security.Security;
 import java.util.*;
 
+import static org.junit.Assert.assertEquals;
+
 /**
  * This test performs simple runs of the SLS with different trace types and
  * schedulers.
@@ -86,4 +90,39 @@ public class TestSLSRunner extends BaseSLSRunnerTest {
     runSLS(conf, timeTillShutdownInsec);
   }
 
+  /**
+   * Test to check whether caching is enabled based on config.
+   */
+  @Test
+  public void testEnableCaching() {
+    String networkCacheDefault = Security.getProperty(
+        SLSRunner.NETWORK_CACHE_TTL);
+    String networkNegativeCacheDefault =
+        Security.getProperty(SLSRunner.NETWORK_NEGATIVE_CACHE_TTL);
+
+    try {
+      Configuration conf = new Configuration(false);
+      // check when dns caching is disabled
+      conf.setBoolean(SLSConfiguration.DNS_CACHING_ENABLED, false);
+      SLSRunner.enableDNSCaching(conf);
+      assertEquals(networkCacheDefault,
+          Security.getProperty(SLSRunner.NETWORK_CACHE_TTL));
+      assertEquals(networkNegativeCacheDefault,
+          Security.getProperty(SLSRunner.NETWORK_NEGATIVE_CACHE_TTL));
+
+      // check when dns caching is enabled
+      conf.setBoolean(SLSConfiguration.DNS_CACHING_ENABLED, true);
+      SLSRunner.enableDNSCaching(conf);
+      assertEquals("-1",
+          Security.getProperty(SLSRunner.NETWORK_CACHE_TTL));
+      assertEquals("-1",
+          Security.getProperty(SLSRunner.NETWORK_NEGATIVE_CACHE_TTL));
+    } finally {
+      // set security settings back to default
+      Security.setProperty(SLSRunner.NETWORK_CACHE_TTL,
+          String.valueOf(networkCacheDefault));
+      Security.setProperty(SLSRunner.NETWORK_NEGATIVE_CACHE_TTL,
+          String.valueOf(networkNegativeCacheDefault));
+    }
+  }
 }