Skip to content

Commit f0e661e

Browse files
author
Zhen Li
committed
Config option to allow config netty event loop threads
Added configuration option to allow users to config how many netty event loop threads they want to use.
1 parent 5ad532b commit f0e661e

File tree

6 files changed

+60
-9
lines changed

6 files changed

+60
-9
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,14 @@ public class Config
9494
private final int routingFailureLimit;
9595
private final long routingRetryDelayMillis;
9696
private final long fetchSize;
97-
private long routingTablePurgeDelayMillis;
97+
private final long routingTablePurgeDelayMillis;
9898

9999
private final int connectionTimeoutMillis;
100100
private final RetrySettings retrySettings;
101101
private final ServerAddressResolver resolver;
102102

103103
private final boolean isMetricsEnabled;
104+
private final int eventLoopThreads;
104105

105106
private Config( ConfigBuilder builder )
106107
{
@@ -122,6 +123,7 @@ private Config( ConfigBuilder builder )
122123
this.resolver = builder.resolver;
123124
this.fetchSize = builder.fetchSize;
124125

126+
this.eventLoopThreads = builder.eventLoopThreads;
125127
this.isMetricsEnabled = builder.isMetricsEnabled;
126128
}
127129

@@ -242,6 +244,11 @@ public long fetchSize()
242244
return fetchSize;
243245
}
244246

247+
public int eventLoopThreads()
248+
{
249+
return eventLoopThreads;
250+
}
251+
245252
/**
246253
* @return if the metrics is enabled or not on this driver.
247254
*/
@@ -271,6 +278,7 @@ public static class ConfigBuilder
271278
private ServerAddressResolver resolver;
272279
private boolean isMetricsEnabled = false;
273280
private long fetchSize = FetchSizeUtil.DEFAULT_FETCH_SIZE;
281+
private int eventLoopThreads = 0;
274282

275283
private ConfigBuilder() {}
276284

@@ -699,6 +707,23 @@ public ConfigBuilder withoutDriverMetrics()
699707
return this;
700708
}
701709

710+
/**
711+
* Configure the event loop thread count. This specifies how many threads the driver can use to handle network I/O events
712+
* and user's events in driver's I/O threads. By default, 2 * NumberOfProcessors amount of threads will be used instead.
713+
* @param size the thread count.
714+
* @return this builder.
715+
* @throws IllegalArgumentException if the value of the size is set to a number that is less than 1.
716+
*/
717+
public ConfigBuilder withEventLoopThreads( int size )
718+
{
719+
if ( size < 1 )
720+
{
721+
throw new IllegalArgumentException( String.format( "The event loop thread may not be smaller than 1, but was %d.", size ) );
722+
}
723+
this.eventLoopThreads = size;
724+
return this;
725+
}
726+
702727
/**
703728
* Create a config instance from this builder.
704729
*

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public final Driver newInstance ( URI uri, AuthToken authToken, RoutingSettings
8282
boolean ownsEventLoopGroup;
8383
if ( eventLoopGroup == null )
8484
{
85-
bootstrap = createBootstrap();
85+
bootstrap = createBootstrap( config.eventLoopThreads() );
8686
ownsEventLoopGroup = true;
8787
}
8888
else
@@ -272,9 +272,9 @@ protected RetryLogic createRetryLogic( RetrySettings settings, EventExecutorGrou
272272
* <p>
273273
* <b>This method is protected only for testing</b>
274274
*/
275-
protected Bootstrap createBootstrap()
275+
protected Bootstrap createBootstrap( int size )
276276
{
277-
return BootstrapFactory.newBootstrap();
277+
return BootstrapFactory.newBootstrap( size );
278278
}
279279

280280
/**

driver/src/test/java/org/neo4j/driver/integration/DirectDriverIT.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525

2626
import java.net.URI;
2727

28+
import org.neo4j.driver.Config;
2829
import org.neo4j.driver.Driver;
2930
import org.neo4j.driver.GraphDatabase;
30-
import org.neo4j.driver.Session;
3131
import org.neo4j.driver.Result;
32+
import org.neo4j.driver.Session;
3233
import org.neo4j.driver.internal.BoltServerAddress;
3334
import org.neo4j.driver.util.DatabaseExtension;
3435
import org.neo4j.driver.util.ParallelizableIT;
@@ -37,6 +38,7 @@
3738
import static org.hamcrest.core.IsEqual.equalTo;
3839
import static org.hamcrest.junit.MatcherAssert.assertThat;
3940
import static org.junit.jupiter.api.Assertions.assertThrows;
41+
import static org.neo4j.driver.Values.parameters;
4042
import static org.neo4j.driver.internal.util.Matchers.directDriverWithAddress;
4143

4244
@ParallelizableIT
@@ -109,4 +111,28 @@ void shouldConnectIPv6Uri()
109111
assertThat( result.single().get( 0 ).asInt(), CoreMatchers.equalTo( 1 ) );
110112
}
111113
}
114+
115+
@Test
116+
void shouldRespectEventLoopThreadsConfig()
117+
{
118+
// Given
119+
URI uri = neo4j.uri();
120+
Config config = Config.builder().withEventLoopThreads( 2 ).withoutEncryption().build();
121+
int start = Thread.activeCount();
122+
123+
// When
124+
driver = GraphDatabase.driver( uri, neo4j.authToken(), config );
125+
126+
for ( int i = 0; i < 1000; i++ )
127+
{
128+
try ( Session session = driver.session() )
129+
{
130+
int size = i;
131+
session.readTransaction( tx -> tx.run( "UNWIND range(1, $size) AS x RETURN count(x)", parameters( "size", size ) ).consume() );
132+
}
133+
}
134+
135+
// Then only two more threads are added
136+
assertThat( Thread.activeCount() - start, equalTo( 2 ) );
137+
}
112138
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ private static class DriverFactoryWithSessions extends DriverFactory
262262
}
263263

264264
@Override
265-
protected Bootstrap createBootstrap()
265+
protected Bootstrap createBootstrap( int ignored )
266266
{
267267
return BootstrapFactory.newBootstrap( 1 );
268268
}

driver/src/test/java/org/neo4j/driver/internal/util/DriverFactoryWithOneEventLoopThread.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public Driver newInstance( URI uri, AuthToken authToken, Config config )
3838
}
3939

4040
@Override
41-
protected Bootstrap createBootstrap()
41+
protected Bootstrap createBootstrap( int ignored )
4242
{
4343
return BootstrapFactory.newBootstrap( 1 );
4444
}

driver/src/test/java/org/neo4j/driver/internal/util/io/ChannelTrackingDriverFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public ChannelTrackingDriverFactory( int eventLoopThreads, Clock clock )
6060
}
6161

6262
@Override
63-
protected Bootstrap createBootstrap()
63+
protected Bootstrap createBootstrap( int size )
6464
{
65-
return eventLoopThreads == 0 ? super.createBootstrap() : BootstrapFactory.newBootstrap( eventLoopThreads );
65+
return BootstrapFactory.newBootstrap( eventLoopThreads );
6666
}
6767

6868
@Override

0 commit comments

Comments
 (0)