forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBPortPool.java
More file actions
232 lines (187 loc) · 7.33 KB
/
DBPortPool.java
File metadata and controls
232 lines (187 loc) · 7.33 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
// DBPortPool.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 java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import com.mongodb.util.SimplePool;
import com.mongodb.util.management.JMException;
import com.mongodb.util.management.MBeanServerFactory;
public class DBPortPool extends SimplePool<DBPort> {
static class Holder {
Holder( MongoOptions options ){
_options = options;
}
DBPortPool get( ServerAddress addr ){
DBPortPool p = _pools.get( addr );
if (p != null)
return p;
synchronized (_pools) {
p = _pools.get( addr );
if (p != null) {
return p;
}
p = new DBPortPool( addr , _options );
_pools.put( addr , p);
try {
String on = createObjectName(addr);
if (MBeanServerFactory.getMBeanServer().isRegistered(on)) {
MBeanServerFactory.getMBeanServer().unregisterMBean(on);
Bytes.LOGGER.log(Level.INFO, "multiple Mongo instances for same host, jmx numbers might be off");
}
MBeanServerFactory.getMBeanServer().registerMBean(p, on);
} catch (JMException e) {
Bytes.LOGGER.log(Level.WARNING, "jmx registration error: " + e + " continuing...");
} catch (java.security.AccessControlException e) {
Bytes.LOGGER.log(Level.WARNING, "jmx registration error: " + e + " continuing...");
}
}
return p;
}
void close(){
synchronized ( _pools ){
for ( DBPortPool p : _pools.values() ){
p.close();
try {
String on = createObjectName( p._addr );
if ( MBeanServerFactory.getMBeanServer().isRegistered(on) ){
MBeanServerFactory.getMBeanServer().unregisterMBean(on);
}
} catch ( JMException e ){
Bytes.LOGGER.log( Level.WARNING , "jmx de-registration error, continuing" , e );
}
}
}
}
private String createObjectName( ServerAddress addr ) {
String name = "com.mongodb:type=ConnectionPool,host=" + addr.toString().replace( ":" , ",port=" ) + ",instance=" + _serial;
if ( _options.description != null )
name += ",description=" + _options.description;
return name;
}
final MongoOptions _options;
final Map<ServerAddress,DBPortPool> _pools = Collections.synchronizedMap( new HashMap<ServerAddress,DBPortPool>() );
final int _serial = nextSerial.incrementAndGet();
// we use this to give each Holder a different mbean name
static AtomicInteger nextSerial = new AtomicInteger(0);
}
// ----
public static class NoMoreConnection extends MongoInternalException {
private static final long serialVersionUID = -4415279469780082174L;
NoMoreConnection( String msg ){
super( msg );
}
}
public static class SemaphoresOut extends NoMoreConnection {
private static final long serialVersionUID = -4415279469780082174L;
SemaphoresOut(){
super( "Out of semaphores to get db connection" );
}
}
public static class ConnectionWaitTimeOut extends NoMoreConnection {
private static final long serialVersionUID = -4415279469780082174L;
ConnectionWaitTimeOut(int timeout) {
super("Connection wait timeout after " + timeout + " ms");
}
}
// ----
DBPortPool( ServerAddress addr , MongoOptions options ){
super( "DBPortPool-" + addr.toString() + ", options = " + options.toString() , options.connectionsPerHost , options.connectionsPerHost );
_options = options;
_addr = addr;
_waitingSem = new Semaphore( _options.connectionsPerHost * _options.threadsAllowedToBlockForConnectionMultiplier );
}
protected long memSize( DBPort p ){
return 0;
}
protected int pick( int iThink , boolean couldCreate ){
final int id = System.identityHashCode(Thread.currentThread());
final int s = _availSafe.size();
for ( int i=0; i<s; i++ ){
DBPort p = _availSafe.get(i);
if ( p._lastThread == id )
return i;
}
if ( couldCreate )
return -1;
return iThink;
}
public DBPort get(){
DBPort port = null;
if ( ! _waitingSem.tryAcquire() )
throw new SemaphoresOut();
try {
port = get( _options.maxWaitTime );
}
finally {
_waitingSem.release();
}
if ( port == null )
throw new ConnectionWaitTimeOut( _options.maxWaitTime );
port._lastThread = System.identityHashCode(Thread.currentThread());
return port;
}
void gotError( Exception e ){
if ( e instanceof java.nio.channels.ClosedByInterruptException ||
e instanceof InterruptedException ){
// this is probably a request that is taking too long
// so usually doesn't mean there is a real db problem
return;
}
if ( e instanceof java.net.SocketTimeoutException ){
// we don't want to clear the port pool for a connection timing out
return;
}
Bytes.LOGGER.log( Level.WARNING , "emptying DBPortPool to " + getServerAddress() + " b/c of error" , e );
// force close all sockets
List<DBPort> all = new ArrayList<DBPort>();
while ( true ){
DBPort temp = get(0);
if ( temp == null )
break;
all.add( temp );
}
for ( DBPort p : all ){
p.close();
done(p);
}
}
void close(){
clear();
}
public void cleanup( DBPort p ){
p.close();
}
public boolean ok( DBPort t ){
return _addr.getSocketAddress().equals( t._addr );
}
protected DBPort createNew(){
return new DBPort( _addr , this , _options );
}
public ServerAddress getServerAddress() {
return _addr;
}
final MongoOptions _options;
final private Semaphore _waitingSem;
final ServerAddress _addr;
boolean _everWorked = false;
}