Skip to content

Commit 5bc7c2c

Browse files
committed
Merge branch 1.5 into 1.6
2 parents a294307 + ef48d78 commit 5bc7c2c

File tree

14 files changed

+313
-237
lines changed

14 files changed

+313
-237
lines changed

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,6 @@ else if ( state == State.ROLLED_BACK )
173173
{
174174
return failedFuture( new ClientException( "Can't commit, transaction has been rolled back" ) );
175175
}
176-
else if ( state == State.TERMINATED )
177-
{
178-
transactionClosed( State.ROLLED_BACK );
179-
return failedFuture( new ClientException( "Can't commit, transaction has been terminated" ) );
180-
}
181176
else
182177
{
183178
return resultCursors.retrieveNotConsumedError()
@@ -197,12 +192,6 @@ else if ( state == State.ROLLED_BACK )
197192
{
198193
return completedWithNull();
199194
}
200-
else if ( state == State.TERMINATED )
201-
{
202-
// no need for explicit rollback, transaction should've been rolled back by the database
203-
transactionClosed( State.ROLLED_BACK );
204-
return completedWithNull();
205-
}
206195
else
207196
{
208197
return resultCursors.retrieveNotConsumedError()
@@ -344,6 +333,11 @@ public void setBookmark( Bookmark bookmark )
344333

345334
private CompletionStage<Void> doCommitAsync()
346335
{
336+
if ( state == State.TERMINATED )
337+
{
338+
return failedFuture( new ClientException( "Can't commit, transaction has been terminated" ) );
339+
}
340+
347341
CompletableFuture<Void> commitFuture = new CompletableFuture<>();
348342
ResponseHandler pullAllHandler = new CommitTxResponseHandler( commitFuture, this );
349343
connection.runAndFlush( COMMIT_QUERY, emptyMap(), NoOpResponseHandler.INSTANCE, pullAllHandler );
@@ -352,6 +346,11 @@ private CompletionStage<Void> doCommitAsync()
352346

353347
private CompletionStage<Void> doRollbackAsync()
354348
{
349+
if ( state == State.TERMINATED )
350+
{
351+
return completedWithNull();
352+
}
353+
355354
CompletableFuture<Void> rollbackFuture = new CompletableFuture<>();
356355
ResponseHandler pullAllHandler = new RollbackTxResponseHandler( rollbackFuture );
357356
connection.runAndFlush( ROLLBACK_QUERY, emptyMap(), NoOpResponseHandler.INSTANCE, pullAllHandler );

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,8 @@ public void exceptionCaught( ChannelHandlerContext ctx, Throwable error )
9797
else
9898
{
9999
failed = true;
100-
101-
Throwable cause = error instanceof DecoderException ? error.getCause() : error;
102-
if ( cause instanceof SSLHandshakeException )
103-
{
104-
fail( ctx, new SecurityException( "Failed to establish secured connection with the server", cause ) );
105-
}
106-
else
107-
{
108-
fail( ctx, cause );
109-
}
100+
Throwable cause = transformError( error );
101+
fail( ctx, cause );
110102
}
111103
}
112104

@@ -170,4 +162,21 @@ private static Throwable protocolNoSupportedByDriverError( int suggestedProtocol
170162
return new ClientException(
171163
"Protocol error, server suggested unexpected protocol version: " + suggestedProtocolVersion );
172164
}
165+
166+
private static Throwable transformError( Throwable error )
167+
{
168+
Throwable cause = error instanceof DecoderException ? error.getCause() : error;
169+
if ( cause instanceof ServiceUnavailableException )
170+
{
171+
return cause;
172+
}
173+
else if ( cause instanceof SSLHandshakeException )
174+
{
175+
return new SecurityException( "Failed to establish secured connection with the server", cause );
176+
}
177+
else
178+
{
179+
return new ServiceUnavailableException( "Failed to establish connection with the server", cause );
180+
}
181+
}
173182
}

driver/src/main/java/org/neo4j/driver/internal/async/inbound/ChannelErrorHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private void fail( ChannelHandlerContext ctx, Throwable error )
101101
ctx.close();
102102
}
103103

104-
private Throwable transformError( Throwable error )
104+
private static Throwable transformError( Throwable error )
105105
{
106106
if ( error instanceof CodecException )
107107
{

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818
*/
1919
package org.neo4j.driver.internal.cluster;
2020

21+
import static java.util.concurrent.TimeUnit.SECONDS;
22+
2123
public class RoutingSettings
2224
{
25+
public static final RoutingSettings DEFAULT = new RoutingSettings( 1, SECONDS.toMillis( 5 ) );
26+
2327
private final int maxRoutingFailures;
2428
private final long retryTimeoutDelay;
2529
private final RoutingContext routingContext;

driver/src/main/java/org/neo4j/driver/internal/handlers/BookmarkResponseHandler.java

Lines changed: 0 additions & 56 deletions
This file was deleted.

driver/src/main/java/org/neo4j/driver/v1/Config.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ public static class ConfigBuilder
267267
private boolean encrypted = true;
268268
private TrustStrategy trustStrategy = trustAllCertificates();
269269
private LoadBalancingStrategy loadBalancingStrategy = LoadBalancingStrategy.LEAST_CONNECTED;
270-
private int routingFailureLimit = 1;
271-
private long routingRetryDelayMillis = TimeUnit.SECONDS.toMillis( 5 );
270+
private int routingFailureLimit = RoutingSettings.DEFAULT.maxRoutingFailures();
271+
private long routingRetryDelayMillis = RoutingSettings.DEFAULT.retryTimeoutDelay();
272272
private int connectionTimeoutMillis = (int) TimeUnit.SECONDS.toMillis( 5 );
273273
private RetrySettings retrySettings = RetrySettings.DEFAULT;
274274

driver/src/test/java/org/neo4j/driver/internal/async/ChannelConnectorImplIT.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
import org.junit.Test;
3030

3131
import java.io.IOException;
32+
import java.io.UncheckedIOException;
3233
import java.net.ServerSocket;
34+
import java.net.Socket;
3335
import java.util.concurrent.ExecutionException;
3436
import java.util.concurrent.TimeUnit;
3537

@@ -44,6 +46,7 @@
4446
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
4547
import org.neo4j.driver.v1.util.TestNeo4j;
4648

49+
import static java.util.concurrent.CompletableFuture.runAsync;
4750
import static org.hamcrest.Matchers.instanceOf;
4851
import static org.hamcrest.Matchers.startsWith;
4952
import static org.junit.Assert.assertEquals;
@@ -186,6 +189,42 @@ public void shouldFailWhenTLSHandshakeTakesTooLong() throws Exception
186189
testReadTimeoutOnConnect( SecurityPlan.forAllCertificates() );
187190
}
188191

192+
@Test
193+
public void shouldThrowServiceUnavailableExceptionOnFailureDuringConnect() throws Exception
194+
{
195+
ServerSocket server = new ServerSocket( 0 );
196+
BoltServerAddress address = new BoltServerAddress( "localhost", server.getLocalPort() );
197+
198+
runAsync( () ->
199+
{
200+
try
201+
{
202+
// wait for a connection
203+
Socket socket = server.accept();
204+
// and terminate it immediately so that client gets a "reset by peer" IOException
205+
socket.close();
206+
server.close();
207+
}
208+
catch ( IOException e )
209+
{
210+
throw new UncheckedIOException( e );
211+
}
212+
} );
213+
214+
ChannelConnector connector = newConnector( neo4j.authToken() );
215+
ChannelFuture channelFuture = connector.connect( address, bootstrap );
216+
217+
// connect operation should fail with ServiceUnavailableException
218+
try
219+
{
220+
await( channelFuture );
221+
fail( "Exception expected" );
222+
}
223+
catch ( ServiceUnavailableException ignore )
224+
{
225+
}
226+
}
227+
189228
private void testReadTimeoutOnConnect( SecurityPlan securityPlan ) throws IOException
190229
{
191230
try ( ServerSocket server = new ServerSocket( 0 ) ) // server that accepts connections but does not reply

driver/src/test/java/org/neo4j/driver/internal/async/HandshakeHandlerTest.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,34 @@ public void shouldFailGivenPromiseWhenExceptionCaught()
9191
await( handshakeCompletedPromise );
9292
fail( "Exception expected" );
9393
}
94-
catch ( Exception e )
94+
catch ( ServiceUnavailableException e )
95+
{
96+
assertEquals( cause, e.getCause() );
97+
}
98+
99+
// channel should be closed
100+
assertNull( await( channel.closeFuture() ) );
101+
}
102+
103+
@Test
104+
public void shouldFailGivenPromiseWhenServiceUnavailableExceptionCaught()
105+
{
106+
ChannelPromise handshakeCompletedPromise = channel.newPromise();
107+
HandshakeHandler handler = newHandler( handshakeCompletedPromise );
108+
channel.pipeline().addLast( handler );
109+
110+
ServiceUnavailableException error = new ServiceUnavailableException( "Bad error" );
111+
channel.pipeline().fireExceptionCaught( error );
112+
113+
try
114+
{
115+
// promise should fail
116+
await( handshakeCompletedPromise );
117+
fail( "Exception expected" );
118+
}
119+
catch ( ServiceUnavailableException e )
95120
{
96-
assertEquals( cause, e );
121+
assertEquals( error, e );
97122
}
98123

99124
// channel should be closed
@@ -118,9 +143,9 @@ public void shouldFailGivenPromiseWhenMultipleExceptionsCaught()
118143
await( handshakeCompletedPromise );
119144
fail( "Exception expected" );
120145
}
121-
catch ( RuntimeException e )
146+
catch ( ServiceUnavailableException e )
122147
{
123-
assertEquals( error1, e );
148+
assertEquals( error1, e.getCause() );
124149
}
125150

126151
// channel should be closed
@@ -153,9 +178,9 @@ public void shouldUnwrapDecoderException()
153178
await( handshakeCompletedPromise );
154179
fail( "Exception expected" );
155180
}
156-
catch ( Exception e )
181+
catch ( ServiceUnavailableException e )
157182
{
158-
assertEquals( cause, e );
183+
assertEquals( cause, e.getCause() );
159184
}
160185

161186
// channel should be closed

0 commit comments

Comments
 (0)