欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MongoDB查询字段没有创建索引导致的连接超时异常解案例分享

程序员文章站 2022-05-16 23:20:11
今天在现场的哥们发来异常,让我解决,错误信息如下: 复制代码 代码如下: http status 500 - read operation to server 192....

今天在现场的哥们发来异常,让我解决,错误信息如下:

复制代码 代码如下:

http status 500 - read operation to server 192.168.1.110:20001 failed on database wpdb; nested exception is com.mongodb.mongoexception$network: read operation to server 192.168.1.110:20001 failed on database wpdb
 
--------------------------------------------------------------------------------
 
type exception report
 
message read operation to server 192.168.1.110:20001 failed on database wpdb; nested exception is com.mongodb.mongoexception$network: read operation to server 192.168.1.110:20001 failed on database wpdb
 
description the server encountered an internal error that prevented it from fulfilling this request.
 
exception
 
org.springframework.dao.dataaccessresourcefailureexception: read operation to server 192.168.1.110:20001 failed on database wpdb; nested exception is com.mongodb.mongoexception$network: read operation to server 192.168.1.110:20001 failed on database wpdb
    org.springframework.data.mongodb.core.mongoexceptiontranslator.translateexceptionifpossible(mongoexceptiontranslator.java:56)
    org.springframework.data.mongodb.core.mongotemplate.potentiallyconvertruntimeexception(mongotemplate.java:1913)
    org.springframework.data.mongodb.core.mongotemplate.executefindoneinternal(mongotemplate.java:1743)
    org.springframework.data.mongodb.core.mongotemplate.dofindone(mongotemplate.java:1557)
    org.springframework.data.mongodb.core.mongotemplate.findone(mongotemplate.java:497)
    com.geoway.ime.tile.dao.impl.tiledaoimpl.gettile(tiledaoimpl.java:60)
    com.geoway.ime.tile.service.impl.tileserviceimpl.gettile(tileserviceimpl.java:105)
    sun.reflect.nativemethodaccessorimpl.invoke0(native method)
    sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:39)
    sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:25)
    java.lang.reflect.method.invoke(method.java:597)
    org.springframework.aop.support.aoputils.invokejoinpointusingreflection(aoputils.java:317)
    org.springframework.aop.framework.reflectivemethodinvocation.invokejoinpoint(reflectivemethodinvocation.java:183)
    org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:150)
    org.springframework.cache.interceptor.cacheinterceptor$1.invoke(cacheinterceptor.java:58)
    org.springframework.cache.interceptor.cacheaspectsupport.execute(cacheaspectsupport.java:211)
    org.springframework.cache.interceptor.cacheinterceptor.invoke(cacheinterceptor.java:66)
    org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:172)
    org.springframework.aop.framework.jdkdynamicaopproxy.invoke(jdkdynamicaopproxy.java:204)
    com.sun.proxy.$proxy50.gettile(unknown source)
    com.geoway.ime.server.rest.tileserver.gettilekvp(tileserver.java:60)
    sun.reflect.nativemethodaccessorimpl.invoke0(native method)
    sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:39)
    sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:25)
    java.lang.reflect.method.invoke(method.java:597)
    com.sun.jersey.spi.container.javamethodinvokerfactory$1.invoke(javamethodinvokerfactory.java:60)
    com.sun.jersey.server.impl.model.method.dispatch.abstractresourcemethoddispatchprovider$typeoutinvoker._dispatch(abstractresourcemethoddispatchprovider.java:185)
    com.sun.jersey.server.impl.model.method.dispatch.resourcejavamethoddispatcher.dispatch(resourcejavamethoddispatcher.java:75)
    com.sun.jersey.server.impl.uri.rules.httpmethodrule.accept(httpmethodrule.java:302)
    com.sun.jersey.server.impl.uri.rules.resourceclassrule.accept(resourceclassrule.java:108)
    com.sun.jersey.server.impl.uri.rules.righthandpathrule.accept(righthandpathrule.java:147)
    com.sun.jersey.server.impl.uri.rules.rootresourceclassesrule.accept(rootresourceclassesrule.java:84)
    com.sun.jersey.server.impl.application.webapplicationimpl._handlerequest(webapplicationimpl.java:1511)
    com.sun.jersey.server.impl.application.webapplicationimpl._handlerequest(webapplicationimpl.java:1442)
    com.sun.jersey.server.impl.application.webapplicationimpl.handlerequest(webapplicationimpl.java:1391)
    com.sun.jersey.server.impl.application.webapplicationimpl.handlerequest(webapplicationimpl.java:1381)
    com.sun.jersey.spi.container.servlet.webcomponent.service(webcomponent.java:416)
    com.sun.jersey.spi.container.servlet.servletcontainer.service(servletcontainer.java:538)
    com.sun.jersey.spi.container.servlet.servletcontainer.service(servletcontainer.java:716)
    javax.servlet.http.httpservlet.service(httpservlet.java:723)
    org.springframework.web.filter.characterencodingfilter.dofilterinternal(characterencodingfilter.java:88)
    org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:106)
 
 
