3232import java .util .concurrent .Executors ;
3333import java .util .concurrent .TimeoutException ;
3434
35+ import com .google .common .collect .ImmutableMap ;
36+
3537import static java .util .concurrent .TimeUnit .SECONDS ;
3638
3739public 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 ();
0 commit comments