From 6285b87b970f7b0323385074164ce7428fc0c72a Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Tue, 23 Jun 2020 15:14:59 -0700 Subject: [PATCH] Adjust gradle base port by one (#58368) When assigning ports for internal cluster tests, we use the gradle worker id as an adjustment on the base port of 10300. In order to not go outside the max port range, we modulo the worker id by 223. Since gradle worker ids start at 1, we expect to never actually get the base port of 10300. However, as the gradle daemon lasts for longer, the module can result in a value of 0, which cases the test to fail. This commit adjusts the modulo to ensure the value is never 0. closes #58279 --- .../java/org/elasticsearch/test/ESTestCase.java | 13 +++++++++++-- .../elasticsearch/test/test/ESTestCaseTests.java | 1 - 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java index c6dbcc3aa97..d9e21a4a976 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java @@ -1430,8 +1430,17 @@ public abstract class ESTestCase extends LuceneTestCase { // Ephemeral ports on Linux start at 32768 so we modulo to make sure that we don't exceed that. // This is safe as long as we have fewer than 224 Gradle workers running in parallel // See also: https://github.com/elastic/elasticsearch/issues/44134 - final String workerId = System.getProperty(ESTestCase.TEST_WORKER_SYS_PROPERTY); - final int startAt = workerId == null ? 0 : (int) Math.floorMod(Long.valueOf(workerId), 223); + final String workerIdStr = System.getProperty(ESTestCase.TEST_WORKER_SYS_PROPERTY); + final int startAt; + if (workerIdStr == null) { + startAt = 0; // IDE + } else { + // we adjust the gradle worker id with mod so as to not go over the ephemoral port ranges, but gradle continually + // increases this value, so the mod can eventually become zero, thus we shift on both sides by 1 + final long workerId = Long.valueOf(workerIdStr); + assert workerId >= 1 : "Non positive gradle worker id: " + workerIdStr; + startAt = Math.floorMod(workerId - 1, 223) + 1; + } assert startAt >= 0 : "Unexpected test worker Id, resulting port range would be negative"; return 10300 + (startAt * 100); } diff --git a/test/framework/src/test/java/org/elasticsearch/test/test/ESTestCaseTests.java b/test/framework/src/test/java/org/elasticsearch/test/test/ESTestCaseTests.java index 2bb90ce03b4..af041a70d97 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/test/ESTestCaseTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/test/ESTestCaseTests.java @@ -189,7 +189,6 @@ public class ESTestCaseTests extends ESTestCase { assertThat(ESTestCase.TEST_WORKER_VM_ID, not(equals(ESTestCase.DEFAULT_TEST_WORKER_ID))); } - @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/58279") public void testBasePortGradle() { assumeTrue("requires running tests with Gradle", System.getProperty("tests.gradle") != null); // Gradle worker IDs are 1 based