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.sharded;
21
22 import java.io.IOException;
23 import java.net.InetSocketAddress;
24 import java.util.List;
25 import java.util.concurrent.ExecutionException;
26 import java.util.logging.Level;
27
28 import com.allanbank.mongodb.MongoClientConfiguration;
29 import com.allanbank.mongodb.MongoDbException;
30 import com.allanbank.mongodb.ReadPreference;
31 import com.allanbank.mongodb.bson.Document;
32 import com.allanbank.mongodb.bson.Element;
33 import com.allanbank.mongodb.bson.element.StringElement;
34 import com.allanbank.mongodb.builder.Find;
35 import com.allanbank.mongodb.client.ClusterStats;
36 import com.allanbank.mongodb.client.ClusterType;
37 import com.allanbank.mongodb.client.Message;
38 import com.allanbank.mongodb.client.callback.FutureReplyCallback;
39 import com.allanbank.mongodb.client.connection.Connection;
40 import com.allanbank.mongodb.client.connection.ConnectionFactory;
41 import com.allanbank.mongodb.client.connection.ReconnectStrategy;
42 import com.allanbank.mongodb.client.connection.proxy.ProxiedConnectionFactory;
43 import com.allanbank.mongodb.client.message.GetMore;
44 import com.allanbank.mongodb.client.message.Query;
45 import com.allanbank.mongodb.client.message.Reply;
46 import com.allanbank.mongodb.client.state.Cluster;
47 import com.allanbank.mongodb.client.state.ClusterPinger;
48 import com.allanbank.mongodb.client.state.LatencyServerSelector;
49 import com.allanbank.mongodb.client.state.Server;
50 import com.allanbank.mongodb.client.state.ServerSelector;
51 import com.allanbank.mongodb.util.IOUtils;
52 import com.allanbank.mongodb.util.log.Log;
53 import com.allanbank.mongodb.util.log.LogFactory;
54
55
56
57
58
59
60
61
62
63 public class ShardedConnectionFactory implements ConnectionFactory {
64
65
66 protected static final Log LOG = LogFactory
67 .getLog(ShardedConnectionFactory.class);
68
69
70 protected final Cluster myCluster;
71
72
73 protected final MongoClientConfiguration myConfig;
74
75
76 protected final ProxiedConnectionFactory myConnectionFactory;
77
78
79 protected final ClusterPinger myPinger;
80
81
82 protected final ServerSelector mySelector;
83
84
85
86
87
88
89
90
91
92 public ShardedConnectionFactory(final ProxiedConnectionFactory factory,
93 final MongoClientConfiguration config) {
94 myConnectionFactory = factory;
95 myConfig = config;
96 myCluster = createCluster(config);
97 mySelector = createSelector();
98 myPinger = createClusterPinger(factory, config);
99
100
101 for (final InetSocketAddress address : config.getServerAddresses()) {
102 myCluster.add(address);
103 }
104
105 bootstrap();
106 }
107
108
109
110
111 public void bootstrap() {
112 final BootstrapState state = createBootstrapState();
113 if (!state.done()) {
114 for (final Server addr : myCluster.getServers()) {
115 Connection conn = null;
116 try {
117
118 conn = myConnectionFactory.connect(addr, myConfig);
119
120 update(state, conn);
121
122 if (state.done()) {
123 break;
124 }
125 }
126 catch (final IOException ioe) {
127 LOG.warn(ioe, "I/O error during sharded bootstrap to {}.",
128 addr);
129 }
130 catch (final MongoDbException me) {
131 LOG.warn(me,
132 "MongoDB error during sharded bootstrap to {}.",
133 addr);
134 }
135 catch (final InterruptedException e) {
136 LOG.warn(e, "Interrupted during sharded bootstrap to {}.",
137 addr);
138 }
139 catch (final ExecutionException e) {
140 LOG.warn(e, "Error during sharded bootstrap to {}.", addr);
141 }
142 finally {
143 IOUtils.close(conn, Level.WARNING,
144 "I/O error shutting down sharded bootstrap connection to "
145 + addr + ".");
146 }
147 }
148 }
149
150
151
152 myPinger.initialSweep(myCluster);
153 myPinger.start();
154 }
155
156
157
158
159
160
161
162
163 @Override
164 public void close() {
165 IOUtils.close(myPinger);
166 IOUtils.close(myConnectionFactory);
167 }
168
169
170
171
172
173
174 @Override
175 public Connection connect() throws IOException {
176 IOException lastError = null;
177 for (final Server server : mySelector.pickServers()) {
178 try {
179 final Connection primaryConn = myConnectionFactory.connect(
180 server, myConfig);
181
182 return wrap(primaryConn, server);
183 }
184 catch (final IOException e) {
185 lastError = e;
186 }
187 }
188
189 if (lastError != null) {
190 throw lastError;
191 }
192
193 throw new IOException(
194 "Could not determine a shard server to connect to.");
195 }
196
197
198
199
200
201
202
203 @Override
204 public ClusterStats getClusterStats() {
205 return myCluster;
206 }
207
208
209
210
211
212
213
214 @Override
215 public ClusterType getClusterType() {
216 return ClusterType.SHARDED;
217 }
218
219
220
221
222
223
224
225
226 @Override
227 public ReconnectStrategy getReconnectStrategy() {
228 final ReconnectStrategy delegates = myConnectionFactory
229 .getReconnectStrategy();
230
231 delegates.setState(myCluster);
232 delegates.setSelector(mySelector);
233 delegates.setConnectionFactory(myConnectionFactory);
234
235 return delegates;
236 }
237
238
239
240
241
242
243
244 protected BootstrapState createBootstrapState() {
245 return new BootstrapState(!myConfig.isAutoDiscoverServers());
246 }
247
248
249
250
251
252
253
254
255
256 protected Cluster createCluster(final MongoClientConfiguration config) {
257 return new Cluster(config, ClusterType.SHARDED);
258 }
259
260
261
262
263
264
265
266
267
268
269
270
271
272 protected ClusterPinger createClusterPinger(
273 final ProxiedConnectionFactory factory,
274 final MongoClientConfiguration config) {
275 return new ClusterPinger(myCluster, factory, config);
276 }
277
278
279
280
281
282
283
284
285
286
287
288 protected ServerSelector createSelector() {
289 return new LatencyServerSelector(myCluster, true);
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 protected boolean findMongosServers(final Connection conn)
319 throws InterruptedException, ExecutionException {
320 boolean found = false;
321
322
323
324 Message message = new Query("config", "mongos", Find.ALL,
325 null, 0,
326 0, 0, false,
327 ReadPreference.PRIMARY, false,
328 false, false,
329 false);
330
331 while (message != null) {
332
333 final FutureReplyCallback future = new FutureReplyCallback();
334 conn.send(message, future);
335
336
337 message = null;
338
339
340 final Reply reply = future.get();
341
342
343 final List<Document> docs = reply.getResults();
344 for (final Document doc : docs) {
345 final Element idElem = doc.get("_id");
346 if (idElem instanceof StringElement) {
347 final StringElement id = (StringElement) idElem;
348
349 myCluster.add(id.getValue());
350 found = true;
351
352 LOG.debug("Adding shard mongos: {}", id.getValue());
353 }
354 }
355
356
357 if (reply.getCursorId() != 0) {
358
359 message = new GetMore("config", "mongos", reply.getCursorId(),
360 0, ReadPreference.PRIMARY);
361 }
362 }
363
364 return found;
365 }
366
367
368
369
370
371
372 protected Cluster getCluster() {
373 return myCluster;
374 }
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390 protected void update(final BootstrapState state, final Connection conn)
391 throws InterruptedException, ExecutionException {
392 if (state.isMongosFound() || findMongosServers(conn)) {
393 state.setMongosFound(true);
394 }
395 }
396
397
398
399
400
401
402
403
404
405
406 protected Connection wrap(final Connection primaryConn, final Server server) {
407 return new ShardedConnection(primaryConn, server, myCluster,
408 mySelector, myConnectionFactory, myConfig);
409 }
410
411
412
413
414
415
416
417 protected static class BootstrapState {
418
419 private boolean myMongosFound;
420
421
422
423
424
425
426
427 protected BootstrapState(final boolean mongosFound) {
428 myMongosFound = mongosFound;
429 }
430
431
432
433
434
435
436
437
438
439 public boolean done() {
440 return myMongosFound;
441 }
442
443
444
445
446
447
448
449
450 public boolean isMongosFound() {
451 return myMongosFound;
452 }
453
454
455
456
457
458
459
460
461 public void setMongosFound(final boolean mongosFound) {
462 myMongosFound = mongosFound;
463 }
464 }
465 }