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,16 @@ 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 ());
56+         propertiesBuilder . put ("SSL" , String .valueOf (backendStateConfiguration .getSsl ()));
5657        // explicitPrepare is a valid property for Trino versions >= 431. To avoid compatibility 
5758        // issues with versions < 431, this property is left unset when explicitPrepare=true, which is the default 
5859        if  (!monitorConfiguration .isExplicitPrepare ()) {
59-             properties . setProperty ("explicitPrepare" , "false" );
60+             propertiesBuilder . put ("explicitPrepare" , "false" );
6061        }
62+         properties  = propertiesBuilder .build ();
6163        queryTimeout  = monitorConfiguration .getQueryTimeout ();
6264        log .info ("state check configured" );
6365    }
@@ -68,23 +70,32 @@ public ClusterStats monitor(ProxyBackendConfiguration backend)
6870        String  url  = backend .getProxyTo ();
6971        ClusterStats .Builder  clusterStats  = ClusterStatsMonitor .getClusterStatsBuilder (backend );
7072        String  jdbcUrl ;
73+         Properties  connectionProperties ;
7174        try  {
7275            URL  parsedUrl  = new  URL (url );
7376            jdbcUrl  = String 
7477                    .format ("jdbc:trino://%s:%s/system" ,
7578                            parsedUrl .getHost (),
7679                            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" )));
80+             // Create connection properties from immutable map 
81+             connectionProperties  = new  Properties ();
82+             // Remove any existing SSL property to avoid confusion 
83+             for  (String  key  : properties .keySet ()) {
84+                 if  (!key .equalsIgnoreCase ("SSL" )) {
85+                     connectionProperties .setProperty (key , properties .get (key ));
86+                 }
87+             }
88+             // Set SSL config based on url protocol, always taking precedence 
89+             connectionProperties .setProperty ("SSL" , String .valueOf (parsedUrl .getProtocol ().equals ("https" )));
7990        }
8091        catch  (MalformedURLException  e ) {
8192            throw  new  IllegalArgumentException ("Invalid backend URL: "  + url , e );
8293        }
8394
84-         try  (Connection  conn  = DriverManager .getConnection (jdbcUrl , properties );
95+         try  (Connection  conn  = DriverManager .getConnection (jdbcUrl , connectionProperties );
8596                PreparedStatement  statement  = SimpleTimeLimiter .create (Executors .newSingleThreadExecutor ()).callWithTimeout (
8697                        () -> conn .prepareStatement (STATE_QUERY ), 10 , SECONDS )) {
87-             statement .setString (1 , ( String )  properties .get ("user" ));
98+             statement .setString (1 , properties .get ("user" ));
8899            statement .setQueryTimeout ((int ) queryTimeout .roundTo (SECONDS ));
89100            Map <String , Integer > partialState  = new  HashMap <>();
90101            ResultSet  rs  = statement .executeQuery ();
0 commit comments