| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
package com.allanbank.mongodb.client.connection.socket; |
| 21 | |
|
| 22 | |
import java.io.IOException; |
| 23 | |
import java.lang.ref.Reference; |
| 24 | |
import java.lang.ref.SoftReference; |
| 25 | |
import java.net.Socket; |
| 26 | |
import java.net.SocketException; |
| 27 | |
|
| 28 | |
import com.allanbank.mongodb.MongoClientConfiguration; |
| 29 | |
import com.allanbank.mongodb.MongoDbException; |
| 30 | |
import com.allanbank.mongodb.bson.io.BufferingBsonOutputStream; |
| 31 | |
import com.allanbank.mongodb.bson.io.RandomAccessOutputStream; |
| 32 | |
import com.allanbank.mongodb.bson.io.StringDecoderCache; |
| 33 | |
import com.allanbank.mongodb.bson.io.StringEncoderCache; |
| 34 | |
import com.allanbank.mongodb.client.Message; |
| 35 | |
import com.allanbank.mongodb.client.callback.AddressAware; |
| 36 | |
import com.allanbank.mongodb.client.callback.ReplyCallback; |
| 37 | |
import com.allanbank.mongodb.client.message.BuildInfo; |
| 38 | |
import com.allanbank.mongodb.client.message.PendingMessage; |
| 39 | |
import com.allanbank.mongodb.client.state.Server; |
| 40 | |
import com.allanbank.mongodb.client.state.ServerUpdateCallback; |
| 41 | |
import com.allanbank.mongodb.util.IOUtils; |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
public class SocketConnection extends AbstractSocketConnection { |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
private final ThreadLocal<Reference<BufferingBsonOutputStream>> myBuffers; |
| 68 | |
|
| 69 | |
|
| 70 | |
private final Thread myReceiver; |
| 71 | |
|
| 72 | |
|
| 73 | |
private final Sequence mySendSequence; |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
public SocketConnection(final Server server, |
| 88 | |
final MongoClientConfiguration config) throws SocketException, |
| 89 | |
IOException { |
| 90 | 58 | this(server, config, new StringEncoderCache(), |
| 91 | |
new StringDecoderCache(), |
| 92 | |
new ThreadLocal<Reference<BufferingBsonOutputStream>>()); |
| 93 | 52 | } |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
public SocketConnection(final Server server, |
| 116 | |
final MongoClientConfiguration config, |
| 117 | |
final StringEncoderCache encoderCache, |
| 118 | |
final StringDecoderCache decoderCache, |
| 119 | |
final ThreadLocal<Reference<BufferingBsonOutputStream>> buffers) |
| 120 | |
throws SocketException, IOException { |
| 121 | 164 | super(server, config, encoderCache, decoderCache); |
| 122 | |
|
| 123 | 136 | myBuffers = buffers; |
| 124 | |
|
| 125 | 136 | mySendSequence = new Sequence(1L, config.getLockType()); |
| 126 | |
|
| 127 | 136 | myReceiver = config.getThreadFactory().newThread( |
| 128 | |
new ReceiveRunnable(this)); |
| 129 | 136 | myReceiver.setDaemon(true); |
| 130 | 136 | myReceiver.setName("MongoDB " + mySocket.getLocalPort() + "<--" |
| 131 | |
+ myServer.getCanonicalName()); |
| 132 | 136 | } |
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
@Override |
| 138 | |
public void close() throws IOException { |
| 139 | 407 | if (myOpen.compareAndSet(true, false)) { |
| 140 | 136 | myReceiver.interrupt(); |
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | 136 | myOutput.close(); |
| 145 | 136 | myInput.close(); |
| 146 | 136 | mySocket.close(); |
| 147 | |
|
| 148 | |
try { |
| 149 | 136 | if (Thread.currentThread() != myReceiver) { |
| 150 | 119 | myReceiver.join(); |
| 151 | |
} |
| 152 | |
} |
| 153 | 0 | catch (final InterruptedException ie) { |
| 154 | |
|
| 155 | 136 | } |
| 156 | |
|
| 157 | 136 | myEventSupport.firePropertyChange(OPEN_PROP_NAME, true, false); |
| 158 | |
} |
| 159 | 407 | } |
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
@Override |
| 168 | |
public int getPendingCount() { |
| 169 | 3 | return super.getPendingCount() + mySendSequence.getWaitersCount(); |
| 170 | |
} |
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
@Override |
| 179 | |
public boolean isIdle() { |
| 180 | 90 | return super.isIdle() && mySendSequence.isIdle(); |
| 181 | |
} |
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
@Override |
| 187 | |
public void send(final Message message1, final Message message2, |
| 188 | |
final ReplyCallback replyCallback) throws MongoDbException { |
| 189 | |
|
| 190 | 298 | validate(message1, message2); |
| 191 | |
|
| 192 | 295 | if (replyCallback instanceof AddressAware) { |
| 193 | 0 | ((AddressAware) replyCallback).setAddress(myServer |
| 194 | |
.getCanonicalName()); |
| 195 | |
} |
| 196 | |
|
| 197 | 295 | final int count = (message2 == null) ? 1 : 2; |
| 198 | 295 | final long seq = mySendSequence.reserve(count); |
| 199 | 295 | final long end = seq + count; |
| 200 | |
|
| 201 | 295 | boolean sawError = false; |
| 202 | 295 | final PendingMessage pendingMessage = new PendingMessage(); |
| 203 | |
try { |
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | 295 | final Reference<BufferingBsonOutputStream> outRef = myBuffers.get(); |
| 208 | 295 | BufferingBsonOutputStream out = (outRef != null) ? outRef.get() |
| 209 | |
: null; |
| 210 | 295 | if (out == null) { |
| 211 | 75 | out = new BufferingBsonOutputStream( |
| 212 | |
new RandomAccessOutputStream(myEncoderCache)); |
| 213 | 75 | myBuffers |
| 214 | |
.set(new SoftReference<BufferingBsonOutputStream>(out)); |
| 215 | |
} |
| 216 | |
|
| 217 | 295 | message1.write((int) (seq & 0xFFFFFF), out); |
| 218 | 292 | if (message2 != null) { |
| 219 | 1 | message2.write((int) ((seq + 1) & 0xFFFFFF), out); |
| 220 | |
} |
| 221 | |
|
| 222 | |
|
| 223 | 292 | mySendSequence.waitFor(seq); |
| 224 | |
|
| 225 | 292 | if (count == 1) { |
| 226 | 291 | pendingMessage.set((int) (seq & 0xFFFFFF), message1, |
| 227 | |
replyCallback); |
| 228 | 291 | send(pendingMessage, out.getOutput()); |
| 229 | |
} |
| 230 | |
else { |
| 231 | 1 | pendingMessage.set((int) ((seq + 1) & 0xFFFFFF), message2, |
| 232 | |
replyCallback); |
| 233 | 1 | send(pendingMessage, out.getOutput()); |
| 234 | |
} |
| 235 | |
|
| 236 | |
|
| 237 | 291 | if (mySendSequence.noWaiter(end)) { |
| 238 | 291 | if (myReceiver != Thread.currentThread()) { |
| 239 | 289 | flush(); |
| 240 | |
} |
| 241 | |
else { |
| 242 | 2 | markReaderNeedsToFlush(); |
| 243 | |
} |
| 244 | |
} |
| 245 | |
} |
| 246 | 0 | catch (final InterruptedException ie) { |
| 247 | |
|
| 248 | |
|
| 249 | 0 | raiseError(ie, pendingMessage.getReplyCallback()); |
| 250 | |
} |
| 251 | 2 | catch (final IOException ioe) { |
| 252 | 2 | myLog.warn(ioe, "I/O Error sending a message."); |
| 253 | 2 | raiseError(ioe, pendingMessage.getReplyCallback()); |
| 254 | 2 | sawError = true; |
| 255 | |
} |
| 256 | 1 | catch (final RuntimeException re) { |
| 257 | 1 | myLog.warn(re, "Runtime error sending a message."); |
| 258 | 1 | raiseError(re, pendingMessage.getReplyCallback()); |
| 259 | 1 | sawError = true; |
| 260 | |
} |
| 261 | 1 | catch (final Error error) { |
| 262 | 1 | myLog.error(error, "Error sending a message."); |
| 263 | 1 | raiseError(error, pendingMessage.getReplyCallback()); |
| 264 | 1 | sawError = true; |
| 265 | |
} |
| 266 | |
finally { |
| 267 | 295 | pendingMessage.clear(); |
| 268 | 295 | mySendSequence.release(seq, end); |
| 269 | |
|
| 270 | 295 | if (sawError) { |
| 271 | |
|
| 272 | |
try { |
| 273 | 4 | if (myOpen.get()) { |
| 274 | 3 | flush(); |
| 275 | |
} |
| 276 | |
} |
| 277 | 0 | catch (final IOException ioe) { |
| 278 | 0 | myLog.warn(ioe, "I/O Error on final flush of messages."); |
| 279 | |
} |
| 280 | |
finally { |
| 281 | |
|
| 282 | 4 | IOUtils.close(SocketConnection.this); |
| 283 | 4 | } |
| 284 | |
} |
| 285 | |
} |
| 286 | 295 | } |
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
@Override |
| 292 | |
public void send(final Message message, final ReplyCallback replyCallback) |
| 293 | |
throws MongoDbException { |
| 294 | 296 | send(message, null, replyCallback); |
| 295 | 294 | } |
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
@Override |
| 301 | |
public void start() { |
| 302 | 135 | myReceiver.start(); |
| 303 | |
|
| 304 | 135 | if (myServer.needBuildInfo()) { |
| 305 | 83 | send(new BuildInfo(), new ServerUpdateCallback(myServer)); |
| 306 | |
} |
| 307 | 135 | } |
| 308 | |
} |