Skip to content

Commit 7c2c86d

Browse files
Eliminate mutable field anti-patterns
1 parent 535802e commit 7c2c86d

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

gateway-ha/src/main/java/io/trino/gateway/ha/clustermonitor/ClusterStatsJdbcMonitor.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
package io.trino.gateway.ha.clustermonitor;
1515

16+
import com.google.common.collect.ImmutableMap;
1617
import com.google.common.util.concurrent.SimpleTimeLimiter;
1718
import io.airlift.log.Logger;
1819
import io.airlift.units.Duration;
@@ -39,7 +40,7 @@ public class ClusterStatsJdbcMonitor
3940
{
4041
private static final Logger log = Logger.get(ClusterStatsJdbcMonitor.class);
4142

42-
private final Properties properties; // TODO Avoid using a mutable field
43+
private final ImmutableMap<String, String> properties;
4344
private final Duration queryTimeout;
4445

4546
private static final String STATE_QUERY = "SELECT state, COUNT(*) as count "
@@ -49,15 +50,15 @@ public class ClusterStatsJdbcMonitor
4950

5051
public ClusterStatsJdbcMonitor(BackendStateConfiguration backendStateConfiguration, MonitorConfiguration monitorConfiguration)
5152
{
52-
properties = new Properties();
53-
properties.setProperty("user", backendStateConfiguration.getUsername());
54-
properties.setProperty("password", backendStateConfiguration.getPassword());
55-
properties.setProperty("SSL", String.valueOf(backendStateConfiguration.getSsl()));
53+
ImmutableMap.Builder<String, String> propertiesBuilder = ImmutableMap.builder();
54+
propertiesBuilder.put("user", backendStateConfiguration.getUsername());
55+
propertiesBuilder.put("password", backendStateConfiguration.getPassword());
5656
// explicitPrepare is a valid property for Trino versions >= 431. To avoid compatibility
5757
// issues with versions < 431, this property is left unset when explicitPrepare=true, which is the default
5858
if (!monitorConfiguration.isExplicitPrepare()) {
59-
properties.setProperty("explicitPrepare", "false");
59+
propertiesBuilder.put("explicitPrepare", "false");
6060
}
61+
properties = propertiesBuilder.build();
6162
queryTimeout = monitorConfiguration.getQueryTimeout();
6263
log.info("state check configured");
6364
}
@@ -68,23 +69,32 @@ public ClusterStats monitor(ProxyBackendConfiguration backend)
6869
String url = backend.getProxyTo();
6970
ClusterStats.Builder clusterStats = ClusterStatsMonitor.getClusterStatsBuilder(backend);
7071
String jdbcUrl;
72+
Properties connectionProperties;
7173
try {
7274
URL parsedUrl = new URL(url);
7375
jdbcUrl = String
7476
.format("jdbc:trino://%s:%s/system",
7577
parsedUrl.getHost(),
7678
parsedUrl.getPort() == -1 ? parsedUrl.getDefaultPort() : parsedUrl.getPort());
77-
// automatically set ssl config based on url protocol
78-
properties.setProperty("SSL", String.valueOf(parsedUrl.getProtocol().equals("https")));
79+
// Create connection properties from immutable map
80+
connectionProperties = new Properties();
81+
// Remove any existing SSL property to avoid confusion
82+
for (String key : properties.keySet()) {
83+
if (!key.equalsIgnoreCase("SSL")) {
84+
connectionProperties.setProperty(key, properties.get(key));
85+
}
86+
}
87+
// Set SSL config based on url protocol, always taking precedence
88+
connectionProperties.setProperty("SSL", String.valueOf(parsedUrl.getProtocol().equals("https")));
7989
}
8090
catch (MalformedURLException e) {
8191
throw new IllegalArgumentException("Invalid backend URL: " + url, e);
8292
}
8393

84-
try (Connection conn = DriverManager.getConnection(jdbcUrl, properties);
94+
try (Connection conn = DriverManager.getConnection(jdbcUrl, connectionProperties);
8595
PreparedStatement statement = SimpleTimeLimiter.create(Executors.newSingleThreadExecutor()).callWithTimeout(
8696
() -> conn.prepareStatement(STATE_QUERY), 10, SECONDS)) {
87-
statement.setString(1, (String) properties.get("user"));
97+
statement.setString(1, properties.get("user"));
8898
statement.setQueryTimeout((int) queryTimeout.roundTo(SECONDS));
8999
Map<String, Integer> partialState = new HashMap<>();
90100
ResultSet rs = statement.executeQuery();

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public class BackendStateConfiguration
1717
{
1818
private String username;
1919
private String password = "";
20-
private Boolean ssl = false;
2120
private boolean xForwardedProtoHeader;
2221

2322
public BackendStateConfiguration() {}
@@ -42,16 +41,6 @@ public void setPassword(String password)
4241
this.password = password;
4342
}
4443

45-
public Boolean getSsl()
46-
{
47-
return this.ssl;
48-
}
49-
50-
public void setSsl(Boolean ssl)
51-
{
52-
this.ssl = ssl;
53-
}
54-
5544
public boolean getXForwardedProtoHeader()
5645
{
5746
return xForwardedProtoHeader;

gateway-ha/src/main/java/io/trino/gateway/ha/resource/GatewayWebAppResource.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ public class GatewayWebAppResource
7878
private final ResourceGroupsManager resourceGroupsManager;
7979
private final boolean isRulesEngineEnabled;
8080
private final RulesType ruleType;
81-
// TODO Avoid putting mutable objects in fields
8281
private final UIConfiguration uiConfiguration;
8382
private final RoutingRulesManager routingRulesManager;
8483

@@ -95,7 +94,7 @@ public GatewayWebAppResource(
9594
this.queryHistoryManager = requireNonNull(queryHistoryManager, "queryHistoryManager is null");
9695
this.backendStateManager = requireNonNull(backendStateManager, "backendStateManager is null");
9796
this.resourceGroupsManager = requireNonNull(resourceGroupsManager, "resourceGroupsManager is null");
98-
this.uiConfiguration = configuration.getUiConfiguration();
97+
this.uiConfiguration = requireNonNull(configuration.getUiConfiguration(), "uiConfiguration is null");
9998
this.routingRulesManager = requireNonNull(routingRulesManager, "routingRulesManager is null");
10099
RoutingRulesConfiguration routingRules = configuration.getRoutingRules();
101100
isRulesEngineEnabled = routingRules.isRulesEngineEnabled();

0 commit comments

Comments
 (0)