Skip to content

Commit 291a1c4

Browse files
Eliminate mutable field anti-patterns
1 parent 487137c commit 291a1c4

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,16 @@
3232
import java.util.concurrent.Executors;
3333
import java.util.concurrent.TimeoutException;
3434

35+
import com.google.common.collect.ImmutableMap;
36+
3537
import static java.util.concurrent.TimeUnit.SECONDS;
3638

3739
public class ClusterStatsJdbcMonitor
3840
implements ClusterStatsMonitor
3941
{
4042
private static final Logger log = Logger.get(ClusterStatsJdbcMonitor.class);
4143

42-
private final Properties properties; // TODO Avoid using a mutable field
44+
private final ImmutableMap<String, String> properties;
4345
private final Duration queryTimeout;
4446

4547
private static final String STATE_QUERY = "SELECT state, COUNT(*) as count "
@@ -49,15 +51,16 @@ public class ClusterStatsJdbcMonitor
4951

5052
public ClusterStatsJdbcMonitor(BackendStateConfiguration backendStateConfiguration, MonitorConfiguration monitorConfiguration)
5153
{
52-
properties = new Properties();
53-
properties.setProperty("user", backendStateConfiguration.getUsername());
54-
properties.setProperty("password", backendStateConfiguration.getPassword());
55-
properties.setProperty("SSL", String.valueOf(backendStateConfiguration.getSsl()));
54+
ImmutableMap.Builder<String, String> propertiesBuilder = ImmutableMap.builder();
55+
propertiesBuilder.put("user", backendStateConfiguration.getUsername());
56+
propertiesBuilder.put("password", backendStateConfiguration.getPassword());
57+
propertiesBuilder.put("SSL", String.valueOf(backendStateConfiguration.getSsl()));
5658
// explicitPrepare is a valid property for Trino versions >= 431. To avoid compatibility
5759
// issues with versions < 431, this property is left unset when explicitPrepare=true, which is the default
5860
if (!monitorConfiguration.isExplicitPrepare()) {
59-
properties.setProperty("explicitPrepare", "false");
61+
propertiesBuilder.put("explicitPrepare", "false");
6062
}
63+
properties = propertiesBuilder.build();
6164
queryTimeout = monitorConfiguration.getQueryTimeout();
6265
log.info("state check configured");
6366
}
@@ -68,23 +71,27 @@ public ClusterStats monitor(ProxyBackendConfiguration backend)
6871
String url = backend.getProxyTo();
6972
ClusterStats.Builder clusterStats = ClusterStatsMonitor.getClusterStatsBuilder(backend);
7073
String jdbcUrl;
74+
Properties connectionProperties;
7175
try {
7276
URL parsedUrl = new URL(url);
7377
jdbcUrl = String
7478
.format("jdbc:trino://%s:%s/system",
7579
parsedUrl.getHost(),
7680
parsedUrl.getPort() == -1 ? parsedUrl.getDefaultPort() : parsedUrl.getPort());
81+
// Create connection properties from immutable map
82+
connectionProperties = new Properties();
83+
connectionProperties.putAll(properties);
7784
// automatically set ssl config based on url protocol
78-
properties.setProperty("SSL", String.valueOf(parsedUrl.getProtocol().equals("https")));
85+
connectionProperties.setProperty("SSL", String.valueOf(parsedUrl.getProtocol().equals("https")));
7986
}
8087
catch (MalformedURLException e) {
8188
throw new IllegalArgumentException("Invalid backend URL: " + url, e);
8289
}
8390

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

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import io.trino.gateway.ha.config.ProxyBackendConfiguration;
2222
import io.trino.gateway.ha.config.RoutingRulesConfiguration;
2323
import io.trino.gateway.ha.config.RulesType;
24-
import io.trino.gateway.ha.config.UIConfiguration;
24+
import io.trino.gateway.ha.config.ImmutableUIConfiguration;
2525
import io.trino.gateway.ha.domain.Result;
2626
import io.trino.gateway.ha.domain.RoutingRule;
2727
import io.trino.gateway.ha.domain.TableData;
@@ -78,8 +78,7 @@ 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
82-
private final UIConfiguration uiConfiguration;
81+
private final ImmutableUIConfiguration immutableUiConfiguration;
8382
private final RoutingRulesManager routingRulesManager;
8483

8584
@Inject
@@ -95,7 +94,8 @@ 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+
// Create immutable copy of UI configuration to ensure thread safety
98+
this.immutableUiConfiguration = ImmutableUIConfiguration.copyOf(configuration.getUiConfiguration());
9999
this.routingRulesManager = requireNonNull(routingRulesManager, "routingRulesManager is null");
100100
RoutingRulesConfiguration routingRules = configuration.getRoutingRules();
101101
isRulesEngineEnabled = routingRules.isRulesEngineEnabled();
@@ -487,6 +487,6 @@ public Response updateRoutingRules(RoutingRule routingRule)
487487
@Path("/getUIConfiguration")
488488
public Response getUIConfiguration()
489489
{
490-
return Response.ok(Result.ok(uiConfiguration)).build();
490+
return Response.ok(Result.ok(immutableUiConfiguration)).build();
491491
}
492492
}

0 commit comments

Comments
 (0)