kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | ce76b793024bcc7229676cd2694a8af1bf101eb67611db695ff6a983b0a5431d |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2016-10-20 13:21 -07:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-10-20 13:21 -07:00 |
| Parents | 65bb1c9e7c9e, 0a3b17a0cb34 |
12 files changed
modified changelog/README.md +35 −0
| @@ -1,5 +1,14 @@ | ||
| 1 | 1 | ## Changelog |
| 2 | 2 | |
| 3 | +### 3.1.2 (in progress) | |
| 4 | + | |
| 5 | +Merged from 3.0.x: | |
| 6 | + | |
| 7 | +- [bug] JAVA-1312: QueryBuilder modifies selected columns when manually selected. | |
| 8 | +- [improvement] JAVA-1303: Add missing BoundStatement.setRoutingKey(ByteBuffer...) | |
| 9 | +- [improvement] JAVA-262: Make internal executors customizable | |
| 10 | + | |
| 11 | + | |
| 3 | 12 | ### 3.1.1 |
| 4 | 13 | |
| 5 | 14 | - [bug] JAVA-1284: ClockFactory should check system property before attempting to load Native class. |
| @@ -95,6 +104,32 @@ | ||
| 95 | 104 | - [improvement] JAVA-444: Add Java process information to UUIDs.makeNode() hash. |
| 96 | 105 | |
| 97 | 106 | |
| 107 | +### 3.0.5 | |
| 108 | + | |
| 109 | +- [bug] JAVA-1312: QueryBuilder modifies selected columns when manually selected. | |
| 110 | +- [improvement] JAVA-1303: Add missing BoundStatement.setRoutingKey(ByteBuffer...) | |
| 111 | +- [improvement] JAVA-262: Make internal executors customizable | |
| 112 | + | |
| 113 | + | |
| 114 | +### 3.0.4 | |
| 115 | + | |
| 116 | +- [improvement] JAVA-1246: Driver swallows the real exception in a few cases | |
| 117 | +- [improvement] JAVA-1261: Throw error when attempting to page in I/O thread. | |
| 118 | +- [bug] JAVA-1258: Regression: Mapper cannot map a materialized view after JAVA-1126. | |
| 119 | +- [bug] JAVA-1101: Batch and BatchStatement should consider inner statements to determine query idempotence | |
| 120 | +- [improvement] JAVA-1262: Use ParseUtils for quoting & unquoting. | |
| 121 | +- [improvement] JAVA-1275: Use Netty's default thread factory | |
| 122 | +- [bug] JAVA-1285: QueryBuilder routing key auto-discovery should handle case-sensitive column names. | |
| 123 | +- [bug] JAVA-1283: Don't cache failed query preparations in the mapper. | |
| 124 | +- [improvement] JAVA-1277: Expose AbstractSession.checkNotInEventLoop. | |
| 125 | +- [bug] JAVA-1272: BuiltStatement not able to print its query string if it contains mapped UDTs. | |
| 126 | +- [bug] JAVA-1292: 'Adjusted frame length' error breaks driver's ability to read data. | |
| 127 | +- [improvement] JAVA-1293: Make DecoderForStreamIdSize.MAX_FRAME_LENGTH configurable. | |
| 128 | +- [improvement] JAVA-1053: Add a metric for authentication errors | |
| 129 | +- [improvement] JAVA-1263: Eliminate unnecessary memory copies in FrameCompressor implementations. | |
| 130 | +- [improvement] JAVA-893: Make connection pool non-blocking | |
| 131 | + | |
| 132 | + | |
| 98 | 133 | ### 3.0.3 |
| 99 | 134 | |
| 100 | 135 | - [improvement] JAVA-1147: Upgrade Netty to 4.0.37. |
modified driver-core/src/main/java/com/datastax/driver/core/BoundStatement.java +18 −0
| @@ -270,6 +270,24 @@ | ||
| 270 | 270 | return this; |
| 271 | 271 | } |
| 272 | 272 | |
| 273 | + /** | |
| 274 | + * Sets the routing key for this bound statement, when the query partition key is composite and the routing key must | |
| 275 | + * be built from multiple values. | |
| 276 | + * <p/> | |
| 277 | + * This is useful when the routing key can neither be set on the {@code PreparedStatement} this bound statement | |
| 278 | + * was built from, nor automatically computed from bound variables. In particular, this is the case if the | |
| 279 | + * partition key is composite and only some of its components are bound. | |
| 280 | + * | |
| 281 | + * @param routingKeyComponents the raw (binary) values to compose to obtain | |
| 282 | + * the routing key. | |
| 283 | + * @return this {@code BoundStatement} object. | |
| 284 | + * @see BoundStatement#getRoutingKey | |
| 285 | + */ | |
| 286 | + public BoundStatement setRoutingKey(ByteBuffer... routingKeyComponents) { | |
| 287 | + this.routingKey = SimpleStatement.compose(routingKeyComponents); | |
| 288 | + return this; | |
| 289 | + } | |
| 290 | + | |
| 273 | 291 | /** |
| 274 | 292 | * {@inheritDoc} |
| 275 | 293 | */ |
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; |
| @@ -1227,6 +1222,19 @@ | ||
| 1227 | 1222 | return this; |
| 1228 | 1223 | } |
| 1229 | 1224 | |
| 1225 | + /** | |
| 1226 | + * Sets the threading options to use for the newly created Cluster. | |
| 1227 | + * <p/> | |
| 1228 | + * If no options are set through this method, a new instance of {@link ThreadingOptions} will be used. | |
| 1229 | + * | |
| 1230 | + * @param options the options. | |
| 1231 | + * @return this builder. | |
| 1232 | + */ | |
| 1233 | + public Builder withThreadingOptions(ThreadingOptions options) { | |
| 1234 | + configurationBuilder.withThreadingOptions(options); | |
| 1235 | + return this; | |
| 1236 | + } | |
| 1237 | + | |
| 1230 | 1238 | /** |
| 1231 | 1239 | * Set the {@link NettyOptions} to use for the newly created Cluster. |
| 1232 | 1240 | * <p/> |
| @@ -1316,20 +1324,15 @@ | ||
| 1316 | 1324 | |
| 1317 | 1325 | final ConvictionPolicy.Factory convictionPolicyFactory = new ConvictionPolicy.DefaultConvictionPolicy.Factory(); |
| 1318 | 1326 | |
| 1319 | - ScheduledThreadPoolExecutor reconnectionExecutor; | |
| 1320 | - ScheduledThreadPoolExecutor scheduledTasksExecutor; | |
| 1321 | - | |
| 1322 | - // Executor used for tasks that shouldn't be executed on an IO thread. Used for short-lived, generally non-blocking tasks | |
| 1323 | 1327 | ListeningExecutorService executor; |
| 1324 | - | |
| 1325 | - // Work Queue used by executor. | |
| 1326 | - LinkedBlockingQueue<Runnable> executorQueue; | |
| 1327 | - | |
| 1328 | - // An executor for tasks that might block some time, like creating new connection, but are generally not too critical. | |
| 1329 | 1328 | ListeningExecutorService blockingExecutor; |
| 1329 | + ScheduledExecutorService reconnectionExecutor; | |
| 1330 | + ScheduledExecutorService scheduledTasksExecutor; | |
| 1330 | 1331 | |
| 1331 | - // Work Queue used by blockingExecutor. | |
| 1332 | - LinkedBlockingQueue<Runnable> blockingExecutorQueue; | |
| 1332 | + BlockingQueue<Runnable> executorQueue; | |
| 1333 | + BlockingQueue<Runnable> blockingExecutorQueue; | |
| 1334 | + BlockingQueue<Runnable> reconnectionExecutorQueue; | |
| 1335 | + BlockingQueue<Runnable> scheduledTasksExecutorQueue; | |
| 1333 | 1336 | |
| 1334 | 1337 | ConnectionReaper reaper; |
| 1335 | 1338 | |
| @@ -1368,16 +1371,31 @@ | ||
| 1368 | 1371 | |
| 1369 | 1372 | this.configuration.register(this); |
| 1370 | 1373 | |
| 1371 | - this.executorQueue = new LinkedBlockingQueue<Runnable>(); | |
| 1372 | - this.executor = makeExecutor(NON_BLOCKING_EXECUTOR_SIZE, "worker", executorQueue); | |
| 1373 | - this.blockingExecutorQueue = new LinkedBlockingQueue<Runnable>(); | |
| 1374 | - this.blockingExecutor = makeExecutor(2, "blocking-task-worker", blockingExecutorQueue); | |
| 1375 | - this.reconnectionExecutor = new ScheduledThreadPoolExecutor(2, threadFactory("reconnection")); | |
| 1376 | - // scheduledTasksExecutor is used to process C* notifications. So having it mono-threaded ensures notifications are | |
| 1377 | - // applied in the order received. | |
| 1378 | - this.scheduledTasksExecutor = new ScheduledThreadPoolExecutor(1, threadFactory("scheduled-task-worker")); | |
| 1379 | - | |
| 1380 | - this.reaper = new ConnectionReaper(this); | |
| 1374 | + ThreadingOptions threadingOptions = this.configuration.getThreadingOptions(); | |
| 1375 | + | |
| 1376 | + // executor | |
| 1377 | + ExecutorService tmpExecutor = threadingOptions.createExecutor(clusterName); | |
| 1378 | + this.executorQueue = (tmpExecutor instanceof ThreadPoolExecutor) | |
| 1379 | + ? ((ThreadPoolExecutor) tmpExecutor).getQueue() : null; | |
| 1380 | + this.executor = MoreExecutors.listeningDecorator(tmpExecutor); | |
| 1381 | + | |
| 1382 | + // blocking executor | |
| 1383 | + ExecutorService tmpBlockingExecutor = threadingOptions.createBlockingExecutor(clusterName); | |
| 1384 | + this.blockingExecutorQueue = (tmpBlockingExecutor instanceof ThreadPoolExecutor) | |
| 1385 | + ? ((ThreadPoolExecutor) tmpBlockingExecutor).getQueue() : null; | |
| 1386 | + this.blockingExecutor = MoreExecutors.listeningDecorator(tmpBlockingExecutor); | |
| 1387 | + | |
| 1388 | + // reconnection executor | |
| 1389 | + this.reconnectionExecutor = threadingOptions.createReconnectionExecutor(clusterName); | |
| 1390 | + this.reconnectionExecutorQueue = (reconnectionExecutor instanceof ThreadPoolExecutor) | |
| 1391 | + ? ((ThreadPoolExecutor) reconnectionExecutor).getQueue() : null; | |
| 1392 | + | |
| 1393 | + // scheduled tasks executor | |
| 1394 | + this.scheduledTasksExecutor = threadingOptions.createScheduledTasksExecutor(clusterName); | |
| 1395 | + this.scheduledTasksExecutorQueue = (scheduledTasksExecutor instanceof ThreadPoolExecutor) | |
| 1396 | + ? ((ThreadPoolExecutor) scheduledTasksExecutor).getQueue() : null; | |
| 1397 | + | |
| 1398 | + this.reaper = new ConnectionReaper(threadingOptions.createReaperExecutor(clusterName)); | |
| 1381 | 1399 | this.metadata = new Metadata(this); |
| 1382 | 1400 | this.connectionFactory = new Connection.Factory(this, configuration); |
| 1383 | 1401 | this.controlConnection = new ControlConnection(this); |
| @@ -1544,29 +1562,6 @@ | ||
| 1544 | 1562 | return connectionFactory.protocolVersion; |
| 1545 | 1563 | } |
| 1546 | 1564 | |
| 1547 | - ThreadFactory threadFactory(String name) { | |
| 1548 | - return new ThreadFactoryBuilder() | |
| 1549 | - .setNameFormat(clusterName + "-" + name + "-%d") | |
| 1550 | - // Back with Netty's thread factory in order to create FastThreadLocalThread instances. This allows | |
| 1551 | - // an optimization around ThreadLocals (we could use DefaultThreadFactory directly but it creates | |
| 1552 | - // slightly different thread names, so keep we keep a ThreadFactoryBuilder wrapper for backward | |
| 1553 | - // compatibility). | |
| 1554 | - .setThreadFactory(new DefaultThreadFactory("ignored name")) | |
| 1555 | - .build(); | |
| 1556 | - } | |
| 1557 | - | |
| 1558 | - private ListeningExecutorService makeExecutor(int threads, String name, LinkedBlockingQueue<Runnable> workQueue) { | |
| 1559 | - ThreadPoolExecutor executor = new ThreadPoolExecutor(threads, | |
| 1560 | - threads, | |
| 1561 | - DEFAULT_THREAD_KEEP_ALIVE, | |
| 1562 | - TimeUnit.SECONDS, | |
| 1563 | - workQueue, | |
| 1564 | - threadFactory(name)); | |
| 1565 | - | |
| 1566 | - executor.allowCoreThreadTimeOut(true); | |
| 1567 | - return MoreExecutors.listeningDecorator(executor); | |
| 1568 | - } | |
| 1569 | - | |
| 1570 | 1565 | Cluster getCluster() { |
| 1571 | 1566 | return Cluster.this; |
| 1572 | 1567 | } |
| @@ -2864,9 +2859,9 @@ | ||
| 2864 | 2859 | } |
| 2865 | 2860 | }; |
| 2866 | 2861 | |
| 2867 | - ConnectionReaper(Cluster.Manager manager) { | |
| 2868 | - executor = Executors.newScheduledThreadPool(1, manager.threadFactory("connection-reaper")); | |
| 2869 | - executor.scheduleWithFixedDelay(reaperTask, INTERVAL_MS, INTERVAL_MS, TimeUnit.MILLISECONDS); | |
| 2862 | + ConnectionReaper(ScheduledExecutorService executor) { | |
| 2863 | + this.executor = executor; | |
| 2864 | + this.executor.scheduleWithFixedDelay(reaperTask, INTERVAL_MS, INTERVAL_MS, TimeUnit.MILLISECONDS); | |
| 2870 | 2865 | } |
| 2871 | 2866 | |
| 2872 | 2867 | 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 | +} |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Select.java +2 −0
| @@ -674,6 +674,7 @@ | ||
| 674 | 674 | public Select from(String keyspace, String table) { |
| 675 | 675 | if (previousSelection != null) |
| 676 | 676 | addName(previousSelection); |
| 677 | + previousSelection = null; | |
| 677 | 678 | return super.from(keyspace, table); |
| 678 | 679 | } |
| 679 | 680 | |
| @@ -681,6 +682,7 @@ | ||
| 681 | 682 | public Select from(TableMetadata table) { |
| 682 | 683 | if (previousSelection != null) |
| 683 | 684 | addName(previousSelection); |
| 685 | + previousSelection = null; | |
| 684 | 686 | return super.from(table); |
| 685 | 687 | } |
| 686 | 688 | } |
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 | +} |
modified driver-core/src/test/java/com/datastax/driver/core/policies/TokenAwarePolicyTest.java +73 −1
| @@ -27,7 +27,8 @@ | ||
| 27 | 27 | |
| 28 | 28 | import static com.datastax.driver.core.Assertions.assertThat; |
| 29 | 29 | import static com.datastax.driver.core.CreateCCM.TestMode.PER_METHOD; |
| 30 | -import static com.datastax.driver.core.TestUtils.*; | |
| 30 | +import static com.datastax.driver.core.TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT; | |
| 31 | +import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions; | |
| 31 | 32 | |
| 32 | 33 | @CreateCCM(PER_METHOD) |
| 33 | 34 | @CCMConfig(createCcm = false) |
| @@ -312,6 +313,77 @@ | ||
| 312 | 313 | } |
| 313 | 314 | } |
| 314 | 315 | |
| 316 | + /** | |
| 317 | + * Validates that when overriding a routing key on a {@link BoundStatement} | |
| 318 | + * using {@link BoundStatement#setRoutingKey(ByteBuffer...)} and | |
| 319 | + * {@link BoundStatement#setRoutingKey(ByteBuffer)} that this routing key is used to determine | |
| 320 | + * which hosts to route queries to. | |
| 321 | + * | |
| 322 | + * @test_category load_balancing:token_aware | |
| 323 | + */ | |
| 324 | + @Test(groups = "short") | |
| 325 | + public void should_use_provided_routing_key_boundstatement() { | |
| 326 | + // given: A 4 node cluster using TokenAwarePolicy with a replication factor of 1. | |
| 327 | + ScassandraCluster sCluster = ScassandraCluster.builder() | |
| 328 | + .withNodes(4) | |
| 329 | + .withSimpleKeyspace("keyspace", 1) | |
| 330 | + .build(); | |
| 331 | + Cluster cluster = Cluster.builder() | |
| 332 | + .addContactPoints(sCluster.address(2).getAddress()) | |
| 333 | + .withPort(sCluster.getBinaryPort()) | |
| 334 | + .withNettyOptions(nonQuietClusterCloseOptions) | |
| 335 | + // Don't shuffle replicas just to keep test deterministic. | |
| 336 | + .withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy(), false)) | |
| 337 | + .build(); | |
| 338 | + | |
| 339 | + try { | |
| 340 | + sCluster.init(); | |
| 341 | + | |
| 342 | + Session session = cluster.connect("keyspace"); | |
| 343 | + | |
| 344 | + PreparedStatement preparedStatement = session.prepare("insert into tbl (k0, v) values (?, ?)"); | |
| 345 | + // bind text values since scassandra defaults to use varchar if not primed. | |
| 346 | + // this is inconsequential in this case since we are explicitly providing the routing key. | |
| 347 | + BoundStatement bs = preparedStatement.bind("a", "b"); | |
| 348 | + | |
| 349 | + // Derive a routing key for single routing key component, this should resolve to | |
| 350 | + // '4891967783720036163' | |
| 351 | + ByteBuffer routingKey = TypeCodec.bigint().serialize(33L, ProtocolVersion.NEWEST_SUPPORTED); | |
| 352 | + bs.setRoutingKey(routingKey); | |
| 353 | + | |
| 354 | + queryTracker.query(session, 10, bs); | |
| 355 | + | |
| 356 | + // Expect only node 3 to have been queried, give it has ownership of that partition | |
| 357 | + // (token range is (4611686018427387902, 6917529027641081853]) | |
| 358 | + queryTracker.assertQueried(sCluster, 1, 1, 0); | |
| 359 | + queryTracker.assertQueried(sCluster, 1, 2, 0); | |
| 360 | + queryTracker.assertQueried(sCluster, 1, 3, 0); | |
| 361 | + queryTracker.assertQueried(sCluster, 1, 4, 10); | |
| 362 | + | |
| 363 | + // reset counts. | |
| 364 | + queryTracker.reset(); | |
| 365 | + | |
| 366 | + // Derive a routing key for multiple routing key components, this should resolve to | |
| 367 | + // '3735658072872431718' | |
| 368 | + bs = preparedStatement.bind("a", "b"); | |
| 369 | + ByteBuffer routingKeyK0Part = TypeCodec.bigint().serialize(42L, ProtocolVersion.NEWEST_SUPPORTED); | |
| 370 | + ByteBuffer routingKeyK1Part = TypeCodec.varchar().serialize("hello_world", ProtocolVersion.NEWEST_SUPPORTED); | |
| 371 | + bs.setRoutingKey(routingKeyK0Part, routingKeyK1Part); | |
| 372 | + | |
| 373 | + queryTracker.query(session, 10, bs); | |
| 374 | + | |
| 375 | + // Expect only node 3 to have been queried, give it has ownership of that partition | |
| 376 | + // (token range is (2305843009213693951, 4611686018427387902]) | |
| 377 | + queryTracker.assertQueried(sCluster, 1, 1, 0); | |
| 378 | + queryTracker.assertQueried(sCluster, 1, 2, 0); | |
| 379 | + queryTracker.assertQueried(sCluster, 1, 3, 10); | |
| 380 | + queryTracker.assertQueried(sCluster, 1, 4, 0); | |
| 381 | + } finally { | |
| 382 | + cluster.close(); | |
| 383 | + sCluster.stop(); | |
| 384 | + } | |
| 385 | + } | |
| 386 | + | |
| 315 | 387 | /** |
| 316 | 388 | * Ensures that {@link TokenAwarePolicy} will properly handle a routing key for a {@link PreparedStatement} |
| 317 | 389 | * whose table uses multiple columns for its partition key. |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +13 −0
| @@ -1156,4 +1156,17 @@ | ||
| 1156 | 1156 | .isEqualTo("INSERT INTO users (id) VALUES (42);"); |
| 1157 | 1157 | } |
| 1158 | 1158 | |
| 1159 | + /** | |
| 1160 | + * @jira_ticket JAVA-1312 | |
| 1161 | + */ | |
| 1162 | + @Test(groups = "unit") | |
| 1163 | + public void should_not_append_last_column_twice() throws Exception { | |
| 1164 | + Select.SelectionOrAlias select = select().column("a").column("b"); | |
| 1165 | + Select fromUsers1 = select.from("users"); | |
| 1166 | + Select fromUsers2 = select.from("users"); | |
| 1167 | + assertThat(fromUsers1.getQueryString()) | |
| 1168 | + .isEqualTo(fromUsers2.getQueryString()) | |
| 1169 | + .isEqualTo("SELECT a,b FROM users;"); | |
| 1170 | + } | |
| 1171 | + | |
| 1159 | 1172 | } |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperUDTTest.java +1 −1
| @@ -513,7 +513,7 @@ | ||
| 513 | 513 | manager.mapper(User.class); |
| 514 | 514 | fail("Expected IllegalArgumentException"); |
| 515 | 515 | } catch (IllegalArgumentException e) { |
| 516 | - assertThat(e.getMessage()).isEqualTo(String.format("Column mainaddress does not exist in table \"%s\".users", keyspace)); | |
| 516 | + assertThat(e.getMessage()).isIn(String.format("Column mainaddress does not exist in table \"%s\".users", keyspace)); | |
| 517 | 517 | } |
| 518 | 518 | } |
| 519 | 519 |