Skip to content
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

Move server info into result summary #272

Merged
merged 2 commits into from
Nov 18, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import java.util.List;
import java.util.Queue;

import org.neo4j.driver.internal.spi.Connection;
import org.neo4j.driver.internal.spi.Collector;
import org.neo4j.driver.internal.spi.Connection;
import org.neo4j.driver.internal.summary.SummaryBuilder;
import org.neo4j.driver.v1.Record;
import org.neo4j.driver.v1.Statement;
Expand All @@ -37,6 +37,7 @@
import org.neo4j.driver.v1.summary.Plan;
import org.neo4j.driver.v1.summary.ProfiledPlan;
import org.neo4j.driver.v1.summary.ResultSummary;
import org.neo4j.driver.v1.summary.ServerInfo;
import org.neo4j.driver.v1.summary.StatementType;
import org.neo4j.driver.v1.summary.SummaryCounters;
import org.neo4j.driver.v1.util.Function;
Expand All @@ -61,7 +62,7 @@ public class InternalStatementResult implements StatementResult
{
this.connection = connection;
this.runResponseCollector = newRunResponseCollector();
this.pullAllResponseCollector = newStreamResponseCollector( transaction, statement );
this.pullAllResponseCollector = newStreamResponseCollector( transaction, statement, connection.server() );
}

private Collector newRunResponseCollector()
Expand Down Expand Up @@ -91,9 +92,10 @@ public void resultAvailableAfter( long l )
};
}

private Collector newStreamResponseCollector( final ExplicitTransaction transaction, final Statement statement )
private Collector newStreamResponseCollector( final ExplicitTransaction transaction, final Statement statement,
final ServerInfo serverInfo )
{
final SummaryBuilder summaryBuilder = new SummaryBuilder( statement );
final SummaryBuilder summaryBuilder = new SummaryBuilder( statement, serverInfo );

return new Collector.NoOperationCollector()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,6 @@ private void closeConnection()
connection.close();
}

@Override
public String server()
{
return connection.server();
}

@Override
public Transaction beginTransaction()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.neo4j.driver.internal.spi.ConnectionPool;
import org.neo4j.driver.internal.util.Clock;
import org.neo4j.driver.v1.AccessMode;
import org.neo4j.driver.v1.Config;
import org.neo4j.driver.v1.Logging;
import org.neo4j.driver.v1.Session;
import org.neo4j.driver.v1.exceptions.ClientException;
Expand Down Expand Up @@ -70,7 +69,7 @@ public Session session()
public Session session( final AccessMode mode )
{
Connection connection = acquireConnection( mode );
return new RoutingNetworkSession( new NetworkSession( connection ), mode, connection.address(), loadBalancer );
return new RoutingNetworkSession( new NetworkSession( connection ), mode, connection.boltServerAddress(), loadBalancer );
}

private Connection acquireConnection( AccessMode role )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,6 @@ public void close()
}
}

@Override
public String server()
{
return delegate.server();
}

