Skip to content

Commit c2ba1ea

Browse files
willmostlywendigo
authored andcommitted
Update to latest airbase
Adds proper OpenTracing support to gateway Co-authored-by: Will Morrison <[email protected]>
1 parent 1c8ce25 commit c2ba1ea

File tree

12 files changed

+67
-63
lines changed

12 files changed

+67
-63
lines changed

gateway-ha/pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,6 @@
123123
<artifactId>concurrent</artifactId>
124124
</dependency>
125125

126-
<dependency>
127-
<groupId>io.airlift</groupId>
128-
<artifactId>event</artifactId>
129-
</dependency>
130-
131126
<dependency>
132127
<groupId>io.airlift</groupId>
133128
<artifactId>http-client</artifactId>
@@ -183,6 +178,11 @@
183178
<artifactId>stats</artifactId>
184179
</dependency>
185180

181+
<dependency>
182+
<groupId>io.airlift</groupId>
183+
<artifactId>tracing</artifactId>
184+
</dependency>
185+
186186
<dependency>
187187
<groupId>io.airlift</groupId>
188188
<artifactId>units</artifactId>

gateway-ha/src/main/java/io/trino/gateway/ha/HaGatewayLauncher.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.google.inject.Module;
2020
import io.airlift.bootstrap.ApplicationConfigurationException;
2121
import io.airlift.bootstrap.Bootstrap;
22-
import io.airlift.event.client.EventModule;
2322
import io.airlift.http.server.HttpServerModule;
2423
import io.airlift.jaxrs.JaxrsModule;
2524
import io.airlift.jmx.JmxHttpModule;
@@ -29,6 +28,7 @@
2928
import io.airlift.log.Logger;
3029
import io.airlift.node.NodeModule;
3130
import io.airlift.openmetrics.JmxOpenMetricsModule;
31+
import io.airlift.tracing.TracingModule;
3232
import io.airlift.units.Duration;
3333
import io.trino.gateway.baseapp.BaseApp;
3434
import io.trino.gateway.ha.config.HaGatewayConfiguration;
@@ -42,6 +42,7 @@
4242
import static io.trino.gateway.baseapp.BaseApp.addModules;
4343
import static io.trino.gateway.ha.util.ConfigurationUtils.replaceEnvironmentVariables;
4444
import static java.lang.String.format;
45+
import static java.util.Objects.requireNonNullElse;
4546

