Elffuss BraidHub

kikocis/java-driver public

Rename threads to indicate that they are for the driver (JAVA-583).
Changeset1f8ff93daf4ca31f49d2ab54bafdbf0059689c4ff90cb56b87ad5b031df0a8d9
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-03-19 15:34 -05:00
Committerolim7t <omichallat+github@gmail.com> · 2015-03-30 16:00 +02:00
Parents7327bc67fe06

Browse the files at this changeset

3 files changed

modified driver-core/CHANGELOG.rst +2 −0
@@ -25,6 +25,8 @@
2525 - [improvement] Upgrade Netty to 4.0.26 (JAVA-622)
2626 - [improvement] Coalesce frames before flushing them to the connection
2727 (JAVA-562)
28+- [improvement] Rename threads to indicate that they are for the driver
29+ (JAVA-583)
2830
2931 Merged from 2.0.9_fixes branch:
3032
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +26 −23
@@ -1069,10 +1069,6 @@
10691069 }
10701070 }
10711071
1072- private static ThreadFactory threadFactory(String nameFormat) {
1073- return new ThreadFactoryBuilder().setNameFormat(nameFormat).build();
1074- }
1075-
10761072 static long timeSince(long startNanos, TimeUnit destUnit) {
10771073 return destUnit.convert(System.nanoTime() - startNanos, TimeUnit.NANOSECONDS);
10781074 }
@@ -1081,18 +1077,6 @@
10811077 return "cluster" + CLUSTER_ID.incrementAndGet();
10821078 }
10831079
1084- private static ListeningExecutorService makeExecutor(int threads, String name, LinkedBlockingQueue<Runnable> workQueue) {
1085- ThreadPoolExecutor executor = new ThreadPoolExecutor(threads,
1086- threads,
1087- DEFAULT_THREAD_KEEP_ALIVE,
1088- TimeUnit.SECONDS,
1089- workQueue,
1090- threadFactory(name));
1091-
1092- executor.allowCoreThreadTimeOut(true);
1093- return MoreExecutors.listeningDecorator(executor);
1094- }
1095-
10961080 /**
10971081 * The sessions and hosts managed by this a Cluster instance.
10981082 * <p>
@@ -1119,10 +1103,10 @@
11191103
11201104 final ConvictionPolicy.Factory convictionPolicyFactory = new ConvictionPolicy.Simple.Factory();
11211105
1122- final ScheduledThreadPoolExecutor reconnectionExecutor = new ScheduledThreadPoolExecutor(2, threadFactory("Reconnection-%d"));
1106+ final ScheduledThreadPoolExecutor reconnectionExecutor;
11231107 // scheduledTasksExecutor is used to process C* notifications. So having it mono-threaded ensures notifications are
11241108 // applied in the order received.
1125- final ScheduledThreadPoolExecutor scheduledTasksExecutor = new ScheduledThreadPoolExecutor(1, threadFactory("Scheduled Tasks-%d"));
1109+ final ScheduledThreadPoolExecutor scheduledTasksExecutor;
11261110
11271111 // Executor used for tasks that shouldn't be executed on an IO thread. Used for short-lived, generally non-blocking tasks
11281112 final ListeningExecutorService executor;
@@ -1158,8 +1142,10 @@
11581142
11591143 this.executor = makeExecutor(NON_BLOCKING_EXECUTOR_SIZE, "Cassandra Java Driver worker-%d", executorQueue);
11601144 this.blockingExecutor = makeExecutor(2, "Cassandra Java Driver blocking tasks worker-%d", blockingExecutorQueue);
1145+ this.reconnectionExecutor = new ScheduledThreadPoolExecutor(2, threadFactory("Reconnection-%d"));
1146+ this.scheduledTasksExecutor = new ScheduledThreadPoolExecutor(1, threadFactory("Scheduled Tasks-%d"));
11611147
1162- this.reaper = new ConnectionReaper();
1148+ this.reaper = new ConnectionReaper(this);
11631149
11641150 this.metadata = new Metadata(this);
11651151 this.contactPoints = contactPoints;
@@ -1260,6 +1246,22 @@
12601246 return connectionFactory.protocolVersion;
12611247 }
12621248
1249+ ThreadFactory threadFactory(String name) {
1250+ return new ThreadFactoryBuilder().setNameFormat(clusterName + "-" + name + "-%d").build();
1251+ }
1252+
1253+ private ListeningExecutorService makeExecutor(int threads, String name, LinkedBlockingQueue<Runnable> workQueue) {
1254+ ThreadPoolExecutor executor = new ThreadPoolExecutor(threads,
1255+ threads,
1256+ DEFAULT_THREAD_KEEP_ALIVE,
1257+ TimeUnit.SECONDS,
1258+ workQueue,
1259+ threadFactory(name));
1260+
1261+ executor.allowCoreThreadTimeOut(true);
1262+ return MoreExecutors.listeningDecorator(executor);
1263+ }
1264+
12631265 Cluster getCluster() {
12641266 return Cluster.this;
12651267 }
@@ -1338,8 +1340,8 @@
13381340
13391341 // The rest will happen asynchronously, when all connections are successfully closed
13401342 return closeFuture.compareAndSet(null, future)
1341- ? future
1342- : closeFuture.get(); // We raced, it's ok, return the future that was actually set
1343+ ? future
1344+ : closeFuture.get(); // We raced, it's ok, return the future that was actually set
13431345 }
13441346
13451347 private void shutdownNow(ExecutorService executor) {
@@ -2255,7 +2257,7 @@
22552257 static class ConnectionReaper {
22562258 private static final int INTERVAL_MS = 15000;
22572259
2258- private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1, threadFactory("Reaper-%d"));
2260+ private final ScheduledExecutorService executor;
22592261 private final Map<Connection, Long> connections = new ConcurrentHashMap<Connection, Long>();
22602262
22612263 private volatile boolean shutdown;
@@ -2278,7 +2280,8 @@
22782280 }
22792281 };
22802282
2281- ConnectionReaper() {
2283+ ConnectionReaper(Cluster.Manager manager) {
2284+ executor = Executors.newScheduledThreadPool(1, manager.threadFactory("connection-reaper"));
22822285 executor.scheduleWithFixedDelay(reaperTask, INTERVAL_MS, INTERVAL_MS, TimeUnit.MILLISECONDS);
22832286 }
22842287
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +26 −7
@@ -15,11 +15,9 @@
1515 */
1616 package com.datastax.driver.core;
1717
18+import java.lang.reflect.Constructor;
1819 import java.net.InetSocketAddress;
19-import java.util.HashSet;
20-import java.util.Iterator;
21-import java.util.List;
22-import java.util.Queue;
20+import java.util.*;
2321 import java.util.concurrent.*;
2422 import java.util.concurrent.atomic.AtomicBoolean;
2523 import java.util.concurrent.atomic.AtomicInteger;
@@ -538,8 +536,10 @@
538536
539537 public static class Factory {
540538
539+ private static final Class[] EVENT_GROUP_ARGUMENTS = {int.class, ThreadFactory.class};
541540 private static final Class<? extends EventLoopGroup> EVENT_LOOP_GROUP_CLASS;
542541 private static final Class<? extends Channel> CHANNEL_CLASS;
542+ private static final Constructor<? extends EventLoopGroup> EVENT_LOOP_GROUP_CONSTRUCTOR;
543543 static {
544544 boolean useEpoll;
545545 try {
@@ -548,6 +548,10 @@
548548 logger.info("Found Netty's native epoll transport in the classpath, "
549549 + "but NIO was forced through the FORCE_NIO system property");
550550 useEpoll = false;
551+ } else if(!System.getProperty("os.name", "").toLowerCase(Locale.US).equals("linux")) {
552+ logger.warn("Found Netty's native epoll transport, but not running on linux-based operating " +
553+ "system. Using NIO instead.");
554+ useEpoll = false;
551555 } else {
552556 logger.info("Found Netty's native epoll transport in the classpath, using it");
553557 useEpoll = true;
@@ -558,12 +562,20 @@
558562 }
559563 EVENT_LOOP_GROUP_CLASS = useEpoll ? EpollEventLoopGroup.class : NioEventLoopGroup.class;
560564 CHANNEL_CLASS = useEpoll ? EpollSocketChannel.class : NioSocketChannel.class;
565+
566+ Constructor<? extends EventLoopGroup> constructor = null;
567+ try {
568+ constructor = EVENT_LOOP_GROUP_CLASS.getDeclaredConstructor(EVENT_GROUP_ARGUMENTS);
569+ } catch (NoSuchMethodException e) {
570+ logger.warn("Could not resolve {}(int, ThreadFactory), using default constructor instead.",
571+ EVENT_LOOP_GROUP_CLASS.getName());
572+ }
573+ EVENT_LOOP_GROUP_CONSTRUCTOR = constructor;
561574 }
562575
563- public final HashedWheelTimer timer = new HashedWheelTimer(new ThreadFactoryBuilder().setNameFormat("Timeouter-%d").build());
576+ public final HashedWheelTimer timer;
564577
565578 private final EventLoopGroup eventLoopGroup;
566-
567579 private final ChannelGroup allChannels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
568580
569581 private final ConcurrentMap<Host, AtomicInteger> idGenerators = new ConcurrentHashMap<Host, AtomicInteger>();
@@ -586,12 +598,19 @@
586598 this.protocolVersion = configuration.getProtocolOptions().initialProtocolVersion;
587599
588600 try {
589- eventLoopGroup = EVENT_LOOP_GROUP_CLASS.newInstance();
601+ // Use default constructor if the specialized constructor could not be resolved.
602+ if(EVENT_LOOP_GROUP_CONSTRUCTOR != null) {
603+ eventLoopGroup = EVENT_LOOP_GROUP_CONSTRUCTOR.newInstance(0, manager.threadFactory("nio-worker"));
604+ } else {
605+ eventLoopGroup = EVENT_LOOP_GROUP_CLASS.newInstance();
606+ }
590607 } catch (Exception e) {
591608 throw new AssertionError("Could not create an instance of "
592609 + EVENT_LOOP_GROUP_CLASS.getName()
593610 + ", this should not happen");
594611 }
612+
613+ this.timer = new HashedWheelTimer(manager.threadFactory("timeouter"));
595614 }
596615
597616 public int getPort() {