From c40d884848ba4e3a9e20ff24592179f3ab29a31c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=B6ren=20Henning?= <soeren.henning@email.uni-kiel.de>
Date: Tue, 1 Mar 2022 14:06:28 +0100
Subject: [PATCH] Allow disabling DNS caching

---
 docs/theodolite-benchmarks/load-generator.md      |  1 +
 .../loadgenerator/ConfigurationKeys.java          |  3 +++
 .../loadgenerator/EnvVarLoadGeneratorFactory.java | 15 +++++++++++++++
 3 files changed, 19 insertions(+)

diff --git a/docs/theodolite-benchmarks/load-generator.md b/docs/theodolite-benchmarks/load-generator.md
index e0d35332e..8fefa1961 100644
--- a/docs/theodolite-benchmarks/load-generator.md
+++ b/docs/theodolite-benchmarks/load-generator.md
@@ -62,6 +62,7 @@ The prebuilt container images can be configured with the following environment v
 | `PERIOD_MS` | The time in milliseconds between generating two messages for the same sensor. With our Theodolite benchmarks, we apply an [open workload model](https://www.usenix.org/legacy/event/nsdi06/tech/full_papers/schroeder/schroeder.pdf) in which new messages are generated at a fixed rate, without considering the think time of the target server nor the time required for generating a message. | 1000 |
 | `VALUE` | The constant `valueInW` of an `ActivePowerRecord`. | 10 |
 | `THREADS` | Number of worker threads used to generate the load. | 4 |
+| `DISABLE_DNS_CACHING` | Set to `true` to disable DNS caching by the underlying JVM. You might want to do so when generating load via HTTP that should be sent to different target instances. | `false` |
 
 Please note that there are some additional configuration options for benchmark [UC4's load generator](hhttps://github.com/cau-se/theodolite/blob/master/theodolite-benchmarks/uc4-load-generator/src/main/java/rocks/theodolite/benchmarks/uc4/loadgenerator/LoadGenerator.java).
 
diff --git a/theodolite-benchmarks/load-generator-commons/src/main/java/rocks/theodolite/benchmarks/loadgenerator/ConfigurationKeys.java b/theodolite-benchmarks/load-generator-commons/src/main/java/rocks/theodolite/benchmarks/loadgenerator/ConfigurationKeys.java
index 30735a561..3ffa7a647 100644
--- a/theodolite-benchmarks/load-generator-commons/src/main/java/rocks/theodolite/benchmarks/loadgenerator/ConfigurationKeys.java
+++ b/theodolite-benchmarks/load-generator-commons/src/main/java/rocks/theodolite/benchmarks/loadgenerator/ConfigurationKeys.java
@@ -23,6 +23,8 @@ public final class ConfigurationKeys {
 
   public static final String THREADS = "THREADS";
 
+  public static final String DISABLE_DNS_CACHING = "DISABLE_DNS_CACHING";
+
   public static final String TARGET = "TARGET";
 
   public static final String KAFKA_BOOTSTRAP_SERVERS = "KAFKA_BOOTSTRAP_SERVERS";
@@ -45,6 +47,7 @@ public final class ConfigurationKeys {
 
   public static final String PUBSUB_EMULATOR_HOST = "PUBSUB_EMULATOR_HOST";
 
+
   private ConfigurationKeys() {}
 
 }
diff --git a/theodolite-benchmarks/load-generator-commons/src/main/java/rocks/theodolite/benchmarks/loadgenerator/EnvVarLoadGeneratorFactory.java b/theodolite-benchmarks/load-generator-commons/src/main/java/rocks/theodolite/benchmarks/loadgenerator/EnvVarLoadGeneratorFactory.java
index 7123cfe54..9faa4b3bd 100644
--- a/theodolite-benchmarks/load-generator-commons/src/main/java/rocks/theodolite/benchmarks/loadgenerator/EnvVarLoadGeneratorFactory.java
+++ b/theodolite-benchmarks/load-generator-commons/src/main/java/rocks/theodolite/benchmarks/loadgenerator/EnvVarLoadGeneratorFactory.java
@@ -13,7 +13,17 @@ class EnvVarLoadGeneratorFactory {
 
   private static final Logger LOGGER = LoggerFactory.getLogger(EnvVarLoadGeneratorFactory.class);
 
+  public static final boolean DISABLE_DNS_CACHING_DEFAULT = false;
+
   public LoadGenerator create(final LoadGenerator loadGeneratorTemplate) {
+
+    final boolean disableDnsCaching = Boolean.parseBoolean(Objects.requireNonNullElse(
+        System.getenv(ConfigurationKeys.DISABLE_DNS_CACHING),
+        Boolean.toString(DISABLE_DNS_CACHING_DEFAULT)));
+    if (disableDnsCaching) {
+      this.disableDnsCaching();
+    }
+
     final int numSensors = Integer.parseInt(Objects.requireNonNullElse(
         System.getenv(ConfigurationKeys.NUM_SENSORS),
         Integer.toString(LoadGenerator.NUMBER_OF_KEYS_DEFAULT)));
@@ -135,4 +145,9 @@ class EnvVarLoadGeneratorFactory {
     return recordSender;
   }
 
+  private void disableDnsCaching() {
+    LOGGER.info("Disable DNS caching.");
+    java.security.Security.setProperty("networkaddress.cache.ttl", "0");
+  }
+
 }
-- 
GitLab