Skip to content

Commit 0f74b0b

Browse files
authored
Merge pull request #629 from zhenlineo/1.7-conn-timeout
Changing default conneciton timeout to be 30s
2 parents d56154a + 1b14464 commit 0f74b0b

File tree

86 files changed

+890
-950
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+890
-950
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public BeginMessage( Bookmarks bookmarks, TransactionConfig config, AccessMode m
3838

3939
public BeginMessage( Bookmarks bookmarks, Duration txTimeout, Map<String,Value> txMetadata, AccessMode mode )
4040
{
41-
super( bookmarks, txTimeout, txMetadata, mode );
41+
super( buildMetadata( bookmarks, txTimeout, txMetadata, mode ) );
4242
}
4343

4444
@Override

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,41 @@
2424

2525
import org.neo4j.driver.internal.Bookmarks;
2626
import org.neo4j.driver.v1.AccessMode;
27+
import org.neo4j.driver.v1.Statement;
2728
import org.neo4j.driver.v1.TransactionConfig;
2829
import org.neo4j.driver.v1.Value;
2930

31+
import static java.util.Collections.emptyMap;
32+
import static org.neo4j.driver.v1.Values.ofValue;
33+
3034
public class RunWithMetadataMessage extends TransactionStartingMessage
3135
{
3236
public final static byte SIGNATURE = 0x10;
3337

3438
private final String statement;
3539
private final Map<String,Value> parameters;
3640

37-
public RunWithMetadataMessage( String statement, Map<String,Value> parameters, Bookmarks bookmarks, TransactionConfig config, AccessMode mode )
41+
public static RunWithMetadataMessage autoCommitTxRunMessage( Statement statement, TransactionConfig config, AccessMode mode,
42+
Bookmarks bookmark )
43+
{
44+
return autoCommitTxRunMessage( statement.text(), statement.parameters().asMap( ofValue() ), config.timeout(), config.metadata(), mode, bookmark );
45+
}
46+
47+
public static RunWithMetadataMessage autoCommitTxRunMessage( String statement, Map<String,Value> parameters, Duration txTimeout, Map<String,Value> txMetadata, AccessMode mode,
48+
Bookmarks bookmark )
49+
{
50+
Map<String,Value> metadata = buildMetadata( bookmark, txTimeout, txMetadata, mode );
51+
return new RunWithMetadataMessage( statement, parameters, metadata );
52+
}
53+
54+
public static RunWithMetadataMessage explicitTxRunMessage( Statement statement )
3855
{
39-
this( statement, parameters, bookmarks, config.timeout(), config.metadata(), mode );
56+
return new RunWithMetadataMessage( statement.text(), statement.parameters().asMap( ofValue() ), emptyMap() );
4057
}
4158

42-
public RunWithMetadataMessage( String statement, Map<String,Value> parameters, Bookmarks bookmarks, Duration txTimeout, Map<String,Value> txMetadata,
43-
AccessMode mode )
59+
public RunWithMetadataMessage( String statement, Map<String,Value> parameters, Map<String,Value> metadata )
4460
{
45-
super( bookmarks, txTimeout, txMetadata, mode );
61+
super( metadata );
4662
this.statement = statement;
4763
this.parameters = parameters;
4864
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.neo4j.driver.v1.Value;
2929

3030
import static java.util.Collections.emptyMap;
31+
import static java.util.Objects.requireNonNull;
3132
import static org.neo4j.driver.v1.Values.value;
3233

3334
abstract class TransactionStartingMessage implements Message
@@ -40,17 +41,17 @@ abstract class TransactionStartingMessage implements Message
4041

4142
final Map<String,Value> metadata;
4243

43-
TransactionStartingMessage( Bookmarks bookmarks, Duration txTimeout, Map<String,Value> txMetadata, AccessMode mode )
44+
TransactionStartingMessage( Map<String,Value> metadata )
4445
{
45-
this.metadata = buildMetadata( bookmarks, txTimeout, txMetadata, mode );
46+
this.metadata = requireNonNull( metadata );
4647
}
4748

4849
public final Map<String,Value> metadata()
4950
{
5051
return metadata;
5152
}
5253

53-
private static Map<String,Value> buildMetadata( Bookmarks bookmarks, Duration txTimeout, Map<String,Value> txMetadata, AccessMode mode )
54+
static Map<String,Value> buildMetadata( Bookmarks bookmarks, Duration txTimeout, Map<String,Value> txMetadata, AccessMode mode )
5455
{
5556
boolean bookmarksPresent = bookmarks != null && !bookmarks.isEmpty();
5657
boolean txTimeoutPresent = txTimeout != null;

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import org.neo4j.driver.internal.messaging.request.BeginMessage;
4545
import org.neo4j.driver.internal.messaging.request.GoodbyeMessage;
4646
import org.neo4j.driver.internal.messaging.request.HelloMessage;
47-
import org.neo4j.driver.internal.messaging.request.RunWithMetadataMessage;
4847
import org.neo4j.driver.internal.spi.Connection;
4948
import org.neo4j.driver.internal.util.Futures;
5049
import org.neo4j.driver.internal.util.MetadataExtractor;
@@ -57,6 +56,8 @@
5756
import static org.neo4j.driver.internal.messaging.request.CommitMessage.COMMIT;
5857
import static org.neo4j.driver.internal.messaging.request.PullAllMessage.PULL_ALL;
5958
import static org.neo4j.driver.internal.messaging.request.RollbackMessage.ROLLBACK;
59+
import static org.neo4j.driver.internal.messaging.request.RunWithMetadataMessage.autoCommitTxRunMessage;
60+
import static org.neo4j.driver.internal.messaging.request.RunWithMetadataMessage.explicitTxRunMessage;
6061
import static org.neo4j.driver.v1.Values.ofValue;
6162

6263
public class BoltProtocolV3 implements BoltProtocol
@@ -131,24 +132,25 @@ public CompletionStage<Void> rollbackTransaction( Connection connection )
131132
public CompletionStage<InternalStatementResultCursor> runInAutoCommitTransaction( Connection connection, Statement statement,
132133
BookmarksHolder bookmarksHolder, TransactionConfig config, boolean waitForRunResponse )
133134
{
134-
return runStatement( connection, statement, bookmarksHolder, null, config, waitForRunResponse );
135+
Message runMessage = autoCommitTxRunMessage( statement, config, connection.mode(), bookmarksHolder.getBookmarks() );
136+
return runStatement( connection, statement, bookmarksHolder, null, waitForRunResponse, runMessage );
135137
}
136138

137139
@Override
138140
public CompletionStage<InternalStatementResultCursor> runInExplicitTransaction( Connection connection, Statement statement, ExplicitTransaction tx,
139141
boolean waitForRunResponse )
140142
{
141-
return runStatement( connection, statement, BookmarksHolder.NO_OP, tx, TransactionConfig.empty(), waitForRunResponse );
143+
Message runMessage = explicitTxRunMessage( statement );
144+
return runStatement( connection, statement, BookmarksHolder.NO_OP, tx, waitForRunResponse, runMessage );
142145
}
143146

144-
private static CompletionStage<InternalStatementResultCursor> runStatement( Connection connection, Statement statement,
145-
BookmarksHolder bookmarksHolder, ExplicitTransaction tx, TransactionConfig config, boolean waitForRunResponse )
147+
private static CompletionStage<InternalStatementResultCursor> runStatement( Connection connection, Statement statement, BookmarksHolder bookmarksHolder,
148+
ExplicitTransaction tx, boolean waitForRunResponse, Message runMessage )
146149
{
147150
String query = statement.text();
148151
Map<String,Value> params = statement.parameters().asMap( ofValue() );
149152

150153
CompletableFuture<Void> runCompletedFuture = new CompletableFuture<>();
151-
Message runMessage = new RunWithMetadataMessage( query, params, bookmarksHolder.getBookmarks(), config, connection.mode() );
152154
RunResponseHandler runHandler = new RunResponseHandler( runCompletedFuture, METADATA_EXTRACTOR );
153155
PullAllResponseHandler pullAllHandler = newPullAllHandler( statement, runHandler, connection, bookmarksHolder, tx );
154156

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ public static class ConfigBuilder
297297
private LoadBalancingStrategy loadBalancingStrategy = LoadBalancingStrategy.LEAST_CONNECTED;
298298
private int routingFailureLimit = RoutingSettings.DEFAULT.maxRoutingFailures();
299299
private long routingRetryDelayMillis = RoutingSettings.DEFAULT.retryTimeoutDelay();
300-
private int connectionTimeoutMillis = (int) TimeUnit.SECONDS.toMillis( 5 );
300+
private int connectionTimeoutMillis = (int) TimeUnit.SECONDS.toMillis( 30 );
301301
private RetrySettings retrySettings = RetrySettings.DEFAULT;
302302
private ServerAddressResolver resolver;
303303

@@ -687,7 +687,7 @@ public ConfigBuilder withRoutingRetryDelay( long delay, TimeUnit unit )
687687
* Timeout value should be greater or equal to zero and represent a valid {@code int} value when converted to
688688
* {@link TimeUnit#MILLISECONDS milliseconds}.
689689
* <p>
690-
* The default value of this parameter is {@code 5 SECONDS}.
690+
* The default value of this parameter is {@code 30 SECONDS}.
691691
*
692692
* @param value the timeout duration
693693
* @param unit the unit in which duration is given

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
* The <a href="https://tools.ietf.org/html/rfc3986">URI</a> passed to
3838
* this method determines the type of Driver created.
3939
* <br>
40-
* <table border="1" cellpadding="4" style="border-collapse: collapse" summary="Available schemes and drivers">
40+
* <table border="1" style="border-collapse: collapse">
41+
* <caption>Available schemes and drivers</caption>
4142
* <thead>
4243
* <tr><th>URI Scheme</th><th>Driver</th></tr>
4344
* </thead>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
* <p>
4646
* Driver logging API defines the following log levels: ERROR, INFO, WARN, DEBUG and TRACE. They are similar to levels defined by SLF4J but different from
4747
* log levels defined for {@link java.util.logging}. The following mapping takes place:
48-
* <table border="1" cellpadding="4" summary="Driver and JUL log levels">
48+
* <table border="1" style="border-collapse: collapse">
49+
* <caption>Driver and JUL log levels</caption>
4950
* <tr>
5051
* <th>Driver</th>
5152
* <th>java.util.logging</th>

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public interface Value extends MapAccessor, MapAccessorWithDefaultValue
117117
/**
118118
* If this value represents a list or map, test if the collection is empty.
119119
*
120-
* @return <tt>true</tt> if size() is 0, otherwise <tt>false</tt>
120+
* @return {@code true} if size() is 0, otherwise {@code false}
121121
*/
122122
boolean isEmpty();
123123

@@ -154,17 +154,17 @@ public interface Value extends MapAccessor, MapAccessorWithDefaultValue
154154
boolean hasType( Type type );
155155

156156
/**
157-
* @return <tt>true</tt> if the value is a Boolean value and has the value True.
157+
* @return {@code true} if the value is a Boolean value and has the value True.
158158
*/
159159
boolean isTrue();
160160

161161
/**
162-
* @return <tt>true</tt> if the value is a Boolean value and has the value False.
162+
* @return {@code true} if the value is a Boolean value and has the value False.
163163
*/
164164
boolean isFalse();
165165

166166
/**
167-
* @return <tt>true</tt> if the value is a Null, otherwise <tt>false</tt>
167+
* @return {@code true} if the value is a Null, otherwise {@code false}
168168
*/
169169
boolean isNull();
170170

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ public static Function<Value,List<Object>> ofList()
679679
}
680680

681681
/**
682-
* Converts values to {@link List} of <tt>T</tt>.
682+
* Converts values to {@link List} of {@code T}.
683683
*
684684
* @param innerMap converter for the values inside the list
685685
* @param <T> the type of values inside the list

driver/src/main/java/org/neo4j/driver/v1/types/MapAccessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public interface MapAccessor
4646
* Check if the list of keys contains the given key
4747
*
4848
* @param key the key
49-
* @return <tt>true</tt> if this map keys contains the given key otherwise <tt>false</tt>
49+
* @return {@code true} if this map keys contains the given key otherwise {@code false}
5050
*/
5151
boolean containsKey( String key );
5252

driver/src/test/java/org/neo4j/driver/internal/DirectDriverBoltKitTest.java

Lines changed: 41 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.net.URI;
2626
import java.util.List;
2727
import java.util.Optional;
28+
import java.util.function.Consumer;
2829

2930
import org.neo4j.driver.internal.cluster.RoutingSettings;
3031
import org.neo4j.driver.internal.retry.RetrySettings;
@@ -150,7 +151,7 @@ void shouldSendReadAccessModeInStatementMetadata() throws Exception
150151
StubServer server = StubServer.start( "hello_run_exit_read.script", 9001 );
151152

152153
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG );
153-
Session session = driver.session( AccessMode.READ ) )
154+
Session session = driver.session( AccessMode.READ ) )
154155
{
155156
List<String> names = session.run( "MATCH (n) RETURN n.name" ).list( record -> record.get( 0 ).asString() );
156157
assertEquals( asList( "Foo", "Bar" ), names );
@@ -167,7 +168,7 @@ void shouldNotSendWriteAccessModeInStatementMetadata() throws Exception
167168
StubServer server = StubServer.start( "hello_run_exit.script", 9001 );
168169

169170
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG );
170-
Session session = driver.session( AccessMode.WRITE ) )
171+
Session session = driver.session( AccessMode.WRITE ) )
171172
{
172173
List<String> names = session.run( "MATCH (n) RETURN n.name" ).list( record -> record.get( 0 ).asString() );
173174
assertEquals( asList( "Foo", "Bar" ), names );
@@ -209,36 +210,62 @@ void shouldCloseChannelWhenResetFails() throws Exception
209210
}
210211

