This is an automated email from the ASF dual-hosted git repository.

absurdfarce pushed a commit to branch 4.x
in repository https://gitbox.apache.org/repos/asf/cassandra-java-driver.git

commit c7719aed14705b735571ecbfbda23d3b8506eb11
Author: Abe Ratnofsky <a...@aber.io>
AuthorDate: Tue Jan 23 16:09:35 2024 -0500

    PR feedback: avoid extra exception wrapping, provide thread naming, improve 
error messages, etc.
---
 .../api/core/config/DefaultDriverOption.java       | 12 +++---
 .../internal/core/ssl/DefaultSslEngineFactory.java |  4 +-
 .../core/ssl/ReloadingKeyManagerFactory.java       | 44 ++++++++++------------
 3 files changed, 28 insertions(+), 32 deletions(-)

diff --git 
a/core/src/main/java/com/datastax/oss/driver/api/core/config/DefaultDriverOption.java
 
b/core/src/main/java/com/datastax/oss/driver/api/core/config/DefaultDriverOption.java
index c10a8237c..afe16e968 100644
--- 
a/core/src/main/java/com/datastax/oss/driver/api/core/config/DefaultDriverOption.java
+++ 
b/core/src/main/java/com/datastax/oss/driver/api/core/config/DefaultDriverOption.java
@@ -255,12 +255,6 @@ public enum DefaultDriverOption implements DriverOption {
    * <p>Value-type: {@link String}
    */
   SSL_KEYSTORE_PASSWORD("advanced.ssl-engine-factory.keystore-password"),
-  /**
-   * The duration between attempts to reload the keystore.
-   *
-   * <p>Value-type: {@link java.time.Duration}
-   */
-  
SSL_KEYSTORE_RELOAD_INTERVAL("advanced.ssl-engine-factory.keystore-reload-interval"),
   /**
    * The location of the truststore file.
    *
@@ -982,6 +976,12 @@ public enum DefaultDriverOption implements DriverOption {
    * <p>Value-type: boolean
    */
   
METRICS_GENERATE_AGGREGABLE_HISTOGRAMS("advanced.metrics.histograms.generate-aggregable"),
+  /**
+   * The duration between attempts to reload the keystore.
+   *
+   * <p>Value-type: {@link java.time.Duration}
+   */
+  
SSL_KEYSTORE_RELOAD_INTERVAL("advanced.ssl-engine-factory.keystore-reload-interval"),
   ;
 
   private final String path;
diff --git 
a/core/src/main/java/com/datastax/oss/driver/internal/core/ssl/DefaultSslEngineFactory.java
 
b/core/src/main/java/com/datastax/oss/driver/internal/core/ssl/DefaultSslEngineFactory.java
index 55a6e9c7d..adf23f8e8 100644
--- 
a/core/src/main/java/com/datastax/oss/driver/internal/core/ssl/DefaultSslEngineFactory.java
+++ 
b/core/src/main/java/com/datastax/oss/driver/internal/core/ssl/DefaultSslEngineFactory.java
@@ -150,8 +150,8 @@ public class DefaultSslEngineFactory implements 
SslEngineFactory {
     }
   }
 
-  private ReloadingKeyManagerFactory buildReloadingKeyManagerFactory(
-      DriverExecutionProfile config) {
+  private ReloadingKeyManagerFactory 
buildReloadingKeyManagerFactory(DriverExecutionProfile config)
+      throws Exception {
     Path keystorePath = 
Paths.get(config.getString(DefaultDriverOption.SSL_KEYSTORE_PATH));
     String password =
         config.isDefined(DefaultDriverOption.SSL_KEYSTORE_PASSWORD)
diff --git 
a/core/src/main/java/com/datastax/oss/driver/internal/core/ssl/ReloadingKeyManagerFactory.java
 
b/core/src/main/java/com/datastax/oss/driver/internal/core/ssl/ReloadingKeyManagerFactory.java
index 9aaee7011..540ddfd79 100644
--- 
a/core/src/main/java/com/datastax/oss/driver/internal/core/ssl/ReloadingKeyManagerFactory.java
+++ 
b/core/src/main/java/com/datastax/oss/driver/internal/core/ssl/ReloadingKeyManagerFactory.java
@@ -73,26 +73,17 @@ public class ReloadingKeyManagerFactory extends 
KeyManagerFactory implements Aut
    * @return
    */
   public static ReloadingKeyManagerFactory create(
-      Path keystorePath, String keystorePassword, Duration reloadInterval) {
-    KeyManagerFactory kmf;
-    try {
-      kmf = 
KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
-    } catch (NoSuchAlgorithmException e) {
-      throw new RuntimeException(e);
-    }
+      Path keystorePath, String keystorePassword, Duration reloadInterval)
+      throws UnrecoverableKeyException, KeyStoreException, 
NoSuchAlgorithmException,
+          CertificateException, IOException {
+    KeyManagerFactory kmf = 
KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
 
     KeyStore ks;
     try (InputStream ksf = Files.newInputStream(keystorePath)) {
       ks = KeyStore.getInstance(KEYSTORE_TYPE);
       ks.load(ksf, keystorePassword.toCharArray());
-    } catch (IOException | CertificateException | KeyStoreException | 
NoSuchAlgorithmException e) {
-      throw new RuntimeException(e);
-    }
-    try {
-      kmf.init(ks, keystorePassword.toCharArray());
-    } catch (KeyStoreException | NoSuchAlgorithmException | 
UnrecoverableKeyException e) {
-      throw new RuntimeException(e);
     }
+    kmf.init(ks, keystorePassword.toCharArray());
 
     ReloadingKeyManagerFactory reloadingKeyManagerFactory = new 
ReloadingKeyManagerFactory(kmf);
     reloadingKeyManagerFactory.start(keystorePath, keystorePassword, 
reloadInterval);
@@ -115,24 +106,26 @@ public class ReloadingKeyManagerFactory extends 
KeyManagerFactory implements Aut
   private void start(Path keystorePath, String keystorePassword, Duration 
reloadInterval) {
     this.keystorePath = keystorePath;
     this.keystorePassword = keystorePassword;
-    this.executor =
-        Executors.newScheduledThreadPool(
-            1,
-            runnable -> {
-              Thread t = Executors.defaultThreadFactory().newThread(runnable);
-              t.setDaemon(true);
-              return t;
-            });
 
     // Ensure that reload is called once synchronously, to make sure the file 
exists etc.
     reload();
 
-    if (!reloadInterval.isZero())
+    if (!reloadInterval.isZero()) {
+      this.executor =
+          Executors.newScheduledThreadPool(
+              1,
+              runnable -> {
+                Thread t = 
Executors.defaultThreadFactory().newThread(runnable);
+                t.setName(String.format("%s-%%d", 
this.getClass().getSimpleName()));
+                t.setDaemon(true);
+                return t;
+              });
       this.executor.scheduleWithFixedDelay(
           this::reload,
           reloadInterval.toMillis(),
           reloadInterval.toMillis(),
           TimeUnit.MILLISECONDS);
+    }
   }
 
   @VisibleForTesting
@@ -140,7 +133,10 @@ public class ReloadingKeyManagerFactory extends 
KeyManagerFactory implements Aut
     try {
       reload0();
     } catch (Exception e) {
-      logger.warn("Failed to reload", e);
+      String msg =
+          "Failed to reload KeyStore. If this continues to happen, your client 
may use stale identity"
+              + "certificates and fail to re-establish connections to 
Cassandra hosts.";
+      logger.warn(msg, e);
     }
   }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@cassandra.apache.org
For additional commands, e-mail: commits-h...@cassandra.apache.org

Reply via email to