Skip to content

Commit c7719ae

Browse files
aratnoabsurdfarce
authored andcommitted
PR feedback: avoid extra exception wrapping, provide thread naming, improve error messages, etc.
1 parent 8e73232 commit c7719ae

File tree

3 files changed

+28
-32
lines changed

3 files changed

+28
-32
lines changed

core/src/main/java/com/datastax/oss/driver/api/core/config/DefaultDriverOption.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,6 @@ public enum DefaultDriverOption implements DriverOption {
255255
* <p>Value-type: {@link String}
256256
*/
257257
SSL_KEYSTORE_PASSWORD("advanced.ssl-engine-factory.keystore-password"),
258-
/**
259-
* The duration between attempts to reload the keystore.
260-
*
261-
* <p>Value-type: {@link java.time.Duration}
262-
*/
263-
SSL_KEYSTORE_RELOAD_INTERVAL("advanced.ssl-engine-factory.keystore-reload-interval"),
264258
/**
265259
* The location of the truststore file.
266260
*
@@ -982,6 +976,12 @@ public enum DefaultDriverOption implements DriverOption {
982976
* <p>Value-type: boolean
983977
*/
984978
METRICS_GENERATE_AGGREGABLE_HISTOGRAMS("advanced.metrics.histograms.generate-aggregable"),
979+
/**
980+
* The duration between attempts to reload the keystore.
981+
*
982+
* <p>Value-type: {@link java.time.Duration}
983+
*/
984+
SSL_KEYSTORE_RELOAD_INTERVAL("advanced.ssl-engine-factory.keystore-reload-interval"),
985985
;
986986

987987
private final String path;

core/src/main/java/com/datastax/oss/driver/internal/core/ssl/DefaultSslEngineFactory.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ protected SSLContext buildContext(DriverExecutionProfile config) throws Exceptio
150150
}
151151
}
152152

153-
private ReloadingKeyManagerFactory buildReloadingKeyManagerFactory(
154-
DriverExecutionProfile config) {
153+
private ReloadingKeyManagerFactory buildReloadingKeyManagerFactory(DriverExecutionProfile config)
154+
throws Exception {
155155
Path keystorePath = Paths.get(config.getString(DefaultDriverOption.SSL_KEYSTORE_PATH));
156156
String password =
157157
config.isDefined(DefaultDriverOption.SSL_KEYSTORE_PASSWORD)

core/src/main/java/com/datastax/oss/driver/internal/core/ssl/ReloadingKeyManagerFactory.java

+20-24
Original file line numberDiff line numberDiff line change
@@ -73,26 +73,17 @@ public class ReloadingKeyManagerFactory extends KeyManagerFactory implements Aut
7373
* @return
7474
*/
7575
public static ReloadingKeyManagerFactory create(
76-
Path keystorePath, String keystorePassword, Duration reloadInterval) {
77-
KeyManagerFactory kmf;
78-
try {
79-
kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
80-
} catch (NoSuchAlgorithmException e) {
81-
throw new RuntimeException(e);
82-
}
76+
Path keystorePath, String keystorePassword, Duration reloadInterval)
77+
throws UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException,
78+
CertificateException, IOException {
79+
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
8380

8481
KeyStore ks;
8582
try (InputStream ksf = Files.newInputStream(keystorePath)) {
8683
ks = KeyStore.getInstance(KEYSTORE_TYPE);
8784
ks.load(ksf, keystorePassword.toCharArray());
88-
} catch (IOException | CertificateException | KeyStoreException | NoSuchAlgorithmException e) {
89-
throw new RuntimeException(e);
90-
}
91-
try {
92-
kmf.init(ks, keystorePassword.toCharArray());
93-
} catch (KeyStoreException | NoSuchAlgorithmException | UnrecoverableKeyException e) {
94-
throw new RuntimeException(e);
9585
}
86+
kmf.init(ks, keystorePassword.toCharArray());
9687

9788
ReloadingKeyManagerFactory reloadingKeyManagerFactory = new ReloadingKeyManagerFactory(kmf);
9889
reloadingKeyManagerFactory.start(keystorePath, keystorePassword, reloadInterval);
@@ -115,32 +106,37 @@ private ReloadingKeyManagerFactory(Spi spi, Provider provider, String algorithm)
115106
private void start(Path keystorePath, String keystorePassword, Duration reloadInterval) {
116107
this.keystorePath = keystorePath;
117108
this.keystorePassword = keystorePassword;
118-
this.executor =
119-
Executors.newScheduledThreadPool(
120-
1,
121-
runnable -> {
122-
Thread t = Executors.defaultThreadFactory().newThread(runnable);
123-
t.setDaemon(true);
124-
return t;
125-
});
126109

127110
// Ensure that reload is called once synchronously, to make sure the file exists etc.
128111
reload();
129112

130-
if (!reloadInterval.isZero())
113+
if (!reloadInterval.isZero()) {
114+
this.executor =
115+
Executors.newScheduledThreadPool(
116+
1,
117+
runnable -> {
118+
Thread t = Executors.defaultThreadFactory().newThread(runnable);
119+
t.setName(String.format("%s-%%d", this.getClass().getSimpleName()));
120+
t.setDaemon(true);
121+
return t;
122+
});
131123
this.executor.scheduleWithFixedDelay(
132124
this::reload,
133125
reloadInterval.toMillis(),
134126
reloadInterval.toMillis(),
135127
TimeUnit.MILLISECONDS);
128+
}
136129
}
137130

138131
@VisibleForTesting
139132
void reload() {
140133
try {
141134
reload0();
142135
} catch (Exception e) {
143-
logger.warn("Failed to reload", e);
136+
String msg =
137+
"Failed to reload KeyStore. If this continues to happen, your client may use stale identity"
138+
+ "certificates and fail to re-establish connections to Cassandra hosts.";
139+
logger.warn(msg, e);
144140
}
145141
}
146142

0 commit comments

Comments
 (0)