211212
@Test
212-
void shouldPropagateTransactionCommitErrorWhenSessionClosed() throws Exception
213+
void shouldPropagateTransactionRollbackErrorWhenSessionClosed() throws Exception
213214
{
214-
testTransactionCloseErrorPropagationWhenSessionClosed( "commit_error.script", true, "Unable to commit" );
215+
StubServer server = StubServer.start( "rollback_error.script", 9001 );
216+
try
217+
{
218+
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ) )
219+
{
220+
Session session = driver.session();
221+
222+
Transaction tx = session.beginTransaction();
223+
StatementResult result = tx.run( "CREATE (n {name:'Alice'}) RETURN n.name AS name" );
224+
assertEquals( "Alice", result.single().get( "name" ).asString() );
225+
226+
TransientException e = assertThrows( TransientException.class, session::close );
227+
assertEquals( "Neo.TransientError.General.DatabaseUnavailable", e.code() );
228+
assertEquals( "Unable to rollback", e.getMessage() );
229+
}
230+
}
231+
finally
232+
{
233+
assertEquals( 0, server.exitStatus() );
234+
}
215235
}
216236

217237
@Test
218-
void shouldPropagateTransactionRollbackErrorWhenSessionClosed() throws Exception
238+
void shouldThrowCommitErrorWhenTransactionCommit() throws Exception
219239
{
220-
testTransactionCloseErrorPropagationWhenSessionClosed( "rollback_error.script", false, "Unable to rollback" );
240+
testTxCloseErrorPropagation( "commit_error.script", tx -> {
241+
tx.success();
242+
tx.close();
243+
}, "Unable to commit" );
221244
}
222245

223246
@Test
224-
void shouldThrowCommitErrorWhenTransactionClosed() throws Exception
247+
void shouldThrowRollbackErrorWhenTransactionRollback() throws Exception
225248
{
226-
testTxCloseErrorPropagation( "commit_error.script", true, "Unable to commit" );
249+
testTxCloseErrorPropagation( "rollback_error.script", tx -> {
250+
tx.failure();
251+
tx.close();
252+
}, "Unable to rollback" );
227253
}
228254

229255
@Test
230-
void shouldThrowRollbackErrorWhenTransactionClosed() throws Exception
256+
void shouldThrowRollbackErrorWhenTransactionClose() throws Exception
231257
{
232-
testTxCloseErrorPropagation( "rollback_error.script", false, "Unable to rollback" );
258+
testTxCloseErrorPropagation( "rollback_error.script", Transaction::close, "Unable to rollback" );
233259
}
234260

261+
235262
@Test
236263
void shouldThrowCorrectErrorOnRunFailure() throws Throwable
237264
{
238265
StubServer server = StubServer.start( "database_shutdown.script", 9001 );
239266

240267
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG );
241-
Session session = driver.session( "neo4j:bookmark:v1:tx0" );
268+
Session session = driver.session( "neo4j:bookmark:v1:tx0" );
242269
// has to enforce to flush BEGIN to have tx started.
243270
Transaction transaction = session.beginTransaction() )
244271
{
@@ -260,7 +287,7 @@ void shouldThrowCorrectErrorOnCommitFailure() throws Throwable
260287
StubServer server = StubServer.start( "database_shutdown_at_commit.script", 9001 );
261288

262289
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG );
263-
Session session = driver.session() )
290+
Session session = driver.session() )
264291
{
265292
Transaction transaction = session.beginTransaction();
266293
StatementResult result = transaction.run( "CREATE (n {name:'Bob'})" );
@@ -276,41 +303,7 @@ void shouldThrowCorrectErrorOnCommitFailure() throws Throwable
276303
}
277304
}
278305

279-
private static void testTransactionCloseErrorPropagationWhenSessionClosed( String script, boolean commit,
280-
String expectedErrorMessage ) throws Exception
281-
{
282-
StubServer server = StubServer.start( script, 9001 );
283-
try
284-
{
285-
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG ) )
286-
{
287-
Session session = driver.session();
288-
289-
Transaction tx = session.beginTransaction();
290-
StatementResult result = tx.run( "CREATE (n {name:'Alice'}) RETURN n.name AS name" );
291-
assertEquals( "Alice", result.single().get( "name" ).asString() );
292-
293-
if ( commit )
294-
{
295-
tx.success();
296-
}
297-
else
298-
{
299-
tx.failure();
300-
}
301-
302-
TransientException e = assertThrows( TransientException.class, session::close );
303-
assertEquals( "Neo.TransientError.General.DatabaseUnavailable", e.code() );
304-
assertEquals( expectedErrorMessage, e.getMessage() );
305-
}
306-
}
307-
finally
308-
{
309-
assertEquals( 0, server.exitStatus() );
310-
}
311-
}
312-
313-
private static void testTxCloseErrorPropagation( String script, boolean commit, String expectedErrorMessage )
306+
private static void testTxCloseErrorPropagation( String script, Consumer<Transaction> txAction, String expectedErrorMessage )
314307
throws Exception
315308
{
316309
StubServer server = StubServer.start( script, 9001 );
@@ -323,16 +316,8 @@ private static void testTxCloseErrorPropagation( String script, boolean commit,
323316
StatementResult result = tx.run( "CREATE (n {name:'Alice'}) RETURN n.name AS name" );
324317
assertEquals( "Alice", result.single().get( "name" ).asString() );
325318

326-
if ( commit )
327-
{
328-
tx.success();
329-
}
330-
else
331-
{
332-
tx.failure();
333-
}
319+
TransientException e = assertThrows( TransientException.class, () -> txAction.accept( tx ) );
334320

335-
TransientException e = assertThrows( TransientException.class, tx::close );
336321
assertEquals( "Neo.TransientError.General.DatabaseUnavailable", e.code() );
337322
assertEquals( expectedErrorMessage, e.getMessage() );
338323
}

0 commit comments

Comments
 (0)