今天做redis replica碰到一个问题。
执行了slaveof命令后,slave info显示与master断开,查看系统日志显示报错如下:
Connecting to MASTER...
[17727] 07 Dec 11:31:03 * MASTER <-> SLAVE sync started
[17727] 07 Dec 11:31:03 * Non blocking connect for SYNC fired the event.
[17727] 07 Dec 11:31:03 # MASTER aborted replication with an error: ERR Unable to perform background save
于是查看master的系统日志,报错如下:
Slave ask for synchronization
[2487] 07 Dec 14:08:09 * Starting BGSAVE for SYNC
[2487] 07 Dec 14:08:09 # Can't save in background: fork: Cannot allocate memory
[2487] 07 Dec 14:08:09 * Replication failed, can't BGSAVE
在控制台执行bgsave果然不行,google了良久,
找到了问题根源:
再一看master的系统日志里启动部分,果然有警告如下:
[5660] 08 Oct 13:49:56 # 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.
定位到问题的关键是参数vm.overcommit_
memory的设定,参考
http://iloutuoli. blogspot.com/2011/11/sysctl- vmovercommitmemory.html所说:
vm.overcommit_memory 表示内核在分配内存时候做检查的方式。这个变量可以取到0,1,2三个值。对取不同的值时的处理方式都定义在内核源码 mm/mmap.c 的 __vm_enough_memory 函数中。
取 1 的时候 :
此时宏为 OVERCOMMIT_ALWAYS,函数直接 return 0,分配成功。
取 2 的时候:
此时宏为 OVERCOMMIT_NEVER,内核计算:内存总量×vm.overcommit_ratio/100+SWAP 的总量,如果申请空间超过此数值,则分配失败。vm. overcommit_ratio 的默认值为50。
取 0 的时候:
此时宏为 OVERCOMMIT_GUESS,内核计算:NR_FILE_PAGES 总量+SWAP总量+slab中可以释放的内存总量, 如果申请空间超过此数值,则将此数值与空闲内存总量减掉 totalreserve_pages(?) 的总量相加。如果申请空间依然超过此数值,则分配失败。
以上为粗略描述,在实际计算时,如果非root进程,则在计算时候会保留3%的空间,而root进程则没有该限制。 详细过程可看源码。
由于这块配置涉及系统内核,需要root权限,
接下来就只有等着ops帮忙操作了。