Skip to content

Commit

Permalink
Update internal driver clock usage (#1375)
Browse files Browse the repository at this point in the history
This update migrates driver internal implementation to `java.time.Clock` and deletes `org.neo4j.driver.internal.util.Clock`.
  • Loading branch information
injectives authored Feb 13, 2023
1 parent 4633115 commit 7d0e75c
Show file tree
Hide file tree
Showing 40 changed files with 199 additions and 269 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.netty.util.concurrent.EventExecutorGroup;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.net.URI;
import java.time.Clock;
import java.util.Objects;
import java.util.function.Supplier;
import org.neo4j.driver.AuthToken;
Expand Down Expand Up @@ -59,7 +60,6 @@
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;
import org.neo4j.driver.internal.util.Futures;
import org.neo4j.driver.net.ServerAddressResolver;

Expand Down Expand Up @@ -336,7 +336,7 @@ private static ServerAddressResolver createResolver(Config config) {
* Creates new {@link Clock}.
*/
protected Clock createClock() {
return Clock.SYSTEM;
return Clock.systemUTC();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import java.time.Clock;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;
Expand All @@ -45,7 +46,6 @@
import org.neo4j.driver.internal.metrics.MetricsListener;
import org.neo4j.driver.internal.spi.Connection;
import org.neo4j.driver.internal.spi.ResponseHandler;
import org.neo4j.driver.internal.util.Clock;

/**
* This connection represents a simple network connection to a remote server. It wraps a channel obtained from a connection pool. The life cycle of this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.netty.resolver.AddressResolverGroup;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.time.Clock;
import org.neo4j.driver.AuthToken;
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Logging;
Expand All @@ -40,7 +41,6 @@
import org.neo4j.driver.internal.cluster.RoutingContext;
import org.neo4j.driver.internal.security.InternalAuthToken;
import org.neo4j.driver.internal.security.SecurityPlan;
import org.neo4j.driver.internal.util.Clock;

public class ChannelConnectorImpl implements ChannelConnector {
private final String userAgent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.handler.ssl.SslHandler;
import java.time.Clock;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLParameters;
import org.neo4j.driver.Logging;
import org.neo4j.driver.internal.BoltServerAddress;
import org.neo4j.driver.internal.async.inbound.InboundMessageDispatcher;
import org.neo4j.driver.internal.security.SecurityPlan;
import org.neo4j.driver.internal.util.Clock;

public class NettyChannelInitializer extends ChannelInitializer<Channel> {
private final BoltServerAddress address;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import java.time.Clock;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
Expand All @@ -50,7 +51,6 @@
import org.neo4j.driver.internal.metrics.MetricsListener;
import org.neo4j.driver.internal.spi.Connection;
import org.neo4j.driver.internal.spi.ConnectionPool;
import org.neo4j.driver.internal.util.Clock;
import org.neo4j.driver.internal.util.Futures;
import org.neo4j.driver.net.ServerAddress;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.netty.channel.pool.ChannelHealthChecker;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.Promise;
import java.time.Clock;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import org.neo4j.driver.Logger;
Expand All @@ -34,7 +35,6 @@
import org.neo4j.driver.internal.async.connection.AuthorizationStateListener;
import org.neo4j.driver.internal.handlers.PingResponseHandler;
import org.neo4j.driver.internal.messaging.request.ResetMessage;
import org.neo4j.driver.internal.util.Clock;

public class NettyChannelHealthChecker implements ChannelHealthChecker, AuthorizationStateListener {
private final PoolSettings poolSettings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
package org.neo4j.driver.internal.async.pool;

import io.netty.channel.Channel;
import java.time.Clock;
import org.neo4j.driver.Logging;
import org.neo4j.driver.internal.async.NetworkConnection;
import org.neo4j.driver.internal.metrics.MetricsListener;
import org.neo4j.driver.internal.spi.Connection;
import org.neo4j.driver.internal.util.Clock;

public class NetworkConnectionFactory implements ConnectionFactory {
private final Clock clock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static java.util.Arrays.asList;
import static org.neo4j.driver.internal.util.LockUtil.executeWithLock;

import java.time.Clock;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
Expand All @@ -34,7 +35,6 @@
import org.neo4j.driver.AccessMode;
import org.neo4j.driver.internal.BoltServerAddress;
import org.neo4j.driver.internal.DatabaseName;
import org.neo4j.driver.internal.util.Clock;

public class ClusterRoutingTable implements RoutingTable {
private static final int MIN_ROUTERS = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.neo4j.driver.internal.messaging.request.MultiDatabaseUtil.supportsMultiDatabase;
import static org.neo4j.driver.internal.messaging.request.MultiDatabaseUtil.supportsRouteMessage;

import java.time.Clock;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletionException;
Expand All @@ -33,7 +34,6 @@
import org.neo4j.driver.exceptions.value.ValueException;
import org.neo4j.driver.internal.DatabaseName;
import org.neo4j.driver.internal.spi.Connection;
import org.neo4j.driver.internal.util.Clock;

public class RoutingProcedureClusterCompositionProvider implements ClusterCompositionProvider {
private static final String PROTOCOL_ERROR_MESSAGE = "Failed to parse '%s' result received from server due to ";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static java.util.Objects.requireNonNull;
import static org.neo4j.driver.internal.async.ConnectionContext.PENDING_DATABASE_NAME_EXCEPTION_SUPPLIER;

import java.time.Clock;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand All @@ -39,7 +40,6 @@
import org.neo4j.driver.internal.DatabaseNameUtil;
import org.neo4j.driver.internal.async.ConnectionContext;
import org.neo4j.driver.internal.spi.ConnectionPool;
import org.neo4j.driver.internal.util.Clock;
import org.neo4j.driver.internal.util.Futures;

public class RoutingTableRegistryImpl implements RoutingTableRegistry {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static org.neo4j.driver.internal.util.Futures.onErrorContinue;

import io.netty.util.concurrent.EventExecutorGroup;
import java.time.Clock;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
Expand All @@ -50,7 +51,6 @@
import org.neo4j.driver.internal.spi.Connection;
import org.neo4j.driver.internal.spi.ConnectionPool;
import org.neo4j.driver.internal.spi.ConnectionProvider;
import org.neo4j.driver.internal.util.Clock;
import org.neo4j.driver.internal.util.Futures;

public class LoadBalancer implements ConnectionProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
import static org.neo4j.driver.internal.util.Futures.completedWithNull;

import io.netty.channel.Channel;
import java.time.Clock;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import org.neo4j.driver.internal.async.inbound.InboundMessageDispatcher;
import org.neo4j.driver.internal.async.pool.ExtendedChannelPool;
import org.neo4j.driver.internal.util.Clock;

public class ChannelReleasingResetResponseHandler extends ResetResponseHandler {
private final Channel channel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static java.lang.String.format;
import static java.util.Collections.unmodifiableCollection;

import java.time.Clock;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
Expand All @@ -30,7 +31,6 @@
import org.neo4j.driver.Logger;
import org.neo4j.driver.Logging;
import org.neo4j.driver.Metrics;
import org.neo4j.driver.internal.util.Clock;
import org.neo4j.driver.net.ServerAddress;

final class InternalMetrics implements Metrics, MetricsListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
*/
package org.neo4j.driver.internal.metrics;

import java.time.Clock;
import org.neo4j.driver.Logging;
import org.neo4j.driver.Metrics;
import org.neo4j.driver.internal.util.Clock;

public final class InternalMetricsProvider implements MetricsProvider {
private final InternalMetrics metrics;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.neo4j.driver.internal.metrics;

import org.neo4j.driver.internal.util.Clock;
import java.time.Clock;

final class TimeRecorderListenerEvent implements ListenerEvent<Long> {
private final Clock clock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import io.netty.util.concurrent.EventExecutor;
import io.netty.util.concurrent.EventExecutorGroup;
import java.time.Clock;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -35,7 +36,6 @@
import org.neo4j.driver.exceptions.ClientException;
import org.neo4j.driver.exceptions.Neo4jException;
import org.neo4j.driver.exceptions.RetryableException;
import org.neo4j.driver.internal.util.Clock;
import org.neo4j.driver.internal.util.Futures;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
Expand All @@ -58,18 +58,29 @@ public class ExponentialBackoffRetryLogic implements RetryLogic {
private final double jitterFactor;
private final EventExecutorGroup eventExecutorGroup;
private final Clock clock;
private final SleepTask sleepTask;
private final Logger log;

public ExponentialBackoffRetryLogic(
long maxTransactionRetryTime, EventExecutorGroup eventExecutorGroup, Clock clock, Logging logging) {
this(maxTransactionRetryTime, eventExecutorGroup, clock, logging, Thread::sleep);
}

protected ExponentialBackoffRetryLogic(
long maxTransactionRetryTime,
EventExecutorGroup eventExecutorGroup,
Clock clock,
Logging logging,
SleepTask sleepTask) {
this(
maxTransactionRetryTime,
INITIAL_RETRY_DELAY_MS,
RETRY_DELAY_MULTIPLIER,
RETRY_DELAY_JITTER_FACTOR,
eventExecutorGroup,
clock,
logging);
logging,
sleepTask);
}

ExponentialBackoffRetryLogic(
Expand All @@ -79,13 +90,15 @@ public ExponentialBackoffRetryLogic(
double jitterFactor,
EventExecutorGroup eventExecutorGroup,
Clock clock,
Logging logging) {
Logging logging,
SleepTask sleepTask) {
this.maxRetryTimeMs = maxRetryTimeMs;
this.initialRetryDelayMs = initialRetryDelayMs;
this.multiplier = multiplier;
this.jitterFactor = jitterFactor;
this.eventExecutorGroup = eventExecutorGroup;
this.clock = clock;
this.sleepTask = sleepTask;
this.log = logging.getLog(getClass());

verifyAfterConstruction();
Expand Down Expand Up @@ -300,7 +313,7 @@ private long computeDelayWithJitter(long delayMs) {

private void sleep(long delayMs) {
try {
clock.sleep(delayMs);
sleepTask.sleep(delayMs);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException("Retries interrupted", e);
Expand Down Expand Up @@ -342,4 +355,9 @@ private static void addSuppressed(Throwable error, List<Throwable> suppressedErr
}
}
}

@FunctionalInterface
public interface SleepTask {
void sleep(long millis) throws InterruptedException;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@

import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;
import java.time.Clock;
import org.neo4j.driver.Config;
import org.neo4j.driver.MetricsAdapter;
import org.neo4j.driver.internal.DriverFactory;
import org.neo4j.driver.internal.metrics.DevNullMetricsProvider;
import org.neo4j.driver.internal.metrics.InternalMetricsProvider;
import org.neo4j.driver.internal.metrics.MetricsProvider;
import org.neo4j.driver.internal.metrics.MicrometerMetricsProvider;
import org.neo4j.driver.internal.util.Clock;

@TargetClass(DriverFactory.class)
final class Target_org_neo4j_driver_internal_DriverFactory {
Expand Down
41 changes: 0 additions & 41 deletions driver/src/main/java/org/neo4j/driver/internal/util/Clock.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import static org.neo4j.driver.testutil.TestUtil.await;

import io.netty.bootstrap.Bootstrap;
import java.time.Clock;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
Expand Down Expand Up @@ -68,7 +69,6 @@
import org.neo4j.driver.internal.security.SecurityPlanImpl;
import org.neo4j.driver.internal.spi.Connection;
import org.neo4j.driver.internal.spi.ConnectionPool;
import org.neo4j.driver.internal.util.Clock;
import org.neo4j.driver.internal.util.EnabledOnNeo4jWith;
import org.neo4j.driver.reactive.RxResult;
import org.neo4j.driver.reactive.RxSession;
Expand Down
Loading

0 comments on commit 7d0e75c

Please sign in to comment.