Skip to content

Commit aaf817e

Browse files
author
Zhen Li
committed
Merge branch '1.7' into 2.0
2 parents ff9a7ab + 742d280 commit aaf817e

40 files changed

+859
-128
lines changed

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.net.SocketAddress;
2424
import java.net.URI;
2525
import java.net.UnknownHostException;
26+
import java.util.Objects;
2627

2728
import org.neo4j.driver.v1.net.ServerAddress;
2829

@@ -36,7 +37,8 @@ public class BoltServerAddress implements ServerAddress
3637
public static final int DEFAULT_PORT = 7687;
3738
public static final BoltServerAddress LOCAL_DEFAULT = new BoltServerAddress( "localhost", DEFAULT_PORT );
3839

39-
private final String host;
40+
private final String originalHost; // This keeps the original host name provided by the user.
41+
private final String host; // This could either be the same as originalHost or it is an IP address resolved from the original host.
4042
private final int port;
4143
private final String stringValue;
4244

@@ -52,6 +54,12 @@ public BoltServerAddress( URI uri )
5254

5355
public BoltServerAddress( String host, int port )
5456
{
57+
this( host, host, port );
58+
}
59+
60+
public BoltServerAddress( String originalHost, String host, int port )
61+
{
62+
this.originalHost = requireNonNull( originalHost, "original host" );
5563
this.host = requireNonNull( host, "host" );
5664
this.port = requireValidPort( port );
5765
this.stringValue = String.format( "%s:%d", host, port );
@@ -76,13 +84,13 @@ public boolean equals( Object o )
7684
return false;
7785
}
7886
BoltServerAddress that = (BoltServerAddress) o;
79-
return port == that.port && host.equals( that.host );
87+
return port == that.port && originalHost.equals( that.originalHost ) && host.equals( that.host );
8088
}
8189

8290
@Override
8391
public int hashCode()
8492
{
85-
return 31 * host.hashCode() + port;
93+
return Objects.hash( originalHost, host, port );
8694
}
8795

8896
@Override
@@ -112,14 +120,14 @@ public SocketAddress toSocketAddress()
112120
*/
113121
public BoltServerAddress resolve() throws UnknownHostException
114122
{
115-
String hostAddress = InetAddress.getByName( host ).getHostAddress();
116-
if ( hostAddress.equals( host ) )
123+
String ipAddress = InetAddress.getByName( host ).getHostAddress();
124+
if ( ipAddress.equals( host ) )
117125
{
118126
return this;
119127
}
120128
else
121129
{
122-
return new BoltServerAddress( hostAddress, port );
130+
return new BoltServerAddress( host, ipAddress, port );
123131
}
124132
}
125133

@@ -129,6 +137,11 @@ public String host()
129137
return host;
130138
}
131139

