Sphinx 如何在长时间没有 activity 的情况下保持连接活跃?
Sphinx How can i keep connection active even if no activity for longer time?
我在 RealTime Index
中使用 PHP 并通过禁用 AUTOCOMIT 来做 bulk inserts
,
例如
// sphinx connection
$sphinxql = mysqli_connect($sphinxql_host.':'.$sphinxql_port,'','');
//do some other time consuming work
//sphinx start transaction
mysqli_begin_transaction($sphinxql);
//do 50k updates or inserts
// Commit transaction
mysqli_commit($sphinxql);
并把脚本运行保存了一夜,早上我看到了
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate
212334 bytes) in
所以当我仔细检查 nohup.out
文件时,我注意到,这些行,
PHP Warning: mysqli_query(): MySQL server has gone away in /home/script.php on line 502
Warning: mysqli_query(): MySQL server has gone away in /home/script.php on line 502
这些行之前的内存使用是正常的,但是这些行之后的内存使用开始增加,它达到了 php
mem_limit
并给出了 PHP Fatal error
并且死了。
in script.php , line 502 is
mysqli_query($sphinxql,$update_query_sphinx);
所以我的猜测是,sphinx 服务器 closed/died 在 activity.
的几个小时/分钟后
我试过在sphinx.conf
中设置
client_timeout = 3600
重新开始搜索
systemctl restart searchd
我仍然面临同样的问题。
那么当 activity 长时间不存在时,我如何才能不让 sphinx 服务器死机呢?
添加了更多信息 -
我一次从 mysql 获取 50k 块的数据,并执行 while 循环以获取每一行并在 sphinx RT 索引中更新它。像这样
//6mil rows update in mysql, so it takes around 18-20 minutes to complete this then comes this following part.
$subset_count = 50000 ;
$total_count_query = "SELECT COUNT(*) as total_count FROM content WHERE enabled = '1'" ;
$total_count = mysqli_query ($conn,$total_count_query);
$total_count = mysqli_fetch_assoc($total_count);
$total_count = $total_count['total_count'];
$current_count = 0;
while ($current_count <= $total_count){
$get_mysql_data_query = "SELECT record_num, views , comments, votes FROM content WHERE enabled = 1 ORDER BY record_num ASC LIMIT $current_count , $subset_count ";
//sphinx start transaction
mysqli_begin_transaction($sphinxql);
if ($result = mysqli_query($conn, $get_mysql_data_query)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
//sphinx escape whole array
$escaped_sphinx = mysqli_real_escape_array($sphinxql,$row);
//update data in sphinx index
$update_query_sphinx = "UPDATE $sphinx_index
SET
views = ".$escaped_sphinx['views']." ,
comments = ".$escaped_sphinx['comments']." ,
votes = ".$escaped_sphinx['votes']."
WHERE
id = ".$escaped_sphinx['record_num']." ";
mysqli_query ($sphinxql,$update_query_sphinx);
}
/* free result set */
mysqli_free_result($result);
}
// Commit transaction
mysqli_commit($sphinxql);
$current_count = $current_count + $subset_count ;
}
所以这里有几个问题,都与 运行宁大进程有关。
MySQL server has gone away
- 这个通常意味着MySQL已经超时,但它可能也意味着MySQL 进程由于 运行 内存不足而崩溃。简而言之,就是MySQL已经停止响应,并且没有告诉客户端原因(即没有直接查询错误)。正如您所说,您在单个 t运行 操作中 运行ning 50k 更新,很可能 MySQL 只是 运行 内存不足。
Allowed memory size of 134217728 bytes exhausted
- 表示 PHP 运行 内存不足。这也使人们相信 MySQL 运行 内存不足。
那该怎么办呢?
最初的权宜之计是增加 PHP 和 MySQL 的内存限制。这并没有真正解决根本原因,并且根据您对部署堆栈的控制量(以及您拥有的知识),这可能是不可能的。
正如一些人提到的,批处理过程可能会有所帮助。如果不知道您正在努力解决的实际问题,很难说出最好的方法。如果您可以计算出 10000 或 20000 条记录而不是 50000 条记录,那么可以解决您的问题。如果这在单个进程中花费的时间太长,您还可以考虑使用消息队列(RabbitMQ 是一个很好的队列,我已经在许多项目中使用过),这样您就可以 运行 多个进程 同时 处理较小的批次。
如果您正在做的事情需要了解 所有 600 万条以上的记录来执行计算,您可能会将过程分成许多较小的步骤,缓存工作完成 "to date"(如此),然后在下一个过程中进行下一步。如何干净地做到这一点很困难(同样,像 RabbitMQ 这样的东西可以通过在每个进程完成时触发一个事件来简化它,以便下一个进程可以启动)。
所以,简而言之,有两个最佳选择:
- 尽可能多resources/memory解决问题
- 将问题分解成更小的独立块。
您需要在 mysqli_begin_transaction($sphinxql)
之前重新连接或重启数据库会话
像这样。
<?php
//reconnect to spinx if it is disconnected due to timeout or whatever , or force reconnect
function sphinxReconnect($force = false) {
global $sphinxql_host;
global $sphinxql_port;
global $sphinxql;
if($force){
mysqli_close($sphinxql);
$sphinxql = @mysqli_connect($sphinxql_host.':'.$sphinxql_port,'','') or die('ERROR');
}else{
if(!mysqli_ping($sphinxql)){
mysqli_close($sphinxql);
$sphinxql = @mysqli_connect($sphinxql_host.':'.$sphinxql_port,'','') or die('ERROR');
}
}
}
//10mil+ rows update in mysql, so it takes around 18-20 minutes to complete this then comes this following part.
//reconnect to sphinx
sphinxReconnect(true);
//sphinx start transaction
mysqli_begin_transaction($sphinxql);
//do your otherstuff
// Commit transaction
mysqli_commit($sphinxql);
我在 RealTime Index
中使用 PHP 并通过禁用 AUTOCOMIT 来做 bulk inserts
,
例如
// sphinx connection
$sphinxql = mysqli_connect($sphinxql_host.':'.$sphinxql_port,'','');
//do some other time consuming work
//sphinx start transaction
mysqli_begin_transaction($sphinxql);
//do 50k updates or inserts
// Commit transaction
mysqli_commit($sphinxql);
并把脚本运行保存了一夜,早上我看到了
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate
212334 bytes) in
所以当我仔细检查 nohup.out
文件时,我注意到,这些行,
PHP Warning: mysqli_query(): MySQL server has gone away in /home/script.php on line 502
Warning: mysqli_query(): MySQL server has gone away in /home/script.php on line 502
这些行之前的内存使用是正常的,但是这些行之后的内存使用开始增加,它达到了 php
mem_limit
并给出了 PHP Fatal error
并且死了。
in script.php , line 502 is
mysqli_query($sphinxql,$update_query_sphinx);
所以我的猜测是,sphinx 服务器 closed/died 在 activity.
的几个小时/分钟后我试过在sphinx.conf
中设置client_timeout = 3600
重新开始搜索
systemctl restart searchd
我仍然面临同样的问题。
那么当 activity 长时间不存在时,我如何才能不让 sphinx 服务器死机呢?
添加了更多信息 -
我一次从 mysql 获取 50k 块的数据,并执行 while 循环以获取每一行并在 sphinx RT 索引中更新它。像这样
//6mil rows update in mysql, so it takes around 18-20 minutes to complete this then comes this following part.
$subset_count = 50000 ;
$total_count_query = "SELECT COUNT(*) as total_count FROM content WHERE enabled = '1'" ;
$total_count = mysqli_query ($conn,$total_count_query);
$total_count = mysqli_fetch_assoc($total_count);
$total_count = $total_count['total_count'];
$current_count = 0;
while ($current_count <= $total_count){
$get_mysql_data_query = "SELECT record_num, views , comments, votes FROM content WHERE enabled = 1 ORDER BY record_num ASC LIMIT $current_count , $subset_count ";
//sphinx start transaction
mysqli_begin_transaction($sphinxql);
if ($result = mysqli_query($conn, $get_mysql_data_query)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
//sphinx escape whole array
$escaped_sphinx = mysqli_real_escape_array($sphinxql,$row);
//update data in sphinx index
$update_query_sphinx = "UPDATE $sphinx_index
SET
views = ".$escaped_sphinx['views']." ,
comments = ".$escaped_sphinx['comments']." ,
votes = ".$escaped_sphinx['votes']."
WHERE
id = ".$escaped_sphinx['record_num']." ";
mysqli_query ($sphinxql,$update_query_sphinx);
}
/* free result set */
mysqli_free_result($result);
}
// Commit transaction
mysqli_commit($sphinxql);
$current_count = $current_count + $subset_count ;
}
所以这里有几个问题,都与 运行宁大进程有关。
MySQL server has gone away
- 这个通常意味着MySQL已经超时,但它可能也意味着MySQL 进程由于 运行 内存不足而崩溃。简而言之,就是MySQL已经停止响应,并且没有告诉客户端原因(即没有直接查询错误)。正如您所说,您在单个 t运行 操作中 运行ning 50k 更新,很可能 MySQL 只是 运行 内存不足。Allowed memory size of 134217728 bytes exhausted
- 表示 PHP 运行 内存不足。这也使人们相信 MySQL 运行 内存不足。
那该怎么办呢?
最初的权宜之计是增加 PHP 和 MySQL 的内存限制。这并没有真正解决根本原因,并且根据您对部署堆栈的控制量(以及您拥有的知识),这可能是不可能的。
正如一些人提到的,批处理过程可能会有所帮助。如果不知道您正在努力解决的实际问题,很难说出最好的方法。如果您可以计算出 10000 或 20000 条记录而不是 50000 条记录,那么可以解决您的问题。如果这在单个进程中花费的时间太长,您还可以考虑使用消息队列(RabbitMQ 是一个很好的队列,我已经在许多项目中使用过),这样您就可以 运行 多个进程 同时 处理较小的批次。
如果您正在做的事情需要了解 所有 600 万条以上的记录来执行计算,您可能会将过程分成许多较小的步骤,缓存工作完成 "to date"(如此),然后在下一个过程中进行下一步。如何干净地做到这一点很困难(同样,像 RabbitMQ 这样的东西可以通过在每个进程完成时触发一个事件来简化它,以便下一个进程可以启动)。
所以,简而言之,有两个最佳选择:
- 尽可能多resources/memory解决问题
- 将问题分解成更小的独立块。
您需要在 mysqli_begin_transaction($sphinxql)
像这样。
<?php
//reconnect to spinx if it is disconnected due to timeout or whatever , or force reconnect
function sphinxReconnect($force = false) {
global $sphinxql_host;
global $sphinxql_port;
global $sphinxql;
if($force){
mysqli_close($sphinxql);
$sphinxql = @mysqli_connect($sphinxql_host.':'.$sphinxql_port,'','') or die('ERROR');
}else{
if(!mysqli_ping($sphinxql)){
mysqli_close($sphinxql);
$sphinxql = @mysqli_connect($sphinxql_host.':'.$sphinxql_port,'','') or die('ERROR');
}
}
}
//10mil+ rows update in mysql, so it takes around 18-20 minutes to complete this then comes this following part.
//reconnect to sphinx
sphinxReconnect(true);
//sphinx start transaction
mysqli_begin_transaction($sphinxql);
//do your otherstuff
// Commit transaction
mysqli_commit($sphinxql);