Skip to content

Commit a0f5b07

Browse files
authored
Add 2 access options to Config and refactor DriverFactory (#1356)
This update adds 2 getters for existing configuration options in `Config`: - `routingTablePurgeDelayMillis()` - provides access to value configured via `ConfigBuilder.withRoutingTablePurgeDelay(long, TimeUnit)` - `maxTransactionRetryTimeMillis()` - provides access to value configured via `ConfigBuilder.withMaxTransactionRetryTime(long, TimeUnit)` It also includes internal `DriverFactory` refactoring.
1 parent b2fe6c2 commit a0f5b07

25 files changed

+462
-482
lines changed

driver/src/main/java/org/neo4j/driver/Config.java

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import org.neo4j.driver.internal.async.pool.PoolSettings;
3737
import org.neo4j.driver.internal.cluster.RoutingSettings;
3838
import org.neo4j.driver.internal.handlers.pulln.FetchSizeUtil;
39-
import org.neo4j.driver.internal.retry.RetrySettings;
39+
import org.neo4j.driver.internal.retry.ExponentialBackoffRetryLogic;
4040
import org.neo4j.driver.net.ServerAddressResolver;
4141
import org.neo4j.driver.util.Experimental;
4242
import org.neo4j.driver.util.Immutable;
@@ -75,7 +75,9 @@ public final class Config implements Serializable {
7575

7676
private static final Config EMPTY = builder().build();
7777

78-
/** User defined logging */
78+
/**
79+
* User defined logging
80+
*/
7981
private final Logging logging;
8082

8183
private final boolean logLeakedSessions;
@@ -90,9 +92,9 @@ public final class Config implements Serializable {
9092

9193
private final long fetchSize;
9294
private final long routingTablePurgeDelayMillis;
95+
private final long maxTransactionRetryTimeMillis;
9396

9497
private final int connectionTimeoutMillis;
95-
private final RetrySettings retrySettings;
9698
private final ServerAddressResolver resolver;
9799

98100
private final int eventLoopThreads;
@@ -113,7 +115,7 @@ private Config(ConfigBuilder builder) {
113115

114116
this.connectionTimeoutMillis = builder.connectionTimeoutMillis;
115117
this.routingTablePurgeDelayMillis = builder.routingTablePurgeDelayMillis;
116-
this.retrySettings = builder.retrySettings;
118+
this.maxTransactionRetryTimeMillis = builder.maxTransactionRetryTimeMillis;
117119
this.resolver = builder.resolver;
118120
this.fetchSize = builder.fetchSize;
119121

@@ -123,6 +125,7 @@ private Config(ConfigBuilder builder) {
123125

124126
/**
125127
* Logging provider
128+
*
126129
* @return the Logging provider to use
127130
*/
128131
public Logging logging() {
@@ -212,18 +215,21 @@ public static Config defaultConfig() {
212215
}
213216

214217
/**
215-
* @return the security setting to use when creating connections.
218+
* Returns stale routing table purge delay.
219+
*
220+
* @return routing table purge delay
216221
*/
217-
SecuritySettings securitySettings() {
218-
return securitySettings;
222+
public long routingTablePurgeDelayMillis() {
223+
return routingTablePurgeDelayMillis;
219224
}
220225

221-
RoutingSettings routingSettings() {
222-
return new RoutingSettings(routingTablePurgeDelayMillis);
223-
}
224-
225-
RetrySettings retrySettings() {
226-
return retrySettings;
226+
/**
227+
* Returns managed transactions maximum retry time.
228+
*
229+
* @return maximum retry time
230+
*/
231+
public long maxTransactionRetryTimeMillis() {
232+
return maxTransactionRetryTimeMillis;
227233
}
228234

229235
public long fetchSize() {
@@ -265,9 +271,9 @@ public static final class ConfigBuilder {
265271
private String userAgent = format("neo4j-java/%s", driverVersion());
266272
private final SecuritySettings.SecuritySettingsBuilder securitySettingsBuilder =
267273
new SecuritySettings.SecuritySettingsBuilder();
268-
private long routingTablePurgeDelayMillis = RoutingSettings.DEFAULT.routingTablePurgeDelayMs();
274+
private long routingTablePurgeDelayMillis = RoutingSettings.STALE_ROUTING_TABLE_PURGE_DELAY_MS;
269275
private int connectionTimeoutMillis = (int) TimeUnit.SECONDS.toMillis(30);
270-
private RetrySettings retrySettings = RetrySettings.DEFAULT;
276+
private long maxTransactionRetryTimeMillis = ExponentialBackoffRetryLogic.DEFAULT_MAX_RETRY_TIME_MS;
271277
private ServerAddressResolver resolver;
272278
private MetricsAdapter metricsAdapter = MetricsAdapter.DEV_NULL;
273279
private long fetchSize = FetchSizeUtil.DEFAULT_FETCH_SIZE;
@@ -331,7 +337,7 @@ public ConfigBuilder withLeakedSessionsLogging() {
331337
* validity and negative values mean connections will never be tested.
332338
*
333339
* @param value the minimum idle time
334-
* @param unit the unit in which the duration is given
340+
* @param unit the unit in which the duration is given
335341
* @return this builder
336342
*/
337343
public ConfigBuilder withConnectionLivenessCheckTimeout(long value, TimeUnit unit) {
@@ -356,7 +362,7 @@ public ConfigBuilder withConnectionLivenessCheckTimeout(long value, TimeUnit uni
356362
* checked.
357363
*
358364
* @param value the maximum connection lifetime
359-
* @param unit the unit in which the duration is given
365+
* @param unit the unit in which the duration is given
360366
* @return this builder
361367
*/
362368
public ConfigBuilder withMaxConnectionLifetime(long value, TimeUnit unit) {
@@ -402,7 +408,7 @@ public ConfigBuilder withMaxConnectionPoolSize(int value) {
402408
* of {@code 0} is allowed and results in no timeout and immediate failure when connection is unavailable.
403409
*
404410
* @param value the acquisition timeout
405-
* @param unit the unit in which the duration is given
411+
* @param unit the unit in which the duration is given
406412
* @return this builder
407413
* @see #withMaxConnectionPoolSize(int)
408414
*/
@@ -418,6 +424,7 @@ public ConfigBuilder withConnectionAcquisitionTimeout(long value, TimeUnit unit)
418424

419425
/**
420426
* Set to use encrypted traffic.
427+
*
421428
* @return this builder
422429
*/
423430
public ConfigBuilder withEncryption() {
@@ -427,6 +434,7 @@ public ConfigBuilder withEncryption() {
427434

428435
/**
429436
* Set to use unencrypted traffic.
437+
*
430438
* @return this builder
431439
*/
432440
public ConfigBuilder withoutEncryption() {
@@ -461,13 +469,12 @@ public ConfigBuilder withTrustStrategy(TrustStrategy trustStrategy) {
461469
* The routing table of a database get refreshed if the database is used frequently.
462470
* If the database is not used for a long time,
463471
* the driver use the timeout specified here to purge the stale routing table.
464-
*
472+
* <p>
465473
* After a routing table is removed, next time when using the database of the purged routing table,
466474
* the driver will fall back to use seed URI for a new routing table.
467-
* @param delay
468-
* the amount of time to wait before purging routing tables
469-
* @param unit
470-
* the unit in which the duration is given
475+
*
476+
* @param delay the amount of time to wait before purging routing tables
477+
* @param unit the unit in which the duration is given
471478
* @return this builder
472479
*/
473480
public ConfigBuilder withRoutingTablePurgeDelay(long delay, TimeUnit unit) {
@@ -483,15 +490,16 @@ public ConfigBuilder withRoutingTablePurgeDelay(long delay, TimeUnit unit) {
483490
/**
484491
* Specify how many records to fetch in each batch.
485492
* This config is only valid when the driver is used with servers that support Bolt V4 (Server version 4.0 and later).
486-
*
493+
* <p>
487494
* Bolt V4 enables pulling records in batches to allow client to take control of data population and apply back pressure to server.
488495
* This config specifies the default fetch size for all query runs using {@link Session} and {@link org.neo4j.driver.async.AsyncSession}.
489496
* By default, the value is set to {@code 1000}.
490497
* Use {@code -1} to disables back pressure and config client to pull all records at once after each run.
491-
*
498+
* <p>
492499
* This config only applies to run result obtained via {@link Session} and {@link org.neo4j.driver.async.AsyncSession}.
493500
* As with {@link org.neo4j.driver.reactive.RxSession}, the batch size is provided via
494501
* {@link org.reactivestreams.Subscription#request(long)} instead.
502+
*
495503
* @param size the default record fetch size when pulling records in batches using Bolt V4.
496504
* @return this builder
497505
*/
@@ -512,10 +520,10 @@ public ConfigBuilder withFetchSize(long size) {
512520
* The default value of this parameter is {@code 30 SECONDS}.
513521
*
514522
* @param value the timeout duration
515-
* @param unit the unit in which duration is given
523+
* @param unit the unit in which duration is given
516524
* @return this builder
517525
* @throws IllegalArgumentException when given value is negative or does not fit in {@code int} when
518-
* converted to milliseconds.
526+
* converted to milliseconds.
519527
*/
520528
public ConfigBuilder withConnectionTimeout(long value, TimeUnit unit) {
521529
long connectionTimeoutMillis = unit.toMillis(value);
@@ -534,16 +542,14 @@ public ConfigBuilder withConnectionTimeout(long value, TimeUnit unit) {
534542
}
535543

536544
/**
537-
* Specify the maximum time transactions are allowed to retry via
538-
* {@link Session#readTransaction(TransactionWork)} and {@link Session#writeTransaction(TransactionWork)}
539-
* methods. These methods will retry the given unit of work on {@link org.neo4j.driver.exceptions.ServiceUnavailableException},
540-
* {@link org.neo4j.driver.exceptions.SessionExpiredException} and {@link org.neo4j.driver.exceptions.TransientException} with
541-
* exponential backoff using initial delay of 1 second.
545+
* Specify the maximum time managed transactions are allowed to retry.
546+
* <p>
547+
* Managed transactions are available via methods like {@link Session#executeRead(TransactionCallback)}, {@link Session#executeWrite(TransactionCallback, TransactionConfig)} and some other variations available under similar naming.
542548
* <p>
543549
* Default value is 30 seconds.
544550
*
545551
* @param value the timeout duration
546-
* @param unit the unit in which duration is given
552+
* @param unit the unit in which duration is given
547553
* @return this builder
548554
* @throws IllegalArgumentException when given value is negative
549555
*/
@@ -553,7 +559,7 @@ public ConfigBuilder withMaxTransactionRetryTime(long value, TimeUnit unit) {
553559
throw new IllegalArgumentException(
554560
String.format("The max retry time may not be smaller than 0, but was %d %s.", value, unit));
555561
}
556-
this.retrySettings = new RetrySettings(maxRetryTimeMs);
562+
this.maxTransactionRetryTimeMillis = maxRetryTimeMs;
557563
return this;
558564
}
559565

@@ -586,6 +592,7 @@ public ConfigBuilder withDriverMetrics() {
586592

587593
/**
588594
* Disable driver metrics. When disabled, driver metrics cannot be accessed via {@link Driver#metrics()}.
595+
*
589596
* @return this builder.
590597
*/
591598
public ConfigBuilder withoutDriverMetrics() {
@@ -619,6 +626,7 @@ public ConfigBuilder withMetricsAdapter(MetricsAdapter metricsAdapter) {
619626
/**
620627
* Configure the event loop thread count. This specifies how many threads the driver can use to handle network I/O events
621628
* and user's events in driver's I/O threads. By default, 2 * NumberOfProcessors amount of threads will be used instead.
629+
*
622630
* @param size the thread count.
623631
* @return this builder.
624632
* @throws IllegalArgumentException if the value of the size is set to a number that is less than 1.
@@ -634,6 +642,7 @@ public ConfigBuilder withEventLoopThreads(int size) {
634642

635643
/**
636644
* Configure the user_agent field sent to the server to identify the connected client.
645+
*
637646
* @param userAgent the string to configure user_agent.
638647
* @return this builder.
639648
*/
@@ -802,6 +811,7 @@ public static TrustStrategy trustAllCertificates() {
802811

803812
/**
804813
* The revocation strategy used for verifying certificates.
814+
*
805815
* @return this {@link TrustStrategy}'s revocation strategy
806816
*/
807817
public RevocationCheckingStrategy revocationCheckingStrategy() {
@@ -811,6 +821,7 @@ public RevocationCheckingStrategy revocationCheckingStrategy() {
811821
/**
812822
* Configures the {@link TrustStrategy} to not carry out OCSP revocation checks on certificates. This is the
813823
* option that is configured by default.
824+
*
814825
* @return the current trust strategy
815826
*/
816827
public TrustStrategy withoutCertificateRevocationChecks() {
@@ -823,6 +834,7 @@ public TrustStrategy withoutCertificateRevocationChecks() {
823834
* stapled to the certificate. If no stapled response is found, then certificate verification continues
824835
* (and does not fail verification). This setting also requires the server to be configured to enable
825836
* OCSP stapling.
837+
*
826838
* @return the current trust strategy
827839
*/
828840
public TrustStrategy withVerifyIfPresentRevocationChecks() {
@@ -834,9 +846,10 @@ public TrustStrategy withVerifyIfPresentRevocationChecks() {
834846
* Configures the {@link TrustStrategy} to carry out strict OCSP revocation checks for revocation status that
835847
* are stapled to the certificate. If no stapled response is found, then the driver will fail certificate verification
836848
* and not connect to the server. This setting also requires the server to be configured to enable OCSP stapling.
837-
*
849+
* <p>
838850
* Note: enabling this setting will prevent the driver connecting to the server when the server is unable to reach
839851
* the certificate's configured OCSP responder URL.
852+
*
840853
* @return the current trust strategy
841854
*/
842855
public TrustStrategy withStrictRevocationChecks() {

driver/src/main/java/org/neo4j/driver/GraphDatabase.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020

2121
import java.net.URI;
2222
import org.neo4j.driver.internal.DriverFactory;
23-
import org.neo4j.driver.internal.SecuritySettings;
24-
import org.neo4j.driver.internal.cluster.RoutingSettings;
25-
import org.neo4j.driver.internal.retry.RetrySettings;
26-
import org.neo4j.driver.internal.security.SecurityPlan;
2723

2824
/**
2925
* Creates {@link Driver drivers}, optionally letting you {@link #driver(URI, Config)} to configure them.
@@ -123,11 +119,7 @@ public static Driver driver(URI uri, AuthToken authToken, Config config) {
123119

124120
static Driver driver(URI uri, AuthToken authToken, Config config, DriverFactory driverFactory) {
125121
config = getOrDefault(config);
126-
RoutingSettings routingSettings = config.routingSettings();
127-
RetrySettings retrySettings = config.retrySettings();
128-
SecuritySettings securitySettings = config.securitySettings();
129-
SecurityPlan securityPlan = securitySettings.createSecurityPlan(uri.getScheme());
130-
return driverFactory.newInstance(uri, authToken, routingSettings, retrySettings, config, securityPlan);
122+
return driverFactory.newInstance(uri, authToken, config);
131123
}
132124

133125
private static Config getOrDefault(Config config) {

driver/src/main/java/org/neo4j/driver/internal/DriverFactory.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555
import org.neo4j.driver.internal.metrics.MicrometerMetricsProvider;
5656
import org.neo4j.driver.internal.retry.ExponentialBackoffRetryLogic;
5757
import org.neo4j.driver.internal.retry.RetryLogic;
58-
import org.neo4j.driver.internal.retry.RetrySettings;
5958
import org.neo4j.driver.internal.security.SecurityPlan;
59+
import org.neo4j.driver.internal.security.SecurityPlans;
6060
import org.neo4j.driver.internal.spi.ConnectionPool;
6161
import org.neo4j.driver.internal.spi.ConnectionProvider;
6262
import org.neo4j.driver.internal.util.Clock;
@@ -67,25 +67,18 @@ public class DriverFactory {
6767
public static final String NO_ROUTING_CONTEXT_ERROR_MESSAGE =
6868
"Routing parameters are not supported with scheme 'bolt'. Given URI: ";
6969

70-
public final Driver newInstance(
71-
URI uri,
72-
AuthToken authToken,
73-
RoutingSettings routingSettings,
74-
RetrySettings retrySettings,
75-
Config config,
76-
SecurityPlan securityPlan) {
77-
return newInstance(uri, authToken, routingSettings, retrySettings, config, null, securityPlan, null);
70+
public final Driver newInstance(URI uri, AuthToken authToken, Config config) {
71+
return newInstance(uri, authToken, config, null, null, null);
7872
}
7973

8074
public final Driver newInstance(
8175
URI uri,
8276
AuthToken authToken,
83-
RoutingSettings routingSettings,
84-
RetrySettings retrySettings,
8577
Config config,
86-
EventLoopGroup eventLoopGroup,
8778
SecurityPlan securityPlan,
79+
EventLoopGroup eventLoopGroup,
8880
Supplier<Rediscovery> rediscoverySupplier) {
81+
8982
Bootstrap bootstrap;
9083
boolean ownsEventLoopGroup;
9184
if (eventLoopGroup == null) {
@@ -96,14 +89,21 @@ public final Driver newInstance(
9689
ownsEventLoopGroup = false;
9790
}
9891

92+
if (securityPlan == null) {
93+
var settings = new SecuritySettings(config.encrypted(), config.trustStrategy());
94+
securityPlan = SecurityPlans.createSecurityPlan(settings, uri.getScheme());
95+
}
96+
9997
authToken = authToken == null ? AuthTokens.none() : authToken;
10098

10199
BoltServerAddress address = new BoltServerAddress(uri);
102-
RoutingSettings newRoutingSettings = routingSettings.withRoutingContext(new RoutingContext(uri));
100+
RoutingSettings routingSettings =
101+
new RoutingSettings(config.routingTablePurgeDelayMillis(), new RoutingContext(uri));
103102

104103
InternalLoggerFactory.setDefaultFactory(new NettyLogging(config.logging()));
105104
EventExecutorGroup eventExecutorGroup = bootstrap.config().group();
106-
RetryLogic retryLogic = createRetryLogic(retrySettings, eventExecutorGroup, config.logging());
105+
RetryLogic retryLogic =
106+
createRetryLogic(config.maxTransactionRetryTimeMillis(), eventExecutorGroup, config.logging());
107107

108108
MetricsProvider metricsProvider = getOrCreateMetricsProvider(config, createClock());
109109
ConnectionPool connectionPool = createConnectionPool(
@@ -113,15 +113,15 @@ public final Driver newInstance(
113113
metricsProvider,
114114
config,
115115
ownsEventLoopGroup,
116-
newRoutingSettings.routingContext());
116+
routingSettings.routingContext());
117117

118118
return createDriver(
119119
uri,
120120
securityPlan,
121121
address,
122122
connectionPool,
123123
eventExecutorGroup,
124-
newRoutingSettings,
124+
routingSettings,
125125
retryLogic,
126126
metricsProvider,
127127
rediscoverySupplier,
@@ -354,8 +354,8 @@ protected SessionFactory createSessionFactory(
354354
* <b>This method is protected only for testing</b>
355355
*/
356356
protected RetryLogic createRetryLogic(
357-
RetrySettings settings, EventExecutorGroup eventExecutorGroup, Logging logging) {
358-
return new ExponentialBackoffRetryLogic(settings, eventExecutorGroup, createClock(), logging);
357+
long maxTransactionRetryTime, EventExecutorGroup eventExecutorGroup, Logging logging) {
358+
return new ExponentialBackoffRetryLogic(maxTransactionRetryTime, eventExecutorGroup, createClock(), logging);
359359
}
360360

361361
/**

0 commit comments

Comments
 (0)