forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBPort.java
More file actions
355 lines (285 loc) · 10.3 KB
/
DBPort.java
File metadata and controls
355 lines (285 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// DBPort.java
/**
* Copyright (C) 2008 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb;
import com.mongodb.util.ThreadUtil;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* represents a Port to the database, which is effectively a single connection to a server
* Methods implemented at the port level should throw the raw exceptions like IOException,
* so that the connector above can make appropriate decisions on how to handle.
*/
public class DBPort {
/**
* the default port
*/
public static final int PORT = 27017;
static final boolean USE_NAGLE = false;
static final long CONN_RETRY_TIME_MS = 15000;
/**
* creates a new DBPort
* @param addr the server address
*/
public DBPort( ServerAddress addr ){
this( addr , null , new MongoOptions() );
}
DBPort( ServerAddress addr, DBPortPool pool, MongoOptions options ){
_options = options;
_sa = addr;
_addr = addr.getSocketAddress();
_pool = pool;
_hashCode = _addr.hashCode();
_logger = Logger.getLogger( _rootLogger.getName() + "." + addr.toString() );
_decoder = _options.dbDecoderFactory.create();
}
Response call( OutMessage msg , DBCollection coll ) throws IOException{
return go( msg, coll );
}
Response call(OutMessage msg, DBCollection coll, DBDecoder decoder) throws IOException{
return go( msg, coll, false, decoder);
}
void say( OutMessage msg )
throws IOException {
go( msg , null );
}
private synchronized Response go( OutMessage msg , DBCollection coll )
throws IOException {
return go( msg , coll , false, null );
}
private synchronized Response go( OutMessage msg , DBCollection coll , DBDecoder decoder ) throws IOException{
return go( msg, coll, false, decoder );
}
private synchronized Response go(OutMessage msg, DBCollection coll, boolean forceResponse, DBDecoder decoder)
throws IOException {
if ( _processingResponse ){
if ( coll == null ){
// this could be a pipeline and should be safe
}
else {
// this could cause issues since we're reading data off the wire
throw new IllegalStateException( "DBPort.go called and expecting a response while processing another response" );
}
}
_calls++;
if ( _socket == null )
_open();
if ( _out == null )
throw new IllegalStateException( "_out shouldn't be null" );
try {
msg.prepare();
_activeState = new ActiveState(msg);
msg.pipe( _out );
if ( _pool != null )
_pool._everWorked = true;
if ( coll == null && ! forceResponse )
return null;
_processingResponse = true;
return new Response( _sa , coll , _in , (decoder == null ? _decoder : decoder) );
}
catch ( IOException ioe ){
close();
throw ioe;
}
finally {
_activeState = null;
_processingResponse = false;
}
}
synchronized CommandResult getLastError( DB db , WriteConcern concern ) throws IOException{
DBApiLayer dbAL = (DBApiLayer) db;
return runCommand( dbAL, concern.getCommand() );
}
synchronized private Response findOne( DB db , String coll , DBObject q ) throws IOException {
OutMessage msg = OutMessage.query( db.getCollection(coll) , 0 , 0 , -1 , q , null );
Response res = go( msg , db.getCollection( coll ) , null );
return res;
}
synchronized CommandResult runCommand( DB db , DBObject cmd ) throws IOException {
Response res = findOne(db, "$cmd", cmd);
return convertToCommandResult(cmd, res);
}
private CommandResult convertToCommandResult(DBObject cmd, Response res) {
if ( res.size() == 0 )
return null;
if ( res.size() > 1 )
throw new MongoInternalException( "something is wrong. size:" + res.size() );
DBObject data = res.get(0);
if ( data == null )
throw new MongoInternalException( "something is wrong, no command result" );
CommandResult cr = new CommandResult(cmd, res.serverUsed());
cr.putAll( data );
return cr;
}
synchronized CommandResult tryGetLastError( DB db , long last, WriteConcern concern) throws IOException {
if ( last != _calls )
return null;
return getLastError(db, concern);
}
/**
* makes sure that a connection to the server has been opened
* @throws IOException
*/
public synchronized void ensureOpen()
throws IOException {
if ( _socket != null )
return;
_open();
}
boolean _open()
throws IOException {
long sleepTime = 100;
long maxAutoConnectRetryTime = CONN_RETRY_TIME_MS;
if (_options.maxAutoConnectRetryTime > 0) {
maxAutoConnectRetryTime = _options.maxAutoConnectRetryTime;
}
final long start = System.currentTimeMillis();
while ( true ){
IOException lastError = null;
try {
_socket = _options.socketFactory.createSocket();
_socket.connect( _addr , _options.connectTimeout );
_socket.setTcpNoDelay( ! USE_NAGLE );
_socket.setKeepAlive( _options.socketKeepAlive );
_socket.setSoTimeout( _options.socketTimeout );
_in = new BufferedInputStream( _socket.getInputStream() );
_out = _socket.getOutputStream();
return true;
}
catch ( IOException ioe ){
lastError = new IOException( "couldn't connect to [" + _addr + "] bc:" + ioe );
_logger.log( Level.INFO , "connect fail to : " + _addr , ioe );
close();
}
if ( ! _options.autoConnectRetry || ( _pool != null && ! _pool._everWorked ) )
throw lastError;
long sleptSoFar = System.currentTimeMillis() - start;
if ( sleptSoFar >= maxAutoConnectRetryTime )
throw lastError;
if ( sleepTime + sleptSoFar > maxAutoConnectRetryTime )
sleepTime = maxAutoConnectRetryTime - sleptSoFar;
_logger.severe( "going to sleep and retry. total sleep time after = " + ( sleptSoFar + sleptSoFar ) + "ms this time:" + sleepTime + "ms" );
ThreadUtil.sleep( sleepTime );
sleepTime *= 2;
}
}
@Override
public int hashCode(){
return _hashCode;
}
/**
* returns a String representation of the target host
* @return
*/
public String host(){
return _addr.toString();
}
/**
* @return the server address for this port
*/
public ServerAddress serverAddress() {
return _sa;
}
@Override
public String toString(){
return "{DBPort " + host() + "}";
}
@Override
protected void finalize() throws Throwable{
super.finalize();
close();
}
ActiveState getActiveState() {
return _activeState;
}
int getLocalPort() {
return _socket != null ? _socket.getLocalPort() : -1;
}
/**
* closes the underlying connection and streams
*/
protected void close(){
_authed.clear();
if ( _socket != null ){
try {
_socket.close();
}
catch ( Exception e ){
// don't care
}
}
_in = null;
_out = null;
_socket = null;
}
void checkAuth( DB db ) throws IOException {
DB.AuthenticationCredentials credentials = db.getAuthenticationCredentials();
if ( credentials == null ){
if ( db._name.equals( "admin" ) )
return;
checkAuth( db._mongo.getDB( "admin" ) );
return;
}
if ( _authed.containsKey( db ) )
return;
CommandResult res = runCommand( db , credentials.getNonceCommand() );
res.throwOnError();
res = runCommand( db , credentials.getAuthCommand( res.getString( "nonce" ) ) );
res.throwOnError();
_authed.put( db , true );
}
/**
* Gets the pool that this port belongs to
* @return
*/
public DBPortPool getPool() {
return _pool;
}
final int _hashCode;
final ServerAddress _sa;
final InetSocketAddress _addr;
final DBPortPool _pool;
final MongoOptions _options;
final Logger _logger;
final DBDecoder _decoder;
private Socket _socket;
private InputStream _in;
private OutputStream _out;
private boolean _processingResponse;
private Map<DB,Boolean> _authed = new ConcurrentHashMap<DB, Boolean>( );
int _lastThread;
long _calls = 0;
private volatile ActiveState _activeState;
class ActiveState {
ActiveState(final OutMessage outMessage) {
this.outMessage = outMessage;
this.startTime = System.nanoTime();
this.threadName = Thread.currentThread().getName();
}
final OutMessage outMessage;
final long startTime;
final String threadName;
}
private static Logger _rootLogger = Logger.getLogger( "com.mongodb.port" );
}