diff --git a/gateway-ha/src/test/java/io/trino/gateway/ha/router/TestRoutingGroupSelector.java b/gateway-ha/src/test/java/io/trino/gateway/ha/router/TestRoutingGroupSelector.java index 991274def..eeb170c7d 100644 --- a/gateway-ha/src/test/java/io/trino/gateway/ha/router/TestRoutingGroupSelector.java +++ b/gateway-ha/src/test/java/io/trino/gateway/ha/router/TestRoutingGroupSelector.java @@ -19,6 +19,8 @@ import org.junit.jupiter.api.TestInstance.Lifecycle; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.File; import java.io.FileWriter; @@ -34,6 +36,7 @@ @TestInstance(Lifecycle.PER_CLASS) public class TestRoutingGroupSelector { + private static final Logger log = LoggerFactory.getLogger(TestRoutingGroupSelector.class); public static final String TRINO_SOURCE_HEADER = "X-Trino-Source"; public static final String TRINO_CLIENT_TAGS_HEADER = "X-Trino-Client-Tags"; @@ -107,7 +110,6 @@ void testByRoutingRulesEngineNoMatch(String rulesConfigPath) assertNull(routingGroupSelector.findRoutingGroup(mockRequest)); } - //Todo: The functionality of reading the file before every request needs to be smarter @Test public void testByRoutingRulesEngineFileChange() throws Exception @@ -123,6 +125,17 @@ public void testByRoutingRulesEngineFileChange() + "actions:\n" + " - \"result.put(\\\"routingGroup\\\", \\\"etl\\\")\""); fw.close(); + boolean successfullySetLastModifiedTime = file.setLastModified(0); + long etlLastModified = file.lastModified(); + log.info("etl rule file created at: " + etlLastModified); + if (!successfullySetLastModifiedTime) { + log.warn("File update time could not be set, sleep 10s"); + Thread.sleep(10_000); + // RuleReloadingRoutingGroupSelector compares file timestamps to see if the rules file + // has been updated, and java.nio.file.attribute.FileTime offers a maximum precision of 1 ms. + // If the first half of this test runs in <1ms, and the lastModifiedTime cannot be set, + // then the gateway may not recognize that the file has changed. + } RoutingGroupSelector routingGroupSelector = RoutingGroupSelector.byRoutingRulesEngine(file.getPath()); @@ -133,11 +146,6 @@ public void testByRoutingRulesEngineFileChange() assertEquals("etl", routingGroupSelector.findRoutingGroup(mockRequest)); - Thread.sleep(1); - // java.nio.file.attribute.FileTime offers a maximum precision of 1 ms. If the first - // half of this test runs in <1ms, the gateway may not recognize that the file - // has changed. - fw = new FileWriter(file, UTF_8); fw.write( "---\n" @@ -147,6 +155,8 @@ public void testByRoutingRulesEngineFileChange() + "actions:\n" + " - \"result.put(\\\"routingGroup\\\", \\\"etl2\\\")\""); // change from etl to etl2 fw.close(); + long etl2LastModified = file.lastModified(); + log.info("etl2 rule file created at: " + etl2LastModified); when(mockRequest.getHeader(TRINO_SOURCE_HEADER)).thenReturn("airflow"); assertEquals("etl2",