Skip to content

Commit d90f192

Browse files
authored
JAVA-1275: Use Netty's default thread factory (apache#734)
This makes all of our threads instances of Netty's FastThreadLocalThread, which allows an optimization of ThreadLocal variables.
1 parent 5368cc0 commit d90f192

3 files changed

Lines changed: 18 additions & 3 deletions

File tree

changelog/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [bug] JAVA-1258: Regression: Mapper cannot map a materialized view after JAVA-1126.
88
- [bug] JAVA-1101: Batch and BatchStatement should consider inner statements to determine query idempotence
99
- [improvement] JAVA-1262: Use ParseUtils for quoting & unquoting.
10+
- [improvement] JAVA-1275: Use Netty's default thread factory
1011

1112

1213
### 3.0.3

driver-core/src/main/java/com/datastax/driver/core/Cluster.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.common.base.Throwables;
2626
import com.google.common.collect.*;
2727
import com.google.common.util.concurrent.*;
28+
import io.netty.util.concurrent.DefaultThreadFactory;
2829
import org.slf4j.Logger;
2930
import org.slf4j.LoggerFactory;
3031

@@ -1515,7 +1516,14 @@ ProtocolVersion protocolVersion() {
15151516
}
15161517

15171518
ThreadFactory threadFactory(String name) {
1518-
return new ThreadFactoryBuilder().setNameFormat(clusterName + "-" + name + "-%d").build();
1519+
return new ThreadFactoryBuilder()
1520+
.setNameFormat(clusterName + "-" + name + "-%d")
1521+
// Back with Netty's thread factory in order to create FastThreadLocalThread instances. This allows
1522+
// an optimization around ThreadLocals (we could use DefaultThreadFactory directly but it creates
1523+
// slightly different thread names, so keep we keep a ThreadFactoryBuilder wrapper for backward
1524+
// compatibility).
1525+
.setThreadFactory(new DefaultThreadFactory("ignored name"))
1526+
.build();
15191527
}
15201528

15211529
private ListeningExecutorService makeExecutor(int threads, String name, LinkedBlockingQueue<Runnable> workQueue) {

driver-core/src/main/java/com/datastax/driver/core/NettyOptions.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ public class NettyOptions {
8989
*
9090
* @param threadFactory The {@link ThreadFactory} to use when creating a new {@code EventLoopGroup} instance;
9191
* The driver will provide its own internal thread factory here.
92-
* It is safe to ignore it and use another thread factory.
92+
* It is safe to ignore it and use another thread factory. Note however that for optimal
93+
* performance it is recommended to use a factory that returns
94+
* {@link io.netty.util.concurrent.FastThreadLocalThread} instances (such as Netty's
95+
* {@link java.util.concurrent.Executors.DefaultThreadFactory}).
9396
* @return the {@code EventLoopGroup} instance to use.
9497
*/
9598
public EventLoopGroup eventLoopGroup(ThreadFactory threadFactory) {
@@ -205,7 +208,10 @@ public void onClusterClose(EventLoopGroup eventLoopGroup) {
205208
*
206209
* @param threadFactory The {@link ThreadFactory} to use when creating a new {@link HashedWheelTimer} instance;
207210
* The driver will provide its own internal thread factory here.
208-
* It is safe to ignore it and use another thread factory.
211+
* It is safe to ignore it and use another thread factory. Note however that for optimal
212+
* performance it is recommended to use a factory that returns
213+
* {@link io.netty.util.concurrent.FastThreadLocalThread} instances (such as Netty's
214+
* {@link java.util.concurrent.Executors.DefaultThreadFactory}).
209215
* @return the {@link Timer} instance to use.
210216
*/
211217
public Timer timer(ThreadFactory threadFactory) {

0 commit comments

Comments
 (0)