1313 */
1414package io .trino .gateway .ha .clustermonitor ;
1515
16+ import com .google .common .collect .ImmutableMap ;
1617import com .google .common .util .concurrent .SimpleTimeLimiter ;
1718import io .airlift .log .Logger ;
1819import 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 ();
0 commit comments