4647
public class HaGatewayLauncher
4748
{
@@ -51,10 +52,10 @@ private void start(List<Module> additionalModules, HaGatewayConfiguration config
5152
{
5253
long startTime = System.nanoTime();
5354

55+
String version = requireNonNullElse(HaGatewayLauncher.class.getPackage().getImplementationVersion(), "unknown");
5456
ImmutableList.Builder<Module> modules = ImmutableList.builder();
5557
modules.add(
5658
new NodeModule(),
57-
new EventModule(),
5859
new HttpServerModule(),
5960
new JmxModule(),
6061
new JmxHttpModule(),
@@ -63,6 +64,7 @@ private void start(List<Module> additionalModules, HaGatewayConfiguration config
6364
new MBeanModule(),
6465
new JsonModule(),
6566
new JaxrsModule(),
67+
new TracingModule("trino-gateway", version),
6668
new BaseApp(configuration));
6769
modules.addAll(additionalModules);
6870

gateway-ha/src/main/java/io/trino/gateway/ha/config/LdapConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
1818
import io.airlift.log.Logger;
1919

20-
import java.io.File;
2120
import java.io.IOException;
21+
import java.nio.file.Path;
2222

2323
public class LdapConfiguration
2424
{
@@ -80,7 +80,7 @@ public static LdapConfiguration load(String path)
8080
{
8181
LdapConfiguration configuration = null;
8282
try {
83-
configuration = OBJECT_MAPPER.readValue(new File(path), LdapConfiguration.class);
83+
configuration = OBJECT_MAPPER.readValue(Path.of(path).toFile(), LdapConfiguration.class);
8484
}
8585
catch (IOException e) {
8686
log.error(e, "Error loading configuration file");

gateway-ha/src/main/java/io/trino/gateway/ha/router/RuleReloadingRoutingGroupSelector.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.jeasy.rules.mvel.MVELRuleFactory;
2424
import org.jeasy.rules.support.reader.YamlRuleDefinitionReader;
2525

26-
import java.io.FileReader;
2726
import java.nio.file.Files;
2827
import java.nio.file.Path;
2928
import java.nio.file.attribute.BasicFileAttributes;
@@ -40,7 +39,7 @@ public class RuleReloadingRoutingGroupSelector
4039
private static final Logger log = Logger.get(RuleReloadingRoutingGroupSelector.class);
4140
private final RulesEngine rulesEngine = new DefaultRulesEngine();
4241
private final MVELRuleFactory ruleFactory = new MVELRuleFactory(new YamlRuleDefinitionReader());
43-
private final String rulesConfigPath;
42+
private final Path rulesConfigPath;
4443
private volatile Rules rules = new Rules();
4544
private volatile long lastUpdatedTime;
4645
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(true);
@@ -49,13 +48,13 @@ public class RuleReloadingRoutingGroupSelector
4948

5049
RuleReloadingRoutingGroupSelector(String rulesConfigPath, RequestAnalyzerConfig requestAnalyzerConfig)
5150
{
52-
this.rulesConfigPath = rulesConfigPath;
51+
this.rulesConfigPath = Path.of(rulesConfigPath);
5352
this.requestAnalyzerConfig = requestAnalyzerConfig;
5453
trinoRequestUserProvider = new TrinoRequestUser.TrinoRequestUserProvider(requestAnalyzerConfig);
5554
try {
5655
rules = ruleFactory.createRules(
57-
new FileReader(rulesConfigPath, UTF_8));
58-
BasicFileAttributes attr = Files.readAttributes(Path.of(rulesConfigPath),
56+
Files.newBufferedReader(this.rulesConfigPath, UTF_8));
57+
BasicFileAttributes attr = Files.readAttributes(this.rulesConfigPath,
5958
BasicFileAttributes.class);
6059
lastUpdatedTime = attr.lastModifiedTime().toMillis();
6160
}
@@ -70,7 +69,7 @@ public class RuleReloadingRoutingGroupSelector
7069
public String findRoutingGroup(HttpServletRequest request)
7170
{
7271
try {
73-
BasicFileAttributes attr = Files.readAttributes(Path.of(rulesConfigPath),
72+
BasicFileAttributes attr = Files.readAttributes(rulesConfigPath,
7473
BasicFileAttributes.class);
7574
log.debug("File modified time: %s. lastUpdatedTime: %s", attr.lastModifiedTime(), lastUpdatedTime);
7675
if (attr.lastModifiedTime().toMillis() > lastUpdatedTime) {
@@ -82,7 +81,7 @@ public String findRoutingGroup(HttpServletRequest request)
8281
// thread finds the condition true and acquires the lock before this one
8382
log.info("Updating rules to file modified at %s", attr.lastModifiedTime());
8483
rules = ruleFactory.createRules(
85-
new FileReader(rulesConfigPath, UTF_8));
84+
Files.newBufferedReader(rulesConfigPath, UTF_8));
8685
lastUpdatedTime = attr.lastModifiedTime().toMillis();
8786
}
8887
}

gateway-ha/src/main/java/io/trino/gateway/ha/security/LbKeyProvider.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
import org.bouncycastle.util.io.pem.PemObject;
1818
import org.bouncycastle.util.io.pem.PemReader;
1919

20-
import java.io.FileReader;
20+
import java.io.BufferedReader;
21+
import java.nio.file.Files;
22+
import java.nio.file.Path;
2123
import java.security.KeyFactory;
2224
import java.security.NoSuchAlgorithmException;
2325
import java.security.PrivateKey;
@@ -49,7 +51,7 @@ public LbKeyProvider(SelfSignKeyPairConfiguration keypairConfig)
4951

5052
try {
5153
String publicKeyRsa = keypairConfig.publicKeyRsa();
52-
try (FileReader keyReader = new FileReader(publicKeyRsa, UTF_8);
54+
try (BufferedReader keyReader = Files.newBufferedReader(Path.of(publicKeyRsa), UTF_8);
5355
PemReader pemReader = new PemReader(keyReader)) {
5456
PemObject pemObject = pemReader.readPemObject();
5557
byte[] content = pemObject.getContent();
@@ -58,7 +60,7 @@ public LbKeyProvider(SelfSignKeyPairConfiguration keypairConfig)
5860
}
5961

6062
String privateKeyRsa = keypairConfig.privateKeyRsa();
61-
try (FileReader keyReader = new FileReader(privateKeyRsa, UTF_8);
63+
try (BufferedReader keyReader = Files.newBufferedReader(Path.of(privateKeyRsa), UTF_8);
6264
PemReader pemReader = new PemReader(keyReader)) {
6365
PemObject pemObject = pemReader.readPemObject();
6466
byte[] content = pemObject.getContent();

gateway-ha/src/main/java/io/trino/gateway/proxyserver/ProxyRequestHandler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ private void performRequest(
179179
cookieBuilder.addAll(getOAuth2GatewayCookie(remoteUri, servletRequest));
180180

181181
Request request = requestBuilder
182-
.setPreserveAuthorizationOnRedirect(true)
183182
.setFollowRedirects(false)
184183
.build();
185184

gateway-ha/src/main/java/io/trino/gateway/proxyserver/ProxyResponseHandler.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import java.io.IOException;
2424

25-
import static com.google.common.io.ByteStreams.toByteArray;
2625
import static java.util.Objects.requireNonNull;
2726

2827
public class ProxyResponseHandler
@@ -38,7 +37,7 @@ public ProxyResponse handleException(Request request, Exception exception)
3837
public ProxyResponse handle(Request request, Response response)
3938
{
4039
try {
41-
return new ProxyResponse(response.getStatusCode(), response.getHeaders(), toByteArray(response.getInputStream()));
40+
return new ProxyResponse(response.getStatusCode(), response.getHeaders(), response.getInputStream().readAllBytes());
4241
}
4342
catch (IOException e) {
4443
throw new ProxyException("Failed reading response from remote Trino server", e);

gateway-ha/src/test/java/io/trino/gateway/ha/HaGatewayTestUtils.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@
2828
import org.jdbi.v3.core.Handle;
2929
import org.jdbi.v3.core.Jdbi;
3030

31+
import java.io.BufferedWriter;
3132
import java.io.File;
32-
import java.io.FileWriter;
3333
import java.io.IOException;
3434
import java.io.InputStream;
3535
import java.net.URL;
36-
import java.nio.file.Paths;
36+
import java.nio.file.Files;
37+
import java.nio.file.Path;
3738
import java.util.Random;
3839
import java.util.Scanner;
3940
import java.util.concurrent.TimeUnit;
@@ -66,21 +67,20 @@ public static void seedRequiredData(TestConfig testConfig)
6667
}
6768

6869
public static void prepareMockBackend(
69-
MockWebServer backend, int customBackendPort, String expectedResonse)
70+
MockWebServer backend, int customBackendPort, String expectedResponse)
7071
throws IOException
7172
{
7273
backend.start(customBackendPort);
7374
backend.enqueue(new MockResponse()
74-
.setBody(expectedResonse)
75+
.setBody(expectedResponse)
7576
.addHeader(CONTENT_ENCODING, PLAIN_TEXT_UTF_8)
7677
.setResponseCode(200));
7778
}
7879

7980
public static TestConfig buildGatewayConfigAndSeedDb(int routerPort, String configFile)
8081
throws Exception
8182
{
82-
File baseDir = new File(System.getProperty("java.io.tmpdir"));
83-
File tempH2DbDir = new File(baseDir, "h2db-" + RANDOM.nextInt() + System.currentTimeMillis());
83+
File tempH2DbDir = Path.of(System.getProperty("java.io.tmpdir"), "h2db-" + RANDOM.nextInt() + System.currentTimeMillis()).toFile();
8484
tempH2DbDir.deleteOnExit();
8585

8686
URL resource = HaGatewayTestUtils.class.getClassLoader().getResource("auth/localhost.jks");
@@ -91,14 +91,15 @@ public static TestConfig buildGatewayConfigAndSeedDb(int routerPort, String conf
9191
.replace(
9292
"APPLICATION_CONNECTOR_PORT", String.valueOf(30000 + (int) (Math.random() * 1000)))
9393
.replace("ADMIN_CONNECTOR_PORT", String.valueOf(31000 + (int) (Math.random() * 1000)))
94-
.replace("LOCALHOST_JKS", Paths.get(resource.toURI()).toFile().getAbsolutePath())
95-
.replace("RESOURCES_DIR", Paths.get("src", "test", "resources").toFile().getAbsolutePath());
94+
.replace("LOCALHOST_JKS", Path.of(resource.toURI()).toString())
95+
.replace("RESOURCES_DIR", Path.of("src", "test", "resources").toAbsolutePath().toString());
9696

9797
File target = File.createTempFile("config-" + System.currentTimeMillis(), "config.yaml");
9898

99-
FileWriter fw = new FileWriter(target, UTF_8);
100-
fw.append(configStr);
101-
fw.flush();
99+
try (BufferedWriter writer = Files.newBufferedWriter(target.toPath(), UTF_8)) {
100+
writer.append(configStr);
101+
}
102+
102103
log.info("Test Gateway Config \n[%s]", configStr);
103104
TestConfig testConfig = new TestConfig(target.getAbsolutePath(), tempH2DbDir.getAbsolutePath());
104105
seedRequiredData(testConfig);

gateway-ha/src/test/java/io/trino/gateway/ha/TestingJdbcConnectionManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
import org.jdbi.v3.core.Jdbi;
1919

2020
import java.io.File;
21+
import java.nio.file.Path;
2122

2223
public final class TestingJdbcConnectionManager
2324
{
2425
private TestingJdbcConnectionManager() {}
2526

2627
public static JdbcConnectionManager createTestingJdbcConnectionManager()
2728
{
28-
File baseDir = new File(System.getProperty("java.io.tmpdir"));
29-
File tempH2DbDir = new File(baseDir, "h2db-" + System.currentTimeMillis());
29+
File tempH2DbDir = Path.of(System.getProperty("java.io.tmpdir"), "h2db-" + System.currentTimeMillis()).toFile();
3030
tempH2DbDir.deleteOnExit();
3131
String jdbcUrl = "jdbc:h2:" + tempH2DbDir.getAbsolutePath();
3232
HaGatewayTestUtils.seedRequiredData(new HaGatewayTestUtils.TestConfig("", tempH2DbDir.getAbsolutePath()));

gateway-ha/src/test/java/io/trino/gateway/ha/router/TestRoutingGroupSelector.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@
2727
import org.junit.jupiter.params.provider.MethodSource;
2828

2929
import java.io.BufferedReader;
30+
import java.io.BufferedWriter;
3031
import java.io.File;
31-
import java.io.FileReader;
32-
import java.io.FileWriter;
3332
import java.io.IOException;
3433
import java.io.Reader;
3534
import java.io.StringReader;
35+
import java.nio.file.Files;
36+
import java.nio.file.Path;
3637
import java.util.Arrays;
3738
import java.util.Base64;
3839
import java.util.Collections;
@@ -251,16 +252,16 @@ void testByRoutingRulesEngineFileChange()
251252
{
252253
File file = File.createTempFile("routing_rules", ".yml");
253254

254-
FileWriter fw = new FileWriter(file, UTF_8);
255-
fw.write(
256-
"---\n"
257-
+ "name: \"airflow1\"\n"
258-
+ "description: \"original rule\"\n"
259-
+ "condition: \"request.getHeader(\\\"X-Trino-Source\\\") == \\\"airflow\\\"\"\n"
260-
+ "actions:\n"
261-
+ " - \"result.put(\\\"routingGroup\\\", \\\"etl\\\")\"");
262-
fw.close();
263-
long lastModifed = file.lastModified();
255+
try (BufferedWriter writer = Files.newBufferedWriter(file.toPath(), UTF_8)) {
256+
writer.write(
257+
"---\n"
258+
+ "name: \"airflow1\"\n"
259+
+ "description: \"original rule\"\n"
260+
+ "condition: \"request.getHeader(\\\"X-Trino-Source\\\") == \\\"airflow\\\"\"\n"
261+
+ "actions:\n"
262+
+ " - \"result.put(\\\"routingGroup\\\", \\\"etl\\\")\"");
263+
}
264+
long lastModified = file.lastModified();
264265

265266
RoutingGroupSelector routingGroupSelector =
266267
RoutingGroupSelector.byRoutingRulesEngine(file.getPath(), requestAnalyzerConfig);
@@ -271,16 +272,16 @@ void testByRoutingRulesEngineFileChange()
271272
assertThat(routingGroupSelector.findRoutingGroup(mockRequest))
272273
.isEqualTo("etl");
273274

274-
fw = new FileWriter(file, UTF_8);
275-
fw.write(
276-
"---\n"
277-
+ "name: \"airflow2\"\n"
278-
+ "description: \"updated rule\"\n"
279-
+ "condition: \"request.getHeader(\\\"X-Trino-Source\\\") == \\\"airflow\\\"\"\n"
280-
+ "actions:\n"
281-
+ " - \"result.put(\\\"routingGroup\\\", \\\"etl2\\\")\""); // change from etl to etl2
282-
fw.close();
283-
assertThat(file.setLastModified(lastModifed + 1000)).isTrue();
275+
try (BufferedWriter writer = Files.newBufferedWriter(file.toPath(), UTF_8)) {
276+
writer.write(
277+
"---\n"
278+
+ "name: \"airflow2\"\n"
279+
+ "description: \"updated rule\"\n"
280+
+ "condition: \"request.getHeader(\\\"X-Trino-Source\\\") == \\\"airflow\\\"\"\n"
281+
+ "actions:\n"
282+
+ " - \"result.put(\\\"routingGroup\\\", \\\"etl2\\\")\""); // change from etl to etl2
283+
}
284+
assertThat(file.setLastModified(lastModified + 1000)).isTrue();
284285

285286
when(mockRequest.getHeader(TRINO_SOURCE_HEADER)).thenReturn("airflow");
286287
assertThat(routingGroupSelector.findRoutingGroup(mockRequest))
@@ -450,7 +451,7 @@ private HttpServletRequest prepareMockRequest()
450451
void testLongQuery()
451452
throws IOException
452453
{
453-
BufferedReader bufferedReader = new BufferedReader(new FileReader("src/test/resources/wide_select.sql", UTF_8));
454+
BufferedReader bufferedReader = Files.newBufferedReader(Path.of("src/test/resources/wide_select.sql"), UTF_8);
454455
HttpServletRequest mockRequest = prepareMockRequest();
455456
when(mockRequest.getReader()).thenReturn(bufferedReader);
456457
TrinoQueryProperties trinoQueryProperties = new TrinoQueryProperties(

0 commit comments

Comments
 (0)