Skip to content

Commit

Permalink
Update driver factory
Browse files Browse the repository at this point in the history
  • Loading branch information
injectives committed Dec 15, 2022
1 parent b2fe6c2 commit 5b645b3
Show file tree
Hide file tree
Showing 27 changed files with 561 additions and 584 deletions.
28 changes: 14 additions & 14 deletions driver/src/main/java/org/neo4j/driver/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import org.neo4j.driver.internal.SecuritySettings;
import org.neo4j.driver.internal.InternalSecuritySettings;
import org.neo4j.driver.internal.async.pool.PoolSettings;
import org.neo4j.driver.internal.cluster.RoutingSettings;
import org.neo4j.driver.internal.handlers.pulln.FetchSizeUtil;
import org.neo4j.driver.internal.retry.RetrySettings;
import org.neo4j.driver.internal.retry.ExponentialBackoffRetryLogic;
import org.neo4j.driver.net.ServerAddressResolver;
import org.neo4j.driver.util.Experimental;
import org.neo4j.driver.util.Immutable;
Expand Down Expand Up @@ -92,7 +92,7 @@ public final class Config implements Serializable {
private final long routingTablePurgeDelayMillis;

private final int connectionTimeoutMillis;
private final RetrySettings retrySettings;
private final long maxTransactionRetryTime;
private final ServerAddressResolver resolver;

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

this.connectionTimeoutMillis = builder.connectionTimeoutMillis;
this.routingTablePurgeDelayMillis = builder.routingTablePurgeDelayMillis;
this.retrySettings = builder.retrySettings;
this.maxTransactionRetryTime = builder.maxTransactionRetryTime;
this.resolver = builder.resolver;
this.fetchSize = builder.fetchSize;

Expand Down Expand Up @@ -214,16 +214,16 @@ public static Config defaultConfig() {
/**
* @return the security setting to use when creating connections.
*/
SecuritySettings securitySettings() {
public SecuritySettings securitySettings() {
return securitySettings;
}

RoutingSettings routingSettings() {
return new RoutingSettings(routingTablePurgeDelayMillis);
public long routingTablePurgeDelay() {
return routingTablePurgeDelayMillis;
}

RetrySettings retrySettings() {
return retrySettings;
public long maxTransactionRetryTime() {
return maxTransactionRetryTime;
}

public long fetchSize() {
Expand Down Expand Up @@ -263,11 +263,11 @@ public static final class ConfigBuilder {
private long maxConnectionLifetimeMillis = PoolSettings.DEFAULT_MAX_CONNECTION_LIFETIME;
private long connectionAcquisitionTimeoutMillis = PoolSettings.DEFAULT_CONNECTION_ACQUISITION_TIMEOUT;
private String userAgent = format("neo4j-java/%s", driverVersion());
private final SecuritySettings.SecuritySettingsBuilder securitySettingsBuilder =
new SecuritySettings.SecuritySettingsBuilder();
private long routingTablePurgeDelayMillis = RoutingSettings.DEFAULT.routingTablePurgeDelayMs();
private final InternalSecuritySettings.SecuritySettingsBuilder securitySettingsBuilder =
new InternalSecuritySettings.SecuritySettingsBuilder();
private long routingTablePurgeDelayMillis = RoutingSettings.STALE_ROUTING_TABLE_PURGE_DELAY_MS;
private int connectionTimeoutMillis = (int) TimeUnit.SECONDS.toMillis(30);
private RetrySettings retrySettings = RetrySettings.DEFAULT;
private long maxTransactionRetryTime = ExponentialBackoffRetryLogic.DEFAULT_MAX_RETRY_TIME_MS;
private ServerAddressResolver resolver;
private MetricsAdapter metricsAdapter = MetricsAdapter.DEV_NULL;
private long fetchSize = FetchSizeUtil.DEFAULT_FETCH_SIZE;
Expand Down Expand Up @@ -553,7 +553,7 @@ public ConfigBuilder withMaxTransactionRetryTime(long value, TimeUnit unit) {
throw new IllegalArgumentException(
String.format("The max retry time may not be smaller than 0, but was %d %s.", value, unit));
}
this.retrySettings = new RetrySettings(maxRetryTimeMs);
this.maxTransactionRetryTime = maxRetryTimeMs;
return this;
}

Expand Down
10 changes: 1 addition & 9 deletions driver/src/main/java/org/neo4j/driver/GraphDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@

import java.net.URI;
import org.neo4j.driver.internal.DriverFactory;
import org.neo4j.driver.internal.SecuritySettings;
import org.neo4j.driver.internal.cluster.RoutingSettings;
import org.neo4j.driver.internal.retry.RetrySettings;
import org.neo4j.driver.internal.security.SecurityPlan;

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

static Driver driver(URI uri, AuthToken authToken, Config config, DriverFactory driverFactory) {
config = getOrDefault(config);
RoutingSettings routingSettings = config.routingSettings();
RetrySettings retrySettings = config.retrySettings();
SecuritySettings securitySettings = config.securitySettings();
SecurityPlan securityPlan = securitySettings.createSecurityPlan(uri.getScheme());
return driverFactory.newInstance(uri, authToken, routingSettings, retrySettings, config, securityPlan);
return driverFactory.newInstance(uri, authToken, config);
}

private static Config getOrDefault(Config config) {
Expand Down
29 changes: 29 additions & 0 deletions driver/src/main/java/org/neo4j/driver/SecuritySettings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.neo4j.driver;

import java.io.Serializable;

public interface SecuritySettings extends Serializable {
// TODO
boolean encrypted();

// TODO
Config.TrustStrategy trustStrategy();
}
34 changes: 16 additions & 18 deletions driver/src/main/java/org/neo4j/driver/internal/DriverFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
import org.neo4j.driver.internal.metrics.MicrometerMetricsProvider;
import org.neo4j.driver.internal.retry.ExponentialBackoffRetryLogic;
import org.neo4j.driver.internal.retry.RetryLogic;
import org.neo4j.driver.internal.retry.RetrySettings;
import org.neo4j.driver.internal.security.SecurityPlan;
import org.neo4j.driver.internal.security.SecurityPlans;
import org.neo4j.driver.internal.spi.ConnectionPool;
import org.neo4j.driver.internal.spi.ConnectionProvider;
import org.neo4j.driver.internal.util.Clock;
Expand All @@ -67,25 +67,18 @@ public class DriverFactory {
public static final String NO_ROUTING_CONTEXT_ERROR_MESSAGE =
"Routing parameters are not supported with scheme 'bolt'. Given URI: ";

public final Driver newInstance(
URI uri,
AuthToken authToken,
RoutingSettings routingSettings,
RetrySettings retrySettings,
Config config,
SecurityPlan securityPlan) {
return newInstance(uri, authToken, routingSettings, retrySettings, config, null, securityPlan, null);
public final Driver newInstance(URI uri, AuthToken authToken, Config config) {
return newInstance(uri, authToken, config, null, null, null);
}

public final Driver newInstance(
URI uri,
AuthToken authToken,
RoutingSettings routingSettings,
RetrySettings retrySettings,
Config config,
EventLoopGroup eventLoopGroup,
SecurityPlan securityPlan,
EventLoopGroup eventLoopGroup,
Supplier<Rediscovery> rediscoverySupplier) {

Bootstrap bootstrap;
boolean ownsEventLoopGroup;
if (eventLoopGroup == null) {
Expand All @@ -96,14 +89,19 @@ public final Driver newInstance(
ownsEventLoopGroup = false;
}

if (securityPlan == null) {
securityPlan = SecurityPlans.createSecurityPlan(config.securitySettings(), uri.getScheme());
}

authToken = authToken == null ? AuthTokens.none() : authToken;

BoltServerAddress address = new BoltServerAddress(uri);
RoutingSettings newRoutingSettings = routingSettings.withRoutingContext(new RoutingContext(uri));
RoutingSettings routingSettings = new RoutingSettings(config.routingTablePurgeDelay(), new RoutingContext(uri));

InternalLoggerFactory.setDefaultFactory(new NettyLogging(config.logging()));
EventExecutorGroup eventExecutorGroup = bootstrap.config().group();
RetryLogic retryLogic = createRetryLogic(retrySettings, eventExecutorGroup, config.logging());
RetryLogic retryLogic =
createRetryLogic(config.maxTransactionRetryTime(), eventExecutorGroup, config.logging());

MetricsProvider metricsProvider = getOrCreateMetricsProvider(config, createClock());
ConnectionPool connectionPool = createConnectionPool(
Expand All @@ -113,15 +111,15 @@ public final Driver newInstance(
metricsProvider,
config,
ownsEventLoopGroup,
newRoutingSettings.routingContext());
routingSettings.routingContext());

return createDriver(
uri,
securityPlan,
address,
connectionPool,
eventExecutorGroup,
newRoutingSettings,
routingSettings,
retryLogic,
metricsProvider,
rediscoverySupplier,
Expand Down Expand Up @@ -354,8 +352,8 @@ protected SessionFactory createSessionFactory(
* <b>This method is protected only for testing</b>
*/
protected RetryLogic createRetryLogic(
RetrySettings settings, EventExecutorGroup eventExecutorGroup, Logging logging) {
return new ExponentialBackoffRetryLogic(settings, eventExecutorGroup, createClock(), logging);
long maxTransactionRetryTime, EventExecutorGroup eventExecutorGroup, Logging logging) {
return new ExponentialBackoffRetryLogic(maxTransactionRetryTime, eventExecutorGroup, createClock(), logging);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.neo4j.driver.internal;

import org.neo4j.driver.Config;

public class InternalSecuritySettings implements org.neo4j.driver.SecuritySettings {
private static final long serialVersionUID = 4494615367164106576L;

private static final boolean DEFAULT_ENCRYPTED = false;
private static final Config.TrustStrategy DEFAULT_TRUST_STRATEGY = Config.TrustStrategy.trustSystemCertificates();
public static final InternalSecuritySettings DEFAULT =
new InternalSecuritySettings(DEFAULT_ENCRYPTED, DEFAULT_TRUST_STRATEGY);
private final boolean encrypted;
private final Config.TrustStrategy trustStrategy;

public InternalSecuritySettings(boolean encrypted, Config.TrustStrategy trustStrategy) {
this.encrypted = encrypted;
this.trustStrategy = trustStrategy == null ? DEFAULT_TRUST_STRATEGY : trustStrategy;
}

public boolean encrypted() {
return encrypted;
}

public Config.TrustStrategy trustStrategy() {
return trustStrategy;
}

public static class SecuritySettingsBuilder {
private boolean isCustomized = false;
private boolean encrypted;
private Config.TrustStrategy trustStrategy;

public SecuritySettingsBuilder withEncryption() {
encrypted = true;
isCustomized = true;
return this;
}

public SecuritySettingsBuilder withoutEncryption() {
encrypted = false;
isCustomized = true;
return this;
}

public SecuritySettingsBuilder withTrustStrategy(Config.TrustStrategy strategy) {
trustStrategy = strategy;
isCustomized = true;
return this;
}

public InternalSecuritySettings build() {
return isCustomized
? new InternalSecuritySettings(encrypted, trustStrategy)
: InternalSecuritySettings.DEFAULT;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

public class RoutingSettings {
public static final long STALE_ROUTING_TABLE_PURGE_DELAY_MS = SECONDS.toMillis(30);
public static final RoutingSettings DEFAULT = new RoutingSettings(STALE_ROUTING_TABLE_PURGE_DELAY_MS);

private final RoutingContext routingContext;
private final long routingTablePurgeDelayMs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import reactor.util.retry.Retry;

public class ExponentialBackoffRetryLogic implements RetryLogic {
static final long DEFAULT_MAX_RETRY_TIME_MS = SECONDS.toMillis(30);
public static final long DEFAULT_MAX_RETRY_TIME_MS = SECONDS.toMillis(30);

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

public ExponentialBackoffRetryLogic(
RetrySettings settings, EventExecutorGroup eventExecutorGroup, Clock clock, Logging logging) {
long maxTransactionRetryTime, EventExecutorGroup eventExecutorGroup, Clock clock, Logging logging) {
this(
settings.maxRetryTimeMs(),
maxTransactionRetryTime,
INITIAL_RETRY_DELAY_MS,
RETRY_DELAY_MULTIPLIER,
RETRY_DELAY_JITTER_FACTOR,
Expand Down
Loading

0 comments on commit 5b645b3

Please sign in to comment.