140+
public String originalHost()
141+
{
142+
return originalHost;
143+
}
144+
132145
@Override
133146
public int port()
134147
{

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.util.concurrent.CompletionStage;
2222

23+
import org.neo4j.driver.internal.async.AccessModeConnection;
2324
import org.neo4j.driver.internal.spi.Connection;
2425
import org.neo4j.driver.internal.spi.ConnectionPool;
2526
import org.neo4j.driver.internal.spi.ConnectionProvider;
@@ -45,7 +46,7 @@ public class DirectConnectionProvider implements ConnectionProvider
4546
@Override
4647
public CompletionStage<Connection> acquireConnection( AccessMode mode )
4748
{
48-
return connectionPool.acquire( address );
49+
return connectionPool.acquire( address ).thenApply( connection -> new AccessModeConnection( connection, mode ) );
4950
}
5051

5152
@Override
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Copyright (c) 2002-2019 "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.async;
20+
21+
import java.util.concurrent.CompletionStage;
22+
23+
import org.neo4j.driver.internal.BoltServerAddress;
24+
import org.neo4j.driver.internal.messaging.BoltProtocol;
25+
import org.neo4j.driver.internal.messaging.Message;
26+
import org.neo4j.driver.internal.spi.Connection;
27+
import org.neo4j.driver.internal.spi.ResponseHandler;
28+
import org.neo4j.driver.internal.util.ServerVersion;
29+
import org.neo4j.driver.v1.AccessMode;
30+
31+
public class AccessModeConnection implements Connection
32+
{
33+
private final Connection delegate;
34+
private final AccessMode mode;
35+
36+
public AccessModeConnection( Connection delegate, AccessMode mode )
37+
{
38+
this.delegate = delegate;
39+
this.mode = mode;
40+
}
41+
42+
public Connection connection()
43+
{
44+
return delegate;
45+
}
46+
47+
@Override
48+
public boolean isOpen()
49+
{
50+
return delegate.isOpen();
51+
}
52+
53+
@Override
54+
public void enableAutoRead()
55+
{
56+
delegate.enableAutoRead();
57+
}
58+
59+
@Override
60+
public void disableAutoRead()
61+
{
62+
delegate.disableAutoRead();
63+
}
64+
65+
@Override
66+
public void write( Message message, ResponseHandler handler )
67+
{
68+
delegate.write( message, handler );
69+
}
70+
71+
@Override
72+
public void write( Message message1, ResponseHandler handler1, Message message2, ResponseHandler handler2 )
73+
{
74+
delegate.write( message1, handler1, message2, handler2 );
75+
}
76+
77+
@Override
78+
public void writeAndFlush( Message message, ResponseHandler handler )
79+
{
80+
delegate.writeAndFlush( message, handler );
81+
}
82+
83+
@Override
84+
public void writeAndFlush( Message message1, ResponseHandler handler1, Message message2, ResponseHandler handler2 )
85+
{
86+
delegate.writeAndFlush( message1, handler1, message2, handler2 );
87+
}
88+
89+
@Override
90+
public CompletionStage<Void> reset()
91+
{
92+
return delegate.reset();
93+
}
94+
95+
@Override
96+
public CompletionStage<Void> release()
97+
{
98+
return delegate.release();
99+
}
100+
101+
@Override
102+
public void terminateAndRelease( String reason )
103+
{
104+
delegate.terminateAndRelease( reason );
105+
}
106+
107+
@Override
108+
public BoltServerAddress serverAddress()
109+
{
110+
return delegate.serverAddress();
111+
}
112+
113+
@Override
114+
public ServerVersion serverVersion()
115+
{
116+
return delegate.serverVersion();
117+
}
118+
119+
@Override
120+
public BoltProtocol protocol()
121+
{
122+
return delegate.protocol();
123+
}
124+
125+
@Override
126+
public AccessMode mode()
127+
{
128+
return mode;
129+
}
130+
}

driver/src/main/java/org/neo4j/driver/internal/async/NettyChannelInitializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private SslHandler createSslHandler()
7777
private SSLEngine createSslEngine()
7878
{
7979
SSLContext sslContext = securityPlan.sslContext();
80-
SSLEngine sslEngine = sslContext.createSSLEngine( address.host(), address.port() );
80+
SSLEngine sslEngine = sslContext.createSSLEngine( address.originalHost(), address.port() );
8181
sslEngine.setUseClientMode( true );
8282
if ( securityPlan.requiresHostnameVerification() )
8383
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public Set<ServerAddress> resolve( ServerAddress initialRouter )
4747
try
4848
{
4949
return Stream.of( InetAddress.getAllByName( initialRouter.host() ) )
50-
.map( address -> new BoltServerAddress( address.getHostAddress(), initialRouter.port() ) )
50+
.map( address -> new BoltServerAddress( initialRouter.host(), address.getHostAddress(), initialRouter.port() ) )
5151
.collect( toSet() );
5252
}
5353
catch ( UnknownHostException e )

driver/src/main/java/org/neo4j/driver/internal/cluster/loadbalancing/LoadBalancer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.neo4j.driver.internal.BoltServerAddress;
2727
import org.neo4j.driver.internal.RoutingErrorHandler;
28+
import org.neo4j.driver.internal.async.AccessModeConnection;
2829
import org.neo4j.driver.internal.async.RoutingConnection;
2930
import org.neo4j.driver.internal.cluster.AddressSet;
3031
import org.neo4j.driver.internal.cluster.ClusterComposition;
@@ -95,7 +96,8 @@ public CompletionStage<Connection> acquireConnection( AccessMode mode )
9596
{
9697
return freshRoutingTable( mode )
9798
.thenCompose( routingTable -> acquire( mode, routingTable ) )
98-
.thenApply( connection -> new RoutingConnection( connection, mode, this ) );
99+
.thenApply( connection -> new RoutingConnection( connection, mode, this ) )
100+
.thenApply( connection -> new AccessModeConnection( connection, mode ) );
99101
}
100102

101103
@Override

driver/src/main/java/org/neo4j/driver/internal/messaging/request/BeginMessage.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,22 @@
2323
import java.util.Objects;
2424

2525
import org.neo4j.driver.internal.Bookmarks;
26+
import org.neo4j.driver.v1.AccessMode;
2627
import org.neo4j.driver.v1.TransactionConfig;
2728
import org.neo4j.driver.v1.Value;
2829

2930
public class BeginMessage extends TransactionStartingMessage
3031
{
3132
public static final byte SIGNATURE = 0x11;
3233

33-
public BeginMessage( Bookmarks bookmarks, TransactionConfig config )
34+
public BeginMessage( Bookmarks bookmarks, TransactionConfig config, AccessMode mode )
3435
{
35-
this( bookmarks, config.timeout(), config.metadata() );
36+
this( bookmarks, config.timeout(), config.metadata(), mode );
3637
}
3738

38-
public BeginMessage( Bookmarks bookmarks, Duration txTimeout, Map<String,Value> txMetadata )
39+
public BeginMessage( Bookmarks bookmarks, Duration txTimeout, Map<String,Value> txMetadata, AccessMode mode )
3940
{
40-
super( bookmarks, txTimeout, txMetadata );
41+
super( bookmarks, txTimeout, txMetadata, mode );
4142
}
4243

4344
@Override

driver/src/main/java/org/neo4j/driver/internal/messaging/request/RunWithMetadataMessage.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Objects;
2424

2525
import org.neo4j.driver.internal.Bookmarks;
26+
import org.neo4j.driver.v1.AccessMode;
2627
import org.neo4j.driver.v1.TransactionConfig;
2728
import org.neo4j.driver.v1.Value;
2829

@@ -33,14 +34,15 @@ public class RunWithMetadataMessage extends TransactionStartingMessage
3334
private final String statement;
3435
private final Map<String,Value> parameters;
3536

36-
public RunWithMetadataMessage( String statement, Map<String,Value> parameters, Bookmarks bookmarks, TransactionConfig config )
37+
public RunWithMetadataMessage( String statement, Map<String,Value> parameters, Bookmarks bookmarks, TransactionConfig config, AccessMode mode )
3738
{
38-
this( statement, parameters, bookmarks, config.timeout(), config.metadata() );
39+
this( statement, parameters, bookmarks, config.timeout(), config.metadata(), mode );
3940
}
4041

41-
public RunWithMetadataMessage( String statement, Map<String,Value> parameters, Bookmarks bookmarks, Duration txTimeout, Map<String,Value> txMetadata )
42+
public RunWithMetadataMessage( String statement, Map<String,Value> parameters, Bookmarks bookmarks, Duration txTimeout, Map<String,Value> txMetadata,
43+
AccessMode mode )
4244
{
43-
super( bookmarks, txTimeout, txMetadata );
45+
super( bookmarks, txTimeout, txMetadata, mode );
4446
this.statement = statement;
4547
this.parameters = parameters;
4648
}

driver/src/main/java/org/neo4j/driver/internal/messaging/request/TransactionStartingMessage.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.neo4j.driver.internal.Bookmarks;
2525
import org.neo4j.driver.internal.messaging.Message;
2626
import org.neo4j.driver.internal.util.Iterables;
27+
import org.neo4j.driver.v1.AccessMode;
2728
import org.neo4j.driver.v1.Value;
2829

2930
import static java.util.Collections.emptyMap;
@@ -34,26 +35,29 @@ abstract class TransactionStartingMessage implements Message
3435
private static final String BOOKMARKS_METADATA_KEY = "bookmarks";
3536
private static final String TX_TIMEOUT_METADATA_KEY = "tx_timeout";
3637
private static final String TX_METADATA_METADATA_KEY = "tx_metadata";
38+
private static final String MODE_KEY = "mode";
39+
private static final String MODE_READ_VALUE = "r";
3740

3841
final Map<String,Value> metadata;
3942

40-
TransactionStartingMessage( Bookmarks bookmarks, Duration txTimeout, Map<String,Value> txMetadata )
43+
TransactionStartingMessage( Bookmarks bookmarks, Duration txTimeout, Map<String,Value> txMetadata, AccessMode mode )
4144
{
42-
this.metadata = buildMetadata( bookmarks, txTimeout, txMetadata );
45+
this.metadata = buildMetadata( bookmarks, txTimeout, txMetadata, mode );
4346
}
4447

4548
public final Map<String,Value> metadata()
4649
{
4750
return metadata;
4851
}
4952

50-
private static Map<String,Value> buildMetadata( Bookmarks bookmarks, Duration txTimeout, Map<String,Value> txMetadata )
53+
private static Map<String,Value> buildMetadata( Bookmarks bookmarks, Duration txTimeout, Map<String,Value> txMetadata, AccessMode mode )
5154
{
5255
boolean bookmarksPresent = bookmarks != null && !bookmarks.isEmpty();
5356
boolean txTimeoutPresent = txTimeout != null;
5457
boolean txMetadataPresent = txMetadata != null && !txMetadata.isEmpty();
58+
boolean accessModePresent = mode == AccessMode.READ;
5559

56-
if ( !bookmarksPresent && !txTimeoutPresent && !txMetadataPresent )
60+
if ( !bookmarksPresent && !txTimeoutPresent && !txMetadataPresent && !accessModePresent )
5761
{
5862
return emptyMap();
5963
}
@@ -73,6 +77,13 @@ private static Map<String,Value> buildMetadata( Bookmarks bookmarks, Duration tx
7377
result.put( TX_METADATA_METADATA_KEY, value( txMetadata ) );
7478
}
7579

80+
switch ( mode )
81+
{
82+
case READ:
83+
result.put( MODE_KEY, value( MODE_READ_VALUE ) );
84+
break;
85+
}
86+
7687
return result;
7788
}
7889
}

driver/src/main/java/org/neo4j/driver/internal/messaging/v3/BoltProtocolV3.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void prepareToCloseChannel( Channel channel )
9696
@Override
9797
public CompletionStage<Void> beginTransaction( Connection connection, Bookmarks bookmarks, TransactionConfig config )
9898
{
99-
BeginMessage beginMessage = new BeginMessage( bookmarks, config );
99+
BeginMessage beginMessage = new BeginMessage( bookmarks, config, connection.mode() );
100100

101101
if ( bookmarks.isEmpty() )
102102
{
@@ -148,7 +148,7 @@ private static CompletionStage<InternalStatementResultCursor> runStatement( Conn
148148
Map<String,Value> params = statement.parameters().asMap( ofValue() );
149149

150150
CompletableFuture<Void> runCompletedFuture = new CompletableFuture<>();
151-
Message runMessage = new RunWithMetadataMessage( query, params, bookmarksHolder.getBookmarks(), config );
151+
Message runMessage = new RunWithMetadataMessage( query, params, bookmarksHolder.getBookmarks(), config, connection.mode() );
152152
RunResponseHandler runHandler = new RunResponseHandler( runCompletedFuture, METADATA_EXTRACTOR );
153153
PullAllResponseHandler pullAllHandler = newPullAllHandler( statement, runHandler, connection, bookmarksHolder, tx );
154154

0 commit comments

Comments
 (0)