kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 7b0de0a8ba8dfd94d68a074e40df9c318cc833af317b6a15814f66ffe0b1c9ee |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-03-30 16:03 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-03-30 16:03 +02:00 |
| Parents | fa3b8ab03aa1, 1f8ff93daf4c |
17 files changed
modified driver-core/CHANGELOG.rst +5 −0
| @@ -22,6 +22,11 @@ | ||
| 22 | 22 | - [new feature] Add getObject to BoundStatement and Row (JAVA-584) |
| 23 | 23 | - [improvement] Improve connection pool resizing algorithm (JAVA-419) |
| 24 | 24 | - [bug] Fix race condition between pool expansion and shutdown (JAVA-599) |
| 25 | +- [improvement] Upgrade Netty to 4.0.26 (JAVA-622) | |
| 26 | +- [improvement] Coalesce frames before flushing them to the connection | |
| 27 | + (JAVA-562) | |
| 28 | +- [improvement] Rename threads to indicate that they are for the driver | |
| 29 | + (JAVA-583) | |
| 25 | 30 | |
| 26 | 31 | Merged from 2.0.9_fixes branch: |
| 27 | 32 |
modified driver-core/pom.xml +22 −6
| @@ -29,7 +29,7 @@ | ||
| 29 | 29 | <dependencies> |
| 30 | 30 | <dependency> |
| 31 | 31 | <groupId>io.netty</groupId> |
| 32 | - <artifactId>netty</artifactId> | |
| 32 | + <artifactId>netty-handler</artifactId> | |
| 33 | 33 | <version>${netty.version}</version> |
| 34 | 34 | </dependency> |
| 35 | 35 | |
| @@ -62,6 +62,13 @@ | ||
| 62 | 62 | <optional>true</optional> |
| 63 | 63 | </dependency> |
| 64 | 64 | |
| 65 | + <dependency> | |
| 66 | + <groupId>io.netty</groupId> | |
| 67 | + <artifactId>netty-transport-native-epoll</artifactId> | |
| 68 | + <version>${netty.version}</version> | |
| 69 | + <optional>true</optional> | |
| 70 | + </dependency> | |
| 71 | + | |
| 65 | 72 | <!-- End of compression libraries --> |
| 66 | 73 | |
| 67 | 74 | <dependency> |
| @@ -162,7 +169,7 @@ | ||
| 162 | 169 | <configuration> |
| 163 | 170 | <manifestLocation>${project.build.directory}/META-INF-shaded</manifestLocation> |
| 164 | 171 | <instructions> |
| 165 | - <Import-Package><![CDATA[com.google.common.*;version="[14.0,19)",!org.jboss.netty.*,*]]></Import-Package> | |
| 172 | + <Import-Package><![CDATA[com.google.common.*;version="[14.0,19)",!io.netty.*,*]]></Import-Package> | |
| 166 | 173 | <Private-Package>com.datastax.shaded.*</Private-Package> |
| 167 | 174 | </instructions> |
| 168 | 175 | </configuration> |
| @@ -180,12 +187,12 @@ | ||
| 180 | 187 | <shadedArtifactAttached>true</shadedArtifactAttached> |
| 181 | 188 | <artifactSet> |
| 182 | 189 | <includes> |
| 183 | - <include>io.netty:netty</include> | |
| 190 | + <include>io.netty:*</include> | |
| 184 | 191 | </includes> |
| 185 | 192 | </artifactSet> |
| 186 | 193 | <relocations> |
| 187 | 194 | <relocation> |
| 188 | - <pattern>org.jboss.netty</pattern> | |
| 195 | + <pattern>io.netty</pattern> | |
| 189 | 196 | <shadedPattern>com.datastax.shaded.netty</shadedPattern> |
| 190 | 197 | </relocation> |
| 191 | 198 | </relocations> |
| @@ -193,8 +200,17 @@ | ||
| 193 | 200 | <transformer implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer"> |
| 194 | 201 | <resources> |
| 195 | 202 | <resource>META-INF/MANIFEST.MF</resource> |
| 196 | - <resource>META-INF/maven/io.netty/netty/pom.properties</resource> | |
| 197 | - <resource>META-INF/maven/io.netty/netty/pom.xml</resource> | |
| 203 | + <resource>META-INF/io.netty.versions.properties</resource> | |
| 204 | + <resource>META-INF/maven/io.netty/netty-buffer/pom.properties</resource> | |
| 205 | + <resource>META-INF/maven/io.netty/netty-buffer/pom.xml</resource> | |
| 206 | + <resource>META-INF/maven/io.netty/netty-codec/pom.properties</resource> | |
| 207 | + <resource>META-INF/maven/io.netty/netty-codec/pom.xml</resource> | |
| 208 | + <resource>META-INF/maven/io.netty/netty-common/pom.properties</resource> | |
| 209 | + <resource>META-INF/maven/io.netty/netty-common/pom.xml</resource> | |
| 210 | + <resource>META-INF/maven/io.netty/netty-handler/pom.properties</resource> | |
| 211 | + <resource>META-INF/maven/io.netty/netty-handler/pom.xml</resource> | |
| 212 | + <resource>META-INF/maven/io.netty/netty-transport/pom.properties</resource> | |
| 213 | + <resource>META-INF/maven/io.netty/netty-transport/pom.xml</resource> | |
| 198 | 214 | </resources> |
| 199 | 215 | </transformer> |
| 200 | 216 | <!-- Pick up the alternate manifest that was generated by the alternate execution of the bundle plugin --> |
modified driver-core/src/main/java/com/datastax/driver/core/CBUtil.java +36 −32
| @@ -22,19 +22,19 @@ | ||
| 22 | 22 | import java.nio.charset.CharacterCodingException; |
| 23 | 23 | import java.util.*; |
| 24 | 24 | |
| 25 | -import org.jboss.netty.buffer.ChannelBuffer; | |
| 26 | -import org.jboss.netty.util.CharsetUtil; | |
| 25 | +import io.netty.buffer.ByteBuf; | |
| 26 | +import io.netty.util.CharsetUtil; | |
| 27 | 27 | |
| 28 | 28 | import com.datastax.driver.core.exceptions.DriverInternalError; |
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | - * ChannelBuffer utility methods. | |
| 31 | + * ByteBuf utility methods. | |
| 32 | 32 | */ |
| 33 | -abstract class CBUtil { | |
| 33 | +abstract class CBUtil { // TODO rename | |
| 34 | 34 | |
| 35 | 35 | private CBUtil() {} |
| 36 | 36 | |
| 37 | - private static String readString(ChannelBuffer cb, int length) { | |
| 37 | + private static String readString(ByteBuf cb, int length) { | |
| 38 | 38 | try { |
| 39 | 39 | String str = cb.toString(cb.readerIndex(), length, CharsetUtil.UTF_8); |
| 40 | 40 | cb.readerIndex(cb.readerIndex() + length); |
| @@ -48,7 +48,7 @@ | ||
| 48 | 48 | } |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | - public static String readString(ChannelBuffer cb) { | |
| 51 | + public static String readString(ByteBuf cb) { | |
| 52 | 52 | try { |
| 53 | 53 | int length = cb.readUnsignedShort(); |
| 54 | 54 | return readString(cb, length); |
| @@ -57,7 +57,7 @@ | ||
| 57 | 57 | } |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | - public static void writeString(String str, ChannelBuffer cb) { | |
| 60 | + public static void writeString(String str, ByteBuf cb) { | |
| 61 | 61 | byte[] bytes = str.getBytes(CharsetUtil.UTF_8); |
| 62 | 62 | cb.writeShort(bytes.length); |
| 63 | 63 | cb.writeBytes(bytes); |
| @@ -82,7 +82,7 @@ | ||
| 82 | 82 | return utflen; |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - public static String readLongString(ChannelBuffer cb) { | |
| 85 | + public static String readLongString(ByteBuf cb) { | |
| 86 | 86 | try { |
| 87 | 87 | int length = cb.readInt(); |
| 88 | 88 | return readString(cb, length); |
| @@ -91,7 +91,7 @@ | ||
| 91 | 91 | } |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | - public static void writeLongString(String str, ChannelBuffer cb) { | |
| 94 | + public static void writeLongString(String str, ByteBuf cb) { | |
| 95 | 95 | byte[] bytes = str.getBytes(CharsetUtil.UTF_8); |
| 96 | 96 | cb.writeInt(bytes.length); |
| 97 | 97 | cb.writeBytes(bytes); |
| @@ -101,7 +101,7 @@ | ||
| 101 | 101 | return 4 + str.getBytes(CharsetUtil.UTF_8).length; |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | - public static byte[] readBytes(ChannelBuffer cb) { | |
| 104 | + public static byte[] readBytes(ByteBuf cb) { | |
| 105 | 105 | try { |
| 106 | 106 | int length = cb.readUnsignedShort(); |
| 107 | 107 | byte[] bytes = new byte[length]; |
| @@ -112,7 +112,7 @@ | ||
| 112 | 112 | } |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | - public static void writeBytes(byte[] bytes, ChannelBuffer cb) { | |
| 115 | + public static void writeBytes(byte[] bytes, ByteBuf cb) { | |
| 116 | 116 | cb.writeShort(bytes.length); |
| 117 | 117 | cb.writeBytes(bytes); |
| 118 | 118 | } |
| @@ -121,11 +121,11 @@ | ||
| 121 | 121 | return 2 + bytes.length; |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | - public static ConsistencyLevel readConsistencyLevel(ChannelBuffer cb) { | |
| 124 | + public static ConsistencyLevel readConsistencyLevel(ByteBuf cb) { | |
| 125 | 125 | return ConsistencyLevel.fromCode(cb.readUnsignedShort()); |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | - public static void writeConsistencyLevel(ConsistencyLevel consistency, ChannelBuffer cb) { | |
| 128 | + public static void writeConsistencyLevel(ConsistencyLevel consistency, ByteBuf cb) { | |
| 129 | 129 | cb.writeShort(consistency.code); |
| 130 | 130 | } |
| 131 | 131 | |
| @@ -133,7 +133,7 @@ | ||
| 133 | 133 | return 2; |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | - public static <T extends Enum<T>> T readEnumValue(Class<T> enumType, ChannelBuffer cb) { | |
| 136 | + public static <T extends Enum<T>> T readEnumValue(Class<T> enumType, ByteBuf cb) { | |
| 137 | 137 | String value = CBUtil.readString(cb); |
| 138 | 138 | try { |
| 139 | 139 | return Enum.valueOf(enumType, value.toUpperCase()); |
| @@ -142,7 +142,7 @@ | ||
| 142 | 142 | } |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | - public static <T extends Enum<T>> void writeEnumValue(T enumValue, ChannelBuffer cb) { | |
| 145 | + public static <T extends Enum<T>> void writeEnumValue(T enumValue, ByteBuf cb) { | |
| 146 | 146 | writeString(enumValue.toString(), cb); |
| 147 | 147 | } |
| 148 | 148 | |
| @@ -150,13 +150,13 @@ | ||
| 150 | 150 | return sizeOfString(enumValue.toString()); |
| 151 | 151 | } |
| 152 | 152 | |
| 153 | - public static UUID readUUID(ChannelBuffer cb) { | |
| 153 | + public static UUID readUUID(ByteBuf cb) { | |
| 154 | 154 | long msb = cb.readLong(); |
| 155 | 155 | long lsb = cb.readLong(); |
| 156 | 156 | return new UUID(msb, lsb); |
| 157 | 157 | } |
| 158 | 158 | |
| 159 | - public static void writeUUID(UUID uuid, ChannelBuffer cb) { | |
| 159 | + public static void writeUUID(UUID uuid, ByteBuf cb) { | |
| 160 | 160 | cb.writeLong(uuid.getMostSignificantBits()); |
| 161 | 161 | cb.writeLong(uuid.getLeastSignificantBits()); |
| 162 | 162 | } |
| @@ -165,7 +165,7 @@ | ||
| 165 | 165 | return 16; |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | - public static List<String> readStringList(ChannelBuffer cb) { | |
| 168 | + public static List<String> readStringList(ByteBuf cb) { | |
| 169 | 169 | int length = cb.readUnsignedShort(); |
| 170 | 170 | List<String> l = new ArrayList<String>(length); |
| 171 | 171 | for (int i = 0; i < length; i++) |
| @@ -173,7 +173,7 @@ | ||
| 173 | 173 | return l; |
| 174 | 174 | } |
| 175 | 175 | |
| 176 | - public static void writeStringList(List<String> l, ChannelBuffer cb) { | |
| 176 | + public static void writeStringList(List<String> l, ByteBuf cb) { | |
| 177 | 177 | cb.writeShort(l.size()); |
| 178 | 178 | for (String str : l) |
| 179 | 179 | writeString(str, cb); |
| @@ -186,7 +186,7 @@ | ||
| 186 | 186 | return size; |
| 187 | 187 | } |
| 188 | 188 | |
| 189 | - public static Map<String, String> readStringMap(ChannelBuffer cb) { | |
| 189 | + public static Map<String, String> readStringMap(ByteBuf cb) { | |
| 190 | 190 | int length = cb.readUnsignedShort(); |
| 191 | 191 | Map<String, String> m = new HashMap<String, String>(length); |
| 192 | 192 | for (int i = 0; i < length; i++) { |
| @@ -197,7 +197,7 @@ | ||
| 197 | 197 | return m; |
| 198 | 198 | } |
| 199 | 199 | |
| 200 | - public static void writeStringMap(Map<String, String> m, ChannelBuffer cb) { | |
| 200 | + public static void writeStringMap(Map<String, String> m, ByteBuf cb) { | |
| 201 | 201 | cb.writeShort(m.size()); |
| 202 | 202 | for (Map.Entry<String, String> entry : m.entrySet()) { |
| 203 | 203 | writeString(entry.getKey(), cb); |
| @@ -214,7 +214,7 @@ | ||
| 214 | 214 | return size; |
| 215 | 215 | } |
| 216 | 216 | |
| 217 | - public static Map<String, List<String>> readStringToStringListMap(ChannelBuffer cb) { | |
| 217 | + public static Map<String, List<String>> readStringToStringListMap(ByteBuf cb) { | |
| 218 | 218 | int length = cb.readUnsignedShort(); |
| 219 | 219 | Map<String, List<String>> m = new HashMap<String, List<String>>(length); |
| 220 | 220 | for (int i = 0; i < length; i++) { |
| @@ -225,7 +225,7 @@ | ||
| 225 | 225 | return m; |
| 226 | 226 | } |
| 227 | 227 | |
| 228 | - public static void writeStringToStringListMap(Map<String, List<String>> m, ChannelBuffer cb) { | |
| 228 | + public static void writeStringToStringListMap(Map<String, List<String>> m, ByteBuf cb) { | |
| 229 | 229 | cb.writeShort(m.size()); |
| 230 | 230 | for (Map.Entry<String, List<String>> entry : m.entrySet()) { |
| 231 | 231 | writeString(entry.getKey(), cb); |
| @@ -242,12 +242,16 @@ | ||
| 242 | 242 | return size; |
| 243 | 243 | } |
| 244 | 244 | |
| 245 | - public static ByteBuffer readValue(ChannelBuffer cb) { | |
| 245 | + public static ByteBuffer readValue(ByteBuf cb) { | |
| 246 | 246 | int length = cb.readInt(); |
| 247 | - return length < 0 ? null : cb.readSlice(length).toByteBuffer(); | |
| 247 | + if (length < 0) | |
| 248 | + return null; | |
| 249 | + ByteBuf slice = cb.readSlice(length); | |
| 250 | + | |
| 251 | + return ByteBuffer.wrap(readRawBytes(slice)); | |
| 248 | 252 | } |
| 249 | 253 | |
| 250 | - public static void writeValue(byte[] bytes, ChannelBuffer cb) { | |
| 254 | + public static void writeValue(byte[] bytes, ByteBuf cb) { | |
| 251 | 255 | if (bytes == null) { |
| 252 | 256 | cb.writeInt(-1); |
| 253 | 257 | return; |
| @@ -257,7 +261,7 @@ | ||
| 257 | 261 | cb.writeBytes(bytes); |
| 258 | 262 | } |
| 259 | 263 | |
| 260 | - public static void writeValue(ByteBuffer bytes, ChannelBuffer cb) { | |
| 264 | + public static void writeValue(ByteBuffer bytes, ByteBuf cb) { | |
| 261 | 265 | if (bytes == null) { |
| 262 | 266 | cb.writeInt(-1); |
| 263 | 267 | return; |
| @@ -275,7 +279,7 @@ | ||
| 275 | 279 | return 4 + (bytes == null ? 0 : bytes.remaining()); |
| 276 | 280 | } |
| 277 | 281 | |
| 278 | - public static List<ByteBuffer> readValueList(ChannelBuffer cb) { | |
| 282 | + public static List<ByteBuffer> readValueList(ByteBuf cb) { | |
| 279 | 283 | int size = cb.readUnsignedShort(); |
| 280 | 284 | if (size == 0) |
| 281 | 285 | return Collections.<ByteBuffer>emptyList(); |
| @@ -286,7 +290,7 @@ | ||
| 286 | 290 | return l; |
| 287 | 291 | } |
| 288 | 292 | |
| 289 | - public static void writeValueList(List<ByteBuffer> values, ChannelBuffer cb) { | |
| 293 | + public static void writeValueList(List<ByteBuffer> values, ByteBuf cb) { | |
| 290 | 294 | cb.writeShort(values.size()); |
| 291 | 295 | for (ByteBuffer value : values) |
| 292 | 296 | CBUtil.writeValue(value, cb); |
| @@ -299,7 +303,7 @@ | ||
| 299 | 303 | return size; |
| 300 | 304 | } |
| 301 | 305 | |
| 302 | - public static InetSocketAddress readInet(ChannelBuffer cb) { | |
| 306 | + public static InetSocketAddress readInet(ByteBuf cb) { | |
| 303 | 307 | int addrSize = cb.readByte(); |
| 304 | 308 | byte[] address = new byte[addrSize]; |
| 305 | 309 | cb.readBytes(address); |
| @@ -311,7 +315,7 @@ | ||
| 311 | 315 | } |
| 312 | 316 | } |
| 313 | 317 | |
| 314 | - public static void writeInet(InetSocketAddress inet, ChannelBuffer cb) { | |
| 318 | + public static void writeInet(InetSocketAddress inet, ByteBuf cb) { | |
| 315 | 319 | byte[] address = inet.getAddress().getAddress(); |
| 316 | 320 | |
| 317 | 321 | cb.writeByte(address.length); |
| @@ -328,7 +332,7 @@ | ||
| 328 | 332 | * Reads *all* readable bytes from {@code cb} and return them. |
| 329 | 333 | * If {@code cb} is backed by an array, this will return the underlying array directly, without copy. |
| 330 | 334 | */ |
| 331 | - public static byte[] readRawBytes(ChannelBuffer cb) { | |
| 335 | + public static byte[] readRawBytes(ByteBuf cb) { | |
| 332 | 336 | if (cb.hasArray() && cb.readableBytes() == cb.array().length) { |
| 333 | 337 | // Move the readerIndex just so we consistently consume the input |
| 334 | 338 | cb.readerIndex(cb.writerIndex()); |
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 +247 −100
| @@ -15,32 +15,40 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import javax.net.ssl.SSLEngine; | |
| 18 | +import java.lang.reflect.Constructor; | |
| 19 | 19 | import java.net.InetSocketAddress; |
| 20 | -import java.util.Iterator; | |
| 20 | +import java.util.*; | |
| 21 | 21 | import java.util.concurrent.*; |
| 22 | 22 | import java.util.concurrent.atomic.AtomicBoolean; |
| 23 | 23 | import java.util.concurrent.atomic.AtomicInteger; |
| 24 | 24 | import java.util.concurrent.atomic.AtomicReference; |
| 25 | 25 | |
| 26 | +import com.google.common.collect.Lists; | |
| 26 | 27 | import com.google.common.util.concurrent.AbstractFuture; |
| 27 | 28 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 28 | 29 | import com.google.common.util.concurrent.Uninterruptibles; |
| 29 | -import org.jboss.netty.bootstrap.ClientBootstrap; | |
| 30 | -import org.jboss.netty.channel.*; | |
| 31 | -import org.jboss.netty.channel.group.ChannelGroup; | |
| 32 | -import org.jboss.netty.channel.group.DefaultChannelGroup; | |
| 33 | -import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; | |
| 34 | -import org.jboss.netty.handler.ssl.SslHandler; | |
| 35 | -import org.jboss.netty.handler.timeout.IdleStateAwareChannelUpstreamHandler; | |
| 36 | -import org.jboss.netty.handler.timeout.IdleStateEvent; | |
| 37 | -import org.jboss.netty.handler.timeout.IdleStateHandler; | |
| 38 | -import org.jboss.netty.util.HashedWheelTimer; | |
| 39 | -import org.jboss.netty.util.Timeout; | |
| 40 | -import org.jboss.netty.util.TimerTask; | |
| 30 | +import io.netty.bootstrap.Bootstrap; | |
| 31 | +import io.netty.channel.*; | |
| 32 | +import io.netty.channel.epoll.EpollEventLoopGroup; | |
| 33 | +import io.netty.channel.epoll.EpollSocketChannel; | |
| 34 | +import io.netty.channel.group.ChannelGroup; | |
| 35 | +import io.netty.channel.group.DefaultChannelGroup; | |
| 36 | +import io.netty.channel.nio.NioEventLoopGroup; | |
| 37 | +import io.netty.channel.socket.SocketChannel; | |
| 38 | +import io.netty.channel.socket.nio.NioSocketChannel; | |
| 39 | +import io.netty.handler.ssl.SslHandler; | |
| 40 | +import io.netty.handler.timeout.IdleStateEvent; | |
| 41 | +import io.netty.handler.timeout.IdleStateHandler; | |
| 42 | +import io.netty.util.HashedWheelTimer; | |
| 43 | +import io.netty.util.Timeout; | |
| 44 | +import io.netty.util.TimerTask; | |
| 45 | +import io.netty.util.concurrent.GlobalEventExecutor; | |
| 46 | +import javax.net.ssl.SSLEngine; | |
| 41 | 47 | import org.slf4j.Logger; |
| 42 | 48 | import org.slf4j.LoggerFactory; |
| 43 | 49 | |
| 50 | +import static io.netty.handler.timeout.IdleState.ALL_IDLE; | |
| 51 | + | |
| 44 | 52 | import com.datastax.driver.core.exceptions.AuthenticationException; |
| 45 | 53 | import com.datastax.driver.core.exceptions.DriverInternalError; |
| 46 | 54 | |
| @@ -56,6 +64,9 @@ | ||
| 56 | 64 | private static final Logger logger = LoggerFactory.getLogger(Connection.class); |
| 57 | 65 | private static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; |
| 58 | 66 | |
| 67 | + private static final boolean DISABLE_COALESCING = SystemProperties.getBoolean("com.datastax.driver.DISABLE_COALESCING", false); | |
| 68 | + private static final boolean FORCE_NIO = SystemProperties.getBoolean("com.datastax.driver.FORCE_NIO", false); | |
| 69 | + | |
| 59 | 70 | public final InetSocketAddress address; |
| 60 | 71 | private final String name; |
| 61 | 72 | |
| @@ -89,24 +100,26 @@ | ||
| 89 | 100 | this.name = name; |
| 90 | 101 | |
| 91 | 102 | try { |
| 92 | - ClientBootstrap bootstrap = factory.newBootstrap(); | |
| 103 | + Bootstrap bootstrap = factory.newBootstrap(); | |
| 93 | 104 | ProtocolOptions protocolOptions = factory.configuration.getProtocolOptions(); |
| 94 | 105 | int protocolVersion = factory.protocolVersion == 1 ? 1 : 2; |
| 95 | - bootstrap.setPipelineFactory(new PipelineFactory(this, protocolVersion, protocolOptions.getCompression().compressor, protocolOptions.getSSLOptions(), | |
| 96 | - factory.configuration.getPoolingOptions().getHeartbeatIntervalSeconds(), factory.timer)); | |
| 106 | + bootstrap.handler( | |
| 107 | + new Initializer(this, protocolVersion, protocolOptions.getCompression().compressor, protocolOptions.getSSLOptions(), | |
| 108 | + factory.configuration.getPoolingOptions().getHeartbeatIntervalSeconds())); | |
| 97 | 109 | |
| 98 | 110 | ChannelFuture future = bootstrap.connect(address); |
| 99 | 111 | |
| 100 | 112 | writer.incrementAndGet(); |
| 101 | 113 | try { |
| 102 | 114 | // Wait until the connection attempt succeeds or fails. |
| 103 | - this.channel = future.awaitUninterruptibly().getChannel(); | |
| 104 | - this.factory.allChannels.add(this.channel); | |
| 115 | + this.channel = future.awaitUninterruptibly().channel(); | |
| 105 | 116 | if (!future.isSuccess()) { |
| 106 | 117 | if (logger.isDebugEnabled()) |
| 107 | - logger.debug(String.format("%s Error connecting to %s%s", this, address, extractMessage(future.getCause()))); | |
| 108 | - throw defunct(new TransportException(address, "Cannot connect", future.getCause())); | |
| 118 | + logger.debug(String.format("%s Error connecting to %s%s", this, address, extractMessage(future.cause()))); | |
| 119 | + throw defunct(new TransportException(address, "Cannot connect", future.cause())); | |
| 109 | 120 | } |
| 121 | + this.factory.allChannels.add(this.channel); | |
| 122 | + channel.closeFuture().addListener(new ChannelCloseListener()); | |
| 110 | 123 | } finally { |
| 111 | 124 | writer.decrementAndGet(); |
| 112 | 125 | } |
| @@ -400,8 +413,12 @@ | ||
| 400 | 413 | |
| 401 | 414 | logger.trace("{} writing request {}", this, request); |
| 402 | 415 | writer.incrementAndGet(); |
| 403 | - channel.write(request).addListener(writeHandler(request, handler)); | |
| 404 | 416 | |
| 417 | + if (DISABLE_COALESCING) { | |
| 418 | + channel.writeAndFlush(request).addListener(writeHandler(request, handler)); | |
| 419 | + } else { | |
| 420 | + flush(new FlushItem(channel, request, writeHandler(request, handler))); | |
| 421 | + } | |
| 405 | 422 | if (startTimeout) |
| 406 | 423 | handler.startTimeout(); |
| 407 | 424 | |
| @@ -422,10 +439,10 @@ | ||
| 422 | 439 | dispatcher.removeHandler(handler, true); |
| 423 | 440 | |
| 424 | 441 | final ConnectionException ce; |
| 425 | - if (writeFuture.getCause() instanceof java.nio.channels.ClosedChannelException) { | |
| 442 | + if (writeFuture.cause() instanceof java.nio.channels.ClosedChannelException) { | |
| 426 | 443 | ce = new TransportException(address, "Error writing: Closed channel"); |
| 427 | 444 | } else { |
| 428 | - ce = new TransportException(address, "Error writing", writeFuture.getCause()); | |
| 445 | + ce = new TransportException(address, "Error writing", writeFuture.cause()); | |
| 429 | 446 | } |
| 430 | 447 | final long latency = System.nanoTime() - handler.startTime; |
| 431 | 448 | // This handler is executed while holding the writeLock of the channel. |
| @@ -519,12 +536,47 @@ | ||
| 519 | 536 | |
| 520 | 537 | public static class Factory { |
| 521 | 538 | |
| 522 | - private final ExecutorService bossExecutor = Executors.newCachedThreadPool(); | |
| 523 | - private final ExecutorService workerExecutor = Executors.newCachedThreadPool(); | |
| 524 | - public final HashedWheelTimer timer = new HashedWheelTimer(new ThreadFactoryBuilder().setNameFormat("Timeouter-%d").build()); | |
| 539 | + private static final Class[] EVENT_GROUP_ARGUMENTS = {int.class, ThreadFactory.class}; | |
| 540 | + private static final Class<? extends EventLoopGroup> EVENT_LOOP_GROUP_CLASS; | |
| 541 | + private static final Class<? extends Channel> CHANNEL_CLASS; | |
| 542 | + private static final Constructor<? extends EventLoopGroup> EVENT_LOOP_GROUP_CONSTRUCTOR; | |
| 543 | + static { | |
| 544 | + boolean useEpoll; | |
| 545 | + try { | |
| 546 | + Class.forName("io.netty.channel.epoll.EpollEventLoopGroup"); | |
| 547 | + if (FORCE_NIO) { | |
| 548 | + logger.info("Found Netty's native epoll transport in the classpath, " | |
| 549 | + + "but NIO was forced through the FORCE_NIO system property"); | |
| 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; | |
| 555 | + } else { | |
| 556 | + logger.info("Found Netty's native epoll transport in the classpath, using it"); | |
| 557 | + useEpoll = true; | |
| 558 | + } | |
| 559 | + } catch (ClassNotFoundException e) { | |
| 560 | + logger.info("Did not find Netty's native epoll transport in the classpath, defaulting to NIO"); | |
| 561 | + useEpoll = false; | |
| 562 | + } | |
| 563 | + EVENT_LOOP_GROUP_CLASS = useEpoll ? EpollEventLoopGroup.class : NioEventLoopGroup.class; | |
| 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; | |
| 574 | + } | |
| 575 | + | |
| 576 | + public final HashedWheelTimer timer; | |
| 525 | 577 | |
| 526 | - private final ChannelFactory channelFactory = new NioClientSocketChannelFactory(bossExecutor, workerExecutor); | |
| 527 | - private final ChannelGroup allChannels = new DefaultChannelGroup(); | |
| 578 | + private final EventLoopGroup eventLoopGroup; | |
| 579 | + private final ChannelGroup allChannels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); | |
| 528 | 580 | |
| 529 | 581 | private final ConcurrentMap<Host, AtomicInteger> idGenerators = new ConcurrentHashMap<Host, AtomicInteger>(); |
| 530 | 582 | public final DefaultResponseHandler defaultHandler; |
| @@ -544,6 +596,21 @@ | ||
| 544 | 596 | this.configuration = configuration; |
| 545 | 597 | this.authProvider = configuration.getProtocolOptions().getAuthProvider(); |
| 546 | 598 | this.protocolVersion = configuration.getProtocolOptions().initialProtocolVersion; |
| 599 | + | |
| 600 | + try { | |
| 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 | + } | |
| 607 | + } catch (Exception e) { | |
| 608 | + throw new AssertionError("Could not create an instance of " | |
| 609 | + + EVENT_LOOP_GROUP_CLASS.getName() | |
| 610 | + + ", this should not happen"); | |
| 611 | + } | |
| 612 | + | |
| 613 | + this.timer = new HashedWheelTimer(manager.threadFactory("timeouter")); | |
| 547 | 614 | } |
| 548 | 615 | |
| 549 | 616 | public int getPort() { |
| @@ -599,30 +666,32 @@ | ||
| 599 | 666 | return configuration.getSocketOptions().getReadTimeoutMillis(); |
| 600 | 667 | } |
| 601 | 668 | |
| 602 | - private ClientBootstrap newBootstrap() { | |
| 603 | - ClientBootstrap b = new ClientBootstrap(channelFactory); | |
| 669 | + private Bootstrap newBootstrap() { | |
| 670 | + Bootstrap b = new Bootstrap(); | |
| 671 | + b.group(eventLoopGroup) | |
| 672 | + .channel(CHANNEL_CLASS); | |
| 604 | 673 | |
| 605 | 674 | SocketOptions options = configuration.getSocketOptions(); |
| 606 | 675 | |
| 607 | - b.setOption("connectTimeoutMillis", options.getConnectTimeoutMillis()); | |
| 676 | + b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, options.getConnectTimeoutMillis()); | |
| 608 | 677 | Boolean keepAlive = options.getKeepAlive(); |
| 609 | 678 | if (keepAlive != null) |
| 610 | - b.setOption("keepAlive", keepAlive); | |
| 679 | + b.option(ChannelOption.SO_KEEPALIVE, keepAlive); | |
| 611 | 680 | Boolean reuseAddress = options.getReuseAddress(); |
| 612 | 681 | if (reuseAddress != null) |
| 613 | - b.setOption("reuseAddress", reuseAddress); | |
| 682 | + b.option(ChannelOption.SO_REUSEADDR, reuseAddress); | |
| 614 | 683 | Integer soLinger = options.getSoLinger(); |
| 615 | 684 | if (soLinger != null) |
| 616 | - b.setOption("soLinger", soLinger); | |
| 685 | + b.option(ChannelOption.SO_LINGER, soLinger); | |
| 617 | 686 | Boolean tcpNoDelay = options.getTcpNoDelay(); |
| 618 | 687 | if (tcpNoDelay != null) |
| 619 | - b.setOption("tcpNoDelay", tcpNoDelay); | |
| 688 | + b.option(ChannelOption.TCP_NODELAY, tcpNoDelay); | |
| 620 | 689 | Integer receiveBufferSize = options.getReceiveBufferSize(); |
| 621 | 690 | if (receiveBufferSize != null) |
| 622 | - b.setOption("receiveBufferSize", receiveBufferSize); | |
| 691 | + b.option(ChannelOption.SO_RCVBUF, receiveBufferSize); | |
| 623 | 692 | Integer sendBufferSize = options.getSendBufferSize(); |
| 624 | 693 | if (sendBufferSize != null) |
| 625 | - b.setOption("sendBufferSize", sendBufferSize); | |
| 694 | + b.option(ChannelOption.SO_SNDBUF, sendBufferSize); | |
| 626 | 695 | |
| 627 | 696 | return b; |
| 628 | 697 | } |
| @@ -635,14 +704,98 @@ | ||
| 635 | 704 | // we're not on an I/O thread or anything, so just call await. |
| 636 | 705 | allChannels.close().awaitUninterruptibly(); |
| 637 | 706 | |
| 638 | - // This will call shutdownNow on the boss and worker executor. Since this is called | |
| 639 | - // only once all connection have been individually closed, it's fine. | |
| 640 | - channelFactory.releaseExternalResources(); | |
| 707 | + // This will shut down I/O threads. Since this is called only once all connection have | |
| 708 | + // been individually closed, it's fine. | |
| 709 | + eventLoopGroup.shutdownGracefully().syncUninterruptibly(); | |
| 641 | 710 | timer.stop(); |
| 642 | 711 | } |
| 643 | 712 | } |
| 644 | 713 | |
| 645 | - private class Dispatcher extends IdleStateAwareChannelUpstreamHandler { | |
| 714 | + private static final class Flusher implements Runnable { | |
| 715 | + final EventLoop eventLoop; | |
| 716 | + final Queue<FlushItem> queued = new ConcurrentLinkedQueue<FlushItem>(); | |
| 717 | + final AtomicBoolean running = new AtomicBoolean(false); | |
| 718 | + final HashSet<Channel> channels = new HashSet<Channel>(); | |
| 719 | + final List<FlushItem> flushed = Lists.newArrayListWithExpectedSize(50); | |
| 720 | + int runsSinceFlush = 0; | |
| 721 | + int runsWithNoWork = 0; | |
| 722 | + | |
| 723 | + private Flusher(EventLoop eventLoop) { | |
| 724 | + this.eventLoop = eventLoop; | |
| 725 | + } | |
| 726 | + | |
| 727 | + void start() { | |
| 728 | + if (!running.get() && running.compareAndSet(false, true)) { | |
| 729 | + this.eventLoop.execute(this); | |
| 730 | + } | |
| 731 | + } | |
| 732 | + | |
| 733 | + @Override | |
| 734 | + public void run() { | |
| 735 | + | |
| 736 | + boolean doneWork = false; | |
| 737 | + FlushItem flush; | |
| 738 | + while (null != (flush = queued.poll())) { | |
| 739 | + channels.add(flush.channel); | |
| 740 | + flush.channel.write(flush.request).addListener(flush.listener); | |
| 741 | + flushed.add(flush); | |
| 742 | + doneWork = true; | |
| 743 | + } | |
| 744 | + | |
| 745 | + runsSinceFlush++; | |
| 746 | + | |
| 747 | + if (!doneWork || runsSinceFlush > 2 || flushed.size() > 50) { | |
| 748 | + for (Channel channel : channels) | |
| 749 | + channel.flush(); | |
| 750 | + | |
| 751 | + channels.clear(); | |
| 752 | + flushed.clear(); | |
| 753 | + runsSinceFlush = 0; | |
| 754 | + } | |
| 755 | + | |
| 756 | + if (doneWork) { | |
| 757 | + runsWithNoWork = 0; | |
| 758 | + } else { | |
| 759 | + // either reschedule or cancel | |
| 760 | + if (++runsWithNoWork > 5) { | |
| 761 | + running.set(false); | |
| 762 | + if (queued.isEmpty() || !running.compareAndSet(false, true)) | |
| 763 | + return; | |
| 764 | + } | |
| 765 | + } | |
| 766 | + | |
| 767 | + eventLoop.schedule(this, 10000, TimeUnit.NANOSECONDS); | |
| 768 | + } | |
| 769 | + } | |
| 770 | + | |
| 771 | + private static final ConcurrentMap<EventLoop, Flusher> flusherLookup = new ConcurrentHashMap<EventLoop, Flusher>(); | |
| 772 | + | |
| 773 | + private static class FlushItem { | |
| 774 | + final Channel channel; | |
| 775 | + final Object request; | |
| 776 | + final ChannelFutureListener listener; | |
| 777 | + | |
| 778 | + private FlushItem(Channel channel, Object request, ChannelFutureListener listener) { | |
| 779 | + this.channel = channel; | |
| 780 | + this.request = request; | |
| 781 | + this.listener = listener; | |
| 782 | + } | |
| 783 | + } | |
| 784 | + | |
| 785 | + private void flush(FlushItem item) { | |
| 786 | + EventLoop loop = item.channel.eventLoop(); | |
| 787 | + Flusher flusher = flusherLookup.get(loop); | |
| 788 | + if (flusher == null) { | |
| 789 | + Flusher alt = flusherLookup.putIfAbsent(loop, flusher = new Flusher(loop)); | |
| 790 | + if (alt != null) | |
| 791 | + flusher = alt; | |
| 792 | + } | |
| 793 | + | |
| 794 | + flusher.queued.add(item); | |
| 795 | + flusher.start(); | |
| 796 | + } | |
| 797 | + | |
| 798 | + private class Dispatcher extends SimpleChannelInboundHandler<Message.Response> { | |
| 646 | 799 | |
| 647 | 800 | public final StreamIdGenerator streamIdHandler = new StreamIdGenerator(); |
| 648 | 801 | private final ConcurrentMap<Integer, ResponseHandler> pending = new ConcurrentHashMap<Integer, ResponseHandler>(); |
| @@ -679,54 +832,49 @@ | ||
| 679 | 832 | } |
| 680 | 833 | |
| 681 | 834 | @Override |
| 682 | - public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) { | |
| 683 | - if (!(e.getMessage() instanceof Message.Response)) { | |
| 684 | - String msg = asDebugString(e.getMessage()); | |
| 685 | - logger.error("{} Received unexpected message: {}", Connection.this, msg); | |
| 686 | - defunct(new TransportException(address, "Unexpected message received: " + msg)); | |
| 687 | - } else { | |
| 688 | - Message.Response response = (Message.Response)e.getMessage(); | |
| 689 | - int streamId = response.getStreamId(); | |
| 835 | + protected void channelRead0(ChannelHandlerContext ctx, Message.Response response) throws Exception { | |
| 836 | + int streamId = response.getStreamId(); | |
| 690 | 837 | |
| 691 | - logger.trace("{} received: {}", Connection.this, e.getMessage()); | |
| 838 | + logger.trace("{} received: {}", Connection.this, asDebugString(response)); | |
| 692 | 839 | |
| 693 | - if (streamId < 0) { | |
| 694 | - factory.defaultHandler.handle(response); | |
| 695 | - return; | |
| 696 | - } | |
| 840 | + if (streamId < 0) { | |
| 841 | + factory.defaultHandler.handle(response); | |
| 842 | + return; | |
| 843 | + } | |
| 697 | 844 | |
| 698 | - ResponseHandler handler = pending.remove(streamId); | |
| 699 | - streamIdHandler.release(streamId); | |
| 700 | - if (handler == null) { | |
| 701 | - /** | |
| 702 | - * During normal operation, we should not receive responses for which we don't have a handler. There is | |
| 703 | - * two cases however where this can happen: | |
| 704 | - * 1) The connection has been defuncted due to some internal error and we've raced between removing the | |
| 705 | - * handler and actually closing the connection; since the original error has been logged, we're fine | |
| 706 | - * ignoring this completely. | |
| 707 | - * 2) This request has timed out. In that case, we've already switched to another host (or errored out | |
| 708 | - * to the user). So log it for debugging purpose, but it's fine ignoring otherwise. | |
| 709 | - */ | |
| 710 | - streamIdHandler.unmark(streamId); | |
| 711 | - if (logger.isDebugEnabled()) | |
| 712 | - logger.debug("{} Response received on stream {} but no handler set anymore (either the request has " | |
| 713 | - + "timed out or it was closed due to another error). Received message is {}", Connection.this, streamId, asDebugString(response)); | |
| 714 | - return; | |
| 715 | - } | |
| 716 | - handler.cancelTimeout(); | |
| 717 | - handler.callback.onSet(Connection.this, response, System.nanoTime() - handler.startTime, handler.retryCount); | |
| 718 | - | |
| 719 | - // If we happen to be closed and we're the last outstanding request, we need to terminate the connection | |
| 720 | - // (note: this is racy as the signaling can be called more than once, but that's not a problem) | |
| 721 | - if (isClosed()) | |
| 722 | - tryTerminate(false); | |
| 845 | + ResponseHandler handler = pending.remove(streamId); | |
| 846 | + streamIdHandler.release(streamId); | |
| 847 | + if (handler == null) { | |
| 848 | + /** | |
| 849 | + * During normal operation, we should not receive responses for which we don't have a handler. There is | |
| 850 | + * two cases however where this can happen: | |
| 851 | + * 1) The connection has been defuncted due to some internal error and we've raced between removing the | |
| 852 | + * handler and actually closing the connection; since the original error has been logged, we're fine | |
| 853 | + * ignoring this completely. | |
| 854 | + * 2) This request has timed out. In that case, we've already switched to another host (or errored out | |
| 855 | + * to the user). So log it for debugging purpose, but it's fine ignoring otherwise. | |
| 856 | + */ | |
| 857 | + streamIdHandler.unmark(streamId); | |
| 858 | + if (logger.isDebugEnabled()) | |
| 859 | + logger.debug("{} Response received on stream {} but no handler set anymore (either the request has " | |
| 860 | + + "timed out or it was closed due to another error). Received message is {}", Connection.this, streamId, asDebugString(response)); | |
| 861 | + return; | |
| 723 | 862 | } |
| 863 | + handler.cancelTimeout(); | |
| 864 | + handler.callback.onSet(Connection.this, response, System.nanoTime() - handler.startTime, handler.retryCount); | |
| 865 | + | |
| 866 | + // If we happen to be closed and we're the last outstanding request, we need to terminate the connection | |
| 867 | + // (note: this is racy as the signaling can be called more than once, but that's not a problem) | |
| 868 | + if (isClosed()) | |
| 869 | + tryTerminate(false); | |
| 724 | 870 | } |
| 725 | 871 | |
| 726 | 872 | @Override |
| 727 | - public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) throws Exception { | |
| 728 | - logger.debug("{} was inactive for {} seconds, sending heartbeat", Connection.this, factory.configuration.getPoolingOptions().getHeartbeatIntervalSeconds()); | |
| 729 | - write(HEARTBEAT_CALLBACK); | |
| 873 | + public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { | |
| 874 | + if (evt instanceof IdleStateEvent && ((IdleStateEvent)evt).state() == ALL_IDLE) { | |
| 875 | + logger.debug("{} was inactive for {} seconds, sending heartbeat", Connection.this, factory.configuration.getPoolingOptions().getHeartbeatIntervalSeconds()); | |
| 876 | + write(HEARTBEAT_CALLBACK); | |
| 877 | + } | |
| 730 | 878 | } |
| 731 | 879 | |
| 732 | 880 | // Make sure we don't print huge responses in debug/error logs. |
| @@ -742,15 +890,15 @@ | ||
| 742 | 890 | } |
| 743 | 891 | |
| 744 | 892 | @Override |
| 745 | - public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) { | |
| 893 | + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { | |
| 746 | 894 | if (logger.isDebugEnabled()) |
| 747 | - logger.debug(String.format("%s connection error", Connection.this), e.getCause()); | |
| 895 | + logger.debug(String.format("%s connection error", Connection.this), cause); | |
| 748 | 896 | |
| 749 | 897 | // Ignore exception while writing, this will be handled by write() directly |
| 750 | 898 | if (writer.get() > 0) |
| 751 | 899 | return; |
| 752 | 900 | |
| 753 | - defunct(new TransportException(address, String.format("Unexpected exception triggered (%s)", e.getCause()), e.getCause())); | |
| 901 | + defunct(new TransportException(address, String.format("Unexpected exception triggered (%s)", cause), cause)); | |
| 754 | 902 | } |
| 755 | 903 | |
| 756 | 904 | public void errorOutAllHandler(ConnectionException ce) { |
| @@ -763,13 +911,15 @@ | ||
| 763 | 911 | iter.remove(); |
| 764 | 912 | } |
| 765 | 913 | } |
| 914 | + } | |
| 766 | 915 | |
| 916 | + private class ChannelCloseListener implements ChannelFutureListener { | |
| 767 | 917 | @Override |
| 768 | - public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) { | |
| 918 | + public void operationComplete(ChannelFuture future) throws Exception { | |
| 769 | 919 | // If we've closed the channel client side then we don't really want to defunct the connection, but |
| 770 | 920 | // if there is remaining thread waiting on us, we still want to wake them up |
| 771 | 921 | if (!isInitialized || isClosed()) { |
| 772 | - errorOutAllHandler(new TransportException(address, "Channel has been closed")); | |
| 922 | + dispatcher.errorOutAllHandler(new TransportException(address, "Channel has been closed")); | |
| 773 | 923 | // we still want to force so that the future completes |
| 774 | 924 | Connection.this.closeAsync().force(); |
| 775 | 925 | } else |
| @@ -837,9 +987,9 @@ | ||
| 837 | 987 | future.addListener(new ChannelFutureListener() { |
| 838 | 988 | public void operationComplete(ChannelFuture future) { |
| 839 | 989 | factory.allChannels.remove(channel); |
| 840 | - if (future.getCause() != null) { | |
| 841 | - logger.warn("Error closing channel", future.getCause()); | |
| 842 | - ConnectionCloseFuture.this.setException(future.getCause()); | |
| 990 | + if (future.cause() != null) { | |
| 991 | + logger.warn("Error closing channel", future.cause()); | |
| 992 | + ConnectionCloseFuture.this.setException(future.cause()); | |
| 843 | 993 | } else |
| 844 | 994 | ConnectionCloseFuture.this.set(null); |
| 845 | 995 | } |
| @@ -972,7 +1122,7 @@ | ||
| 972 | 1122 | public void handle(Message.Response response); |
| 973 | 1123 | } |
| 974 | 1124 | |
| 975 | - private static class PipelineFactory implements ChannelPipelineFactory { | |
| 1125 | + private static class Initializer extends ChannelInitializer<SocketChannel> { | |
| 976 | 1126 | // Stateless handlers |
| 977 | 1127 | private static final Message.ProtocolDecoder messageDecoder = new Message.ProtocolDecoder(); |
| 978 | 1128 | private static final Message.ProtocolEncoder messageEncoderV1 = new Message.ProtocolEncoder(1); |
| @@ -985,28 +1135,27 @@ | ||
| 985 | 1135 | private final SSLOptions sslOptions; |
| 986 | 1136 | private final ChannelHandler idleStateHandler; |
| 987 | 1137 | |
| 988 | - public PipelineFactory(Connection connection, int protocolVersion, FrameCompressor compressor, SSLOptions sslOptions, int heartBeatIntervalSeconds, HashedWheelTimer timer) { | |
| 1138 | + public Initializer(Connection connection, int protocolVersion, FrameCompressor compressor, SSLOptions sslOptions, int heartBeatIntervalSeconds) { | |
| 989 | 1139 | this.connection = connection; |
| 990 | 1140 | this.protocolVersion = protocolVersion; |
| 991 | 1141 | this.compressor = compressor; |
| 992 | 1142 | this.sslOptions = sslOptions; |
| 993 | - this.idleStateHandler = new IdleStateHandler(timer, 0, 0, heartBeatIntervalSeconds); | |
| 1143 | + this.idleStateHandler = new IdleStateHandler(0, 0, heartBeatIntervalSeconds); | |
| 994 | 1144 | } |
| 995 | 1145 | |
| 996 | 1146 | @Override |
| 997 | - public ChannelPipeline getPipeline() throws Exception { | |
| 998 | - ChannelPipeline pipeline = Channels.pipeline(); | |
| 1147 | + protected void initChannel(SocketChannel channel) throws Exception { | |
| 1148 | + ChannelPipeline pipeline = channel.pipeline(); | |
| 999 | 1149 | |
| 1000 | 1150 | if (sslOptions != null) { |
| 1001 | 1151 | SSLEngine engine = sslOptions.context.createSSLEngine(); |
| 1002 | 1152 | engine.setUseClientMode(true); |
| 1003 | 1153 | engine.setEnabledCipherSuites(sslOptions.cipherSuites); |
| 1004 | 1154 | SslHandler handler = new SslHandler(engine); |
| 1005 | - handler.setCloseOnSSLException(true); | |
| 1006 | 1155 | pipeline.addLast("ssl", handler); |
| 1007 | 1156 | } |
| 1008 | 1157 | |
| 1009 | - //pipeline.addLast("debug", new LoggingHandler(InternalLogLevel.INFO)); | |
| 1158 | +// pipeline.addLast("debug", new LoggingHandler(LogLevel.INFO)); | |
| 1010 | 1159 | |
| 1011 | 1160 | pipeline.addLast("frameDecoder", new Frame.Decoder()); |
| 1012 | 1161 | pipeline.addLast("frameEncoder", frameEncoder); |
| @@ -1022,8 +1171,6 @@ | ||
| 1022 | 1171 | pipeline.addLast("idleStateHandler", idleStateHandler); |
| 1023 | 1172 | |
| 1024 | 1173 | pipeline.addLast("dispatcher", connection.dispatcher); |
| 1025 | - | |
| 1026 | - return pipeline; | |
| 1027 | 1174 | } |
| 1028 | 1175 | } |
| 1029 | 1176 | } |
modified driver-core/src/main/java/com/datastax/driver/core/DataType.java +2 −2
| @@ -24,7 +24,7 @@ | ||
| 24 | 24 | import com.google.common.base.Objects; |
| 25 | 25 | import com.google.common.collect.ImmutableList; |
| 26 | 26 | import com.google.common.collect.ImmutableSet; |
| 27 | -import org.jboss.netty.buffer.ChannelBuffer; | |
| 27 | +import io.netty.buffer.ByteBuf; | |
| 28 | 28 | |
| 29 | 29 | import com.datastax.driver.core.exceptions.DriverInternalError; |
| 30 | 30 | import com.datastax.driver.core.exceptions.InvalidTypeException; |
| @@ -171,7 +171,7 @@ | ||
| 171 | 171 | this.codec = codec; |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | - static DataType decode(ChannelBuffer buffer) { | |
| 174 | + static DataType decode(ByteBuf buffer) { | |
| 175 | 175 | Name name = Name.fromProtocolId(buffer.readUnsignedShort()); |
| 176 | 176 | switch (name) { |
| 177 | 177 | case CUSTOM: |
modified driver-core/src/main/java/com/datastax/driver/core/Frame.java +37 −48
| @@ -15,25 +15,20 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import java.io.IOException; | |
| 19 | 18 | import java.util.EnumSet; |
| 19 | +import java.util.List; | |
| 20 | 20 | |
| 21 | -import org.jboss.netty.buffer.ChannelBuffer; | |
| 22 | -import org.jboss.netty.buffer.ChannelBuffers; | |
| 23 | -import org.jboss.netty.channel.Channel; | |
| 24 | -import org.jboss.netty.channel.ChannelHandlerContext; | |
| 25 | -import org.jboss.netty.handler.codec.frame.CorruptedFrameException; | |
| 26 | -import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder; | |
| 27 | -import org.jboss.netty.handler.codec.frame.TooLongFrameException; | |
| 28 | -import org.jboss.netty.handler.codec.oneone.OneToOneDecoder; | |
| 29 | -import org.jboss.netty.handler.codec.oneone.OneToOneEncoder; | |
| 21 | +import io.netty.buffer.ByteBuf; | |
| 22 | +import io.netty.channel.ChannelHandler; | |
| 23 | +import io.netty.channel.ChannelHandlerContext; | |
| 24 | +import io.netty.handler.codec.*; | |
| 30 | 25 | |
| 31 | 26 | import com.datastax.driver.core.exceptions.DriverInternalError; |
| 32 | 27 | |
| 33 | 28 | class Frame { |
| 34 | 29 | |
| 35 | 30 | public final Header header; |
| 36 | - public final ChannelBuffer body; | |
| 31 | + public final ByteBuf body; | |
| 37 | 32 | |
| 38 | 33 | /** |
| 39 | 34 | * On-wire frame. |
| @@ -46,12 +41,12 @@ | ||
| 46 | 41 | * | length | |
| 47 | 42 | * +---------+---------+---------+---------+ |
| 48 | 43 | */ |
| 49 | - private Frame(Header header, ChannelBuffer body) { | |
| 44 | + private Frame(Header header, ByteBuf body) { | |
| 50 | 45 | this.header = header; |
| 51 | 46 | this.body = body; |
| 52 | 47 | } |
| 53 | 48 | |
| 54 | - private static Frame create(ChannelBuffer fullFrame) { | |
| 49 | + private static Frame create(ByteBuf fullFrame) { | |
| 55 | 50 | assert fullFrame.readableBytes() >= Header.LENGTH : String.format("Frame too short (%d bytes)", fullFrame.readableBytes()); |
| 56 | 51 | |
| 57 | 52 | int version = fullFrame.readByte(); |
| @@ -68,7 +63,7 @@ | ||
| 68 | 63 | return new Frame(header, fullFrame); |
| 69 | 64 | } |
| 70 | 65 | |
| 71 | - public static Frame create(int version, int opcode, int streamId, EnumSet<Header.Flag> flags, ChannelBuffer body) { | |
| 66 | + public static Frame create(int version, int opcode, int streamId, EnumSet<Header.Flag> flags, ByteBuf body) { | |
| 72 | 67 | Header header = new Header(version, flags, streamId, opcode); |
| 73 | 68 | return new Frame(header, body); |
| 74 | 69 | } |
| @@ -118,7 +113,7 @@ | ||
| 118 | 113 | } |
| 119 | 114 | } |
| 120 | 115 | |
| 121 | - public Frame with(ChannelBuffer newBody) { | |
| 116 | + public Frame with(ByteBuf newBody) { | |
| 122 | 117 | return new Frame(header, newBody); |
| 123 | 118 | } |
| 124 | 119 | |
| @@ -131,7 +126,7 @@ | ||
| 131 | 126 | } |
| 132 | 127 | |
| 133 | 128 | @Override |
| 134 | - protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception { | |
| 129 | + protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception { | |
| 135 | 130 | try { |
| 136 | 131 | if (buffer.readableBytes() == 0) |
| 137 | 132 | return null; |
| @@ -140,11 +135,12 @@ | ||
| 140 | 135 | if (buffer.readableBytes() >= 4) |
| 141 | 136 | Message.Response.Type.fromOpcode(buffer.getByte(3)); |
| 142 | 137 | |
| 143 | - ChannelBuffer frame = (ChannelBuffer) super.decode(ctx, channel, buffer); | |
| 138 | + ByteBuf frame = (ByteBuf) super.decode(ctx, buffer); | |
| 144 | 139 | if (frame == null) { |
| 145 | 140 | return null; |
| 146 | 141 | } |
| 147 | - | |
| 142 | + // Do not deallocate `frame` just yet, because it is stored as Frame.body and will be used | |
| 143 | + // in Message.ProtocolDecoder (we deallocate it there). | |
| 148 | 144 | return Frame.create(frame); |
| 149 | 145 | } catch (CorruptedFrameException e) { |
| 150 | 146 | throw new DriverInternalError(e.getMessage()); |
| @@ -154,25 +150,25 @@ | ||
| 154 | 150 | } |
| 155 | 151 | } |
| 156 | 152 | |
| 157 | - public static class Encoder extends OneToOneEncoder { | |
| 158 | - | |
| 159 | - public Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws IOException { | |
| 160 | - assert msg instanceof Frame : "Expecting frame, got " + msg; | |
| 161 | - | |
| 162 | - Frame frame = (Frame)msg; | |
| 153 | + @ChannelHandler.Sharable | |
| 154 | + public static class Encoder extends MessageToMessageEncoder<Frame> { | |
| 163 | 155 | |
| 164 | - ChannelBuffer header = ChannelBuffers.buffer(Frame.Header.LENGTH); | |
| 156 | + @Override | |
| 157 | + protected void encode(ChannelHandlerContext ctx, Frame frame, List<Object> out) throws Exception { | |
| 158 | + ByteBuf header = ctx.alloc().ioBuffer(Frame.Header.LENGTH); | |
| 165 | 159 | // We don't bother with the direction, we only send requests. |
| 166 | 160 | header.writeByte(frame.header.version); |
| 167 | 161 | header.writeByte(Header.Flag.serialize(frame.header.flags)); |
| 168 | 162 | header.writeByte(frame.header.streamId); |
| 169 | 163 | header.writeByte(frame.header.opcode); |
| 170 | 164 | header.writeInt(frame.body.readableBytes()); |
| 171 | - return ChannelBuffers.wrappedBuffer(header, frame.body); | |
| 165 | + | |
| 166 | + out.add(header); | |
| 167 | + out.add(frame.body); | |
| 172 | 168 | } |
| 173 | 169 | } |
| 174 | 170 | |
| 175 | - public static class Decompressor extends OneToOneDecoder { | |
| 171 | + public static class Decompressor extends MessageToMessageDecoder<Frame> { | |
| 176 | 172 | |
| 177 | 173 | private final FrameCompressor compressor; |
| 178 | 174 | |
| @@ -181,19 +177,15 @@ | ||
| 181 | 177 | this.compressor = compressor; |
| 182 | 178 | } |
| 183 | 179 | |
| 184 | - public Object decode(ChannelHandlerContext ctx, Channel channel, Object msg) throws IOException { | |
| 185 | - | |
| 186 | - assert msg instanceof Frame : "Expecting frame, got " + msg; | |
| 187 | - | |
| 188 | - Frame frame = (Frame)msg; | |
| 189 | - | |
| 190 | - return frame.header.flags.contains(Header.Flag.COMPRESSED) | |
| 191 | - ? compressor.decompress(frame) | |
| 192 | - : frame; | |
| 180 | + @Override | |
| 181 | + protected void decode(ChannelHandlerContext ctx, Frame frame, List<Object> out) throws Exception { | |
| 182 | + out.add(frame.header.flags.contains(Header.Flag.COMPRESSED) | |
| 183 | + ? compressor.decompress(frame) | |
| 184 | + : frame); | |
| 193 | 185 | } |
| 194 | 186 | } |
| 195 | 187 | |
| 196 | - public static class Compressor extends OneToOneEncoder { | |
| 188 | + public static class Compressor extends MessageToMessageEncoder<Frame> { | |
| 197 | 189 | |
| 198 | 190 | private final FrameCompressor compressor; |
| 199 | 191 | |
| @@ -202,18 +194,15 @@ | ||
| 202 | 194 | this.compressor = compressor; |
| 203 | 195 | } |
| 204 | 196 | |
| 205 | - public Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws IOException { | |
| 206 | - | |
| 207 | - assert msg instanceof Frame : "Expecting frame, got " + msg; | |
| 208 | - | |
| 209 | - Frame frame = (Frame)msg; | |
| 210 | - | |
| 197 | + @Override | |
| 198 | + protected void encode(ChannelHandlerContext ctx, Frame frame, List<Object> out) throws Exception { | |
| 211 | 199 | // Never compress STARTUP messages |
| 212 | - if (frame.header.opcode == Message.Request.Type.STARTUP.opcode) | |
| 213 | - return frame; | |
| 214 | - | |
| 215 | - frame.header.flags.add(Header.Flag.COMPRESSED); | |
| 216 | - return compressor.compress(frame); | |
| 200 | + if (frame.header.opcode == Message.Request.Type.STARTUP.opcode) { | |
| 201 | + out.add(frame); | |
| 202 | + } else { | |
| 203 | + frame.header.flags.add(Header.Flag.COMPRESSED); | |
| 204 | + out.add(compressor.compress(frame)); | |
| 205 | + } | |
| 217 | 206 | } |
| 218 | 207 | } |
| 219 | 208 | } |
modified driver-core/src/main/java/com/datastax/driver/core/FrameCompressor.java +5 −5
| @@ -17,8 +17,8 @@ | ||
| 17 | 17 | |
| 18 | 18 | import java.io.IOException; |
| 19 | 19 | |
| 20 | +import io.netty.buffer.Unpooled; | |
| 20 | 21 | import net.jpountz.lz4.LZ4Factory; |
| 21 | -import org.jboss.netty.buffer.ChannelBuffers; | |
| 22 | 22 | import org.slf4j.Logger; |
| 23 | 23 | import org.slf4j.LoggerFactory; |
| 24 | 24 | import org.xerial.snappy.Snappy; |
| @@ -60,7 +60,7 @@ | ||
| 60 | 60 | byte[] output = new byte[Snappy.maxCompressedLength(input.length)]; |
| 61 | 61 | |
| 62 | 62 | int written = Snappy.compress(input, 0, input.length, output, 0); |
| 63 | - return frame.with(ChannelBuffers.wrappedBuffer(output, 0, written)); | |
| 63 | + return frame.with(Unpooled.wrappedBuffer(output, 0, written)); | |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | public Frame decompress(Frame frame) throws IOException { |
| @@ -71,7 +71,7 @@ | ||
| 71 | 71 | |
| 72 | 72 | byte[] output = new byte[Snappy.uncompressedLength(input)]; |
| 73 | 73 | int size = Snappy.uncompress(input, 0, input.length, output, 0); |
| 74 | - return frame.with(ChannelBuffers.wrappedBuffer(output, 0, size)); | |
| 74 | + return frame.with(Unpooled.wrappedBuffer(output, 0, size)); | |
| 75 | 75 | } |
| 76 | 76 | } |
| 77 | 77 | |
| @@ -115,7 +115,7 @@ | ||
| 115 | 115 | |
| 116 | 116 | try { |
| 117 | 117 | int written = compressor.compress(input, 0, input.length, output, INTEGER_BYTES, maxCompressedLength); |
| 118 | - return frame.with(ChannelBuffers.wrappedBuffer(output, 0, INTEGER_BYTES + written)); | |
| 118 | + return frame.with(Unpooled.wrappedBuffer(output, 0, INTEGER_BYTES + written)); | |
| 119 | 119 | } catch (Exception e) { |
| 120 | 120 | throw new IOException(e); |
| 121 | 121 | } |
| @@ -136,7 +136,7 @@ | ||
| 136 | 136 | if (read != input.length - INTEGER_BYTES) |
| 137 | 137 | throw new IOException("Compressed lengths mismatch"); |
| 138 | 138 | |
| 139 | - return frame.with(ChannelBuffers.wrappedBuffer(output)); | |
| 139 | + return frame.with(Unpooled.wrappedBuffer(output)); | |
| 140 | 140 | } catch (Exception e) { |
| 141 | 141 | throw new IOException(e); |
| 142 | 142 | } |
modified driver-core/src/main/java/com/datastax/driver/core/Message.java +24 −23
| @@ -16,14 +16,14 @@ | ||
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | 18 | import java.util.EnumSet; |
| 19 | +import java.util.List; | |
| 19 | 20 | import java.util.UUID; |
| 20 | 21 | |
| 21 | -import org.jboss.netty.buffer.ChannelBuffer; | |
| 22 | -import org.jboss.netty.buffer.ChannelBuffers; | |
| 23 | -import org.jboss.netty.channel.Channel; | |
| 24 | -import org.jboss.netty.channel.ChannelHandlerContext; | |
| 25 | -import org.jboss.netty.handler.codec.oneone.OneToOneDecoder; | |
| 26 | -import org.jboss.netty.handler.codec.oneone.OneToOneEncoder; | |
| 22 | +import io.netty.buffer.ByteBuf; | |
| 23 | +import io.netty.channel.ChannelHandler; | |
| 24 | +import io.netty.channel.ChannelHandlerContext; | |
| 25 | +import io.netty.handler.codec.MessageToMessageDecoder; | |
| 26 | +import io.netty.handler.codec.MessageToMessageEncoder; | |
| 27 | 27 | import org.slf4j.Logger; |
| 28 | 28 | import org.slf4j.LoggerFactory; |
| 29 | 29 | |
| @@ -37,12 +37,12 @@ | ||
| 37 | 37 | protected static final Logger logger = LoggerFactory.getLogger(Message.class); |
| 38 | 38 | |
| 39 | 39 | public interface Coder<R extends Request> { |
| 40 | - public void encode(R request, ChannelBuffer dest); | |
| 40 | + public void encode(R request, ByteBuf dest); | |
| 41 | 41 | public int encodedSize(R request); |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | public interface Decoder<R extends Response> { |
| 45 | - public R decode(ChannelBuffer body); | |
| 45 | + public R decode(ByteBuf body); | |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | private volatile int streamId; |
| @@ -170,21 +170,25 @@ | ||
| 170 | 170 | } |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | - public static class ProtocolDecoder extends OneToOneDecoder { | |
| 173 | + @ChannelHandler.Sharable | |
| 174 | + public static class ProtocolDecoder extends MessageToMessageDecoder<Frame> { | |
| 174 | 175 | |
| 175 | - public Object decode(ChannelHandlerContext ctx, Channel channel, Object msg) { | |
| 176 | - assert msg instanceof Frame : "Expecting frame, got " + msg; | |
| 177 | - | |
| 178 | - Frame frame = (Frame)msg; | |
| 176 | + @Override | |
| 177 | + protected void decode(ChannelHandlerContext ctx, Frame frame, List<Object> out) throws Exception { | |
| 179 | 178 | boolean isTracing = frame.header.flags.contains(Frame.Header.Flag.TRACING); |
| 180 | 179 | UUID tracingId = isTracing ? CBUtil.readUUID(frame.body) : null; |
| 181 | 180 | |
| 182 | 181 | Response response = Response.Type.fromOpcode(frame.header.opcode).decoder(frame.header.version).decode(frame.body); |
| 183 | - return response.setTracingId(tracingId).setStreamId(frame.header.streamId); | |
| 182 | + frame.body.release(); | |
| 183 | + | |
| 184 | + response.setTracingId(tracingId).setStreamId(frame.header.streamId); | |
| 185 | + | |
| 186 | + out.add(response); | |
| 184 | 187 | } |
| 185 | 188 | } |
| 186 | 189 | |
| 187 | - public static class ProtocolEncoder extends OneToOneEncoder { | |
| 190 | + @ChannelHandler.Sharable | |
| 191 | + public static class ProtocolEncoder extends MessageToMessageEncoder<Request> { | |
| 188 | 192 | |
| 189 | 193 | private final int protocolVersion; |
| 190 | 194 | |
| @@ -192,21 +196,18 @@ | ||
| 192 | 196 | this.protocolVersion = version; |
| 193 | 197 | } |
| 194 | 198 | |
| 195 | - @SuppressWarnings("unchecked") | |
| 196 | - public Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) { | |
| 197 | - assert msg instanceof Request : "Expecting request, got " + msg; | |
| 198 | - | |
| 199 | - Request request = (Request)msg; | |
| 200 | - | |
| 199 | + @Override | |
| 200 | + protected void encode(ChannelHandlerContext ctx, Request request, List<Object> out) throws Exception { | |
| 201 | 201 | EnumSet<Frame.Header.Flag> flags = EnumSet.noneOf(Frame.Header.Flag.class); |
| 202 | 202 | if (request.isTracingRequested()) |
| 203 | 203 | flags.add(Frame.Header.Flag.TRACING); |
| 204 | 204 | |
| 205 | + @SuppressWarnings("unchecked") | |
| 205 | 206 | Coder<Request> coder = (Coder<Request>)request.type.coder(protocolVersion); |
| 206 | - ChannelBuffer body = ChannelBuffers.buffer(coder.encodedSize(request)); | |
| 207 | + ByteBuf body = ctx.alloc().buffer(coder.encodedSize(request)); | |
| 207 | 208 | coder.encode(request, body); |
| 208 | 209 | |
| 209 | - return Frame.create(protocolVersion, request.type.opcode, request.getStreamId(), flags, body); | |
| 210 | + out.add(Frame.create(protocolVersion, request.type.opcode, request.getStreamId(), flags, body)); | |
| 210 | 211 | } |
| 211 | 212 | } |
| 212 | 213 | } |
modified driver-core/src/main/java/com/datastax/driver/core/ProtocolEvent.java +16 −16
| @@ -17,7 +17,7 @@ | ||
| 17 | 17 | |
| 18 | 18 | import java.net.InetSocketAddress; |
| 19 | 19 | |
| 20 | -import org.jboss.netty.buffer.ChannelBuffer; | |
| 20 | +import io.netty.buffer.ByteBuf; | |
| 21 | 21 | |
| 22 | 22 | class ProtocolEvent { |
| 23 | 23 | |
| @@ -29,14 +29,14 @@ | ||
| 29 | 29 | this.type = type; |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | - public static ProtocolEvent deserialize(ChannelBuffer cb) { | |
| 33 | - switch (CBUtil.readEnumValue(Type.class, cb)) { | |
| 32 | + public static ProtocolEvent deserialize(ByteBuf bb) { | |
| 33 | + switch (CBUtil.readEnumValue(Type.class, bb)) { | |
| 34 | 34 | case TOPOLOGY_CHANGE: |
| 35 | - return TopologyChange.deserializeEvent(cb); | |
| 35 | + return TopologyChange.deserializeEvent(bb); | |
| 36 | 36 | case STATUS_CHANGE: |
| 37 | - return StatusChange.deserializeEvent(cb); | |
| 37 | + return StatusChange.deserializeEvent(bb); | |
| 38 | 38 | case SCHEMA_CHANGE: |
| 39 | - return SchemaChange.deserializeEvent(cb); | |
| 39 | + return SchemaChange.deserializeEvent(bb); | |
| 40 | 40 | } |
| 41 | 41 | throw new AssertionError(); |
| 42 | 42 | } |
| @@ -54,9 +54,9 @@ | ||
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | // Assumes the type has already been deserialized |
| 57 | - private static TopologyChange deserializeEvent(ChannelBuffer cb) { | |
| 58 | - Change change = CBUtil.readEnumValue(Change.class, cb); | |
| 59 | - InetSocketAddress node = CBUtil.readInet(cb); | |
| 57 | + private static TopologyChange deserializeEvent(ByteBuf bb) { | |
| 58 | + Change change = CBUtil.readEnumValue(Change.class, bb); | |
| 59 | + InetSocketAddress node = CBUtil.readInet(bb); | |
| 60 | 60 | return new TopologyChange(change, node); |
| 61 | 61 | } |
| 62 | 62 | |
| @@ -80,9 +80,9 @@ | ||
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | // Assumes the type has already been deserialized |
| 83 | - private static StatusChange deserializeEvent(ChannelBuffer cb) { | |
| 84 | - Status status = CBUtil.readEnumValue(Status.class, cb); | |
| 85 | - InetSocketAddress node = CBUtil.readInet(cb); | |
| 83 | + private static StatusChange deserializeEvent(ByteBuf bb) { | |
| 84 | + Status status = CBUtil.readEnumValue(Status.class, bb); | |
| 85 | + InetSocketAddress node = CBUtil.readInet(bb); | |
| 86 | 86 | return new StatusChange(status, node); |
| 87 | 87 | } |
| 88 | 88 | |
| @@ -108,10 +108,10 @@ | ||
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | // Assumes the type has already been deserialized |
| 111 | - private static SchemaChange deserializeEvent(ChannelBuffer cb) { | |
| 112 | - Change change = CBUtil.readEnumValue(Change.class, cb); | |
| 113 | - String keyspace = CBUtil.readString(cb); | |
| 114 | - String table = CBUtil.readString(cb); | |
| 111 | + private static SchemaChange deserializeEvent(ByteBuf bb) { | |
| 112 | + Change change = CBUtil.readEnumValue(Change.class, bb); | |
| 113 | + String keyspace = CBUtil.readString(bb); | |
| 114 | + String table = CBUtil.readString(bb); | |
| 115 | 115 | return new SchemaChange(change, keyspace, table); |
| 116 | 116 | } |
| 117 | 117 |
modified driver-core/src/main/java/com/datastax/driver/core/Requests.java +13 −13
| @@ -22,7 +22,7 @@ | ||
| 22 | 22 | import java.util.Map; |
| 23 | 23 | |
| 24 | 24 | import com.google.common.collect.ImmutableMap; |
| 25 | -import org.jboss.netty.buffer.ChannelBuffer; | |
| 25 | +import io.netty.buffer.ByteBuf; | |
| 26 | 26 | |
| 27 | 27 | class Requests { |
| 28 | 28 | |
| @@ -35,7 +35,7 @@ | ||
| 35 | 35 | public static final String COMPRESSION_OPTION = "COMPRESSION"; |
| 36 | 36 | |
| 37 | 37 | public static final Message.Coder<Startup> coder = new Message.Coder<Startup>() { |
| 38 | - public void encode(Startup msg, ChannelBuffer dest) { | |
| 38 | + public void encode(Startup msg, ByteBuf dest) { | |
| 39 | 39 | CBUtil.writeStringMap(msg.options, dest); |
| 40 | 40 | } |
| 41 | 41 | |
| @@ -67,7 +67,7 @@ | ||
| 67 | 67 | |
| 68 | 68 | public static final Message.Coder<Credentials> coder = new Message.Coder<Credentials>() { |
| 69 | 69 | |
| 70 | - public void encode(Credentials msg, ChannelBuffer dest) { | |
| 70 | + public void encode(Credentials msg, ByteBuf dest) { | |
| 71 | 71 | CBUtil.writeStringMap(msg.credentials, dest); |
| 72 | 72 | } |
| 73 | 73 | |
| @@ -88,7 +88,7 @@ | ||
| 88 | 88 | |
| 89 | 89 | public static final Message.Coder<Options> coder = new Message.Coder<Options>() |
| 90 | 90 | { |
| 91 | - public void encode(Options msg, ChannelBuffer dest) {} | |
| 91 | + public void encode(Options msg, ByteBuf dest) {} | |
| 92 | 92 | |
| 93 | 93 | public int encodedSize(Options msg) { |
| 94 | 94 | return 0; |
| @@ -108,7 +108,7 @@ | ||
| 108 | 108 | public static class Query extends Message.Request { |
| 109 | 109 | |
| 110 | 110 | public static final Message.Coder<Query> coderV1 = new Message.Coder<Query>() { |
| 111 | - public void encode(Query msg, ChannelBuffer dest) { | |
| 111 | + public void encode(Query msg, ByteBuf dest) { | |
| 112 | 112 | CBUtil.writeLongString(msg.query, dest); |
| 113 | 113 | CBUtil.writeConsistencyLevel(msg.options.consistency, dest); |
| 114 | 114 | } |
| @@ -120,7 +120,7 @@ | ||
| 120 | 120 | }; |
| 121 | 121 | |
| 122 | 122 | public static final Message.Coder<Query> coderV2 = new Message.Coder<Query>() { |
| 123 | - public void encode(Query msg, ChannelBuffer dest) { | |
| 123 | + public void encode(Query msg, ByteBuf dest) { | |
| 124 | 124 | CBUtil.writeLongString(msg.query, dest); |
| 125 | 125 | msg.options.encode(dest); |
| 126 | 126 | } |
| @@ -153,7 +153,7 @@ | ||
| 153 | 153 | public static class Execute extends Message.Request { |
| 154 | 154 | |
| 155 | 155 | public static final Message.Coder<Execute> coderV1 = new Message.Coder<Execute>() { |
| 156 | - public void encode(Execute msg, ChannelBuffer dest) { | |
| 156 | + public void encode(Execute msg, ByteBuf dest) { | |
| 157 | 157 | CBUtil.writeBytes(msg.statementId.bytes, dest); |
| 158 | 158 | CBUtil.writeValueList(msg.options.values, dest); |
| 159 | 159 | CBUtil.writeConsistencyLevel(msg.options.consistency, dest); |
| @@ -167,7 +167,7 @@ | ||
| 167 | 167 | }; |
| 168 | 168 | |
| 169 | 169 | public static final Message.Coder<Execute> coderV2 = new Message.Coder<Execute>() { |
| 170 | - public void encode(Execute msg, ChannelBuffer dest) { | |
| 170 | + public void encode(Execute msg, ByteBuf dest) { | |
| 171 | 171 | CBUtil.writeBytes(msg.statementId.bytes, dest); |
| 172 | 172 | msg.options.encode(dest); |
| 173 | 173 | } |
| @@ -264,7 +264,7 @@ | ||
| 264 | 264 | flags.add(Flag.SERIAL_CONSISTENCY); |
| 265 | 265 | } |
| 266 | 266 | |
| 267 | - public void encode(ChannelBuffer dest) { | |
| 267 | + public void encode(ByteBuf dest) { | |
| 268 | 268 | CBUtil.writeConsistencyLevel(consistency, dest); |
| 269 | 269 | |
| 270 | 270 | dest.writeByte((byte)Flag.serialize(flags)); |
| @@ -302,7 +302,7 @@ | ||
| 302 | 302 | public static class Batch extends Message.Request { |
| 303 | 303 | |
| 304 | 304 | public static final Message.Coder<Batch> coder = new Message.Coder<Batch>() { |
| 305 | - public void encode(Batch msg, ChannelBuffer dest) { | |
| 305 | + public void encode(Batch msg, ByteBuf dest) { | |
| 306 | 306 | int queries = msg.queryOrIdList.size(); |
| 307 | 307 | assert queries <= 0xFFFF; |
| 308 | 308 | |
| @@ -377,7 +377,7 @@ | ||
| 377 | 377 | |
| 378 | 378 | public static final Message.Coder<Prepare> coder = new Message.Coder<Prepare>() { |
| 379 | 379 | |
| 380 | - public void encode(Prepare msg, ChannelBuffer dest) { | |
| 380 | + public void encode(Prepare msg, ByteBuf dest) { | |
| 381 | 381 | CBUtil.writeLongString(msg.query, dest); |
| 382 | 382 | } |
| 383 | 383 | |
| @@ -402,7 +402,7 @@ | ||
| 402 | 402 | public static class Register extends Message.Request { |
| 403 | 403 | |
| 404 | 404 | public static final Message.Coder<Register> coder = new Message.Coder<Register>() { |
| 405 | - public void encode(Register msg, ChannelBuffer dest) { | |
| 405 | + public void encode(Register msg, ByteBuf dest) { | |
| 406 | 406 | dest.writeShort(msg.eventTypes.size()); |
| 407 | 407 | for (ProtocolEvent.Type type : msg.eventTypes) |
| 408 | 408 | CBUtil.writeEnumValue(type, dest); |
| @@ -433,7 +433,7 @@ | ||
| 433 | 433 | |
| 434 | 434 | public static final Message.Coder<AuthResponse> coder = new Message.Coder<AuthResponse>() { |
| 435 | 435 | |
| 436 | - public void encode(AuthResponse response, ChannelBuffer dest) { | |
| 436 | + public void encode(AuthResponse response, ByteBuf dest) { | |
| 437 | 437 | CBUtil.writeValue(response.token, dest); |
| 438 | 438 | } |
| 439 | 439 |
modified driver-core/src/main/java/com/datastax/driver/core/Responses.java +17 −18
| @@ -18,9 +18,8 @@ | ||
| 18 | 18 | import java.net.InetSocketAddress; |
| 19 | 19 | import java.nio.ByteBuffer; |
| 20 | 20 | import java.util.*; |
| 21 | -import java.util.concurrent.LinkedBlockingQueue; | |
| 22 | 21 | |
| 23 | -import org.jboss.netty.buffer.ChannelBuffer; | |
| 22 | +import io.netty.buffer.ByteBuf; | |
| 24 | 23 | |
| 25 | 24 | import com.datastax.driver.core.exceptions.*; |
| 26 | 25 | import com.datastax.driver.core.utils.Bytes; |
| @@ -33,7 +32,7 @@ | ||
| 33 | 32 | |
| 34 | 33 | public static final Message.Decoder<Error> decoder = new Message.Decoder<Error>() { |
| 35 | 34 | @Override |
| 36 | - public Error decode(ChannelBuffer body) { | |
| 35 | + public Error decode(ByteBuf body) { | |
| 37 | 36 | ExceptionCode code = ExceptionCode.fromValue(body.readInt()); |
| 38 | 37 | String msg = CBUtil.readString(body); |
| 39 | 38 | Object infos = null; |
| @@ -111,7 +110,7 @@ | ||
| 111 | 110 | public static class Ready extends Message.Response { |
| 112 | 111 | |
| 113 | 112 | public static final Message.Decoder<Ready> decoder = new Message.Decoder<Ready>() { |
| 114 | - public Ready decode(ChannelBuffer body) { | |
| 113 | + public Ready decode(ByteBuf body) { | |
| 115 | 114 | // TODO: Would it be cool to return a singleton? Check we don't need to |
| 116 | 115 | // set the streamId or something |
| 117 | 116 | return new Ready(); |
| @@ -131,7 +130,7 @@ | ||
| 131 | 130 | public static class Authenticate extends Message.Response { |
| 132 | 131 | |
| 133 | 132 | public static final Message.Decoder<Authenticate> decoder = new Message.Decoder<Authenticate>() { |
| 134 | - public Authenticate decode(ChannelBuffer body) { | |
| 133 | + public Authenticate decode(ByteBuf body) { | |
| 135 | 134 | String authenticator = CBUtil.readString(body); |
| 136 | 135 | return new Authenticate(authenticator); |
| 137 | 136 | } |
| @@ -153,7 +152,7 @@ | ||
| 153 | 152 | public static class Supported extends Message.Response { |
| 154 | 153 | |
| 155 | 154 | public static final Message.Decoder<Supported> decoder = new Message.Decoder<Supported>() { |
| 156 | - public Supported decode(ChannelBuffer body) { | |
| 155 | + public Supported decode(ByteBuf body) { | |
| 157 | 156 | return new Supported(CBUtil.readStringToStringListMap(body)); |
| 158 | 157 | } |
| 159 | 158 | }; |
| @@ -189,14 +188,14 @@ | ||
| 189 | 188 | public static abstract class Result extends Message.Response { |
| 190 | 189 | |
| 191 | 190 | public static final Message.Decoder<Result> decoderV1 = new Message.Decoder<Result>() { |
| 192 | - public Result decode(ChannelBuffer body) { | |
| 191 | + public Result decode(ByteBuf body) { | |
| 193 | 192 | Kind kind = Kind.fromId(body.readInt()); |
| 194 | 193 | return kind.subDecoderV1.decode(body); |
| 195 | 194 | } |
| 196 | 195 | }; |
| 197 | 196 | |
| 198 | 197 | public static final Message.Decoder<Result> decoderV2 = new Message.Decoder<Result>() { |
| 199 | - public Result decode(ChannelBuffer body) { | |
| 198 | + public Result decode(ByteBuf body) { | |
| 200 | 199 | Kind kind = Kind.fromId(body.readInt()); |
| 201 | 200 | return kind.subDecoderV2.decode(body); |
| 202 | 201 | } |
| @@ -255,7 +254,7 @@ | ||
| 255 | 254 | } |
| 256 | 255 | |
| 257 | 256 | public static final Message.Decoder<Result> subcodec = new Message.Decoder<Result>() { |
| 258 | - public Result decode(ChannelBuffer body) { | |
| 257 | + public Result decode(ByteBuf body) { | |
| 259 | 258 | return new Void(); |
| 260 | 259 | } |
| 261 | 260 | }; |
| @@ -275,7 +274,7 @@ | ||
| 275 | 274 | } |
| 276 | 275 | |
| 277 | 276 | public static final Message.Decoder<Result> subcodec = new Message.Decoder<Result>() { |
| 278 | - public Result decode(ChannelBuffer body) { | |
| 277 | + public Result decode(ByteBuf body) { | |
| 279 | 278 | return new SetKeyspace(CBUtil.readString(body)); |
| 280 | 279 | } |
| 281 | 280 | }; |
| @@ -327,7 +326,7 @@ | ||
| 327 | 326 | this.pagingState = pagingState; |
| 328 | 327 | } |
| 329 | 328 | |
| 330 | - public static Metadata decode(ChannelBuffer body) { | |
| 329 | + public static Metadata decode(ByteBuf body) { | |
| 331 | 330 | |
| 332 | 331 | // flags & column count |
| 333 | 332 | EnumSet<Flag> flags = Flag.deserialize(body.readInt()); |
| @@ -381,7 +380,7 @@ | ||
| 381 | 380 | } |
| 382 | 381 | |
| 383 | 382 | public static final Message.Decoder<Result> subcodec = new Message.Decoder<Result>() { |
| 384 | - public Result decode(ChannelBuffer body) { | |
| 383 | + public Result decode(ByteBuf body) { | |
| 385 | 384 | |
| 386 | 385 | Metadata metadata = Metadata.decode(body); |
| 387 | 386 | |
| @@ -436,7 +435,7 @@ | ||
| 436 | 435 | public static class Prepared extends Result { |
| 437 | 436 | |
| 438 | 437 | public static final Message.Decoder<Result> subcodecV1 = new Message.Decoder<Result>() { |
| 439 | - public Result decode(ChannelBuffer body) { | |
| 438 | + public Result decode(ByteBuf body) { | |
| 440 | 439 | MD5Digest id = MD5Digest.wrap(CBUtil.readBytes(body)); |
| 441 | 440 | Rows.Metadata metadata = Rows.Metadata.decode(body); |
| 442 | 441 | return new Prepared(id, metadata, Rows.Metadata.EMPTY); |
| @@ -444,7 +443,7 @@ | ||
| 444 | 443 | }; |
| 445 | 444 | |
| 446 | 445 | public static final Message.Decoder<Result> subcodecV2 = new Message.Decoder<Result>() { |
| 447 | - public Result decode(ChannelBuffer body) { | |
| 446 | + public Result decode(ByteBuf body) { | |
| 448 | 447 | MD5Digest id = MD5Digest.wrap(CBUtil.readBytes(body)); |
| 449 | 448 | Rows.Metadata metadata = Rows.Metadata.decode(body); |
| 450 | 449 | Rows.Metadata resultMetadata = Rows.Metadata.decode(body); |
| @@ -478,7 +477,7 @@ | ||
| 478 | 477 | public final String columnFamily; |
| 479 | 478 | |
| 480 | 479 | public static final Message.Decoder<Result> subcodec = new Message.Decoder<Result>() { |
| 481 | - public Result decode(ChannelBuffer body) | |
| 480 | + public Result decode(ByteBuf body) | |
| 482 | 481 | { |
| 483 | 482 | Change change = CBUtil.readEnumValue(Change.class, body); |
| 484 | 483 | String keyspace = CBUtil.readString(body); |
| @@ -504,7 +503,7 @@ | ||
| 504 | 503 | public static class Event extends Message.Response { |
| 505 | 504 | |
| 506 | 505 | public static final Message.Decoder<Event> decoder = new Message.Decoder<Event>() { |
| 507 | - public Event decode(ChannelBuffer body) { | |
| 506 | + public Event decode(ByteBuf body) { | |
| 508 | 507 | return new Event(ProtocolEvent.deserialize(body)); |
| 509 | 508 | } |
| 510 | 509 | }; |
| @@ -525,7 +524,7 @@ | ||
| 525 | 524 | public static class AuthChallenge extends Message.Response { |
| 526 | 525 | |
| 527 | 526 | public static final Message.Decoder<AuthChallenge> decoder = new Message.Decoder<AuthChallenge>() { |
| 528 | - public AuthChallenge decode(ChannelBuffer body) { | |
| 527 | + public AuthChallenge decode(ByteBuf body) { | |
| 529 | 528 | ByteBuffer b = CBUtil.readValue(body); |
| 530 | 529 | if (b == null) |
| 531 | 530 | return new AuthChallenge(null); |
| @@ -547,7 +546,7 @@ | ||
| 547 | 546 | public static class AuthSuccess extends Message.Response { |
| 548 | 547 | |
| 549 | 548 | public static final Message.Decoder<AuthSuccess> decoder = new Message.Decoder<AuthSuccess>() { |
| 550 | - public AuthSuccess decode(ChannelBuffer body) { | |
| 549 | + public AuthSuccess decode(ByteBuf body) { | |
| 551 | 550 | ByteBuffer b = CBUtil.readValue(body); |
| 552 | 551 | if (b == null) |
| 553 | 552 | return new AuthSuccess(null); |
modified driver-core/src/main/java/com/datastax/driver/core/SocketOptions.java +106 −1
| @@ -45,7 +45,7 @@ | ||
| 45 | 45 | private volatile Boolean keepAlive; |
| 46 | 46 | private volatile Boolean reuseAddress; |
| 47 | 47 | private volatile Integer soLinger; |
| 48 | - private volatile Boolean tcpNoDelay; | |
| 48 | + private volatile Boolean tcpNoDelay = Boolean.TRUE; | |
| 49 | 49 | private volatile Integer receiveBufferSize; |
| 50 | 50 | private volatile Integer sendBufferSize; |
| 51 | 51 | |
| @@ -131,55 +131,160 @@ | ||
| 131 | 131 | return this; |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | + /** | |
| 135 | + * Returns whether TCP keepalive is enabled. | |
| 136 | + * | |
| 137 | + * @return the value of the option, or {@code null} if it is not set. | |
| 138 | + * @see #setKeepAlive(boolean) | |
| 139 | + */ | |
| 134 | 140 | public Boolean getKeepAlive() { |
| 135 | 141 | return keepAlive; |
| 136 | 142 | } |
| 137 | 143 | |
| 144 | + /** | |
| 145 | + * Sets whether to enable TCP keepalive. | |
| 146 | + * <p> | |
| 147 | + * By default, this option is not set by the driver. The actual value will be the default | |
| 148 | + * from the underlying Netty transport (Java NIO or native epoll). | |
| 149 | + * | |
| 150 | + * @param keepAlive whether to enable or disable the option. | |
| 151 | + * @return this {@code SocketOptions}. | |
| 152 | + * | |
| 153 | + * @see java.net.SocketOptions#TCP_NODELAY | |
| 154 | + */ | |
| 138 | 155 | public SocketOptions setKeepAlive(boolean keepAlive) { |
| 139 | 156 | this.keepAlive = keepAlive; |
| 140 | 157 | return this; |
| 141 | 158 | } |
| 142 | 159 | |
| 160 | + /** | |
| 161 | + * Returns whether reuse-address is enabled. | |
| 162 | + * | |
| 163 | + * @return the value of the option, or {@code null} if it is not set. | |
| 164 | + * @see #setReuseAddress(boolean) | |
| 165 | + */ | |
| 143 | 166 | public Boolean getReuseAddress() { |
| 144 | 167 | return reuseAddress; |
| 145 | 168 | } |
| 146 | 169 | |
| 170 | + /** | |
| 171 | + * Sets whether to enable reuse-address. | |
| 172 | + * <p> | |
| 173 | + * By default, this option is not set by the driver. The actual value will be the default | |
| 174 | + * from the underlying Netty transport (Java NIO or native epoll). | |
| 175 | + * | |
| 176 | + * @param reuseAddress whether to enable or disable the option. | |
| 177 | + * @return this {@code SocketOptions}. | |
| 178 | + * | |
| 179 | + * @see java.net.SocketOptions#SO_REUSEADDR | |
| 180 | + */ | |
| 147 | 181 | public SocketOptions setReuseAddress(boolean reuseAddress) { |
| 148 | 182 | this.reuseAddress = reuseAddress; |
| 149 | 183 | return this; |
| 150 | 184 | } |
| 151 | 185 | |
| 186 | + /** | |
| 187 | + * Returns the linger-on-close timeout. | |
| 188 | + * | |
| 189 | + * @return the value of the option, or {@code null} if it is not set. | |
| 190 | + * | |
| 191 | + * @see #setSoLinger(int) | |
| 192 | + */ | |
| 152 | 193 | public Integer getSoLinger() { |
| 153 | 194 | return soLinger; |
| 154 | 195 | } |
| 155 | 196 | |
| 197 | + /** | |
| 198 | + * Sets the linger-on-close timeout. | |
| 199 | + * <p> | |
| 200 | + * By default, this option is not set by the driver. The actual value will be the default | |
| 201 | + * from the underlying Netty transport (Java NIO or native epoll). | |
| 202 | + | |
| 203 | + * @param soLinger the new value. | |
| 204 | + * @return this {@code SocketOptions}. | |
| 205 | + * | |
| 206 | + * @see java.net.SocketOptions#SO_LINGER | |
| 207 | + */ | |
| 156 | 208 | public SocketOptions setSoLinger(int soLinger) { |
| 157 | 209 | this.soLinger = soLinger; |
| 158 | 210 | return this; |
| 159 | 211 | } |
| 160 | 212 | |
| 213 | + /** | |
| 214 | + * Returns whether Nagle's algorithm is disabled. | |
| 215 | + * | |
| 216 | + * @return the value of the option ({@code true} means Nagle is disabled), or {@code null} if it is not set. | |
| 217 | + * | |
| 218 | + * @see #setTcpNoDelay(boolean) | |
| 219 | + */ | |
| 161 | 220 | public Boolean getTcpNoDelay() { |
| 162 | 221 | return tcpNoDelay; |
| 163 | 222 | } |
| 164 | 223 | |
| 224 | + /** | |
| 225 | + * Sets whether to disable Nagle's algorithm. | |
| 226 | + * <p> | |
| 227 | + * By default, this option is set to {@code true} (Nagle disabled). | |
| 228 | + * | |
| 229 | + * @param tcpNoDelay whether to enable or disable the option. | |
| 230 | + * @return this {@code SocketOptions}. | |
| 231 | + * | |
| 232 | + * @see java.net.SocketOptions#TCP_NODELAY | |
| 233 | + */ | |
| 165 | 234 | public SocketOptions setTcpNoDelay(boolean tcpNoDelay) { |
| 166 | 235 | this.tcpNoDelay = tcpNoDelay; |
| 167 | 236 | return this; |
| 168 | 237 | } |
| 169 | 238 | |
| 239 | + /** | |
| 240 | + * Returns the hint to the size of the underlying buffers for incoming network I/O. | |
| 241 | + * | |
| 242 | + * @return the value of the option, or {@code null} if it is not set. | |
| 243 | + * | |
| 244 | + * @see #setReceiveBufferSize(int) | |
| 245 | + */ | |
| 170 | 246 | public Integer getReceiveBufferSize() { |
| 171 | 247 | return receiveBufferSize; |
| 172 | 248 | } |
| 173 | 249 | |
| 250 | + /** | |
| 251 | + * Sets a hint to the size of the underlying buffers for incoming network I/O. | |
| 252 | + * <p> | |
| 253 | + * By default, this option is not set by the driver. The actual value will be the default | |
| 254 | + * from the underlying Netty transport (Java NIO or native epoll). | |
| 255 | + * | |
| 256 | + * @param receiveBufferSize the new value. | |
| 257 | + * @return this {@code SocketOptions}. | |
| 258 | + * | |
| 259 | + * @see java.net.SocketOptions#SO_RCVBUF | |
| 260 | + */ | |
| 174 | 261 | public SocketOptions setReceiveBufferSize(int receiveBufferSize) { |
| 175 | 262 | this.receiveBufferSize = receiveBufferSize; |
| 176 | 263 | return this; |
| 177 | 264 | } |
| 178 | 265 | |
| 266 | + /** | |
| 267 | + * Returns the hint to the size of the underlying buffers for outgoing network I/O. | |
| 268 | + * | |
| 269 | + * @return the value of the option, or {@code null} if it is not set. | |
| 270 | + * | |
| 271 | + * @see #setSendBufferSize(int) | |
| 272 | + */ | |
| 179 | 273 | public Integer getSendBufferSize() { |
| 180 | 274 | return sendBufferSize; |
| 181 | 275 | } |
| 182 | 276 | |
| 277 | + /** | |
| 278 | + * Sets a hint to the size of the underlying buffers for outgoing network I/O. | |
| 279 | + * <p> | |
| 280 | + * By default, this option is not set by the driver. The actual value will be the default | |
| 281 | + * from the underlying Netty transport (Java NIO or native epoll). | |
| 282 | + * | |
| 283 | + * @param sendBufferSize the new value. | |
| 284 | + * @return this {@code SocketOptions}. | |
| 285 | + * | |
| 286 | + * @see java.net.SocketOptions#SO_SNDBUF | |
| 287 | + */ | |
| 183 | 288 | public SocketOptions setSendBufferSize(int sendBufferSize) { |
| 184 | 289 | this.sendBufferSize = sendBufferSize; |
| 185 | 290 | return this; |
modified driver-core/src/main/java/com/datastax/driver/core/SystemProperties.java +16 −0
| @@ -26,4 +26,20 @@ | ||
| 26 | 26 | return defaultValue; |
| 27 | 27 | } |
| 28 | 28 | } |
| 29 | + | |
| 30 | + static boolean getBoolean(String key, boolean defaultValue) { | |
| 31 | + String stringValue = System.getProperty(key); | |
| 32 | + if (stringValue == null) { | |
| 33 | + logger.debug("{} is undefined, using default value {}", key, defaultValue); | |
| 34 | + return defaultValue; | |
| 35 | + } | |
| 36 | + try { | |
| 37 | + boolean value = Boolean.parseBoolean(stringValue); | |
| 38 | + logger.warn("{} is defined, using value {}", key, value); | |
| 39 | + return value; | |
| 40 | + } catch (NumberFormatException e) { | |
| 41 | + logger.warn("{} is defined but could not parse value {}, using default value {}", key, stringValue, defaultValue); | |
| 42 | + return defaultValue; | |
| 43 | + } | |
| 44 | + } | |
| 29 | 45 | } |
modified driver-examples/osgi/README.rst +1 −1
| @@ -12,7 +12,7 @@ | ||
| 12 | 12 | |
| 13 | 13 | To build the bundle and run tests execute the following maven task:: |
| 14 | 14 | |
| 15 | - mvn test -P short | |
| 15 | + mvn integration-test -P short | |
| 16 | 16 | |
| 17 | 17 | The short profile needs to be activated since the tests run under |
| 18 | 18 | this group. |
modified driver-examples/osgi/src/test/java/com/datastax/driver/osgi/MailboxServiceIT.java +19 −7
| @@ -57,8 +57,20 @@ | ||
| 57 | 57 | return mavenBundle("com.google.guava", "guava", "14.0.1"); |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | - private MavenArtifactProvisionOption nettyBundle() { | |
| 61 | - return mavenBundle("io.netty", "netty", "3.9.0.Final"); | |
| 60 | + private CompositeOption nettyBundles() { | |
| 61 | + final String nettyVersion = "4.0.26.Final"; | |
| 62 | + return new CompositeOption() { | |
| 63 | + | |
| 64 | + @Override public Option[] getOptions() { | |
| 65 | + return options( | |
| 66 | + mavenBundle("io.netty", "netty-buffer", nettyVersion), | |
| 67 | + mavenBundle("io.netty", "netty-codec", nettyVersion), | |
| 68 | + mavenBundle("io.netty", "netty-common", nettyVersion), | |
| 69 | + mavenBundle("io.netty", "netty-handler", nettyVersion), | |
| 70 | + mavenBundle("io.netty", "netty-transport", nettyVersion) | |
| 71 | + ); | |
| 72 | + } | |
| 73 | + }; | |
| 62 | 74 | } |
| 63 | 75 | |
| 64 | 76 | private CompositeOption defaultOptions() { |
| @@ -92,7 +104,7 @@ | ||
| 92 | 104 | return options( |
| 93 | 105 | driverBundle(), |
| 94 | 106 | guavaBundle(), |
| 95 | - nettyBundle(), | |
| 107 | + nettyBundles(), | |
| 96 | 108 | defaultOptions() |
| 97 | 109 | ); |
| 98 | 110 | } |
| @@ -101,7 +113,7 @@ | ||
| 101 | 113 | public Option[] guava15Config() { |
| 102 | 114 | return options( |
| 103 | 115 | driverBundle(), |
| 104 | - nettyBundle(), | |
| 116 | + nettyBundles(), | |
| 105 | 117 | guavaBundle().version("15.0"), |
| 106 | 118 | defaultOptions() |
| 107 | 119 | ); |
| @@ -111,7 +123,7 @@ | ||
| 111 | 123 | public Option[] guava16Config() { |
| 112 | 124 | return options( |
| 113 | 125 | driverBundle(), |
| 114 | - nettyBundle(), | |
| 126 | + nettyBundles(), | |
| 115 | 127 | guavaBundle().version("16.0.1"), |
| 116 | 128 | defaultOptions() |
| 117 | 129 | ); |
| @@ -121,7 +133,7 @@ | ||
| 121 | 133 | public Option[] guava17Config() { |
| 122 | 134 | return options( |
| 123 | 135 | driverBundle(), |
| 124 | - nettyBundle(), | |
| 136 | + nettyBundles(), | |
| 125 | 137 | guavaBundle().version("17.0"), |
| 126 | 138 | defaultOptions() |
| 127 | 139 | ); |
| @@ -131,7 +143,7 @@ | ||
| 131 | 143 | public Option[] guava18Config() { |
| 132 | 144 | return options( |
| 133 | 145 | driverBundle(), |
| 134 | - nettyBundle(), | |
| 146 | + nettyBundles(), | |
| 135 | 147 | guavaBundle().version("18.0"), |
| 136 | 148 | defaultOptions() |
| 137 | 149 | ); |
modified pom.xml +1 −1
| @@ -46,7 +46,7 @@ | ||
| 46 | 46 | Check with the spark-cassandra-connector team before upgrading this. |
| 47 | 47 | --> |
| 48 | 48 | <guava.version>14.0.1</guava.version> |
| 49 | - <netty.version>3.9.0.Final</netty.version> | |
| 49 | + <netty.version>4.0.26.Final</netty.version> | |
| 50 | 50 | <metrics.version>3.0.2</metrics.version> |
| 51 | 51 | <snappy.version>1.0.5</snappy.version> |
| 52 | 52 | <lz4.version>1.2.0</lz4.version> |