Skip to content

Draft of driver metrics #470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 1, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions driver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
<groupId>io.netty</groupId>
<artifactId>netty-handler</artifactId>
</dependency>
<dependency>
<groupId>org.hdrhistogram</groupId>
<artifactId>HdrHistogram</artifactId>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand Down Expand Up @@ -208,13 +212,18 @@
<artifactSet>
<includes>
<include>io.netty:*</include>
<include>org.HdrHistogram:*</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>io.netty</pattern>
<shadedPattern>org.neo4j.driver.internal.shaded.io.netty</shadedPattern>
</relocation>
<relocation>
<pattern>org.HdrHistogram</pattern>
<shadedPattern>org.neo4j.driver.internal.shaded.org.HdrHistogram</shadedPattern>
</relocation>
</relocations>
<shadeTestJar>true</shadeTestJar>
</configuration>
Expand Down
26 changes: 22 additions & 4 deletions driver/src/main/java/org/neo4j/driver/internal/DriverFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
import org.neo4j.driver.internal.cluster.loadbalancing.LoadBalancingStrategy;
import org.neo4j.driver.internal.cluster.loadbalancing.RoundRobinLoadBalancingStrategy;
import org.neo4j.driver.internal.logging.NettyLogging;
import org.neo4j.driver.internal.metrics.DriverMetricsListener;
import org.neo4j.driver.internal.metrics.InternalAbstractDriverMetrics;
import org.neo4j.driver.internal.metrics.InternalDriverMetrics;
import org.neo4j.driver.internal.retry.ExponentialBackoffRetryLogic;
import org.neo4j.driver.internal.retry.RetryLogic;
import org.neo4j.driver.internal.retry.RetrySettings;
Expand All @@ -56,6 +59,8 @@
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;

import static java.lang.String.format;
import static org.neo4j.driver.internal.metrics.InternalAbstractDriverMetrics.DEV_NULL_METRICS;
import static org.neo4j.driver.internal.metrics.spi.DriverMetrics.isDriverMetricsEnabled;
import static org.neo4j.driver.internal.security.SecurityPlan.insecure;

public class DriverFactory
Expand All @@ -77,18 +82,19 @@ public final Driver newInstance( URI uri, AuthToken authToken, RoutingSettings r
EventExecutorGroup eventExecutorGroup = bootstrap.config().group();
RetryLogic retryLogic = createRetryLogic( retrySettings, eventExecutorGroup, config.logging() );

ConnectionPool connectionPool = createConnectionPool( authToken, securityPlan, bootstrap, config );
InternalAbstractDriverMetrics metrics = createDriverMetrics( config );
ConnectionPool connectionPool = createConnectionPool( authToken, securityPlan, bootstrap, metrics, config );

InternalDriver driver = createDriver( uri, address, connectionPool, config, newRoutingSettings,
eventExecutorGroup, securityPlan, retryLogic );

driver.driverMetrics( metrics );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to pass metrics into the driver constructor and avoid this setter.

verifyConnectivity( driver, connectionPool, config );

return driver;
}

protected ConnectionPool createConnectionPool( AuthToken authToken, SecurityPlan securityPlan,
Bootstrap bootstrap, Config config )
protected ConnectionPool createConnectionPool( AuthToken authToken, SecurityPlan securityPlan, Bootstrap bootstrap, DriverMetricsListener metrics, Config config )
{
Clock clock = createClock();
ConnectionSettings settings = new ConnectionSettings( authToken, config.connectionTimeoutMillis() );
Expand All @@ -97,7 +103,19 @@ protected ConnectionPool createConnectionPool( AuthToken authToken, SecurityPlan
config.connectionAcquisitionTimeoutMillis(), config.maxConnectionLifetimeMillis(),
config.idleTimeBeforeConnectionTest()
);
return new ConnectionPoolImpl( connector, bootstrap, poolSettings, config.logging(), clock );
return new ConnectionPoolImpl( connector, bootstrap, poolSettings, metrics, config.logging(), clock );
}

