1. 问题现象
系统页面刷新的时候,偶尔会报错下面的Warnning,但是不经常出现:
Warning: Unknown: Failed to write session data (Redis). Please verify that the current setting of session.save_path is correct (tcp://x.x.x.x:6379?auth=yyy) in Unknown on line 0
看网络有人说是redis版本的问题、但是没有具体结论,那么本着学习的态度,自己试试看看能不能捉出这个bug.
定位问题:
查看PHP文件是否有设置session的地方,发现没有
继续检查php配置文件,发现配置了session存储到redis里面
[Session] session.save_handler = redis session.save_path = "tcp://x.x.x.x:6379?auth=yyyy" session.use_cookies = 1
1、继续查看php Session扩展源代码,定位出错提示语在函数php_session_save_current_state中
检查session扩展文件中出错提示:
static void php_session_save_current_state(TSRMLS_D) /* {{{ */
{
int ret = FAILURE;
//是否session数组
IF_SESSION_VARS() {
if (PS(mod_data) || PS(mod_user_implemented)) {
char *val;
int vallen;
//变量编码
val = php_session_encode(&vallen TSRMLS_CC);
if (val) {
//保存session数据
ret = PS(mod)->s_write(&PS(mod_data), PS(id), val, vallen TSRMLS_CC);
efree(val);
} else {
//清空session
ret = PS(mod)->s_write(&PS(mod_data), PS(id), "", 0 TSRMLS_CC);
}
}
//看出错提示语在这里
if (ret == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to write session data (%s). Please "
"verify that the current setting of session.save_path "
"is correct (%s)",
PS(mod)->s_name,
PS(save_path));
}
}
if (PS(mod_data) || PS(mod_user_implemented)) {
PS(mod)->s_close(&PS(mod_data) TSRMLS_CC);
}
}
static void php_session_flush(TSRMLS_D){
如果 (PS(session_status) == php_session_active)
session_status = none
这里调用...
php_session_save_current_state
}
下面两个函数调用:php_session_flush()
static PHP_FUNCTION(session_write_close)
{
php_session_flush(TSRMLS_C);
}
static PHP_FUNCTION(session_register_shutdown){
}
由上面可以看到,php_session_save_current_state是在 php_session_flush中调用,即在session保存、清空等刷新写session的时候会产生。
由php seesion源码没有发现什么问题,突然想到会不会是Redis本身有没有啥问题,导致的写出错。因而继续从redis里面找问题:
Redis问题定位
1. 首先查看Redis日志文件:
看到如下每5分钟会刷新一下如下log:
[16723] 04 Jul 15:15:01.987 # Server started, Redis version 2.8.9 [16723] 04 Jul 15:15:01.987 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. [16723] 04 Jul 15:15:01.996 * DB loaded from disk: 0.008 seconds [16723] 04 Jul 15:15:01.996 * The server is now ready to accept connections on port 6379