public BoltServerAddress address()
{
return address;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.neo4j.driver.v1.Logger;
import org.neo4j.driver.v1.Value;
import org.neo4j.driver.v1.exceptions.ClientException;
import org.neo4j.driver.v1.summary.ServerInfo;

/**
* This class ensures there can only ever be one thread using a connection at
Expand Down Expand Up @@ -231,15 +232,15 @@ private void markAsInUse()
}

@Override
public String server()
public ServerInfo server()
{
return delegate.server();
}

@Override
public BoltServerAddress address()
public BoltServerAddress boltServerAddress()
{
return delegate.address();
return delegate.boltServerAddress();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@
import org.neo4j.driver.internal.security.SecurityPlan;
import org.neo4j.driver.internal.spi.Collector;
import org.neo4j.driver.internal.spi.Connection;
import org.neo4j.driver.internal.summary.InternalServerInfo;
import org.neo4j.driver.v1.Logger;
import org.neo4j.driver.v1.Logging;
import org.neo4j.driver.v1.Value;
import org.neo4j.driver.v1.exceptions.ClientException;
import org.neo4j.driver.v1.exceptions.Neo4jException;
import org.neo4j.driver.v1.summary.ServerInfo;

import static java.lang.String.format;
import static org.neo4j.driver.internal.messaging.AckFailureMessage.ACK_FAILURE;
Expand All @@ -50,7 +52,7 @@ public class SocketConnection implements Connection
private final SocketResponseHandler responseHandler;
private AtomicBoolean isInterrupted = new AtomicBoolean( false );
private AtomicBoolean isAckFailureMuted = new AtomicBoolean( false );
private final Collector.InitCollector initCollector = new Collector.InitCollector();
private InternalServerInfo serverInfo;

private final SocketClient socket;

Expand All @@ -59,25 +61,39 @@ public class SocketConnection implements Connection
public SocketConnection( BoltServerAddress address, SecurityPlan securityPlan, Logging logging )
{
this.logger = logging.getLog( format( "conn-%s", UUID.randomUUID().toString() ) );
this.socket = new SocketClient( address, securityPlan, logger );
this.responseHandler = createResponseHandler( logger );
this.socket.start();
}

// for mocked socket testing
SocketConnection( SocketClient socket, Logger logger )
{
this.socket = socket;
this.logger = logger;
this.responseHandler = createResponseHandler( logger );
this.socket.start();
}

private SocketResponseHandler createResponseHandler( Logger logger )
{
if( logger.isDebugEnabled() )
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe extract this if-else in a method like createResponseHandler(Logger), otherwise it is duplicated in both constructors.

{
this.responseHandler = new LoggingResponseHandler( logger );
return new LoggingResponseHandler( logger );
}
else
{
this.responseHandler = new SocketResponseHandler();
return new SocketResponseHandler();
}

this.socket = new SocketClient( address, securityPlan, logger );
socket.start();
}

@Override
public void init( String clientName, Map<String,Value> authToken )
{
Collector.InitCollector initCollector = new Collector.InitCollector();
queueMessage( new InitMessage( clientName, authToken ), initCollector );
sync();
this.serverInfo = new InternalServerInfo( socket.address(), initCollector.serverVersion() );
}

@Override
Expand Down Expand Up @@ -267,15 +283,15 @@ public boolean isAckFailureMuted()
}

@Override
public String server()
public ServerInfo server()
{
return initCollector.server( );
return this.serverInfo;
}

@Override
public BoltServerAddress address()
public BoltServerAddress boltServerAddress()
{
return this.socket.address();
return this.serverInfo.boltServerAddress();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,24 @@ public void handleFailureMessage( String code, String message )
public void handleSuccessMessage( Map<String,Value> meta )
{
Collector collector = collectors.remove();
collectServer( collector, meta.get( "server" ));
collectServerVersion( collector, meta.get( "server" ) );
collectFields( collector, meta.get( "fields" ) );
collectType( collector, meta.get( "type" ) );
collectStatistics( collector, meta.get( "stats" ) );
collectPlan( collector, meta.get( "plan" ) );
collectProfile( collector, meta.get( "profile" ) );
collectNotifications( collector, meta.get( "notifications" ) );
collectResultAvailableAfter( collector, meta.get("result_available_after"));
collectResultConsumedAfter( collector, meta.get("result_consumed_after"));
collectResultAvailableAfter( collector, meta.get("result_available_after") );
collectResultConsumedAfter( collector, meta.get("result_consumed_after") );
collectBookmark( collector, meta.get( "bookmark" ) );
collector.doneSuccess();
}

private void collectServer( Collector collector, Value server )
private void collectServerVersion( Collector collector, Value serverVersion )
{
if (server != null)
if ( serverVersion != null )
{
collector.server( server.asString() );
collector.serverVersion( serverVersion.asString() );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.neo4j.driver.v1.Logger;
import org.neo4j.driver.v1.Value;
import org.neo4j.driver.v1.exceptions.Neo4jException;
import org.neo4j.driver.v1.summary.ServerInfo;

/**
* The state of a pooledConnection from a pool point of view could be one of the following:
* Created,
Expand Down Expand Up @@ -233,15 +235,15 @@ public boolean isAckFailureMuted()
}

@Override
public String server()
public ServerInfo server()
{
return delegate.server();
}

@Override
public BoltServerAddress address()
public BoltServerAddress boltServerAddress()
{
return delegate.address();
return delegate.boltServerAddress();
}

@Override
Expand All @@ -257,7 +259,7 @@ public void dispose()

/**
* If something goes wrong with the delegate, we want to figure out if this "wrong" is something that means
* the connection is screwed (and thus should be evicted from the pool), or if it's something that we can
* the connection cannot be reused (and thus should be evicted from the pool), or if it's something that we can
* safely recover from.
* @param e the exception the delegate threw
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Boolean apply( PooledConnection pooledConnection )
{
// once the pooledConn has marked to have unrecoverable errors, there is no way to remove the error
// and we should close the conn without bothering to reset the conn at all
return pool.hasAddress( pooledConnection.address() ) &&
return pool.hasAddress( pooledConnection.boltServerAddress() ) &&
!pooledConnection.hasUnrecoverableErrors() &&
reset( pooledConnection ) &&
(pooledConnection.idleTime() <= poolSettings.idleTimeBeforeConnectionTest() ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void doneFailure( Neo4jException error )

class InitCollector extends NoOperationCollector
{
private String server;
private String serverVersion;
@Override
public void doneIgnored()
{
Expand All @@ -54,14 +54,14 @@ public void doneIgnored()
}

@Override
public void server( String server )
public void serverVersion( String serverVersion )
{
this.server = server;
this.serverVersion = serverVersion;
}

public String server()
public String serverVersion()
{
return server;
return serverVersion;
}
}

Expand Down Expand Up @@ -160,7 +160,7 @@ public void resultAvailableAfter( long l ) {}
public void resultConsumedAfter( long l ) {}

@Override
public void server( String server ){}
public void serverVersion( String server ){}
}

// TODO: This should be modified to simply have head/record/tail methods
Expand Down Expand Up @@ -193,6 +193,6 @@ public void server( String server ){}

void resultConsumedAfter( long l );

void server( String server );
void serverVersion( String server );
}

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.neo4j.driver.internal.net.BoltServerAddress;
import org.neo4j.driver.v1.Logger;
import org.neo4j.driver.v1.Value;
import org.neo4j.driver.v1.summary.ServerInfo;

/**
* A connection is an abstraction provided by an underlying transport implementation,
Expand Down Expand Up @@ -121,15 +122,15 @@ public interface Connection extends AutoCloseable
boolean isAckFailureMuted();

/**
* Returns the version of the server connected to.
* @return The version of the server connected to.
* Returns the basic information of the server connected to.
* @return The basic information of the server connected to.
*/
String server();
ServerInfo server();

/**
* Returns the BoltServerAddress connected to
*/
BoltServerAddress address();
BoltServerAddress boltServerAddress();

/**
* Returns the logger of this connection
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.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.summary;

import org.neo4j.driver.internal.net.BoltServerAddress;
import org.neo4j.driver.v1.summary.ServerInfo;

public class InternalServerInfo implements ServerInfo
{
private final BoltServerAddress address;
private final String version;

public InternalServerInfo(BoltServerAddress address, String version)
{
this.address = address;
this.version = version;
}

public BoltServerAddress boltServerAddress()
{
return this.address;
}

@Override
public String address()
{
return this.address.toString();
}

@Override
public String version()
{
return version;
}
}
Loading