protected static InternalAbstractDriverMetrics createDriverMetrics( Config config )
{
if( isDriverMetricsEnabled() )
{
return new InternalDriverMetrics( config );
}
else
{
return DEV_NULL_METRICS;
}
}

protected ChannelConnector createConnector( ConnectionSettings settings, SecurityPlan securityPlan,
Expand Down
12 changes: 12 additions & 0 deletions driver/src/main/java/org/neo4j/driver/internal/InternalDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.concurrent.CompletionStage;
import java.util.concurrent.atomic.AtomicBoolean;

import org.neo4j.driver.internal.metrics.spi.DriverMetrics;
import org.neo4j.driver.internal.security.SecurityPlan;
import org.neo4j.driver.internal.util.Futures;
import org.neo4j.driver.v1.AccessMode;
Expand All @@ -38,6 +39,7 @@ public class InternalDriver implements Driver
private final Logger log;

private AtomicBoolean closed = new AtomicBoolean( false );
private DriverMetrics driverMetrics;

InternalDriver( SecurityPlan securityPlan, SessionFactory sessionFactory, Logging logging )
{
Expand Down Expand Up @@ -144,6 +146,16 @@ private void assertOpen()
}
}

public DriverMetrics driverMetrics()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe #metrics()?

{
return this.driverMetrics;
}

void driverMetrics( DriverMetrics driverMetrics )
{
this.driverMetrics = driverMetrics;
}

private static RuntimeException driverCloseException()
{
return new IllegalStateException( "This driver instance has already been closed" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import org.neo4j.driver.internal.messaging.PullAllMessage;
import org.neo4j.driver.internal.messaging.ResetMessage;
import org.neo4j.driver.internal.messaging.RunMessage;
import org.neo4j.driver.internal.metrics.DriverMetricsListener;
import org.neo4j.driver.internal.metrics.ListenerEvent.ConnectionListenerEvent;
import org.neo4j.driver.internal.spi.Connection;
import org.neo4j.driver.internal.spi.ResponseHandler;
import org.neo4j.driver.internal.util.Clock;
Expand All @@ -54,8 +56,10 @@ public class NettyConnection implements Connection
private final Clock clock;

private final AtomicReference<Status> status = new AtomicReference<>( Status.OPEN );
private final DriverMetricsListener metricsListener;
private final ConnectionListenerEvent inUseEvent;

public NettyConnection( Channel channel, ChannelPool channelPool, Clock clock )
public NettyConnection( Channel channel, ChannelPool channelPool, Clock clock, DriverMetricsListener driverMetricsListener )
{
this.channel = channel;
this.messageDispatcher = ChannelAttributes.messageDispatcher( channel );
Expand All @@ -64,6 +68,9 @@ public NettyConnection( Channel channel, ChannelPool channelPool, Clock clock )
this.channelPool = channelPool;
this.releaseFuture = new CompletableFuture<>();
this.clock = clock;
this.metricsListener = driverMetricsListener;
this.inUseEvent = driverMetricsListener.createConnectionListenerEvent();
driverMetricsListener.afterAcquiredOrCreated( this.serverAddress, this.inUseEvent );
}

@Override
Expand Down Expand Up @@ -124,6 +131,7 @@ public CompletionStage<Void> release()
{
if ( status.compareAndSet( Status.OPEN, Status.RELEASED ) )
{
metricsListener.afterReleased( this.serverAddress, this.inUseEvent );
ChannelReleasingResetResponseHandler handler = new ChannelReleasingResetResponseHandler( channel,
channelPool, messageDispatcher, clock, releaseFuture );

Expand All @@ -137,6 +145,7 @@ public void terminateAndRelease( String reason )
{
if ( status.compareAndSet( Status.OPEN, Status.TERMINATED ) )
{
metricsListener.afterReleased( this.serverAddress, this.inUseEvent );
setTerminationReason( channel, reason );
channel.close();
channelPool.release( channel );
Expand Down

This file was deleted.

Loading