| 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.state; |
| 21 | |
|
| 22 | |
import java.io.Closeable; |
| 23 | |
import java.io.IOException; |
| 24 | |
import java.util.ArrayList; |
| 25 | |
import java.util.Collections; |
| 26 | |
import java.util.HashMap; |
| 27 | |
import java.util.Iterator; |
| 28 | |
import java.util.List; |
| 29 | |
import java.util.Map; |
| 30 | |
import java.util.concurrent.CopyOnWriteArrayList; |
| 31 | |
import java.util.concurrent.ExecutionException; |
| 32 | |
import java.util.concurrent.Future; |
| 33 | |
import java.util.concurrent.TimeUnit; |
| 34 | |
import java.util.concurrent.TimeoutException; |
| 35 | |
|
| 36 | |
import com.allanbank.mongodb.MongoClientConfiguration; |
| 37 | |
import com.allanbank.mongodb.MongoDbException; |
| 38 | |
import com.allanbank.mongodb.client.ClusterType; |
| 39 | |
import com.allanbank.mongodb.client.connection.Connection; |
| 40 | |
import com.allanbank.mongodb.client.connection.proxy.ProxiedConnectionFactory; |
| 41 | |
import com.allanbank.mongodb.client.message.IsMaster; |
| 42 | |
import com.allanbank.mongodb.client.message.ReplicaSetStatus; |
| 43 | |
import com.allanbank.mongodb.client.message.Reply; |
| 44 | |
import com.allanbank.mongodb.util.IOUtils; |
| 45 | |
import com.allanbank.mongodb.util.log.Log; |
| 46 | |
import com.allanbank.mongodb.util.log.LogFactory; |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
public class ClusterPinger implements Runnable, Closeable { |
| 57 | |
|
| 58 | |
|
| 59 | |
public static final int DEFAULT_PING_INTERVAL_SECONDS = 600; |
| 60 | |
|
| 61 | |
|
| 62 | 1 | protected static final Log LOG = LogFactory.getLog(ClusterPinger.class); |
| 63 | |
|
| 64 | |
|
| 65 | 1 | private static final Pinger PINGER = new Pinger(); |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
public static boolean ping(final Server server, final Connection conn) { |
| 77 | 6 | return PINGER.ping(server, conn); |
| 78 | |
} |
| 79 | |
|
| 80 | |
|
| 81 | |
private final List<Cluster> myClusters; |
| 82 | |
|
| 83 | |
|
| 84 | |
private final MongoClientConfiguration myConfig; |
| 85 | |
|
| 86 | |
|
| 87 | |
private final ProxiedConnectionFactory myConnectionFactory; |
| 88 | |
|
| 89 | |
|
| 90 | |
private volatile TimeUnit myIntervalUnits; |
| 91 | |
|
| 92 | |
|
| 93 | |
private volatile int myPingSweepInterval; |
| 94 | |
|
| 95 | |
|
| 96 | |
private final Thread myPingThread; |
| 97 | |
|
| 98 | |
|
| 99 | |
private volatile boolean myRunning; |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
public ClusterPinger(final Cluster cluster, |
| 112 | |
final ProxiedConnectionFactory factory, |
| 113 | |
final MongoClientConfiguration config) { |
| 114 | 58 | super(); |
| 115 | |
|
| 116 | 58 | myConnectionFactory = factory; |
| 117 | 58 | myConfig = config; |
| 118 | 58 | myRunning = true; |
| 119 | |
|
| 120 | 58 | myClusters = new CopyOnWriteArrayList<Cluster>(); |
| 121 | 58 | myClusters.add(cluster); |
| 122 | |
|
| 123 | 58 | myIntervalUnits = TimeUnit.SECONDS; |
| 124 | 58 | myPingSweepInterval = DEFAULT_PING_INTERVAL_SECONDS; |
| 125 | |
|
| 126 | 58 | myPingThread = myConfig.getThreadFactory().newThread(this); |
| 127 | 58 | myPingThread.setDaemon(true); |
| 128 | 58 | myPingThread.setName("MongoDB Pinger"); |
| 129 | 58 | myPingThread.setPriority(Thread.MIN_PRIORITY); |
| 130 | 58 | } |
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
public void addCluster(final Cluster cluster) { |
| 139 | 0 | myClusters.add(cluster); |
| 140 | 0 | } |
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
@Override |
| 149 | |
public void close() { |
| 150 | 75 | myRunning = false; |
| 151 | 75 | myPingThread.interrupt(); |
| 152 | 75 | } |
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
public TimeUnit getIntervalUnits() { |
| 160 | 49 | return myIntervalUnits; |
| 161 | |
} |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
public int getPingSweepInterval() { |
| 169 | 49 | return myPingSweepInterval; |
| 170 | |
} |
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
public void initialSweep(final Cluster cluster) { |
| 184 | 46 | final List<Server> servers = cluster.getServers(); |
| 185 | 46 | final List<Future<Reply>> replies = new ArrayList<Future<Reply>>( |
| 186 | |
servers.size()); |
| 187 | 46 | final List<Connection> connections = new ArrayList<Connection>( |
| 188 | |
servers.size()); |
| 189 | |
try { |
| 190 | 46 | for (final Server server : servers) { |
| 191 | |
|
| 192 | 59 | final String name = server.getCanonicalName(); |
| 193 | 59 | Connection conn = null; |
| 194 | |
try { |
| 195 | 59 | conn = myConnectionFactory.connect(server, myConfig); |
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | 47 | final Future<Reply> reply = PINGER.pingAsync( |
| 200 | |
cluster.getType(), server, conn); |
| 201 | 47 | replies.add(reply); |
| 202 | |
} |
| 203 | 12 | catch (final IOException e) { |
| 204 | 12 | LOG.info("Could not ping '{}': {}", name, e.getMessage()); |
| 205 | |
} |
| 206 | |
finally { |
| 207 | 59 | if (conn != null) { |
| 208 | 47 | connections.add(conn); |
| 209 | 47 | conn.shutdown(false); |
| 210 | |
} |
| 211 | |
} |
| 212 | 59 | } |
| 213 | |
|
| 214 | 46 | long now = System.currentTimeMillis(); |
| 215 | 46 | final long deadline = now |
| 216 | |
+ Math.max(5000, myConfig.getConnectTimeout()); |
| 217 | 86 | while ((now < deadline) && !replies.isEmpty()) { |
| 218 | 40 | final Iterator<Future<Reply>> iter = replies.iterator(); |
| 219 | 88 | while (iter.hasNext() && (now < deadline)) { |
| 220 | 48 | Future<Reply> future = iter.next(); |
| 221 | |
try { |
| 222 | 48 | if (future != null) { |
| 223 | |
|
| 224 | 43 | future.get(deadline - now, TimeUnit.MILLISECONDS); |
| 225 | |
} |
| 226 | |
|
| 227 | |
|
| 228 | 46 | iter.remove(); |
| 229 | |
} |
| 230 | 1 | catch (final ExecutionException e) { |
| 231 | |
|
| 232 | 1 | iter.remove(); |
| 233 | |
} |
| 234 | 0 | catch (final TimeoutException e) { |
| 235 | |
|
| 236 | 0 | future = null; |
| 237 | |
} |
| 238 | 1 | catch (final InterruptedException e) { |
| 239 | |
|
| 240 | 1 | future = null; |
| 241 | 47 | } |
| 242 | |
|
| 243 | 48 | now = System.currentTimeMillis(); |
| 244 | 48 | } |
| 245 | 40 | } |
| 246 | |
} |
| 247 | |
finally { |
| 248 | 46 | for (final Connection conn : connections) { |
| 249 | 47 | IOUtils.close(conn); |
| 250 | 47 | } |
| 251 | 46 | } |
| 252 | 46 | } |
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
@Override |
| 262 | |
public void run() { |
| 263 | 100 | while (myRunning) { |
| 264 | |
try { |
| 265 | 49 | final Map<Server, ClusterType> servers = extractAllServers(); |
| 266 | |
|
| 267 | 49 | final long interval = getIntervalUnits().toMillis( |
| 268 | |
getPingSweepInterval()); |
| 269 | 49 | final long perServerSleep = servers.isEmpty() ? interval |
| 270 | |
: interval / servers.size(); |
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | 49 | Thread.sleep(TimeUnit.MILLISECONDS.toMillis(perServerSleep)); |
| 276 | |
|
| 277 | 15 | startSweep(); |
| 278 | |
|
| 279 | 15 | for (final Map.Entry<Server, ClusterType> entry : servers |
| 280 | |
.entrySet()) { |
| 281 | |
|
| 282 | 15 | final Server server = entry.getKey(); |
| 283 | 15 | final String name = server.getCanonicalName(); |
| 284 | 15 | Connection conn = null; |
| 285 | |
try { |
| 286 | 15 | myPingThread.setName("MongoDB Pinger - " + name); |
| 287 | |
|
| 288 | 15 | conn = myConnectionFactory.connect(server, myConfig); |
| 289 | |
|
| 290 | 14 | PINGER.pingAsync(entry.getValue(), server, conn); |
| 291 | |
|
| 292 | |
|
| 293 | 14 | Thread.sleep(TimeUnit.MILLISECONDS |
| 294 | |
.toMillis(perServerSleep)); |
| 295 | |
} |
| 296 | 1 | catch (final IOException e) { |
| 297 | 1 | LOG.info("Could not ping '{}': {}", name, |
| 298 | |
e.getMessage()); |
| 299 | |
} |
| 300 | |
finally { |
| 301 | 15 | myPingThread.setName("MongoDB Pinger - Idle"); |
| 302 | 15 | if (conn != null) { |
| 303 | 14 | conn.shutdown(true); |
| 304 | |
} |
| 305 | |
} |
| 306 | |
|
| 307 | 14 | } |
| 308 | |
} |
| 309 | 35 | catch (final InterruptedException ok) { |
| 310 | 35 | LOG.debug("Pinger interrupted."); |
| 311 | 49 | } |
| 312 | |
} |
| 313 | 51 | } |
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
public void setIntervalUnits(final TimeUnit intervalUnits) { |
| 322 | 12 | myIntervalUnits = intervalUnits; |
| 323 | 12 | } |
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
public void setPingSweepInterval(final int pingSweepInterval) { |
| 333 | 12 | myPingSweepInterval = pingSweepInterval; |
| 334 | 12 | } |
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
public void start() { |
| 340 | 40 | myPingThread.start(); |
| 341 | 40 | } |
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
public void stop() { |
| 347 | 2 | close(); |
| 348 | 2 | } |
| 349 | |
|
| 350 | |
|
| 351 | |
|
| 352 | |
|
| 353 | |
public void wakeUp() { |
| 354 | 0 | myPingThread.interrupt(); |
| 355 | 0 | } |
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
protected void startSweep() { |
| 361 | |
|
| 362 | 15 | } |
| 363 | |
|
| 364 | |
|
| 365 | |
|
| 366 | |
|
| 367 | |
|
| 368 | |
|
| 369 | |
private Map<Server, ClusterType> extractAllServers() { |
| 370 | 49 | final Map<Server, ClusterType> servers = new HashMap<Server, ClusterType>(); |
| 371 | |
|
| 372 | 49 | for (final Cluster cluster : myClusters) { |
| 373 | 49 | for (final Server server : cluster.getServers()) { |
| 374 | 57 | servers.put(server, cluster.getType()); |
| 375 | 57 | } |
| 376 | 49 | } |
| 377 | |
|
| 378 | 49 | return Collections.unmodifiableMap(servers); |
| 379 | |
} |
| 380 | |
|
| 381 | |
|
| 382 | |
|
| 383 | |
|
| 384 | |
|
| 385 | |
|
| 386 | 1 | protected static final class Pinger { |
| 387 | |
|
| 388 | |
|
| 389 | |
|
| 390 | |
|
| 391 | |
|
| 392 | |
|
| 393 | |
|
| 394 | |
|
| 395 | |
|
| 396 | |
|
| 397 | |
|
| 398 | |
|
| 399 | |
|
| 400 | |
public boolean ping(final Server server, final Connection conn) { |
| 401 | |
try { |
| 402 | 6 | final Future<Reply> future = pingAsync(ClusterType.STAND_ALONE, |
| 403 | |
server, conn); |
| 404 | |
|
| 405 | |
|
| 406 | 6 | if (future != null) { |
| 407 | 5 | future.get(1, TimeUnit.MINUTES); |
| 408 | |
|
| 409 | 4 | return true; |
| 410 | |
} |
| 411 | |
} |
| 412 | 1 | catch (final ExecutionException e) { |
| 413 | 1 | LOG.info(e, "Could not ping '{}': {}", |
| 414 | |
server.getCanonicalName(), e.getMessage()); |
| 415 | |
} |
| 416 | 0 | catch (final TimeoutException e) { |
| 417 | 0 | LOG.info(e, "'{}' might be a zombie - not receiving " |
| 418 | |
+ "a response to ping: {}", server.getCanonicalName(), |
| 419 | |
e.getMessage()); |
| 420 | |
} |
| 421 | 0 | catch (final InterruptedException e) { |
| 422 | 0 | LOG.info(e, "Interrupted pinging '{}': {}", |
| 423 | |
server.getCanonicalName(), e.getMessage()); |
| 424 | 2 | } |
| 425 | |
|
| 426 | 2 | return false; |
| 427 | |
} |
| 428 | |
|
| 429 | |
|
| 430 | |
|
| 431 | |
|
| 432 | |
|
| 433 | |
|
| 434 | |
|
| 435 | |
|
| 436 | |
|
| 437 | |
|
| 438 | |
|
| 439 | |
|
| 440 | |
|
| 441 | |
|
| 442 | |
|
| 443 | |
|
| 444 | |
|
| 445 | |
|
| 446 | |
public Future<Reply> pingAsync(final ClusterType type, |
| 447 | |
final Server server, final Connection conn) { |
| 448 | |
try { |
| 449 | 67 | final ServerUpdateCallback future = new ServerUpdateCallback( |
| 450 | |
server); |
| 451 | |
|
| 452 | 67 | conn.send(new IsMaster(), future); |
| 453 | 60 | if (type == ClusterType.REPLICA_SET) { |
| 454 | 26 | conn.send(new ReplicaSetStatus(), new ServerUpdateCallback( |
| 455 | |
server)); |
| 456 | |
} |
| 457 | |
|
| 458 | 60 | return future; |
| 459 | |
} |
| 460 | 7 | catch (final MongoDbException e) { |
| 461 | 7 | LOG.info("Could not ping '{}': {}", server, e.getMessage()); |
| 462 | |
} |
| 463 | 7 | return null; |
| 464 | |
} |
| 465 | |
} |
| 466 | |
} |