root cause
 
com.mongodb.mongoexception$network: read operation to server 192.168.1.110:20001 failed on database wpdb
    com.mongodb.dbtcpconnector.innercall(dbtcpconnector.java:308)
    com.mongodb.dbtcpconnector.call(dbtcpconnector.java:279)
    com.mongodb.dbcollectionimpl.find(dbcollectionimpl.java:84)
    com.mongodb.dbcollectionimpl.find(dbcollectionimpl.java:66)
    com.mongodb.dbcollection.findone(dbcollection.java:870)
    com.mongodb.dbcollection.findone(dbcollection.java:844)
    com.mongodb.dbcollection.findone(dbcollection.java:790)
    org.springframework.data.mongodb.core.mongotemplate$findonecallback.doincollection(mongotemplate.java:1980)
    org.springframework.data.mongodb.core.mongotemplate$findonecallback.doincollection(mongotemplate.java:1965)
    org.springframework.data.mongodb.core.mongotemplate.executefindoneinternal(mongotemplate.java:1739)
    org.springframework.data.mongodb.core.mongotemplate.dofindone(mongotemplate.java:1557)
    org.springframework.data.mongodb.core.mongotemplate.findone(mongotemplate.java:497)
    com.geoway.ime.tile.dao.impl.tiledaoimpl.gettile(tiledaoimpl.java:60)
    com.geoway.ime.tile.service.impl.tileserviceimpl.gettile(tileserviceimpl.java:105)
    sun.reflect.nativemethodaccessorimpl.invoke0(native method)
    sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:39)
    sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:25)
    java.lang.reflect.method.invoke(method.java:597)
    org.springframework.aop.support.aoputils.invokejoinpointusingreflection(aoputils.java:317)
    org.springframework.aop.framework.reflectivemethodinvocation.invokejoinpoint(reflectivemethodinvocation.java:183)
    org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:150)
    org.springframework.cache.interceptor.cacheinterceptor$1.invoke(cacheinterceptor.java:58)
    org.springframework.cache.interceptor.cacheaspectsupport.execute(cacheaspectsupport.java:211)
    org.springframework.cache.interceptor.cacheinterceptor.invoke(cacheinterceptor.java:66)
    org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:172)
    org.springframework.aop.framework.jdkdynamicaopproxy.invoke(jdkdynamicaopproxy.java:204)
    com.sun.proxy.$proxy50.gettile(unknown source)
    com.geoway.ime.server.rest.tileserver.gettilekvp(tileserver.java:60)
    sun.reflect.nativemethodaccessorimpl.invoke0(native method)
    sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:39)
    sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:25)
    java.lang.reflect.method.invoke(method.java:597)
    com.sun.jersey.spi.container.javamethodinvokerfactory$1.invoke(javamethodinvokerfactory.java:60)
    com.sun.jersey.server.impl.model.method.dispatch.abstractresourcemethoddispatchprovider$typeoutinvoker._dispatch(abstractresourcemethoddispatchprovider.java:185)
    com.sun.jersey.server.impl.model.method.dispatch.resourcejavamethoddispatcher.dispatch(resourcejavamethoddispatcher.java:75)
    com.sun.jersey.server.impl.uri.rules.httpmethodrule.accept(httpmethodrule.java:302)
    com.sun.jersey.server.impl.uri.rules.resourceclassrule.accept(resourceclassrule.java:108)
    com.sun.jersey.server.impl.uri.rules.righthandpathrule.accept(righthandpathrule.java:147)
    com.sun.jersey.server.impl.uri.rules.rootresourceclassesrule.accept(rootresourceclassesrule.java:84)
    com.sun.jersey.server.impl.application.webapplicationimpl._handlerequest(webapplicationimpl.java:1511)
    com.sun.jersey.server.impl.application.webapplicationimpl._handlerequest(webapplicationimpl.java:1442)
    com.sun.jersey.server.impl.application.webapplicationimpl.handlerequest(webapplicationimpl.java:1391)
    com.sun.jersey.server.impl.application.webapplicationimpl.handlerequest(webapplicationimpl.java:1381)
    com.sun.jersey.spi.container.servlet.webcomponent.service(webcomponent.java:416)
    com.sun.jersey.spi.container.servlet.servletcontainer.service(servletcontainer.java:538)
    com.sun.jersey.spi.container.servlet.servletcontainer.service(servletcontainer.java:716)
    javax.servlet.http.httpservlet.service(httpservlet.java:723)
    org.springframework.web.filter.characterencodingfilter.dofilterinternal(characterencodingfilter.java:88)
    org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:106)
 
 
