ConnectTimeout and SocketTimeout with exception

1,333 views
Skip to first unread message

nyc

unread,
May 27, 2013, 7:32:05 AM5/27/13
to mongod...@googlegroups.com
Hi All,

Can somebody explain me what is the difference between connectTimeout , socketTimeout both been 0 by default which is always alive.

In my production environment,MongoOptions class has many properties in which
socketTimeout = 120000

where as connectTimeout is not set that means its default value is  0.

I am  getting following exception

Caused by: com.mongodb.MongoException$Network: can't call something
        at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:211)
        at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:303)
        at com.mongodb.DBCursor._check(DBCursor.java:360)
        at com.mongodb.DBCursor._hasNext(DBCursor.java:490)
        at com.mongodb.DBCursor.hasNext(DBCursor.java:515)
        ... 48 more
Caused by: java.net.SocketTimeoutException: Read timed out
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.read(SocketInputStream.java:129)
        at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
        at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
        at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
        at org.bson.io.Bits.readFully(Bits.java:35)
        at org.bson.io.Bits.readFully(Bits.java:28)
        at com.mongodb.Response.<init>(Response.java:39)
        at com.mongodb.DBPort.go(DBPort.java:123)
        at com.mongodb.DBPort.go(DBPort.java:82)
        at com.mongodb.DBPort.call(DBPort.java:72)

Can someone explain how to resolve it and the difference between the too ?

Jim O'Leary

unread,
May 27, 2013, 8:32:09 AM5/27/13
to mongod...@googlegroups.com
Hi,

There's a good explanation of the difference between connectTimeout and socketTimeout at http://stackoverflow.com/questions/7360520/connectiontimout-versus-sockettimeout?answertab=active#tab-top (there's also a very good explanation of the Java driver mongo options at http://stackoverflow.com/questions/6520439/how-to-configure-mongodb-java-driver-mongooptions-for-production-use?answertab=active#tab-top)

There are a lot of reasons why you could be getting these socket timeouts (not all of which are actually network related), for example:
  - networking problems/firewall timeouts
  - overloaded servers
  - slow queries 
  - low ulimits
 
Are you seeing anything in your logs? Can you post the logs from your mongod (or mongos) when the problem is happening?
 
-Jim

nyc

unread,
May 27, 2013, 1:18:36 PM5/27/13
to mongod...@googlegroups.com
Hi Jim

I forgot to mention one more thing we are running mongo production server with 340 millions records at present and mongo version is 2.0.5

Let me check the logs and will update u about it. 

Lastly, Really thanks for kind reply

nyc

unread,
May 28, 2013, 1:14:11 AM5/28/13
to mongod...@googlegroups.com
Sorry but wanna know one more thing is "connectionsPerHost " is working as connection pool i.e if i have set connectionsPerHost=100 then it will make 100 connection pool instance of mongod/mongos instance . Please confirm


On Monday, May 27, 2013 5:02:05 PM UTC+5:30, nyc wrote:

nyc

unread,
May 28, 2013, 1:14:12 AM5/28/13
to mongod...@googlegroups.com
Sorry but wanna know one more thing is "connectionsPerHost " is working as connection pool i.e if i have set connectionsPerHost=100 then it will make 100 connection pool instance of mongod/mongos instance . Please confirm

On Monday, May 27, 2013 5:02:05 PM UTC+5:30, nyc wrote:

Jeff Yemin

unread,
May 28, 2013, 9:14:30 AM5/28/13
to mongod...@googlegroups.com
"connectionsPerHost" is the maximum number of sockets that the driver will pool for each mongod/mongos server that it's connected to. 

Regards
Jeff


--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb
 
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

nyc

unread,
May 28, 2013, 1:29:07 PM5/28/13
to mongod...@googlegroups.com

Can you please explain with an example so that it will be quite helpful for me to understand


On Monday, May 27, 2013 5:02:05 PM UTC+5:30, nyc wrote:

Jim O'Leary

unread,
May 29, 2013, 8:49:37 AM5/29/13
to mongod...@googlegroups.com
Hi,

The "connectionsPerHost" is the maximum number of connections per mongod/mongos process that the driver will open (note in this case we mean per host AND port combinations).

So in the following code sample that follows :
   - the contested method will eventually open 25 connections to the mongod instance.
   - the uncontested method will eventually open 50 connection to the mongod instance.

Both methods create 1000 objects in the collection but because the threads are blocked trying to get a connection from the pool in the contested case, the code takes about twice as long to run (at least for me).

Regards,
-Jim



import com.mongodb.*;
import java.net.UnknownHostException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
public class App
{
    public static void main( String[] args )
    {
        try { 
            long c = contested(); 
            long u = uncontested(); 
            System.out.println("contested took " + c); 
            System.out.println("uncontested took " + u); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        }
    }
    public static long contested() throws InterruptedException, UnknownHostException { 
        int conns = 50; 
        MongoClientOptions.Builder builder = new MongoClientOptions.Builder().writeConcern(WriteConcern.ACKNOWLEDGED); 
        builder.connectionsPerHost(conns / 2); 
        return create(conns, 10000, builder.build()); 
    }
    public static long uncontested() throws InterruptedException, UnknownHostException { 
        int conns = 50; 
        MongoClientOptions.Builder builder = new MongoClientOptions.Builder().writeConcern(WriteConcern.ACKNOWLEDGED); 
        return create(conns, 10000, builder.build()); 
    }
    public static long create(int conns , int total, MongoClientOptions opts) throws InterruptedException, UnknownHostException {
        final AtomicInteger max = new AtomicInteger(total);
        final CountDownLatch start = new CountDownLatch(1);
        final CountDownLatch finish= new CountDownLatch(conns);
        ServerAddress sa = new ServerAddress("localhost", 27017);
        MongoClient mongoClient = new MongoClient( sa ,opts);
        DB db = mongoClient.getDB( "test" );
        final DBCollection collection = db.getCollection("gg");
        collection.drop();
        for (int i =0 ; i < conns; i++)
            {
                new Thread(new Runnable() {
                        public void run() {
                            try
                                {
                                    start.await();
                                    while (max.decrementAndGet() >= 0)
                                        { long ts = System.currentTimeMillis(); collection.insert(BasicDBObjectBuilder.start().append("name", Thread.currentThread().toString() ).append("ts", ts ).get()); }
                                }
                            catch (Exception e)
                                { e.printStackTrace(); }
                            finally { finish.countDown(); }
                        }
                    }).start();
            }
        long s = System.currentTimeMillis();
        start.countDown();
        finish.await();
        return (System.currentTimeMillis() - s );
Reply all
Reply to author
Forward
0 new messages