Skip to content

Commit 5b645b3

Browse files
committed
Update driver factory
1 parent b2fe6c2 commit 5b645b3

27 files changed

+561
-584
lines changed

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
import java.util.Objects;
3333
import java.util.concurrent.TimeUnit;
3434
import java.util.logging.Level;
35-
import org.neo4j.driver.internal.SecuritySettings;
35+
import org.neo4j.driver.internal.InternalSecuritySettings;
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;
@@ -92,7 +92,7 @@ public final class Config implements Serializable {
9292
private final long routingTablePurgeDelayMillis;
9393

9494
private final int connectionTimeoutMillis;
95-
private final RetrySettings retrySettings;
95+
private final long maxTransactionRetryTime;
9696
private final ServerAddressResolver resolver;
9797

9898
private final int eventLoopThreads;
@@ -113,7 +113,7 @@ private Config(ConfigBuilder builder) {
113113

114114
this.connectionTimeoutMillis = builder.connectionTimeoutMillis;
115115
this.routingTablePurgeDelayMillis = builder.routingTablePurgeDelayMillis;
116-
this.retrySettings = builder.retrySettings;
116+
this.maxTransactionRetryTime = builder.maxTransactionRetryTime;
117117
this.resolver = builder.resolver;
118118
this.fetchSize = builder.fetchSize;
119119

@@ -214,16 +214,16 @@ public static Config defaultConfig() {
214214
/**
215215
* @return the security setting to use when creating connections.
216216
*/
217-
SecuritySettings securitySettings() {
217+
public SecuritySettings securitySettings() {
218218
return securitySettings;
219219
}
220220

221-
RoutingSettings routingSettings() {
222-
return new RoutingSettings(routingTablePurgeDelayMillis);
221+
public long routingTablePurgeDelay() {
222+
return routingTablePurgeDelayMillis;
223223
}
224224

225-
RetrySettings retrySettings() {
226-
return retrySettings;
225+
public long maxTransactionRetryTime() {
226+
return maxTransactionRetryTime;
227227
}
228228

229229
public long fetchSize() {
@@ -263,11 +263,11 @@ public static final class ConfigBuilder {
263263
private long maxConnectionLifetimeMillis = PoolSettings.DEFAULT_MAX_CONNECTION_LIFETIME;
264264
private long connectionAcquisitionTimeoutMillis = PoolSettings.DEFAULT_CONNECTION_ACQUISITION_TIMEOUT;
265265
private String userAgent = format("neo4j-java/%s", driverVersion());
266-
private final SecuritySettings.SecuritySettingsBuilder securitySettingsBuilder =
267-
new SecuritySettings.SecuritySettingsBuilder();
268-
private long routingTablePurgeDelayMillis = RoutingSettings.DEFAULT.routingTablePurgeDelayMs();
266+
private final InternalSecuritySettings.SecuritySettingsBuilder securitySettingsBuilder =
267+
new InternalSecuritySettings.SecuritySettingsBuilder();
268+
private long routingTablePurgeDelayMillis = RoutingSettings.STALE_ROUTING_TABLE_PURGE_DELAY_MS;
269269
private int connectionTimeoutMillis = (int) TimeUnit.SECONDS.toMillis(30);
270-
private RetrySettings retrySettings = RetrySettings.DEFAULT;
270+
private long maxTransactionRetryTime = ExponentialBackoffRetryLogic.DEFAULT_MAX_RETRY_TIME_MS;
271271
private ServerAddressResolver resolver;
272272
private MetricsAdapter metricsAdapter = MetricsAdapter.DEV_NULL;
273273
private long fetchSize = FetchSizeUtil.DEFAULT_FETCH_SIZE;
@@ -553,7 +553,7 @@ public ConfigBuilder withMaxTransactionRetryTime(long value, TimeUnit unit) {
553553
throw new IllegalArgumentException(
554554
String.format("The max retry time may not be smaller than 0, but was %d %s.", value, unit));
555555
}
556-
this.retrySettings = new RetrySettings(maxRetryTimeMs);
556+
this.maxTransactionRetryTime = maxRetryTimeMs;
557557
return this;
558558
}
559559

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) {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.driver;
20+
21+
import java.io.Serializable;
22+
23+
public interface SecuritySettings extends Serializable {
24+
// TODO
25+
boolean encrypted();
26+
27+
// TODO
28+
Config.TrustStrategy trustStrategy();
29+
}

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

Lines changed: 16 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,19 @@ public final Driver newInstance(
9689
ownsEventLoopGroup = false;
9790
}
9891

92+
if (securityPlan == null) {
93+
securityPlan = SecurityPlans.createSecurityPlan(config.securitySettings(), uri.getScheme());
94+
}
95+
9996
authToken = authToken == null ? AuthTokens.none() : authToken;
10097

10198
BoltServerAddress address = new BoltServerAddress(uri);
102-
RoutingSettings newRoutingSettings = routingSettings.withRoutingContext(new RoutingContext(uri));
99+
RoutingSettings routingSettings = new RoutingSettings(config.routingTablePurgeDelay(), new RoutingContext(uri));
103100

104101
InternalLoggerFactory.setDefaultFactory(new NettyLogging(config.logging()));
105102
EventExecutorGroup eventExecutorGroup = bootstrap.config().group();
106-
RetryLogic retryLogic = createRetryLogic(retrySettings, eventExecutorGroup, config.logging());
103+
RetryLogic retryLogic =
104+
createRetryLogic(config.maxTransactionRetryTime(), eventExecutorGroup, config.logging());
107105

108106
MetricsProvider metricsProvider = getOrCreateMetricsProvider(config, createClock());
109107
ConnectionPool connectionPool = createConnectionPool(
@@ -113,15 +111,15 @@ public final Driver newInstance(
113111
metricsProvider,
114112
config,
115113
ownsEventLoopGroup,
116-
newRoutingSettings.routingContext());
114+
routingSettings.routingContext());
117115

118116
return createDriver(
119117
uri,
120118
securityPlan,
121119
address,
122120
connectionPool,
123121
eventExecutorGroup,
124-
newRoutingSettings,
122+
routingSettings,
125123
retryLogic,
126124
metricsProvider,
127125
rediscoverySupplier,
@@ -354,8 +352,8 @@ protected SessionFactory createSessionFactory(
354352
* <b>This method is protected only for testing</b>
355353
*/
356354
protected RetryLogic createRetryLogic(
357-
RetrySettings settings, EventExecutorGroup eventExecutorGroup, Logging logging) {
358-
return new ExponentialBackoffRetryLogic(settings, eventExecutorGroup, createClock(), logging);
355+
long maxTransactionRetryTime, EventExecutorGroup eventExecutorGroup, Logging logging) {
356+
return new ExponentialBackoffRetryLogic(maxTransactionRetryTime, eventExecutorGroup, createClock(), logging);
359357
}
360358

361359
/**
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.driver.internal;
20+
21+
import org.neo4j.driver.Config;
22+
23+
public class InternalSecuritySettings implements org.neo4j.driver.SecuritySettings {
24+
private static final long serialVersionUID = 4494615367164106576L;
25+
26+
private static final boolean DEFAULT_ENCRYPTED = false;
27+
private static final Config.TrustStrategy DEFAULT_TRUST_STRATEGY = Config.TrustStrategy.trustSystemCertificates();
28+
public static final InternalSecuritySettings DEFAULT =
29+
new InternalSecuritySettings(DEFAULT_ENCRYPTED, DEFAULT_TRUST_STRATEGY);
30+
private final boolean encrypted;
31+
private final Config.TrustStrategy trustStrategy;
32+
33+
public InternalSecuritySettings(boolean encrypted, Config.TrustStrategy trustStrategy) {
34+
this.encrypted = encrypted;
35+
this.trustStrategy = trustStrategy == null ? DEFAULT_TRUST_STRATEGY : trustStrategy;
36+
}
37+
38+
public boolean encrypted() {
39+
return encrypted;
40+
}
41+
42+
public Config.TrustStrategy trustStrategy() {
43+
return trustStrategy;
44+
}
45+
46+
public static class SecuritySettingsBuilder {
47+
private boolean isCustomized = false;
48+
private boolean encrypted;
49+
private Config.TrustStrategy trustStrategy;
50+
51+
public SecuritySettingsBuilder withEncryption() {
52+
encrypted = true;
53+
isCustomized = true;
54+
return this;
55+
}
56+
57+
public SecuritySettingsBuilder withoutEncryption() {
58+
encrypted = false;
59+
isCustomized = true;
60+
return this;
61+
}
62+
63+
public SecuritySettingsBuilder withTrustStrategy(Config.TrustStrategy strategy) {
64+
trustStrategy = strategy;
65+
isCustomized = true;
66+
return this;
67+
}
68+
69+
public InternalSecuritySettings build() {
70+
return isCustomized
71+
? new InternalSecuritySettings(encrypted, trustStrategy)
72+
: InternalSecuritySettings.DEFAULT;
73+
}
74+
}
75+
}

driver/src/main/java/org/neo4j/driver/internal/cluster/RoutingSettings.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
public class RoutingSettings {
2424
public static final long STALE_ROUTING_TABLE_PURGE_DELAY_MS = SECONDS.toMillis(30);
25-
public static final RoutingSettings DEFAULT = new RoutingSettings(STALE_ROUTING_TABLE_PURGE_DELAY_MS);
2625

2726
private final RoutingContext routingContext;
2827
private final long routingTablePurgeDelayMs;

driver/src/main/java/org/neo4j/driver/internal/retry/ExponentialBackoffRetryLogic.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import reactor.util.retry.Retry;
4646

4747
public class ExponentialBackoffRetryLogic implements RetryLogic {
48-
static final long DEFAULT_MAX_RETRY_TIME_MS = SECONDS.toMillis(30);
48+
public static final long DEFAULT_MAX_RETRY_TIME_MS = SECONDS.toMillis(30);
4949

5050
private static final long INITIAL_RETRY_DELAY_MS = SECONDS.toMillis(1);
5151
private static final double RETRY_DELAY_MULTIPLIER = 2.0;
@@ -61,9 +61,9 @@ public class ExponentialBackoffRetryLogic implements RetryLogic {
6161
private final Logger log;
6262

6363
public ExponentialBackoffRetryLogic(
64-
RetrySettings settings, EventExecutorGroup eventExecutorGroup, Clock clock, Logging logging) {
64+
long maxTransactionRetryTime, EventExecutorGroup eventExecutorGroup, Clock clock, Logging logging) {
6565
this(
66-
settings.maxRetryTimeMs(),
66+
maxTransactionRetryTime,
6767
INITIAL_RETRY_DELAY_MS,
6868
RETRY_DELAY_MULTIPLIER,
6969
RETRY_DELAY_JITTER_FACTOR,

0 commit comments

Comments
 (0)