root cause
 
java.net.sockettimeoutexception: read timed out
    java.net.socketinputstream.socketread0(native method)
    java.net.socketinputstream.read(socketinputstream.java:129)
    java.io.bufferedinputstream.fill(bufferedinputstream.java:218)
    java.io.bufferedinputstream.read1(bufferedinputstream.java:258)
    java.io.bufferedinputstream.read(bufferedinputstream.java:317)
    org.bson.io.bits.readfully(bits.java:48)
    org.bson.io.bits.readfully(bits.java:35)
    org.bson.io.bits.readfully(bits.java:30)
    com.mongodb.response.<init>(response.java:42)
    com.mongodb.dbport$1.execute(dbport.java:141)
    com.mongodb.dbport$1.execute(dbport.java:135)
    com.mongodb.dbport.dooperation(dbport.java:164)
    com.mongodb.dbport.call(dbport.java:135)
    com.mongodb.dbtcpconnector.innercall(dbtcpconnector.java:300)
    com.mongodb.dbtcpconnector.call(dbtcpconnector.java:279)
    com.mongodb.dbcollectionimpl.find(dbcollectionimpl.java:84)
    com.mongodb.dbcollectionimpl.find(dbcollectionimpl.java:66)
    com.mongodb.dbcollection.findone(dbcollection.java:870)
    com.mongodb.dbcollection.findone(dbcollection.java:844)
    com.mongodb.dbcollection.findone(dbcollection.java:790)
    org.springframework.data.mongodb.core.mongotemplate$findonecallback.doincollection(mongotemplate.java:1980)
    org.springframework.data.mongodb.core.mongotemplate$findonecallback.doincollection(mongotemplate.java:1965)
    org.springframework.data.mongodb.core.mongotemplate.executefindoneinternal(mongotemplate.java:1739)
    org.springframework.data.mongodb.core.mongotemplate.dofindone(mongotemplate.java:1557)
    org.springframework.data.mongodb.core.mongotemplate.findone(mongotemplate.java:497)
    com.geoway.ime.tile.dao.impl.tiledaoimpl.gettile(tiledaoimpl.java:60)
    com.geoway.ime.tile.service.impl.tileserviceimpl.gettile(tileserviceimpl.java:105)
    sun.reflect.nativemethodaccessorimpl.invoke0(native method)
    sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:39)
    sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:25)
    java.lang.reflect.method.invoke(method.java:597)
    org.springframework.aop.support.aoputils.invokejoinpointusingreflection(aoputils.java:317)
    org.springframework.aop.framework.reflectivemethodinvocation.invokejoinpoint(reflectivemethodinvocation.java:183)
    org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:150)
    org.springframework.cache.interceptor.cacheinterceptor$1.invoke(cacheinterceptor.java:58)
    org.springframework.cache.interceptor.cacheaspectsupport.execute(cacheaspectsupport.java:211)
    org.springframework.cache.interceptor.cacheinterceptor.invoke(cacheinterceptor.java:66)
    org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:172)
    org.springframework.aop.framework.jdkdynamicaopproxy.invoke(jdkdynamicaopproxy.java:204)
    com.sun.proxy.$proxy50.gettile(unknown source)
    com.geoway.ime.server.rest.tileserver.gettilekvp(tileserver.java:60)
    sun.reflect.nativemethodaccessorimpl.invoke0(native method)
    sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:39)
    sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:25)
    java.lang.reflect.method.invoke(method.java:597)
    com.sun.jersey.spi.container.javamethodinvokerfactory$1.invoke(javamethodinvokerfactory.java:60)
    com.sun.jersey.server.impl.model.method.dispatch.abstractresourcemethoddispatchprovider$typeoutinvoker._dispatch(abstractresourcemethoddispatchprovider.java:185)
    com.sun.jersey.server.impl.model.method.dispatch.resourcejavamethoddispatcher.dispatch(resourcejavamethoddispatcher.java:75)
    com.sun.jersey.server.impl.uri.rules.httpmethodrule.accept(httpmethodrule.java:302)
    com.sun.jersey.server.impl.uri.rules.resourceclassrule.accept(resourceclassrule.java:108)
    com.sun.jersey.server.impl.uri.rules.righthandpathrule.accept(righthandpathrule.java:147)
    com.sun.jersey.server.impl.uri.rules.rootresourceclassesrule.accept(rootresourceclassesrule.java:84)
    com.sun.jersey.server.impl.application.webapplicationimpl._handlerequest(webapplicationimpl.java:1511)
    com.sun.jersey.server.impl.application.webapplicationimpl._handlerequest(webapplicationimpl.java:1442)
    com.sun.jersey.server.impl.application.webapplicationimpl.handlerequest(webapplicationimpl.java:1391)
    com.sun.jersey.server.impl.application.webapplicationimpl.handlerequest(webapplicationimpl.java:1381)
    com.sun.jersey.spi.container.servlet.webcomponent.service(webcomponent.java:416)
    com.sun.jersey.spi.container.servlet.servletcontainer.service(servletcontainer.java:538)
    com.sun.jersey.spi.container.servlet.servletcontainer.service(servletcontainer.java:716)
    javax.servlet.http.httpservlet.service(httpservlet.java:723)
    org.springframework.web.filter.characterencodingfilter.dofilterinternal(characterencodingfilter.java:88)
    org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:106)
 
 
note the full stack trace of the root cause is available in the apache tomcat/6.0.41 logs.

由异常来看,是由于mongodb连接超时造成的。于是,往mongodb上找原因。首先上mongodb的配置文件:

复制代码 代码如下:

mongodb.replica-set=192.98.12.242:30007,192.98.12.245:30007
mongodb.hostname=192.98.12.245
mongodb.port=30007
mongodb.connections-per-host=1000
mongodb.threads-allowed-to-block-for-connection-multiplier=200
mongodb.connect-timeout=2000
mongodb.max-wait-time=2000
mongodb.auto-connect-retry=true
mongodb.socket-keep-alive=true
mongodb.socket-timeout=1500
mongodb.slave-ok=true
mongodb.write-number=1
mongodb.write-timeout=1000
mongodb.write-fsync=true
mongodb.dbname=shardvectordb
mongodb.dbuser=
mongodb.dbpassword=

连接时间2秒。然后在定位mongodb的查询,结果为6、7秒。原来是这个原因。然后定位,一个查询为什么耗时那么久呢?

经过排查,原来是查询字段没有创建索引。然后把索引创建,查询几十毫秒,ok问题全部解决。