简体   繁体   English

经常遇到MongoDB Perl驱动程序中的异常

[英]Frequently run into exceptions from MongoDB perl driver

The web application is running but in my log error frequently run into this exceptions Web应用程序正在运行,但在我的日志中错误经常遇到此异常

can't get db response, not connected at ../MongoDB/Cursor.pm line 160.

invalid header received at ../MongoDB/Cursor.pm line 160.

can't locate object method "new" via package "MongoDB::Timestamp" at ../MongoDB/Collection.pm line 370.

Perl Driver Perl驱动程序

MongoDB server "version 2.4.8" MongoDB服务器“版本2.4.8”

  • 1 master 1个大师
  • 2 replication 2复制

MongoDB server status connection server sometime reach the limit connection MongoDB服务器状态连接服务器有时达到限制连接

"connections" : {
        "current" : 20000,
        "available" : 0,
        "totalCreated" : NumberLong(73428104)
 },

Here my code for get connection from MongoDB 这是我从MongoDB获得连接的代码

sub get_mongodb {
my ($self) = @_;

#master/slave
my $mode    = $self->mode;

#get db name
my $db      = $self->db;

my $obj = $self->{'o_mongodb_' . $mode . '_' . $db};

unless($obj) {
    my %opt;

    #result get_con
    #{
    #    'db'    => 'mongo',
    #    'host'  => [
    #            '10.10.10.1:27017',
    #            '10.10.10.2:27017',
    #            '10.10.10.3:27017'
    #       ]
    #}
    my $con = $self->get_con($db);

    if($mode eq 'master') {
        $opt{find_master} = 1;
    }
    else {
        $opt{find_master} = 0;
    }

    $opt{timeout}       = 50000;
    $opt{query_timeout} = -1;

    my @host    = @{$con->{host}};
    @host       = $self->{t}->util->shuffle(@host);
    $opt{host}  = 'mongodb://' . join(',', @host);

    my @opt = %opt;
    my $client;

    eval { 
        local $SIG{__DIE__}; 
        $client = MongoDB::MongoClient->new(@opt);
    };

    if(!$@) {
        $obj = $self->{'o_mongodb_' . $mode . '_' . $db} = $client->get_database($con->{db});
    }
    else {
        return 0;   
    }
}

if($mode eq 'master') {
    $MongoDB::Cursor::slave_okay = 0;
}
else {
    $MongoDB::Cursor::slave_okay = 1;   
}

return $obj;
}   

Is there something my code is wrong? 我的代码有问题吗? Thanks for your time. 谢谢你的时间。

You are clearly running out of connections. 您显然没有连接了。 The reason for this is clear from your comment, in that you are making a new connection with every request that results in hitting the database. 从您的评论中可以清楚地看出其原因,因为您正在对导致数据库命中的每个请求建立新的连接。

One thing you could try is to reduce the timeout value. 您可以尝试的一件事是减少超时值。 This defaults to 20000ms and you are actually setting that higher. 默认值为20000ms ,实际上您将其设置得更高。 As a result connections are "hanging around", and with a busy server that is going to build up quickly. 结果,连接正在“闲逛”,并且正在忙碌的服务器将很快建立起来。

A better strategy is to implement connection pooling. 更好的策略是实现连接池。 There is no default feature in the Perl driver, but there is a guide on the site that can give you some guidance on how to implement. Perl驱动程序中没有默认功能,但是站点上指南 ,可以为您提供一些实现方法的指南。

As an alternate to building that yourself you might want to look into MongoDBx::Class , which does have a connection pooling strategy. 作为构建自己的替代方法,您可能需要研究MongoDBx :: Class ,它确实具有连接池策略。

The basic premise is that you do not want to be establishing a new connection for every request, but instead re-use from an available pool, of a set number, that will not allow your app to exhaust the available connections. 其基本前提是,你不想被确立为每个请求一个新的连接,而是重新使用从可用的游泳池,一组数字,这不会让你的应用程序耗尽可用的连接。 You will also find that operation times are significantly faster, without the overhead of establishing a connection every time. 您还将发现操作时间明显更快,而无需每次都建立连接。

I have this problem too , check mongodb.log if you have many connection that don't close 我也有这个问题,如果您有很多没有关闭的连接,请检查mongodb.log

https://jira.mongodb.org/browse/PERL-264 https://jira.mongodb.org/browse/PERL-264

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM