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

Allow tx timeout to be 0 or null. #1108

Merged
merged 7 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 11 additions & 4 deletions driver/src/main/java/org/neo4j/driver/TransactionConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,26 +183,33 @@ public static class Builder
private Duration timeout;
private Map<String,Object> metadata = emptyMap();

/**
robsdedude marked this conversation as resolved.
Show resolved Hide resolved
* Value used to signal {@link #withTimeout(Duration)} to use the server-side configured default timeout.
*/
public static final Duration SERVER_DEFAULT_TIMEOUT = null;

private Builder()
{
}

/**
* Set the transaction timeout. Transactions that execute longer than the configured timeout will be terminated by the database.
* Use {@link #SERVER_DEFAULT_TIMEOUT SERVER_DEFAULT_TIMEOUT} (default) to rely on the server-side configured timeout.
robsdedude marked this conversation as resolved.
Show resolved Hide resolved
* <p>
* This functionality allows to limit query/transaction execution time. Specified timeout overrides the default timeout configured in the database
* using {@code dbms.transaction.timeout} setting.
* <p>
* Provided value should not be {@code null} and should not represent a duration of zero or negative duration.
* Provided value should not represent a negative duration.
*
* @param timeout the timeout.
* @return this builder.
*/
public Builder withTimeout( Duration timeout )
{
requireNonNull( timeout, "Transaction timeout should not be null" );
checkArgument( !timeout.isZero(), "Transaction timeout should not be zero" );
checkArgument( !timeout.isNegative(), "Transaction timeout should not be negative" );
if (timeout != null)
{
checkArgument( !timeout.isNegative(), "Transaction timeout should not be negative" );
}

this.timeout = timeout;
return this;
Expand Down
32 changes: 20 additions & 12 deletions driver/src/test/java/org/neo4j/driver/TransactionConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,6 @@ void emptyConfigShouldHaveNoMetadata()
assertEquals( emptyMap(), TransactionConfig.empty().metadata() );
}

@Test
void shouldDisallowNullTimeout()
{
assertThrows( NullPointerException.class, () -> TransactionConfig.builder().withTimeout( null ) );
}

@Test
void shouldDisallowZeroTimeout()
{
assertThrows( IllegalArgumentException.class, () -> TransactionConfig.builder().withTimeout( Duration.ZERO ) );
}

@Test
void shouldDisallowNegativeTimeout()
{
Expand Down Expand Up @@ -98,6 +86,26 @@ void shouldHaveTimeout()
assertEquals( Duration.ofSeconds( 3 ), config.timeout() );
}

@Test
void shouldAllowDefaultTimeout()
{
TransactionConfig config = TransactionConfig.builder()
.withTimeout( TransactionConfig.Builder.SERVER_DEFAULT_TIMEOUT )
.build();

assertNull( config.timeout() );
robsdedude marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
void shouldAllowZeroTimeout()
{
TransactionConfig config = TransactionConfig.builder()
.withTimeout( Duration.ZERO )
.build();

assertEquals( Duration.ZERO, config.timeout() );
}

@Test
void shouldHaveMetadata()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.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 neo4j.org.testkit.backend;

public class CustomDriverError extends java.lang.RuntimeException
robsdedude marked this conversation as resolved.
Show resolved Hide resolved
{
robsdedude marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import neo4j.org.testkit.backend.CustomDriverError;
import neo4j.org.testkit.backend.TestkitState;
import neo4j.org.testkit.backend.messages.requests.TestkitRequest;
import neo4j.org.testkit.backend.messages.responses.BackendError;
Expand Down Expand Up @@ -145,6 +146,20 @@ else if ( isConnectionPoolClosedException( throwable ) || throwable instanceof U
)
.build();
}
else if ( throwable instanceof CustomDriverError )
{
throwable = throwable.getCause();
String id = testkitState.newId();
return DriverError.builder()
.data(
DriverError.DriverErrorBody.builder()
.id( id )
.errorType( throwable.getClass().getName() )
.msg( throwable.getMessage() )
.build()
)
.build();
}
else
{
return BackendError.builder().data( BackendError.BackendErrorBody.builder().msg( throwable.toString() ).build() ).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import lombok.Getter;
import lombok.Setter;
import neo4j.org.testkit.backend.CustomDriverError;
import neo4j.org.testkit.backend.TestkitState;
import neo4j.org.testkit.backend.holder.AsyncTransactionHolder;
import neo4j.org.testkit.backend.holder.RxTransactionHolder;
Expand All @@ -45,6 +46,30 @@ public class SessionBeginTransaction implements TestkitRequest
{
private SessionBeginTransactionBody data;

private void configureTimeout( TransactionConfig.Builder builder )
{
if ( data.getTimeoutPresent() )
{
try
{
if ( data.getTimeout() != null )
{
builder.withTimeout( Duration.ofMillis( data.getTimeout() ) );
}
else
{
builder.withTimeout( TransactionConfig.Builder.SERVER_DEFAULT_TIMEOUT );
}
}
catch ( IllegalArgumentException e )
{
CustomDriverError wrapped = new CustomDriverError();
wrapped.initCause( e );
robsdedude marked this conversation as resolved.
Show resolved Hide resolved
throw wrapped;
}
}
}

@Override
public TestkitResponse process( TestkitState testkitState )
{
Expand All @@ -53,10 +78,7 @@ public TestkitResponse process( TestkitState testkitState )
TransactionConfig.Builder builder = TransactionConfig.builder();
Optional.ofNullable( data.txMeta ).ifPresent( builder::withMetadata );

if ( data.getTimeout() != null )
{
builder.withTimeout( Duration.ofMillis( data.getTimeout() ) );
}
configureTimeout( builder );

org.neo4j.driver.Transaction transaction = session.beginTransaction( builder.build() );
return transaction( testkitState.addTransactionHolder( new TransactionHolder( sessionHolder, transaction ) ) );
Expand All @@ -72,10 +94,7 @@ public CompletionStage<TestkitResponse> processAsync( TestkitState testkitState
TransactionConfig.Builder builder = TransactionConfig.builder();
Optional.ofNullable( data.txMeta ).ifPresent( builder::withMetadata );

if ( data.getTimeout() != null )
{
builder.withTimeout( Duration.ofMillis( data.getTimeout() ) );
}
configureTimeout( builder );

return session.beginTransactionAsync( builder.build() ).thenApply( tx -> transaction(
testkitState.addAsyncTransactionHolder( new AsyncTransactionHolder( sessionHolder, tx ) ) ) );
Expand All @@ -92,10 +111,7 @@ public Mono<TestkitResponse> processRx( TestkitState testkitState )
TransactionConfig.Builder builder = TransactionConfig.builder();
Optional.ofNullable( data.txMeta ).ifPresent( builder::withMetadata );

if ( data.getTimeout() != null )
{
builder.withTimeout( Duration.ofMillis( data.getTimeout() ) );
}
configureTimeout( builder );

return Mono.fromDirect( session.beginTransaction( builder.build() ) )
.map( tx -> transaction(
Expand All @@ -108,12 +124,23 @@ private Transaction transaction( String txId )
return Transaction.builder().data( Transaction.TransactionBody.builder().id( txId ).build() ).build();
}

@Getter
robsdedude marked this conversation as resolved.
Show resolved Hide resolved
@Setter
public static class SessionBeginTransactionBody
{
@Getter
@Setter
private String sessionId;
@Getter
@Setter
private Map<String,Object> txMeta;
@Getter
private Integer timeout;
@Getter
private Boolean timeoutPresent = false;

public void setTimeout( Integer timeout )
{
this.timeout = timeout;
timeoutPresent = true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Getter;
import lombok.Setter;
import neo4j.org.testkit.backend.CustomDriverError;
import neo4j.org.testkit.backend.TestkitState;
import neo4j.org.testkit.backend.holder.ResultCursorHolder;
import neo4j.org.testkit.backend.holder.ResultHolder;
Expand Down Expand Up @@ -49,6 +50,30 @@ public class SessionRun implements TestkitRequest
{
private SessionRunBody data;

private void configureTimeout( TransactionConfig.Builder builder )
{
if ( data.getTimeoutPresent() )
{
try
{
if ( data.getTimeout() != null )
{
builder.withTimeout( Duration.ofMillis( data.getTimeout() ) );
}
else
{
builder.withTimeout( TransactionConfig.Builder.SERVER_DEFAULT_TIMEOUT );
}
}
catch ( IllegalArgumentException e )
{
CustomDriverError wrapped = new CustomDriverError();
wrapped.initCause( e );
robsdedude marked this conversation as resolved.
Show resolved Hide resolved
throw wrapped;
}
}
}

@Override
public TestkitResponse process( TestkitState testkitState )
{
Expand All @@ -59,7 +84,7 @@ public TestkitResponse process( TestkitState testkitState )
.orElseGet( () -> new Query( data.cypher ) );
TransactionConfig.Builder transactionConfig = TransactionConfig.builder();
Optional.ofNullable( data.getTxMeta() ).ifPresent( transactionConfig::withMetadata );
Optional.ofNullable( data.getTimeout() ).ifPresent( to -> transactionConfig.withTimeout( Duration.ofMillis( to ) ) );
configureTimeout( transactionConfig );
org.neo4j.driver.Result result = session.run( query, transactionConfig.build() );
String id = testkitState.addResultHolder( new ResultHolder( sessionHolder, result ) );

Expand All @@ -78,8 +103,7 @@ public CompletionStage<TestkitResponse> processAsync( TestkitState testkitState
.orElseGet( () -> new Query( data.cypher ) );
TransactionConfig.Builder transactionConfig = TransactionConfig.builder();
Optional.ofNullable( data.getTxMeta() ).ifPresent( transactionConfig::withMetadata );
Optional.ofNullable( data.getTimeout() )
.ifPresent( to -> transactionConfig.withTimeout( Duration.ofMillis( to ) ) );
configureTimeout( transactionConfig );

return session.runAsync( query, transactionConfig.build() )
.thenApply( resultCursor ->
Expand All @@ -103,7 +127,7 @@ public Mono<TestkitResponse> processRx( TestkitState testkitState )
.orElseGet( () -> new Query( data.cypher ) );
TransactionConfig.Builder transactionConfig = TransactionConfig.builder();
Optional.ofNullable( data.getTxMeta() ).ifPresent( transactionConfig::withMetadata );
Optional.ofNullable( data.getTimeout() ).ifPresent( to -> transactionConfig.withTimeout( Duration.ofMillis( to ) ) );
configureTimeout( transactionConfig );

RxResult result = session.run( query, transactionConfig.build() );
String id = testkitState.addRxResultHolder( new RxResultHolder( sessionHolder, result ) );
Expand All @@ -120,17 +144,32 @@ private Result createResponse( String resultId )
return Result.builder().data( Result.ResultBody.builder().id( resultId ).build() ).build();
}

@Setter
robsdedude marked this conversation as resolved.
Show resolved Hide resolved
@Getter
public static class SessionRunBody
{
@JsonDeserialize( using = TestkitCypherParamDeserializer.class )
@Setter
@Getter
private Map<String,Object> params;

@Setter
@Getter
private String sessionId;
@Setter
@Getter
private String cypher;
@Setter
@Getter
private Map<String,Object> txMeta;
@Getter
private Integer timeout;
@Getter
private Boolean timeoutPresent = false;

public void setTimeout( Integer timeout )
{
this.timeout = timeout;
timeoutPresent = true;
}

}
}