use of com.mongodb.MongoInterruptedException in project mongo-java-driver by mongodb.
the class BaseCluster method getDescription.
@Override
public ClusterDescription getDescription() {
isTrue("open", !isClosed());
try {
CountDownLatch currentPhase = phase.get();
ClusterDescription curDescription = description;
boolean selectionFailureLogged = false;
long startTimeNanos = System.nanoTime();
long curTimeNanos = startTimeNanos;
long maxWaitTimeNanos = getMaxWaitTimeNanos();
while (curDescription.getType() == ClusterType.UNKNOWN) {
if (curTimeNanos - startTimeNanos > maxWaitTimeNanos) {
throw new MongoTimeoutException(format("Timed out after %d ms while waiting to connect. Client view of cluster state " + "is %s", settings.getServerSelectionTimeout(MILLISECONDS), curDescription.getShortDescription()));
}
if (!selectionFailureLogged) {
if (LOGGER.isInfoEnabled()) {
if (settings.getServerSelectionTimeout(MILLISECONDS) < 0) {
LOGGER.info(format("Cluster description not yet available. Waiting indefinitely."));
} else {
LOGGER.info(format("Cluster description not yet available. Waiting for %d ms before timing out", settings.getServerSelectionTimeout(MILLISECONDS)));
}
}
selectionFailureLogged = true;
}
connect();
currentPhase.await(Math.min(maxWaitTimeNanos - (curTimeNanos - startTimeNanos), getMinWaitTimeNanos()), NANOSECONDS);
curTimeNanos = System.nanoTime();
currentPhase = phase.get();
curDescription = description;
}
return curDescription;
} catch (InterruptedException e) {
throw new MongoInterruptedException(format("Interrupted while waiting to connect"), e);
}
}
use of com.mongodb.MongoInterruptedException in project mongo-java-driver by mongodb.
the class BaseCluster method selectServer.
@Override
public Server selectServer(final ServerSelector serverSelector) {
isTrue("open", !isClosed());
try {
CountDownLatch currentPhase = phase.get();
ClusterDescription curDescription = description;
ServerSelector compositeServerSelector = getCompositeServerSelector(serverSelector);
Server server = selectRandomServer(compositeServerSelector, curDescription);
boolean selectionFailureLogged = false;
long startTimeNanos = System.nanoTime();
long curTimeNanos = startTimeNanos;
long maxWaitTimeNanos = getMaxWaitTimeNanos();
while (true) {
throwIfIncompatible(curDescription);
if (server != null) {
return server;
}
if (curTimeNanos - startTimeNanos > maxWaitTimeNanos) {
throw createTimeoutException(serverSelector, curDescription);
}
if (!selectionFailureLogged) {
logServerSelectionFailure(serverSelector, curDescription);
selectionFailureLogged = true;
}
connect();
currentPhase.await(Math.min(maxWaitTimeNanos - (curTimeNanos - startTimeNanos), getMinWaitTimeNanos()), NANOSECONDS);
curTimeNanos = System.nanoTime();
currentPhase = phase.get();
curDescription = description;
server = selectRandomServer(compositeServerSelector, curDescription);
}
} catch (InterruptedException e) {
throw new MongoInterruptedException(format("Interrupted while waiting for a server that matches %s", serverSelector), e);
}
}
use of com.mongodb.MongoInterruptedException in project mongo-java-driver by mongodb.
the class InternalStreamConnection method receiveMessage.
@Override
public ResponseBuffers receiveMessage(final int responseTo) {
notNull("stream is open", stream);
if (isClosed()) {
throw new MongoSocketClosedException("Cannot read from a closed stream", getServerAddress());
}
CountDownLatch localLatch = new CountDownLatch(1);
readerLock.lock();
try {
ResponseBuffers responseBuffers = receiveResponseBuffers();
messages.put(responseBuffers.getReplyHeader().getResponseTo(), responseBuffers);
readingPhase.getAndSet(localLatch).countDown();
} catch (Throwable t) {
exceptionThatPrecededStreamClosing = translateReadException(t);
close();
readingPhase.getAndSet(localLatch).countDown();
} finally {
readerLock.unlock();
}
while (true) {
if (isClosed()) {
if (exceptionThatPrecededStreamClosing != null) {
throw exceptionThatPrecededStreamClosing;
} else {
throw new MongoSocketClosedException("Socket has been closed", getServerAddress());
}
}
ResponseBuffers myResponse = messages.remove(responseTo);
if (myResponse != null) {
connectionListener.messageReceived(new ConnectionMessageReceivedEvent(getId(), myResponse.getReplyHeader().getResponseTo(), myResponse.getReplyHeader().getMessageLength()));
return myResponse;
}
try {
localLatch.await();
} catch (InterruptedException e) {
throw new MongoInterruptedException("Interrupted while reading from stream", e);
}
localLatch = readingPhase.get();
}
}
use of com.mongodb.MongoInterruptedException in project mongo-java-driver by mongodb.
the class ServerHelper method waitForLastRelease.
public static void waitForLastRelease(final ServerAddress address, final Cluster cluster) {
DefaultServer server = (DefaultServer) cluster.selectServer(new ServerAddressSelector(address));
DefaultConnectionPool connectionProvider = (DefaultConnectionPool) server.getConnectionPool();
ConcurrentPool<UsageTrackingInternalConnection> pool = connectionProvider.getPool();
long startTime = System.currentTimeMillis();
while (pool.getInUseCount() > 0) {
try {
sleep(10);
if (System.currentTimeMillis() > startTime + ClusterFixture.TIMEOUT * 1000) {
throw new MongoTimeoutException("Timed out waiting for pool in use count to drop to 0. Now at: " + pool.getInUseCount());
}
} catch (InterruptedException e) {
throw new MongoInterruptedException("Interrupted", e);
}
}
}
use of com.mongodb.MongoInterruptedException in project mongo-java-driver by mongodb.
the class ServerHelper method waitForLastRelease.
public static void waitForLastRelease(final ServerAddress address, final Cluster cluster) {
ConcurrentPool<UsageTrackingInternalConnection> pool = connectionPool(cluster.selectServer(new ServerAddressSelector(address)).getServer());
long startTime = System.currentTimeMillis();
while (pool.getInUseCount() > 0) {
try {
sleep(10);
if (System.currentTimeMillis() > startTime + ClusterFixture.TIMEOUT * 1000) {
throw new MongoTimeoutException("Timed out waiting for pool in use count to drop to 0. Now at: " + pool.getInUseCount());
}
} catch (InterruptedException e) {
throw new MongoInterruptedException("Interrupted", e);
}
}
}
Aggregations