kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 0a3b17a0cb346803e0e4134d983a0a3661b979c9504086671ff79379f74713f3 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-10-20 11:26 -07:00 |
| Committer | GitHub <noreply@github.com> · 2016-10-20 11:26 -07:00 |
| Parents | 152eef368ed0 |
7 files changed
modified changelog/README.md +1 −0
| @@ -4,6 +4,7 @@ | ||
| 4 | 4 | |
| 5 | 5 | - [bug] JAVA-1312: QueryBuilder modifies selected columns when manually selected. |
| 6 | 6 | - [improvement] JAVA-1303: Add missing BoundStatement.setRoutingKey(ByteBuffer...) |
| 7 | +- [improvement] JAVA-262: Make internal executors customizable | |
| 7 | 8 | |
| 8 | 9 | |
| 9 | 10 | ### 3.0.4 |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +47 −52
| @@ -25,7 +25,6 @@ | ||
| 25 | 25 | import com.google.common.base.Throwables; |
| 26 | 26 | import com.google.common.collect.*; |
| 27 | 27 | import com.google.common.util.concurrent.*; |
| 28 | -import io.netty.util.concurrent.DefaultThreadFactory; | |
| 29 | 28 | import org.slf4j.Logger; |
| 30 | 29 | import org.slf4j.LoggerFactory; |
| 31 | 30 | |
| @@ -71,8 +70,6 @@ | ||
| 71 | 70 | |
| 72 | 71 | @VisibleForTesting |
| 73 | 72 | static final int NEW_NODE_DELAY_SECONDS = SystemProperties.getInt("com.datastax.driver.NEW_NODE_DELAY_SECONDS", 1); |
| 74 | - private static final int NON_BLOCKING_EXECUTOR_SIZE = SystemProperties.getInt("com.datastax.driver.NON_BLOCKING_EXECUTOR_SIZE", | |
| 75 | - Runtime.getRuntime().availableProcessors()); | |
| 76 | 73 | |
| 77 | 74 | private static final ResourceBundle driverProperties = ResourceBundle.getBundle("com.datastax.driver.core.Driver"); |
| 78 | 75 | |
| @@ -80,8 +77,6 @@ | ||
| 80 | 77 | // multiple Cluster instance are created in the same JVM. |
| 81 | 78 | private static final AtomicInteger CLUSTER_ID = new AtomicInteger(0); |
| 82 | 79 | |
| 83 | - private static final int DEFAULT_THREAD_KEEP_ALIVE = 30; | |
| 84 | - | |
| 85 | 80 | private static final int NOTIF_LOCK_TIMEOUT_SECONDS = SystemProperties.getInt("com.datastax.driver.NOTIF_LOCK_TIMEOUT_SECONDS", 60); |
| 86 | 81 | |
| 87 | 82 | final Manager manager; |
| @@ -1221,6 +1216,19 @@ | ||
| 1221 | 1216 | return this; |
| 1222 | 1217 | } |
| 1223 | 1218 | |
| 1219 | + /** | |
| 1220 | + * Sets the threading options to use for the newly created Cluster. | |
| 1221 | + * <p/> | |
| 1222 | + * If no options are set through this method, a new instance of {@link ThreadingOptions} will be used. | |
| 1223 | + * | |
| 1224 | + * @param options the options. | |
| 1225 | + * @return this builder. | |
| 1226 | + */ | |
| 1227 | + public Builder withThreadingOptions(ThreadingOptions options) { | |
| 1228 | + configurationBuilder.withThreadingOptions(options); | |
| 1229 | + return this; | |
| 1230 | + } | |
| 1231 | + | |
| 1224 | 1232 | /** |
| 1225 | 1233 | * Set the {@link NettyOptions} to use for the newly created Cluster. |
| 1226 | 1234 | * <p/> |
| @@ -1310,20 +1318,15 @@ | ||
| 1310 | 1318 | |
| 1311 | 1319 | final ConvictionPolicy.Factory convictionPolicyFactory = new ConvictionPolicy.DefaultConvictionPolicy.Factory(); |
| 1312 | 1320 | |
| 1313 | - ScheduledThreadPoolExecutor reconnectionExecutor; | |
| 1314 | - ScheduledThreadPoolExecutor scheduledTasksExecutor; | |
| 1315 | - | |
| 1316 | - // Executor used for tasks that shouldn't be executed on an IO thread. Used for short-lived, generally non-blocking tasks | |
| 1317 | 1321 | ListeningExecutorService executor; |
| 1318 | - | |
| 1319 | - // Work Queue used by executor. | |
| 1320 | - LinkedBlockingQueue<Runnable> executorQueue; | |
| 1321 | - | |
| 1322 | - // An executor for tasks that might block some time, like creating new connection, but are generally not too critical. | |
| 1323 | 1322 | ListeningExecutorService blockingExecutor; |
| 1323 | + ScheduledExecutorService reconnectionExecutor; | |
| 1324 | + ScheduledExecutorService scheduledTasksExecutor; | |
| 1324 | 1325 | |
| 1325 | - // Work Queue used by blockingExecutor. | |
| 1326 | - LinkedBlockingQueue<Runnable> blockingExecutorQueue; | |
| 1326 | + BlockingQueue<Runnable> executorQueue; | |
| 1327 | + BlockingQueue<Runnable> blockingExecutorQueue; | |
| 1328 | + BlockingQueue<Runnable> reconnectionExecutorQueue; | |
| 1329 | + BlockingQueue<Runnable> scheduledTasksExecutorQueue; | |
| 1327 | 1330 | |
| 1328 | 1331 | ConnectionReaper reaper; |
| 1329 | 1332 | |
| @@ -1362,16 +1365,31 @@ | ||
| 1362 | 1365 | |
| 1363 | 1366 | this.configuration.register(this); |
| 1364 | 1367 | |
| 1365 | - this.executorQueue = new LinkedBlockingQueue<Runnable>(); | |
| 1366 | - this.executor = makeExecutor(NON_BLOCKING_EXECUTOR_SIZE, "worker", executorQueue); | |
| 1367 | - this.blockingExecutorQueue = new LinkedBlockingQueue<Runnable>(); | |
| 1368 | - this.blockingExecutor = makeExecutor(2, "blocking-task-worker", blockingExecutorQueue); | |
| 1369 | - this.reconnectionExecutor = new ScheduledThreadPoolExecutor(2, threadFactory("reconnection")); | |
| 1370 | - // scheduledTasksExecutor is used to process C* notifications. So having it mono-threaded ensures notifications are | |
| 1371 | - // applied in the order received. | |
| 1372 | - this.scheduledTasksExecutor = new ScheduledThreadPoolExecutor(1, threadFactory("scheduled-task-worker")); | |
| 1373 | - | |
| 1374 | - this.reaper = new ConnectionReaper(this); | |
| 1368 | + ThreadingOptions threadingOptions = this.configuration.getThreadingOptions(); | |
| 1369 | + | |
| 1370 | + // executor | |
| 1371 | + ExecutorService tmpExecutor = threadingOptions.createExecutor(clusterName); | |
| 1372 | + this.executorQueue = (tmpExecutor instanceof ThreadPoolExecutor) | |
| 1373 | + ? ((ThreadPoolExecutor) tmpExecutor).getQueue() : null; | |
| 1374 | + this.executor = MoreExecutors.listeningDecorator(tmpExecutor); | |
| 1375 | + | |
| 1376 | + // blocking executor | |
| 1377 | + ExecutorService tmpBlockingExecutor = threadingOptions.createBlockingExecutor(clusterName); | |
| 1378 | + this.blockingExecutorQueue = (tmpBlockingExecutor instanceof ThreadPoolExecutor) | |
| 1379 | + ? ((ThreadPoolExecutor) tmpBlockingExecutor).getQueue() : null; | |
| 1380 | + this.blockingExecutor = MoreExecutors.listeningDecorator(tmpBlockingExecutor); | |
| 1381 | + | |
| 1382 | + // reconnection executor | |
| 1383 | + this.reconnectionExecutor = threadingOptions.createReconnectionExecutor(clusterName); | |
| 1384 | + this.reconnectionExecutorQueue = (reconnectionExecutor instanceof ThreadPoolExecutor) | |
| 1385 | + ? ((ThreadPoolExecutor) reconnectionExecutor).getQueue() : null; | |
| 1386 | + | |
| 1387 | + // scheduled tasks executor | |
| 1388 | + this.scheduledTasksExecutor = threadingOptions.createScheduledTasksExecutor(clusterName); | |
| 1389 | + this.scheduledTasksExecutorQueue = (scheduledTasksExecutor instanceof ThreadPoolExecutor) | |
| 1390 | + ? ((ThreadPoolExecutor) scheduledTasksExecutor).getQueue() : null; | |
| 1391 | + | |
| 1392 | + this.reaper = new ConnectionReaper(threadingOptions.createReaperExecutor(clusterName)); | |
| 1375 | 1393 | this.metadata = new Metadata(this); |
| 1376 | 1394 | this.connectionFactory = new Connection.Factory(this, configuration); |
| 1377 | 1395 | this.controlConnection = new ControlConnection(this); |
| @@ -1515,29 +1533,6 @@ | ||
| 1515 | 1533 | return connectionFactory.protocolVersion; |
| 1516 | 1534 | } |
| 1517 | 1535 | |
| 1518 | - ThreadFactory threadFactory(String name) { | |
| 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(); | |
| 1527 | - } | |
| 1528 | - | |
| 1529 | - private ListeningExecutorService makeExecutor(int threads, String name, LinkedBlockingQueue<Runnable> workQueue) { | |
| 1530 | - ThreadPoolExecutor executor = new ThreadPoolExecutor(threads, | |
| 1531 | - threads, | |
| 1532 | - DEFAULT_THREAD_KEEP_ALIVE, | |
| 1533 | - TimeUnit.SECONDS, | |
| 1534 | - workQueue, | |
| 1535 | - threadFactory(name)); | |
| 1536 | - | |
| 1537 | - executor.allowCoreThreadTimeOut(true); | |
| 1538 | - return MoreExecutors.listeningDecorator(executor); | |
| 1539 | - } | |
| 1540 | - | |
| 1541 | 1536 | Cluster getCluster() { |
| 1542 | 1537 | return Cluster.this; |
| 1543 | 1538 | } |
| @@ -2835,9 +2830,9 @@ | ||
| 2835 | 2830 | } |
| 2836 | 2831 | }; |
| 2837 | 2832 | |
| 2838 | - ConnectionReaper(Cluster.Manager manager) { | |
| 2839 | - executor = Executors.newScheduledThreadPool(1, manager.threadFactory("connection-reaper")); | |
| 2840 | - executor.scheduleWithFixedDelay(reaperTask, INTERVAL_MS, INTERVAL_MS, TimeUnit.MILLISECONDS); | |
| 2833 | + ConnectionReaper(ScheduledExecutorService executor) { | |
| 2834 | + this.executor = executor; | |
| 2835 | + this.executor.scheduleWithFixedDelay(reaperTask, INTERVAL_MS, INTERVAL_MS, TimeUnit.MILLISECONDS); | |
| 2841 | 2836 | } |
| 2842 | 2837 | |
| 2843 | 2838 | void register(Connection connection, long terminateTime) { |
modified driver-core/src/main/java/com/datastax/driver/core/Configuration.java +24 −0
| @@ -51,6 +51,7 @@ | ||
| 51 | 51 | private final SocketOptions socketOptions; |
| 52 | 52 | private final MetricsOptions metricsOptions; |
| 53 | 53 | private final QueryOptions queryOptions; |
| 54 | + private final ThreadingOptions threadingOptions; | |
| 54 | 55 | private final NettyOptions nettyOptions; |
| 55 | 56 | private final CodecRegistry codecRegistry; |
| 56 | 57 | |
| @@ -60,6 +61,7 @@ | ||
| 60 | 61 | SocketOptions socketOptions, |
| 61 | 62 | MetricsOptions metricsOptions, |
| 62 | 63 | QueryOptions queryOptions, |
| 64 | + ThreadingOptions threadingOptions, | |
| 63 | 65 | NettyOptions nettyOptions, |
| 64 | 66 | CodecRegistry codecRegistry) { |
| 65 | 67 | this.policies = policies; |
| @@ -68,6 +70,7 @@ | ||
| 68 | 70 | this.socketOptions = socketOptions; |
| 69 | 71 | this.metricsOptions = metricsOptions; |
| 70 | 72 | this.queryOptions = queryOptions; |
| 73 | + this.threadingOptions = threadingOptions; | |
| 71 | 74 | this.nettyOptions = nettyOptions; |
| 72 | 75 | this.codecRegistry = codecRegistry; |
| 73 | 76 | } |
| @@ -85,6 +88,7 @@ | ||
| 85 | 88 | toCopy.getSocketOptions(), |
| 86 | 89 | toCopy.getMetricsOptions(), |
| 87 | 90 | toCopy.getQueryOptions(), |
| 91 | + toCopy.getThreadingOptions(), | |
| 88 | 92 | toCopy.getNettyOptions(), |
| 89 | 93 | toCopy.getCodecRegistry() |
| 90 | 94 | ); |
| @@ -153,6 +157,13 @@ | ||
| 153 | 157 | return queryOptions; |
| 154 | 158 | } |
| 155 | 159 | |
| 160 | + /** | |
| 161 | + * @return the threading options for this configuration. | |
| 162 | + */ | |
| 163 | + public ThreadingOptions getThreadingOptions() { | |
| 164 | + return threadingOptions; | |
| 165 | + } | |
| 166 | + | |
| 156 | 167 | /** |
| 157 | 168 | * Returns the {@link NettyOptions} instance for this configuration. |
| 158 | 169 | * |
| @@ -186,6 +197,7 @@ | ||
| 186 | 197 | private SocketOptions socketOptions; |
| 187 | 198 | private MetricsOptions metricsOptions; |
| 188 | 199 | private QueryOptions queryOptions; |
| 200 | + private ThreadingOptions threadingOptions; | |
| 189 | 201 | private NettyOptions nettyOptions; |
| 190 | 202 | private CodecRegistry codecRegistry; |
| 191 | 203 | |
| @@ -260,6 +272,17 @@ | ||
| 260 | 272 | return this; |
| 261 | 273 | } |
| 262 | 274 | |
| 275 | + /** | |
| 276 | + * Sets the threading options for this cluster. | |
| 277 | + * | |
| 278 | + * @param threadingOptions the threading options to set. | |
| 279 | + * @return this builder. | |
| 280 | + */ | |
| 281 | + public Builder withThreadingOptions(ThreadingOptions threadingOptions) { | |
| 282 | + this.threadingOptions = threadingOptions; | |
| 283 | + return this; | |
| 284 | + } | |
| 285 | + | |
| 263 | 286 | /** |
| 264 | 287 | * Sets the Netty options for this cluster. |
| 265 | 288 | * |
| @@ -297,6 +320,7 @@ | ||
| 297 | 320 | socketOptions != null ? socketOptions : new SocketOptions(), |
| 298 | 321 | metricsOptions != null ? metricsOptions : new MetricsOptions(), |
| 299 | 322 | queryOptions != null ? queryOptions : new QueryOptions(), |
| 323 | + threadingOptions != null ? threadingOptions : new ThreadingOptions(), | |
| 300 | 324 | nettyOptions != null ? nettyOptions : NettyOptions.DEFAULT_INSTANCE, |
| 301 | 325 | codecRegistry != null ? codecRegistry : CodecRegistry.DEFAULT_INSTANCE); |
| 302 | 326 | } |
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +4 −2
| @@ -766,9 +766,11 @@ | ||
| 766 | 766 | this.authProvider = configuration.getProtocolOptions().getAuthProvider(); |
| 767 | 767 | this.protocolVersion = configuration.getProtocolOptions().initialProtocolVersion; |
| 768 | 768 | this.nettyOptions = configuration.getNettyOptions(); |
| 769 | - this.eventLoopGroup = nettyOptions.eventLoopGroup(manager.threadFactory("nio-worker")); | |
| 769 | + this.eventLoopGroup = nettyOptions.eventLoopGroup( | |
| 770 | + manager.configuration.getThreadingOptions().createThreadFactory(manager.clusterName, "nio-worker")); | |
| 770 | 771 | this.channelClass = nettyOptions.channelClass(); |
| 771 | - this.timer = nettyOptions.timer(manager.threadFactory("timeouter")); | |
| 772 | + this.timer = nettyOptions.timer( | |
| 773 | + manager.configuration.getThreadingOptions().createThreadFactory(manager.clusterName, "timeouter")); | |
| 772 | 774 | } |
| 773 | 775 | |
| 774 | 776 | int getPort() { |
modified driver-core/src/main/java/com/datastax/driver/core/Metrics.java +68 −31
| @@ -20,6 +20,8 @@ | ||
| 20 | 20 | |
| 21 | 21 | import java.util.HashSet; |
| 22 | 22 | import java.util.Set; |
| 23 | +import java.util.concurrent.BlockingQueue; | |
| 24 | +import java.util.concurrent.ThreadPoolExecutor; | |
| 23 | 25 | |
| 24 | 26 | /** |
| 25 | 27 | * Metrics exposed by the driver. |
| @@ -78,36 +80,25 @@ | ||
| 78 | 80 | } |
| 79 | 81 | }); |
| 80 | 82 | |
| 81 | - private final Gauge<Integer> executorQueueDepth = registry.register("executor-queue-depth", new Gauge<Integer>() { | |
| 82 | - @Override | |
| 83 | - public Integer getValue() { | |
| 84 | - return manager.executorQueue.size(); | |
| 85 | - } | |
| 86 | - }); | |
| 87 | - | |
| 88 | - private final Gauge<Integer> blockingExecutorQueueDepth = registry.register("blocking-executor-queue-depth", new Gauge<Integer>() { | |
| 89 | - @Override | |
| 90 | - public Integer getValue() { | |
| 91 | - return manager.blockingExecutorQueue.size(); | |
| 92 | - } | |
| 93 | - }); | |
| 94 | - | |
| 95 | - private final Gauge<Integer> reconnectionSchedulerQueueSize = registry.register("reconnection-scheduler-task-count", new Gauge<Integer>() { | |
| 96 | - @Override | |
| 97 | - public Integer getValue() { | |
| 98 | - return manager.reconnectionExecutor.getQueue().size(); | |
| 99 | - } | |
| 100 | - }); | |
| 101 | - | |
| 102 | - private final Gauge<Integer> taskSchedulerQueueSize = registry.register("task-scheduler-task-count", new Gauge<Integer>() { | |
| 103 | - @Override | |
| 104 | - public Integer getValue() { | |
| 105 | - return manager.scheduledTasksExecutor.getQueue().size(); | |
| 106 | - } | |
| 107 | - }); | |
| 83 | + private final Gauge<Integer> executorQueueDepth; | |
| 84 | + private final Gauge<Integer> blockingExecutorQueueDepth; | |
| 85 | + private final Gauge<Integer> reconnectionSchedulerQueueSize; | |
| 86 | + private final Gauge<Integer> taskSchedulerQueueSize; | |
| 108 | 87 | |
| 109 | 88 | Metrics(Cluster.Manager manager) { |
| 110 | 89 | this.manager = manager; |
| 90 | + this.executorQueueDepth = registry.register( | |
| 91 | + "executor-queue-depth", | |
| 92 | + buildQueueSizeGauge(manager.executorQueue)); | |
| 93 | + this.blockingExecutorQueueDepth = registry.register( | |
| 94 | + "blocking-executor-queue-depth", | |
| 95 | + buildQueueSizeGauge(manager.blockingExecutorQueue)); | |
| 96 | + this.reconnectionSchedulerQueueSize = registry.register( | |
| 97 | + "reconnection-scheduler-task-count", | |
| 98 | + buildQueueSizeGauge(manager.reconnectionExecutorQueue)); | |
| 99 | + this.taskSchedulerQueueSize = registry.register( | |
| 100 | + "task-scheduler-task-count", | |
| 101 | + buildQueueSizeGauge(manager.scheduledTasksExecutorQueue)); | |
| 111 | 102 | if (manager.configuration.getMetricsOptions().isJMXReportingEnabled()) { |
| 112 | 103 | this.jmxReporter = JmxReporter.forRegistry(registry).inDomain(manager.clusterName + "-metrics").build(); |
| 113 | 104 | this.jmxReporter.start(); |
| @@ -228,30 +219,58 @@ | ||
| 228 | 219 | } |
| 229 | 220 | |
| 230 | 221 | /** |
| 231 | - * @return The number of queued up tasks in the non-blocking executor (Cassandra Java Driver workers). | |
| 222 | + * Returns the number of queued up tasks in the {@link ThreadingOptions#createExecutor(String) main internal executor}. | |
| 223 | + * <p/> | |
| 224 | + * If the executor's task queue is not accessible – which happens when the executor | |
| 225 | + * is not an instance of {@link ThreadPoolExecutor} – then this gauge returns -1. | |
| 226 | + * | |
| 227 | + * @return The number of queued up tasks in the main internal executor, | |
| 228 | + * or -1, if that number is unknown. | |
| 232 | 229 | */ |
| 233 | 230 | public Gauge<Integer> getExecutorQueueDepth() { |
| 234 | 231 | return executorQueueDepth; |
| 235 | 232 | } |
| 236 | 233 | |
| 237 | 234 | /** |
| 238 | - * @return The number of queued up tasks in the blocking executor (Cassandra Java Driver blocking tasks worker). | |
| 235 | + * Returns the number of queued up tasks in the {@link ThreadingOptions#createBlockingExecutor(String) blocking executor}. | |
| 236 | + * <p/> | |
| 237 | + * If the executor's task queue is not accessible – which happens when the executor | |
| 238 | + * is not an instance of {@link ThreadPoolExecutor} – then this gauge returns -1. | |
| 239 | + * | |
| 240 | + * @return The number of queued up tasks in the blocking executor, | |
| 241 | + * or -1, if that number is unknown. | |
| 239 | 242 | */ |
| 240 | 243 | public Gauge<Integer> getBlockingExecutorQueueDepth() { |
| 241 | 244 | return blockingExecutorQueueDepth; |
| 242 | 245 | } |
| 243 | 246 | |
| 244 | 247 | /** |
| 245 | - * @return The size of the work queue for the reconnection scheduler (Reconnection). A queue size > 0 does not | |
| 248 | + * Returns the number of queued up tasks in the {@link ThreadingOptions#createReconnectionExecutor(String) reconnection executor}. | |
| 249 | + * <p/> | |
| 250 | + * A queue size > 0 does not | |
| 246 | 251 | * necessarily indicate a backlog as some tasks may not have been scheduled to execute yet. |
| 252 | + * <p/> | |
| 253 | + * If the executor's task queue is not accessible – which happens when the executor | |
| 254 | + * is not an instance of {@link ThreadPoolExecutor} – then this gauge returns -1. | |
| 255 | + * | |
| 256 | + * @return The size of the work queue for the reconnection executor, | |
| 257 | + * or -1, if that number is unknown. | |
| 247 | 258 | */ |
| 248 | 259 | public Gauge<Integer> getReconnectionSchedulerQueueSize() { |
| 249 | 260 | return reconnectionSchedulerQueueSize; |
| 250 | 261 | } |
| 251 | 262 | |
| 252 | 263 | /** |
| 253 | - * @return The size of the work queue for the task scheduler (Scheduled Tasks). A queue size > 0 does not | |
| 264 | + * Returns the number of queued up tasks in the {@link ThreadingOptions#createScheduledTasksExecutor(String) scheduled tasks executor}. | |
| 265 | + * <p/> | |
| 266 | + * A queue size > 0 does not | |
| 254 | 267 | * necessarily indicate a backlog as some tasks may not have been scheduled to execute yet. |
| 268 | + * <p/> | |
| 269 | + * If the executor's task queue is not accessible – which happens when the executor | |
| 270 | + * is not an instance of {@link ThreadPoolExecutor} – then this gauge returns -1. | |
| 271 | + * | |
| 272 | + * @return The size of the work queue for the scheduled tasks executor, | |
| 273 | + * or -1, if that number is unknown. | |
| 255 | 274 | */ |
| 256 | 275 | public Gauge<Integer> getTaskSchedulerQueueSize() { |
| 257 | 276 | return taskSchedulerQueueSize; |
| @@ -262,6 +281,24 @@ | ||
| 262 | 281 | jmxReporter.stop(); |
| 263 | 282 | } |
| 264 | 283 | |
| 284 | + private static Gauge<Integer> buildQueueSizeGauge(final BlockingQueue<?> queue) { | |
| 285 | + if (queue != null) { | |
| 286 | + return new Gauge<Integer>() { | |
| 287 | + @Override | |
| 288 | + public Integer getValue() { | |
| 289 | + return queue.size(); | |
| 290 | + } | |
| 291 | + }; | |
| 292 | + } else { | |
| 293 | + return new Gauge<Integer>() { | |
| 294 | + @Override | |
| 295 | + public Integer getValue() { | |
| 296 | + return -1; | |
| 297 | + } | |
| 298 | + }; | |
| 299 | + } | |
| 300 | + } | |
| 301 | + | |
| 265 | 302 | /** |
| 266 | 303 | * Metrics on errors encountered. |
| 267 | 304 | */ |
added driver-core/src/main/java/com/datastax/driver/core/ThreadingOptions.java +143 −0
| @@ -0,0 +1,143 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core; | |
| 17 | + | |
| 18 | +import com.google.common.util.concurrent.ThreadFactoryBuilder; | |
| 19 | +import io.netty.util.concurrent.DefaultThreadFactory; | |
| 20 | + | |
| 21 | +import java.util.concurrent.*; | |
| 22 | + | |
| 23 | +/** | |
| 24 | + * A set of hooks that allow clients to customize the driver's internal executors. | |
| 25 | + * <p/> | |
| 26 | + * The methods in this class are invoked when the cluster initializes. To customize the behavior, extend the class and | |
| 27 | + * override the appropriate methods. | |
| 28 | + * <p/> | |
| 29 | + * This is mainly intended to allow customization and instrumentation of driver threads. Each method must return a | |
| 30 | + * newly-allocated executor; don't use a shared executor, as this could introduce unintended consequences like deadlocks | |
| 31 | + * (we're working to simplify the driver's architecture and reduce the number of executors in a future release). The | |
| 32 | + * default implementations use unbounded queues, which is appropriate when the driver is properly configured; the only | |
| 33 | + * reason you would want to use bounded queues is to limit memory consumption in case of a bug or bad configuration. In | |
| 34 | + * that case, make sure to use a {@link RejectedExecutionHandler} that throws, such as | |
| 35 | + * {@link java.util.concurrent.ThreadPoolExecutor.AbortPolicy}; a blocking handler could introduce deadlocks. | |
| 36 | + * <p/> | |
| 37 | + * Netty uses a separate pool for I/O operations, that can be configured via {@link NettyOptions}. | |
| 38 | + */ | |
| 39 | +public class ThreadingOptions { | |
| 40 | + // Kept for backward compatibility, but this should be customized via this class now | |
| 41 | + private static final int NON_BLOCKING_EXECUTOR_SIZE = SystemProperties.getInt( | |
| 42 | + "com.datastax.driver.NON_BLOCKING_EXECUTOR_SIZE", Runtime.getRuntime().availableProcessors()); | |
| 43 | + private static final int DEFAULT_THREAD_KEEP_ALIVE_SECONDS = 30; | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * Builds a thread factory for the threads created by a given executor. | |
| 47 | + * <p/> | |
| 48 | + * This is used by the default implementations in this class, and also internally to create the Netty I/O pool. | |
| 49 | + * | |
| 50 | + * @param clusterName the name of the cluster, as specified by | |
| 51 | + * {@link com.datastax.driver.core.Cluster.Builder#withClusterName(String)}. | |
| 52 | + * @param executorName a name that identifies the executor. | |
| 53 | + * @return the thread factory. | |
| 54 | + */ | |
| 55 | + public ThreadFactory createThreadFactory(String clusterName, String executorName) { | |
| 56 | + return new ThreadFactoryBuilder() | |
| 57 | + .setNameFormat(clusterName + "-" + executorName + "-%d") | |
| 58 | + // Back with Netty's thread factory in order to create FastThreadLocalThread instances. This allows | |
| 59 | + // an optimization around ThreadLocals (we could use DefaultThreadFactory directly but it creates | |
| 60 | + // slightly different thread names, so keep we keep a ThreadFactoryBuilder wrapper for backward | |
| 61 | + // compatibility). | |
| 62 | + .setThreadFactory(new DefaultThreadFactory("ignored name")) | |
| 63 | + .build(); | |
| 64 | + } | |
| 65 | + | |
| 66 | + /** | |
| 67 | + * Builds the main internal executor, used for tasks such as scheduling speculative executions, triggering | |
| 68 | + * registered {@link SchemaChangeListener}s, reacting to node state changes, and metadata updates. | |
| 69 | + * <p/> | |
| 70 | + * The default implementation sets the pool size to the number of available cores. | |
| 71 | + * | |
| 72 | + * @param clusterName the name of the cluster, as specified by | |
| 73 | + * {@link com.datastax.driver.core.Cluster.Builder#withClusterName(String)}. | |
| 74 | + * @return the executor. | |
| 75 | + */ | |
| 76 | + public ExecutorService createExecutor(String clusterName) { | |
| 77 | + ThreadPoolExecutor executor = new ThreadPoolExecutor( | |
| 78 | + NON_BLOCKING_EXECUTOR_SIZE, NON_BLOCKING_EXECUTOR_SIZE, | |
| 79 | + DEFAULT_THREAD_KEEP_ALIVE_SECONDS, TimeUnit.SECONDS, | |
| 80 | + new LinkedBlockingQueue<Runnable>(), | |
| 81 | + createThreadFactory(clusterName, "worker")); | |
| 82 | + executor.allowCoreThreadTimeOut(true); | |
| 83 | + return executor; | |
| 84 | + } | |
| 85 | + | |
| 86 | + /** | |
| 87 | + * Builds the executor used to block on new connections before they are added to a pool. | |
| 88 | + * <p/> | |
| 89 | + * The default implementation uses 2 threads. | |
| 90 | + * | |
| 91 | + * @param clusterName the name of the cluster, as specified by | |
| 92 | + * {@link com.datastax.driver.core.Cluster.Builder#withClusterName(String)}. | |
| 93 | + * @return the executor. | |
| 94 | + */ | |
| 95 | + public ExecutorService createBlockingExecutor(String clusterName) { | |
| 96 | + ThreadPoolExecutor executor = new ThreadPoolExecutor( | |
| 97 | + 2, 2, | |
| 98 | + DEFAULT_THREAD_KEEP_ALIVE_SECONDS, TimeUnit.SECONDS, | |
| 99 | + new LinkedBlockingQueue<Runnable>(), | |
| 100 | + createThreadFactory(clusterName, "worker")); | |
| 101 | + executor.allowCoreThreadTimeOut(true); | |
| 102 | + return executor; | |
| 103 | + } | |
| 104 | + | |
| 105 | + /** | |
| 106 | + * Builds the executor when reconnection attempts will be scheduled. | |
| 107 | + * <p/> | |
| 108 | + * The default implementation uses 2 threads. | |
| 109 | + * | |
| 110 | + * @param clusterName the name of the cluster, as specified by | |
| 111 | + * {@link com.datastax.driver.core.Cluster.Builder#withClusterName(String)}. | |
| 112 | + * @return the executor. | |
| 113 | + */ | |
| 114 | + public ScheduledExecutorService createReconnectionExecutor(String clusterName) { | |
| 115 | + return new ScheduledThreadPoolExecutor(2, createThreadFactory(clusterName, "reconnection")); | |
| 116 | + } | |
| 117 | + | |
| 118 | + /** | |
| 119 | + * Builds the executor to handle host state notifications from Cassandra. | |
| 120 | + * <p/> | |
| 121 | + * <b>This executor must have exactly one thread</b> so that notifications are processed in order. | |
| 122 | + * | |
| 123 | + * @param clusterName the name of the cluster, as specified by | |
| 124 | + * {@link com.datastax.driver.core.Cluster.Builder#withClusterName(String)}. | |
| 125 | + * @return the executor. | |
| 126 | + */ | |
| 127 | + public ScheduledExecutorService createScheduledTasksExecutor(String clusterName) { | |
| 128 | + return new ScheduledThreadPoolExecutor(1, createThreadFactory(clusterName, "scheduled-task-worker")); | |
| 129 | + } | |
| 130 | + | |
| 131 | + /** | |
| 132 | + * Builds the executor for an internal maintenance task used to clean up closed connections. | |
| 133 | + * <p/> | |
| 134 | + * A single scheduled task runs on this executor, so there is no reason to use more than one thread. | |
| 135 | + * | |
| 136 | + * @param clusterName the name of the cluster, as specified by | |
| 137 | + * {@link com.datastax.driver.core.Cluster.Builder#withClusterName(String)}. | |
| 138 | + * @return the executor. | |
| 139 | + */ | |
| 140 | + public ScheduledExecutorService createReaperExecutor(String clusterName) { | |
| 141 | + return new ScheduledThreadPoolExecutor(1, createThreadFactory(clusterName, "connection-reaper")); | |
| 142 | + } | |
| 143 | +} |
added driver-core/src/test/java/com/datastax/driver/core/ThreadingOptionsTest.java +162 −0
| @@ -0,0 +1,162 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core; | |
| 17 | + | |
| 18 | +import com.datastax.driver.core.policies.ConstantReconnectionPolicy; | |
| 19 | +import com.google.common.util.concurrent.ThreadFactoryBuilder; | |
| 20 | +import io.netty.util.concurrent.DefaultThreadFactory; | |
| 21 | +import org.assertj.core.api.iterable.Extractor; | |
| 22 | +import org.mockito.Mockito; | |
| 23 | +import org.testng.annotations.Test; | |
| 24 | + | |
| 25 | +import java.util.Set; | |
| 26 | +import java.util.concurrent.*; | |
| 27 | +import java.util.regex.Matcher; | |
| 28 | +import java.util.regex.Pattern; | |
| 29 | + | |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 31 | +import static org.mockito.Mockito.verify; | |
| 32 | +import static org.scassandra.http.client.ClosedConnectionReport.CloseType.CLOSE; | |
| 33 | + | |
| 34 | +public class ThreadingOptionsTest extends ScassandraTestBase { | |
| 35 | + | |
| 36 | + private String customPrefix = "custom"; | |
| 37 | + | |
| 38 | + private ThreadingOptions threadingOptions = new ThreadingOptions() { | |
| 39 | + | |
| 40 | + @Override | |
| 41 | + public ThreadFactory createThreadFactory(String clusterName, String executorName) { | |
| 42 | + return new ThreadFactoryBuilder() | |
| 43 | + .setNameFormat(clusterName + "-" + customPrefix + "-" + executorName + "-%d") | |
| 44 | + // Back with Netty's thread factory in order to create FastThreadLocalThread instances. This allows | |
| 45 | + // an optimization around ThreadLocals (we could use DefaultThreadFactory directly but it creates | |
| 46 | + // slightly different thread names, so keep we keep a ThreadFactoryBuilder wrapper for backward | |
| 47 | + // compatibility). | |
| 48 | + .setThreadFactory(new DefaultThreadFactory("ignored name")) | |
| 49 | + .setDaemon(true) | |
| 50 | + .build(); | |
| 51 | + } | |
| 52 | + | |
| 53 | + @Override | |
| 54 | + public ExecutorService createExecutor(String clusterName) { | |
| 55 | + return new ThreadPoolExecutor(1, 1, | |
| 56 | + 0L, TimeUnit.MILLISECONDS, | |
| 57 | + new LinkedBlockingQueue<Runnable>(), | |
| 58 | + createThreadFactory(clusterName, "myExecutor") | |
| 59 | + ); | |
| 60 | + } | |
| 61 | + | |
| 62 | + @Override | |
| 63 | + public ExecutorService createBlockingExecutor(String clusterName) { | |
| 64 | + return new ThreadPoolExecutor(1, 1, | |
| 65 | + 0L, TimeUnit.MILLISECONDS, | |
| 66 | + new LinkedBlockingQueue<Runnable>(), | |
| 67 | + createThreadFactory(clusterName, "myBlockingExecutor") | |
| 68 | + ); | |
| 69 | + } | |
| 70 | + | |
| 71 | + @Override | |
| 72 | + public ScheduledExecutorService createReconnectionExecutor(String clusterName) { | |
| 73 | + return new ScheduledThreadPoolExecutor(1, createThreadFactory(clusterName, "myReconnection")); | |
| 74 | + } | |
| 75 | + | |
| 76 | + @Override | |
| 77 | + public ScheduledExecutorService createScheduledTasksExecutor(String clusterName) { | |
| 78 | + return new ScheduledThreadPoolExecutor(1, createThreadFactory(clusterName, "myScheduled-task-worker")); | |
| 79 | + } | |
| 80 | + | |
| 81 | + @Override | |
| 82 | + public ScheduledExecutorService createReaperExecutor(String clusterName) { | |
| 83 | + return new ScheduledThreadPoolExecutor(1, createThreadFactory(clusterName, "myConnection-reaper")); | |
| 84 | + } | |
| 85 | + }; | |
| 86 | + | |
| 87 | + /** | |
| 88 | + * Validates that when using a provided {@link ThreadingOptions} that its methods are used for creating | |
| 89 | + * executors and that its {@link ThreadingOptions#createThreadFactory(String, String)} is used for initializing | |
| 90 | + * netty resources. | |
| 91 | + * | |
| 92 | + * @test_category configuration | |
| 93 | + */ | |
| 94 | + @Test(groups = "short") | |
| 95 | + public void should_use_provided_threading_options() { | |
| 96 | + ThreadingOptions spy = Mockito.spy(threadingOptions); | |
| 97 | + Cluster cluster = createClusterBuilder().withPoolingOptions(new PoolingOptions() | |
| 98 | + .setConnectionsPerHost(HostDistance.LOCAL, 1, 1)) | |
| 99 | + .withReconnectionPolicy(new ConstantReconnectionPolicy(100)) | |
| 100 | + .withThreadingOptions(spy).build(); | |
| 101 | + try { | |
| 102 | + String clusterName = cluster.getClusterName(); | |
| 103 | + cluster.init(); | |
| 104 | + | |
| 105 | + // Ensure each method was invoked appropriately: | |
| 106 | + // 1) 1 time for each create*Executor. | |
| 107 | + // 2) createThreadFactory for netty executor group and timeouter. | |
| 108 | + verify(spy).createExecutor(clusterName); | |
| 109 | + verify(spy).createBlockingExecutor(clusterName); | |
| 110 | + verify(spy).createReconnectionExecutor(clusterName); | |
| 111 | + verify(spy).createScheduledTasksExecutor(clusterName); | |
| 112 | + verify(spy).createReaperExecutor(clusterName); | |
| 113 | + verify(spy).createThreadFactory(clusterName, "nio-worker"); | |
| 114 | + verify(spy).createThreadFactory(clusterName, "timeouter"); | |
| 115 | + | |
| 116 | + cluster.connect(); | |
| 117 | + | |
| 118 | + // Close all connections bringing the host down, this should cause some activity on | |
| 119 | + // executor and reconnection executor. | |
| 120 | + currentClient.disableListener(); | |
| 121 | + currentClient.closeConnections(CLOSE); | |
| 122 | + TestUtils.waitForDown(TestUtils.IP_PREFIX + "1", cluster); | |
| 123 | + currentClient.enableListener(); | |
| 124 | + TestUtils.waitForUp(TestUtils.IP_PREFIX + "1", cluster); | |
| 125 | + | |
| 126 | + Set<Thread> threads = Thread.getAllStackTraces().keySet(); | |
| 127 | + for(Thread thread : threads) { | |
| 128 | + // all threads should use the custom factory and thus be marked daemon | |
| 129 | + if(thread.getName().startsWith(clusterName + "-" + customPrefix)) { | |
| 130 | + // all created threads should be daemon this should indicate that our custom thread factory was | |
| 131 | + // used. | |
| 132 | + assertThat(thread.isDaemon()).isTrue(); | |
| 133 | + } | |
| 134 | + } | |
| 135 | + | |
| 136 | + final Pattern threadNamePattern = Pattern.compile(clusterName + "-" + customPrefix + "-(.*)-0"); | |
| 137 | + | |
| 138 | + // Custom executor threads should be present. | |
| 139 | + // NOTE: we don't validate blocking executor since it is hard to deterministically cause it to be used. | |
| 140 | + assertThat(threads).extracting(new Extractor<Thread, String>() { | |
| 141 | + @Override | |
| 142 | + public String extract(Thread thread) { | |
| 143 | + Matcher matcher = threadNamePattern.matcher(thread.getName()); | |
| 144 | + if(matcher.matches()) { | |
| 145 | + return matcher.group(1); | |
| 146 | + } else { | |
| 147 | + return thread.getName(); | |
| 148 | + } | |
| 149 | + } | |
| 150 | + }).contains( | |
| 151 | + "nio-worker", | |
| 152 | + "timeouter", | |
| 153 | + "myExecutor", | |
| 154 | + "myReconnection", | |
| 155 | + "myScheduled-task-worker", | |
| 156 | + "myConnection-reaper" | |
| 157 | + ); | |
| 158 | + } finally { | |
| 159 | + cluster.close(); | |
| 160 | + } | |
| 161 | + } | |
| 162 | +} |