Skip to content

Commit d338d26

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 d338d26

File tree

11 files changed

+53
-34
lines changed

11 files changed

+53
-34
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/main/java/org/neo4j/driver/internal/async/connection/BootstrapFactory.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ private BootstrapFactory()
2828
{
2929
}
3030

31-
public static Bootstrap newBootstrap()
32-
{
33-
return newBootstrap( EventLoopGroupFactory.newEventLoopGroup() );
34-
}
35-
3631
public static Bootstrap newBootstrap( int threadCount )
3732
{
3833
return newBootstrap( EventLoopGroupFactory.newEventLoopGroup( threadCount ) );

driver/src/main/java/org/neo4j/driver/internal/async/connection/EventLoopGroupFactory.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private EventLoopGroupFactory()
4949
* Get class of {@link Channel} for {@link Bootstrap#channel(Class)} method.
5050
*
5151
* @return class of the channel, which should be consistent with {@link EventLoopGroup}s returned by
52-
* {@link #newEventLoopGroup()} and {@link #newEventLoopGroup(int)}.
52+
* {@link #newEventLoopGroup(int)}.
5353
*/
5454
public static Class<? extends Channel> channelClass()
5555
{
@@ -68,17 +68,6 @@ public static EventLoopGroup newEventLoopGroup( int threadCount )
6868
return new DriverEventLoopGroup( threadCount );
6969
}
7070

71-
/**
72-
* Create new {@link EventLoopGroup} with default thread count. Returned group should by given to
73-
* {@link Bootstrap#group(EventLoopGroup)}.
74-
*
75-
* @return new group consistent with channel class returned by {@link #channelClass()}.
76-
*/
77-
public static EventLoopGroup newEventLoopGroup()
78-
{
79-
return new DriverEventLoopGroup();
80-
}
81-
8271
/**
8372
* Assert that current thread is not an event loop used for async IO operations. This check is needed because
8473
* blocking API methods like {@link Session#run(String)} are implemented on top of corresponding async API methods

driver/src/test/java/org/neo4j/driver/ConfigTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,4 +300,19 @@ void shouldErrorWithIllegalFetchSize( long value ) throws Throwable
300300
{
301301
assertThrows( IllegalArgumentException.class, () -> Config.builder().withFetchSize( value ).build() );
302302
}
303+
304+
@ParameterizedTest
305+
@ValueSource( ints = {100, 1, 1000, Integer.MAX_VALUE} )
306+
void shouldChangeEventLoopThreads( int value ) throws Throwable
307+
{
308+
Config config = Config.builder().withEventLoopThreads( value ).build();
309+
assertThat( config.eventLoopThreads(), equalTo( value ) );
310+
}
311+
312+
@ParameterizedTest
313+
@ValueSource( ints = {0, -100, -2} )
314+
void shouldErrorWithIllegalEventLoopThreadsSize( int value ) throws Throwable
315+
{
316+
assertThrows( IllegalArgumentException.class, () -> Config.builder().withEventLoopThreads( value ).build() );
317+
}
303318
}

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

Lines changed: 3 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

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/async/connection/EventLoopGroupFactoryTest.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,6 @@ void shouldCreateEventLoopGroupWithSpecifiedThreadCount()
6363
assertThat( eventLoopGroup, instanceOf( NioEventLoopGroup.class ) );
6464
}
6565

66-
@Test
67-
void shouldCreateEventLoopGroup()
68-
{
69-
eventLoopGroup = EventLoopGroupFactory.newEventLoopGroup();
70-
assertThat( eventLoopGroup, instanceOf( NioEventLoopGroup.class ) );
71-
}
72-
7366
@Test
7467
void shouldAssertNotInEventLoopThread() throws Exception
7568
{

driver/src/test/java/org/neo4j/driver/internal/cluster/loadbalancing/RoutingTableAndConnectionPoolTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ private ConnectionPool newConnectionPool()
311311
{
312312
InternalAbstractMetrics metrics = DEV_NULL_METRICS;
313313
PoolSettings poolSettings = new PoolSettings( 10, 5000, -1, -1 );
314-
Bootstrap bootstrap = BootstrapFactory.newBootstrap();
314+
Bootstrap bootstrap = BootstrapFactory.newBootstrap( 1 );
315315
NettyChannelTracker channelTracker = new NettyChannelTracker( metrics, bootstrap.config().group().next(), logging );
316316

317317
return new TestConnectionPool( bootstrap, channelTracker, poolSettings, metrics, logging, clock, true );

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)