Search in sources :

Example 1 with MongoTimeoutException

use of com.mongodb.MongoTimeoutException 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);
    }
}
Also used : MongoInterruptedException(com.mongodb.MongoInterruptedException) CountDownLatch(java.util.concurrent.CountDownLatch) MongoTimeoutException(com.mongodb.MongoTimeoutException) MongoInterruptedException(com.mongodb.MongoInterruptedException)

Example 2 with MongoTimeoutException

use of com.mongodb.MongoTimeoutException 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);
        }
    }
}
Also used : ServerAddressSelector(com.mongodb.selector.ServerAddressSelector) MongoInterruptedException(com.mongodb.MongoInterruptedException) MongoTimeoutException(com.mongodb.MongoTimeoutException) MongoInterruptedException(com.mongodb.MongoInterruptedException)

Example 3 with MongoTimeoutException

use of com.mongodb.MongoTimeoutException in project mongo-java-driver by mongodb.

the class DefaultConnectionPool method getAsync.

@Override
public void getAsync(final SingleResultCallback<InternalConnection> callback) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace(String.format("Asynchronously getting a connection from the pool for server %s", serverId));
    }
    final SingleResultCallback<InternalConnection> errHandlingCallback = errorHandlingCallback(callback, LOGGER);
    PooledConnection connection = null;
    try {
        connection = getPooledConnection(0, MILLISECONDS);
    } catch (MongoTimeoutException e) {
    // fall through
    } catch (Throwable t) {
        callback.onResult(null, t);
        return;
    }
    if (connection != null) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace(String.format("Asynchronously opening pooled connection %s to server %s", connection.getDescription().getConnectionId(), serverId));
        }
        openAsync(connection, errHandlingCallback);
    } else if (waitQueueSize.incrementAndGet() > settings.getMaxWaitQueueSize()) {
        waitQueueSize.decrementAndGet();
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace(String.format("Asynchronously failing to get a pooled connection to %s because the wait queue is full", serverId));
        }
        callback.onResult(null, createWaitQueueFullException());
    } else {
        final long startTimeMillis = System.currentTimeMillis();
        connectionPoolListener.waitQueueEntered(new ConnectionPoolWaitQueueEnteredEvent(serverId, currentThread().getId()));
        getAsyncGetter().submit(new Runnable() {

            @Override
            public void run() {
                try {
                    if (getRemainingWaitTime() <= 0) {
                        errHandlingCallback.onResult(null, createTimeoutException());
                    } else {
                        PooledConnection connection = getPooledConnection(getRemainingWaitTime(), MILLISECONDS);
                        openAsync(connection, errHandlingCallback);
                    }
                } catch (Throwable t) {
                    errHandlingCallback.onResult(null, t);
                } finally {
                    waitQueueSize.decrementAndGet();
                    connectionPoolListener.waitQueueExited(new ConnectionPoolWaitQueueExitedEvent(serverId, currentThread().getId()));
                }
            }

            private long getRemainingWaitTime() {
                return startTimeMillis + settings.getMaxWaitTime(MILLISECONDS) - System.currentTimeMillis();
            }
        });
    }
}
Also used : ConnectionPoolWaitQueueExitedEvent(com.mongodb.event.ConnectionPoolWaitQueueExitedEvent) MongoTimeoutException(com.mongodb.MongoTimeoutException) ConnectionPoolWaitQueueEnteredEvent(com.mongodb.event.ConnectionPoolWaitQueueEnteredEvent)

Aggregations

MongoTimeoutException (com.mongodb.MongoTimeoutException)3 MongoInterruptedException (com.mongodb.MongoInterruptedException)2 ConnectionPoolWaitQueueEnteredEvent (com.mongodb.event.ConnectionPoolWaitQueueEnteredEvent)1 ConnectionPoolWaitQueueExitedEvent (com.mongodb.event.ConnectionPoolWaitQueueExitedEvent)1 ServerAddressSelector (com.mongodb.selector.ServerAddressSelector)1 CountDownLatch (java.util.concurrent.CountDownLatch)1