kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 1f8ff93daf4ca31f49d2ab54bafdbf0059689c4ff90cb56b87ad5b031df0a8d9 |
|---|---|
| Author | Andrew Tolbert <andrew.tolbert@datastax.com> · 2015-03-19 15:34 -05:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-03-30 16:00 +02:00 |
| Parents | 7327bc67fe06 |
3 files changed
modified driver-core/CHANGELOG.rst +2 −0
| @@ -25,6 +25,8 @@ | ||
| 25 | 25 | - [improvement] Upgrade Netty to 4.0.26 (JAVA-622) |
| 26 | 26 | - [improvement] Coalesce frames before flushing them to the connection |
| 27 | 27 | (JAVA-562) |
| 28 | +- [improvement] Rename threads to indicate that they are for the driver | |
| 29 | + (JAVA-583) | |
| 28 | 30 | |
| 29 | 31 | Merged from 2.0.9_fixes branch: |
| 30 | 32 |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +26 −23
| @@ -1069,10 +1069,6 @@ | ||
| 1069 | 1069 | } |
| 1070 | 1070 | } |
| 1071 | 1071 | |
| 1072 | - private static ThreadFactory threadFactory(String nameFormat) { | |
| 1073 | - return new ThreadFactoryBuilder().setNameFormat(nameFormat).build(); | |
| 1074 | - } | |
| 1075 | - | |
| 1076 | 1072 | static long timeSince(long startNanos, TimeUnit destUnit) { |
| 1077 | 1073 | return destUnit.convert(System.nanoTime() - startNanos, TimeUnit.NANOSECONDS); |
| 1078 | 1074 | } |
| @@ -1081,18 +1077,6 @@ | ||
| 1081 | 1077 | return "cluster" + CLUSTER_ID.incrementAndGet(); |
| 1082 | 1078 | } |
| 1083 | 1079 | |
| 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 | - | |
| 1096 | 1080 | /** |
| 1097 | 1081 | * The sessions and hosts managed by this a Cluster instance. |
| 1098 | 1082 | * <p> |
| @@ -1119,10 +1103,10 @@ | ||
| 1119 | 1103 | |
| 1120 | 1104 | final ConvictionPolicy.Factory convictionPolicyFactory = new ConvictionPolicy.Simple.Factory(); |
| 1121 | 1105 | |
| 1122 | - final ScheduledThreadPoolExecutor reconnectionExecutor = new ScheduledThreadPoolExecutor(2, threadFactory("Reconnection-%d")); | |
| 1106 | + final ScheduledThreadPoolExecutor reconnectionExecutor; | |
| 1123 | 1107 | // scheduledTasksExecutor is used to process C* notifications. So having it mono-threaded ensures notifications are |
| 1124 | 1108 | // applied in the order received. |
| 1125 | - final ScheduledThreadPoolExecutor scheduledTasksExecutor = new ScheduledThreadPoolExecutor(1, threadFactory("Scheduled Tasks-%d")); | |
| 1109 | + final ScheduledThreadPoolExecutor scheduledTasksExecutor; | |
| 1126 | 1110 | |
| 1127 | 1111 | // Executor used for tasks that shouldn't be executed on an IO thread. Used for short-lived, generally non-blocking tasks |
| 1128 | 1112 | final ListeningExecutorService executor; |
| @@ -1158,8 +1142,10 @@ | ||
| 1158 | 1142 | |
| 1159 | 1143 | this.executor = makeExecutor(NON_BLOCKING_EXECUTOR_SIZE, "Cassandra Java Driver worker-%d", executorQueue); |
| 1160 | 1144 | 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")); | |
| 1161 | 1147 | |
| 1162 | - this.reaper = new ConnectionReaper(); | |
| 1148 | + this.reaper = new ConnectionReaper(this); | |
| 1163 | 1149 | |
| 1164 | 1150 | this.metadata = new Metadata(this); |
| 1165 | 1151 | this.contactPoints = contactPoints; |
| @@ -1260,6 +1246,22 @@ | ||
| 1260 | 1246 | return connectionFactory.protocolVersion; |
| 1261 | 1247 | } |
| 1262 | 1248 | |
| 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 | + | |
| 1263 | 1265 | Cluster getCluster() { |
| 1264 | 1266 | return Cluster.this; |
| 1265 | 1267 | } |
| @@ -1338,8 +1340,8 @@ | ||
| 1338 | 1340 | |
| 1339 | 1341 | // The rest will happen asynchronously, when all connections are successfully closed |
| 1340 | 1342 | 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 | |
| 1343 | 1345 | } |
| 1344 | 1346 | |
| 1345 | 1347 | private void shutdownNow(ExecutorService executor) { |
| @@ -2255,7 +2257,7 @@ | ||
| 2255 | 2257 | static class ConnectionReaper { |
| 2256 | 2258 | private static final int INTERVAL_MS = 15000; |
| 2257 | 2259 | |
| 2258 | - private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1, threadFactory("Reaper-%d")); | |
| 2260 | + private final ScheduledExecutorService executor; | |
| 2259 | 2261 | private final Map<Connection, Long> connections = new ConcurrentHashMap<Connection, Long>(); |
| 2260 | 2262 | |
| 2261 | 2263 | private volatile boolean shutdown; |
| @@ -2278,7 +2280,8 @@ | ||
| 2278 | 2280 | } |
| 2279 | 2281 | }; |
| 2280 | 2282 | |
| 2281 | - ConnectionReaper() { | |
| 2283 | + ConnectionReaper(Cluster.Manager manager) { | |
| 2284 | + executor = Executors.newScheduledThreadPool(1, manager.threadFactory("connection-reaper")); | |
| 2282 | 2285 | executor.scheduleWithFixedDelay(reaperTask, INTERVAL_MS, INTERVAL_MS, TimeUnit.MILLISECONDS); |
| 2283 | 2286 | } |
| 2284 | 2287 |
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +26 −7
| @@ -15,11 +15,9 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | +import java.lang.reflect.Constructor; | |
| 18 | 19 | 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.*; | |
| 23 | 21 | import java.util.concurrent.*; |
| 24 | 22 | import java.util.concurrent.atomic.AtomicBoolean; |
| 25 | 23 | import java.util.concurrent.atomic.AtomicInteger; |
| @@ -538,8 +536,10 @@ | ||
| 538 | 536 | |
| 539 | 537 | public static class Factory { |
| 540 | 538 | |
| 539 | + private static final Class[] EVENT_GROUP_ARGUMENTS = {int.class, ThreadFactory.class}; | |
| 541 | 540 | private static final Class<? extends EventLoopGroup> EVENT_LOOP_GROUP_CLASS; |
| 542 | 541 | private static final Class<? extends Channel> CHANNEL_CLASS; |
| 542 | + private static final Constructor<? extends EventLoopGroup> EVENT_LOOP_GROUP_CONSTRUCTOR; | |
| 543 | 543 | static { |
| 544 | 544 | boolean useEpoll; |
| 545 | 545 | try { |
| @@ -548,6 +548,10 @@ | ||
| 548 | 548 | logger.info("Found Netty's native epoll transport in the classpath, " |
| 549 | 549 | + "but NIO was forced through the FORCE_NIO system property"); |
| 550 | 550 | 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; | |
| 551 | 555 | } else { |
| 552 | 556 | logger.info("Found Netty's native epoll transport in the classpath, using it"); |
| 553 | 557 | useEpoll = true; |
| @@ -558,12 +562,20 @@ | ||
| 558 | 562 | } |
| 559 | 563 | EVENT_LOOP_GROUP_CLASS = useEpoll ? EpollEventLoopGroup.class : NioEventLoopGroup.class; |
| 560 | 564 | 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; | |
| 561 | 574 | } |
| 562 | 575 | |
| 563 | - public final HashedWheelTimer timer = new HashedWheelTimer(new ThreadFactoryBuilder().setNameFormat("Timeouter-%d").build()); | |
| 576 | + public final HashedWheelTimer timer; | |
| 564 | 577 | |
| 565 | 578 | private final EventLoopGroup eventLoopGroup; |
| 566 | - | |
| 567 | 579 | private final ChannelGroup allChannels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); |
| 568 | 580 | |
| 569 | 581 | private final ConcurrentMap<Host, AtomicInteger> idGenerators = new ConcurrentHashMap<Host, AtomicInteger>(); |
| @@ -586,12 +598,19 @@ | ||
| 586 | 598 | this.protocolVersion = configuration.getProtocolOptions().initialProtocolVersion; |
| 587 | 599 | |
| 588 | 600 | 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 | + } | |
| 590 | 607 | } catch (Exception e) { |
| 591 | 608 | throw new AssertionError("Could not create an instance of " |
| 592 | 609 | + EVENT_LOOP_GROUP_CLASS.getName() |
| 593 | 610 | + ", this should not happen"); |
| 594 | 611 | } |
| 612 | + | |
| 613 | + this.timer = new HashedWheelTimer(manager.threadFactory("timeouter")); | |
| 595 | 614 | } |
| 596 | 615 | |
| 597 | 616 | public int getPort() { |