Search in sources :

Example 1 with MongoInterruptedException

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

Example 2 with MongoInterruptedException

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);
    }
}
Also used : ServerSelector(com.mongodb.selector.ServerSelector) CompositeServerSelector(com.mongodb.selector.CompositeServerSelector) MongoInterruptedException(com.mongodb.MongoInterruptedException) CountDownLatch(java.util.concurrent.CountDownLatch) MongoInterruptedException(com.mongodb.MongoInterruptedException)

Example 3 with MongoInterruptedException

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();
    }
}
Also used : MongoInterruptedException(com.mongodb.MongoInterruptedException) ConnectionMessageReceivedEvent(com.mongodb.event.ConnectionMessageReceivedEvent) MongoSocketClosedException(com.mongodb.MongoSocketClosedException) CountDownLatch(java.util.concurrent.CountDownLatch) MongoInterruptedException(com.mongodb.MongoInterruptedException)

Example 4 with MongoInterruptedException

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

Example 5 with MongoInterruptedException

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

Aggregations

MongoInterruptedException (com.mongodb.MongoInterruptedException)10 MongoTimeoutException (com.mongodb.MongoTimeoutException)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 ClusterDescription (com.mongodb.connection.ClusterDescription)2 CompositeServerSelector (com.mongodb.selector.CompositeServerSelector)2 ServerSelector (com.mongodb.selector.ServerSelector)2 MongoClient (com.mongodb.MongoClient)1 MongoException (com.mongodb.MongoException)1 MongoSocketClosedException (com.mongodb.MongoSocketClosedException)1 ReplicaSetStatus (com.mongodb.ReplicaSetStatus)1 ConnectionMessageReceivedEvent (com.mongodb.event.ConnectionMessageReceivedEvent)1 LatencyMinimizingServerSelector (com.mongodb.internal.selector.LatencyMinimizingServerSelector)1 ServerAddressSelector (com.mongodb.internal.selector.ServerAddressSelector)1 ServerAddressSelector (com.mongodb.selector.ServerAddressSelector)1 HashSet (java.util.HashSet)1